收藏 分销(赏)

Linux实验报告.doc

上传人:Fis****915 文档编号:553976 上传时间:2023-12-07 格式:DOC 页数:17 大小:437.50KB
下载 相关 举报
Linux实验报告.doc_第1页
第1页 / 共17页
Linux实验报告.doc_第2页
第2页 / 共17页
Linux实验报告.doc_第3页
第3页 / 共17页
Linux实验报告.doc_第4页
第4页 / 共17页
Linux实验报告.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

1、中南大学Linux系统实验报告 目 录实验一39实验二1016实验三1717实验一 Shell程序设计 实验目的 理解Shell程序的设计方法;熟悉Shell程序的编辑、运行、调试方法与过程。 实验内容 考勤模拟Shell程序设计用shell设计一个考勤模拟程序,实现如下功能选择界面:1.上班签到2.下班签出3.缺勤信息查阅4.退出考勤程序运行后,提示用户输入上述功能选择,并验证用户输入的用户名和密码;用户信息保存在userinfo.dat中。如果是上班签到,记录签到信息,如果签到时间大于上午8时,则提示用户迟到,并记录该迟到信息到check.dat。如果是下班签出,记录签出信息,如果签出时间

2、小于下午6时,则提示用户早退,并记录该早退信息到check.dat。如果用户选择缺勤信息查询,则将check.dat中对应该用户的迟到早退信息查出并显示。用户选择功能执行完,shell程序继续回到功能选择界面等待下一个用户进行操作。一、 实验分析本实验是shell程序设计,主要目的是理解shell程序的设计方法以及熟悉shell程序的编辑、运行、调试方法与过程。Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编

3、辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。本实验要求设计一个考情模拟的shell程序,按照题目要求,可将整个程序分为五个部分:main函数以及四个功能函数,及一个功能对应一个函数。1、 上下班签到将此函数定义为 check_in();根据题目要求,运行程序后首先要求用户输入用户名和密码,当用户名密码正确时才可进入,这就要用到shell编程中的变量,shell中的变量和C语言或是JAVA中的变量都有所不同,shell中的变量不需要事先声明,给一个变

4、量赋值实际上就定义了一个变量,并且shell程序中的变量是无类型的。我们定义两个变量name和password,由题目要求,用户名和密码需要由用户输入,因此我们定义的变量应该从键盘获取输入值,使用如下语句:read name password;我们在开发程序前应先创建一个文件userinfo.dat用于存放用户的用户名和密码,当用户输入用户名和密码后就要判断该用户是否合法。方法为从userinfo.dat文件中逐行读取数据,并判断是否与用户输入的用户名相同,当遇到相同的用户名时就表明该用户是合法的,可以退出文件的读取,转入判断该用户输入的密码是否正确。从文件中逐行读取数据方法:if test

5、-e /home/poe/userinfo.datthenwhile read u_name u_passworddoif test $name = $u_namethen break;elsecontinue;fidone /home/poe/userinfo.dat用户名和密码正确后用户进入系统,系统显示用户签到成功,此时还要判断此时的时间是否大于上午8时,则提示用户迟到,并记录该迟到信息到check.dat。2、 下班签出将此函数定义为check_out() ,前面部分和上一个函数check_in()相同,都为判断用户输入的用户名和密码。不同之处在于记录签出信息,如果签出时间小于下午6时

6、,则提示用户早退,并记录该早退信息到check.dat。3、 缺勤信息查阅将此函数定义为look(),首先还是判断该用户输入的用户名和密码是否正确。用户进入系统之后就要打印出该用户的签到信息,将check.dat中对应该用户的迟到早退信息查出并显示。在这一步中,首先还是要从check.dat文件中逐行读取数据,不过并不直接打印,而是要等判断用户名后才能打印。while read recorddouser=$record% *;if test $user = $namethenecho $record;fidone /home/poe/check.dat其中user=$record% *;语句表

7、示截取record字符串从左边起第一个空格前的字符串,也就是用户名,然后检查和该用户的用户名是否匹配,若匹配则打印否则读取下一条。二、 实验源码#! /bin/bashfunction show()echo -Welcome to Attandance System-;echo - 1.check in -;echo - 2.check out -;echo - 3.late -;echo - 4.exit -;echo Please input your choice:;function check_in()echo Please input your name and password:;

8、read name password;if test -e /home/poe/userinfo.datthenwhile read u_name u_passworddoif test $name = $u_namethen break;elsecontinue;fidone check.dat;fifielseecho There is no this file;fifunction check_out()echo Please input your name and password:;read name password;if test -e /home/poe/userinfo.da

9、tthenwhile read u_name u_passworddoif test $name = $u_namethen break;elsecontinue;fidone check.dat;fifielseecho There is no this file;fifunction look()echo Please input your name and password:;read name password;if test -e /home/poe/userinfo.datthenwhile read u_name u_passworddoif test $name = $u_na

10、methen break;elsecontinue;fidone /home/poe/userinfo.datif test $name != $u_namethenecho Sorry,your name is wrong!;elif test $password != $u_passwordthenecho Sorry,your password is wrong!;elsewhile read recorddouser=$record% *;if test $user = $namethenecho $record;fidone /home/poe/check.datfielseecho

11、 There is no this file;fifunction main()clear;show;read choice;case $choice in1) check_in;2) check_out;3) look;4) exit;*) echo Please input 14;esacmain;三、 实验步骤及截图进入终端,输入./attand.sh实验2 Linux 高级程序设计-进程通信1、实验目的(1)了解Linux操作系统下应用程序开发流程(2)掌握GNU工具链的使用(3)了解Linux高级编程技巧(例如IPC机制、系统调用等)2、实验内容(1) 编写一个简单的C语言程序,编写

12、Makefile文件。了解编译过程,并用gdb进行调试。(2) 以下任选其一:1. 编写一个多进程通信程序,采用Message Queue或shared Memory或者Pipeline File机制进行通信2. 编写一个实现读者-写者问题的程序,用信号量机制备注:读者-写者问题 设有一组共享数据DB和两组并发进程,一组进程只对此组数据执行读操作,另一组进程可对此组数据执行写操作(同时也可以执行读操作)。将前面一组进程称为读者,后一组进程称为写者。为了保证共享数据的完整性,要求: (1)多个读者的操作可以同时进行 (2)多个写者的操作不可同时进行 (3)任何读者与写者的操作不可同时进行3、实验

13、要求(1) 写出源程序,并编译运行(2) 详细记录程序调试及运行结果一、 Makefile思路分析在本实验中编写输入学生人数及分数,计算学生的总成绩以及平均成绩的程序,源程序如下:/*main.c*/#include #include chengji.hint main()int n,i;float sum,avg;printf(please input the number ofstudents:);scanf(%d,&n);float scoren;for(i=0;in;i+)printf(please input the score of student%d,i);scanf(%d,&s

14、corei);sum=fun_sum(score,n);avg=fun_avg(score,n);printf(the sum is %f,the avg is %f,sum,avg);/*chengji.h*/float fun_sum(float array,int n);float fun_avg(float array,int n);/*fun_sum.c*/float fun_sum(float array,int n)float sum=0.0;for(int i=0;in;i+)sum+=arrayi;return sum;/*fun_avg.c*/float fun_avg(f

15、loat array,int n)float avg=0.0;for(int i=0;in;i+)avg+=arrayi;avg/=n;return avg;Makefile文件如下:main:main.o fun_sum.o fun_avg.ogcc main.o fun_sum.o fun_avg.o -o mainmain.o:main.c chengji.hgcc main.c -cfun_sum.o:fun_sum.cgcc fun_sum.c -cfun_avg.o:fun_avg.cgcc fun_avg.c -c运行结果如下:二、 读者-写者思路分析读写信号量的特点是:1. 同

16、一时刻最多有一个写者(writer)获得锁;2. 同一时刻可以有多个读者(reader)获得锁;3. 同一时刻写者和读者不能同时获得锁;由于读者可以同时获得锁,因此提高了系统的并发程度,进而提高了系统的性能。源程序如下: #include #include #include / 定义数据类 class data public: data(int i, float f): I(i), F(f) int I; float F; ; / 读者写者读写的内容 data *p_data = NULL; pthread_rwlock_t lock; / 写者数目 const int WRITER_NUMB

17、ER = 2; void *reader(void *arg); void *writer(void *arg); int main(int argc, char *argv) pthread_t reader_tid; pthread_t writer_tidWRITER_NUMBER; pthread_create(&reader_tid, NULL, reader, NULL); for (int i = 0; i I, p_data-F); pthread_rwlock_unlock(&lock); return (void *)0; void *writer(void *arg) p

18、thread_detach(pthread_self(); while (true) pthread_rwlock_wrlock(&lock); printf(writer is writing the data; ); if (p_data = NULL) p_data = new data(1, 1.1f); printf(writer create the data (%d, %f)n, p_data-I, p_data-F); else delete p_data; p_data = NULL; printf(writer free the datan); pthread_rwlock

19、_unlock(&lock); return (void *)0; 实验三: Proc文件系统【实验目的】本实验作业将通过Proc文件系统观察整个系统的一些重要特征,并要求编写一个程序,利用Proc文件系统获得和修改系统的各种配置参数。【实验内容】1、以超级用户的身份登录Linux系统,并进入/proc目录,输入ls命令,查看该目录下的内容,同时查看每个文件的读、写权限。并回答下列问题: CPU的类型和型号 1 Intel(R) Core(TM) i3-2350M CPU 2.30GHz 所使用的Linux的版本Linux ubuntu 4.8.0-52-generic #5516.04.1-Ubuntu SMP Fri Apr 28 14:36:29 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux 从启动到当前时刻经过的时间 当前内存状态2、 编写一个程序,用来获得内核参数(任意的参数均可)3、编写一个程序,用来修改内核参数(任意的参数均可)

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服