安工大Linux实验指导书.doc_第1页
安工大Linux实验指导书.doc_第2页
安工大Linux实验指导书.doc_第3页
安工大Linux实验指导书.doc_第4页
安工大Linux实验指导书.doc_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

安工大Linux程序设计实验指导书 部分实验完成不全请同学们自行补充 姓名:* 学号:* 班级:* 指导老师:阮越 时间:2013年5月31号 实验一 Linux基本命令的使用1、实验目的学习和掌握Linux的基本命令。2、实验内容和步骤步骤1:以user_login用户身份并使用telnet登录Linux服务器,按照提示创建自己的账户和口令。步骤 2:使用新创建的用户账户和口令登录Linux系统,察看登录后的界面。步骤3:使用pwd命令察看当前的工作目录,然后用ls命令查看当前目录下的内容,尝试使用-a,-l,-F,-A,-lF等不同选项并比较不同之处。-a, -all:不隐藏任何以“.” 字符开始的项目。-A, -almost-all:列出除了“ . ”及 “. ”以外的任何项目。-F, -classify:加上文件类型的指示符号 (*/=| 其中一个)。-l:使用较长格式列出信息。-lF :即使用长格式又加上类型的指示符号。步骤4:在当前目录下建立一个名为test的新目录,然后将工作目录切换到test下,尝试将/etc目录下的文件passwd拷贝到该目录下(cp 源文件 目的目录)。察看当前目录下的passwd文件的属主和文件权限。步骤5:尝试向当前目录下的passwd文件和/etc/passwd文件分别写入一些新内容(可使用echo “字符串” 文件的命令),看看操作能否成功,如果不能成功,请说明原因。用cat命令浏览文件password的内容,用more命令进行浏览翻页操作,再用less命令浏览文件的内容。比较这几个命令的不同之处.对当前目录中成功,因为该目录下的passwd文件对当前用户具有写的权限。对/etc/passwd文件的操作被拒绝因为对当前用户不具有写的权限cat命令将文件的内容全部显示,more命令将文件内容分屏显示,less命令进行刷新的全部显示步骤6:用ls命令查看test下文件的权限,用mv命令更改文件password的文件名为test.txt,尝试用chown和chgrp更改文件的属主为root、组为root,看看能否成功,不成功,请说明原因。尝试用chomd将文件权限为“-rw-”。看看能否成功,不成功,请说明原因。用chown和chgrp更改文件的属主为root、组为root,不成功。因为chown和chgrp一般是root的权限步骤7:用rm命令删除test目录下的所有文件,再用rmdir命令删除test目录。(想一想有没有一条命令将目录及目录下的所有文件删除,写出这条命令)rm -rf test步骤8:使用ps命令查看当前系统内的进程,并利用man命令获取ps命令的参数,写出获取当前终端进程执行情况的ps命令。步骤9:使用df命令查看当前系统已安装的文件系统的空间使用情况,记录结果。步骤10:使用du命令查看用户的工作目录占用了多少空间,记录结果。步骤11:使用free命令查看内存资源的使用情况,记录结果。步骤12:使用man获取tar和gzip的帮助信息,尝试将test目录下的文件打包并压缩,然后到另外一目录tmp下解包,写出这几条命令。tar zcvf /tmp/test.tar.gz /home/seasker/test步骤13:尝试执行“ls l tmp”,看看这条命令的执行会出现什么结果,解释一下这条命令。将ls l输出的结果输出重定向到tmp文件中步骤14:尝试执行find /usr/src -name *.c -print | xargs grep “#include”,看看这条命令的执行会出现什么结果,解释一下这条命令。解释:匹配查找列表输出/usr/src下以.c结尾的文件中含有“#include”的内容。步骤15:执行cal和date命令,说说这两条指令的用途。cal:输出当月的日历,date:输出当前的时间。步骤16:执行命令clear和logout,退出系统。(想一想有没有其他的方法,写出这种方法)exit步骤17:执行命令shutdown,关闭系统。(想一想有没有更简单的命令,写出这条命令)init 0;3、实验结论通过对linux 的常用命令的学习,让我对linux的系统有了初步的认识-这是一个完全基于命令操作的操作系统,这些命令组成了强大的命令系统,提供了完善的功能。这些命令操作将为我以后的linux学习打下良好的基础。实验二 简单Shell程序设计1、实验目的(1) 学习和掌握vi编辑器的基本使用方法。(2) 学习和掌握编写简单的shell程序。2、实验内容和步骤2.1 vi的使用步骤1:以root用户身份登录进入Linux系统。步骤 2:启动Vi,切换到输入模式,输入一段英文:Thisisanexercise!WeuseVitoeditit.Left,down,up,right步骤3:尝试匹配其中的一串字符,写出这命令及执行结果。 在命令状态下输入 /Left 即匹配Left这个字符步骤4:尝试替换其中的一串字符,写出这命令及执行结果。 在命令状态下输入 1.$/use/123 即把use字符替换为123步骤5:尝试复制/删除其中的一行或几行文本,写出命令及执行结果。在命令状态下输入nyy(n为复制的行数)然后把光标移动到要复制的地方输入p可以粘贴 同样输入ndd即为删除步骤6:尝试复制/删除其中的一个单词或几个字符,写出命令及执行结果。不断的退格即可步骤7:尝试使用鼠标复制文本,写出命令及执行结果。步骤8:尝试存盘退出操作,写出命令。可以用的命令 :wq 或:x2.2 简单的shell程序步骤1:使用Vi将程序清单2-1的程序输入。步骤 2:尝试运行该程序,如果不能运行,原因是什么,怎样修改。步骤3:执行程序,写出程序的执行结果。步骤4:阅读程序,掌握shell函数的编写方法,补全函数file_run。for i in ls do if test -x $i;then echo $i fi Done步骤5:修改程序中的函数uers(),使得显示在线用户时不分行。(提示:可以使用循环,对每个在线用户分别处理。)echo there are who|wc -l users onlinefor i in who are who|awk print $1do echo -n $i echo -n done3、实验结论通过对shell程序的设计,让我对shell的语法,结构和命令有了进一步的认识,同时认识到shell程序的强大,我可以比较方便的编写一些基础的脚本程序。清单2-1 menuMYDATE=date +%d/%m/%yTHIS_HOST=hostname -sUSER=whoamifile_run()echo you must complete ituers()echo there are who|wc -l users onlineecho -e who arenwho|awk print $1 while :dotput clearcat read CHOICEcase $CHOICE in1) ls -l|awk /d/ print $8 ;2) file_run ;3) uers;H|h)cat aaaThis is the help screen,nothing here yet to help you!aaa;Q|q) exit 0;*) echo -e t007 unkown user response;esacecho -e -n nntHit the return key to continueread DUMMYDone实验三 Linux下简单C程序设计与文件操作1、实验目的(1) 学习和掌握gcc等Linux的开发调试环境。(2) 学习并掌握Linux的文件操作。(3) 编写并实现实验步骤6要求的程序。2、实验内容和步骤步骤1:使用Vi将程序清单3-1和3-2的程序输入,并在当前目录下创建文件“file.in”和文件“file.out”,尽可能的使文件“file.in”大一些。步骤 2:利用gcc分别编译这两个程序,写出编译命令和执行结果,如果不成功,尝试利用gdb调试。gcc o list3_1 list3_1.cgcc o list3_2 list3_2.c步骤3:仔细观察这两个程序,比较标准C的文件操作和Linux的系统调用open、read、write等的使用区别。答:标准c的文件操作一个是库函数的调用,linux中系统调用文件操是底层的文件调用,其运行效率略有差距,同时参数也不同,返回的数据类型也不同。步骤4:按照说明重新修改程序3-2,并使用time命令察看程序执行的时间效率上有何区别。 修改前:修改后:步骤5:输入、编译并运行程序3-3和3-4,写出执行结果,并比较他们fseek和lseek在使用方法上的异同两者都是对文件的读写指针进行设置但两者的返回类型不同,fseek返回的是int型lseek返回的是off_t型步骤6:学习并分别使用标准C的文件操作函数和Linux的系统调用创建一个对学生基本信息进行操作(插入、修改和删除)的C程序,学生基本信息以结构体的形式存储在文件中,struct stu_info的定义如下:struct stu_info char stu_num12;char name10;short int sex;/*0为女生,1为男生*/char mobile_phone12;3、 实验结论通过对文件操作的编程,让我掌握了文件的创建,打开,读写,以及特定位置的读写关闭等操作。并且从系统底层调用和标准c函数库两个角度对文件的操作比较中,认识到两者的异同。4、程序清单/程序清单 3-1#include #include int main(void)char c;FILE *in,*out;if(in = fopen(file.in,r)=NULL)perror(file open error!);exit(0);out = fopen(file.out,w);while(c = fgetc(in)!=EOF)fputc(c,out);/程序清单 3-2#include #include #include int main()/ char block1024; char c; int in, out; int nread; in = open(file.in, O_RDONLY); out = open(file.out, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);/将注释打开,并将两条语句的后一句注释掉,重新编译执行。/ while(nread = read(in,block,sizeof(block) 0) while(nread = read(in,&c,sizeof(c) 0)/ write(out,block,nread); write(out,&c,nread);/程序清单 3-3#include #include #include #include #include char buf1 = abcdefghij;char buf2 = ABCDEFGHIJ;void err_exit(char *err_s)perror(err_s);exit(1);int main(void)FILE *fp;if(fp = fopen(hole.file,w) = NULL)err_exit(file open fail!);if(fwrite(buf1,sizeof(buf1),1,fp)!=1)err_exit(file write buf1 error!);if(fseek(fp,40,SEEK_SET)=-1)err_exit(fseek error!);if(fwrite(buf2,strlen(buf2),1,fp)!=1)err_exit(file write buf2 error!);fclose(fp);/程序清单 3-4#include #include #include #include #include char buf1 = abcdefghij;char buf2 = ABCDEFGHIJ;void err_exit(char *err_s)perror(err_s);exit(1);int main(void)int fd;if(fd = open(hole.file,O_WRONLY|O_CREAT/*|O_APPEND,0644*/) = -1)err_exit(file open fail!);if(write(fd,buf1,10)!=10)err_exit(file write buf1 error!);if(lseek(fd,40,SEEK_SET)=-1)err_exit(lseek error!);if(write(fd,buf2,10)!=10)err_exit(file write buf2 error!);实验四 Linux下进程操作与进程间通信1、实验目的(1) 学习和掌握fork等系统调用的基本使用方法。(2) 利用Linux中的管道实现父子进程间的同步。2、实验内容与步骤步骤1:使用Vi将程序清单4-1的程序输入、编译并运行,学习和掌握fork的基本调用方法。步骤 2:使用Vi将程序清单4-2、4-3和4-4的程序输入、利用gcc分别编译这三个程序,写出编译命令和编译结果,如果不成功,尝试利用gdb调试。gcc -o list4_2 list4_2.cgcc -o father father.cgcc -o child child.c步骤3:写出编译这三个程序的makefile,然后利用make进行编译,谈谈这么做的好处。可以节省劳动时间,让三个程序同时编译,简化了操作。步骤4:运行这三个程序,写出运行结果。 步骤5:屏幕上显示出的执行结果是哪一个进程执行的结果?father和child步骤6:父进程中的printf有向屏幕输出吗?为什么?没有,因为父进程的标准输出已经被重定向。步骤7:利用父子进程间的管道通信方式,改写实验3步骤6要求的程序。要求启用两个进程,其中父进程接受用户对文件的操作命令然后通过管道发给子进程,子进程完成对文件的实际操作。3、 实验结论 通过该节实验,我更进一步理解了fock函数dup函数exercl函数的工作原理,让我了解到了进程并发的概念。4、程序清单/程序清单 4-1#include #include #include err_exit.hint global = 5;int main(void)pid_t pid;char *string = these are values before fork:;int local = 10;printf(before fork * * *nn);if(pid = fork()0)err_exit(fork error);if(pid = 0)string = I am child.;printf(nMy pid is %d,%snpid = %dn global = %dn local = %dn,getpid(),string,pid,global,local);global+;elsestring = I am parent.;printf(nMy pid is %d,%snpid = %dn global = %dn local = %dn,getpid(),string,pid,global,local);local+;printf(%sn Now,global = %d,local = %dn,string,global,local);exit(EXIT_SUCCESS);/清单4-2 管道程序#include #include#include #define STD_INPUT 0/ 定义标准输入设备描述符# define STD_OUTPUT 1/ 定义标准输出设备描述符int fd2;void main()static char process1=father,process2=child; pipe(fd);/ 定义管道 pipeline(process1,process2);/ 调用自定义函数pipeline() exit(1);/ 程序结束pipeline(char* process1,char* process2)int i;if (i=fork()=-1) / 创建进程,失败退出 perror(process fork error!);exit(1);if (i) close(fd0);/ 关闭管道输入描述符 close(STD_OUTPUT);/ 关闭标准输出描述符1 dup(fd1);/ 指定标准输出描述符1为管道写指针 close(fd1);/ 关闭原始管道写指针 execl(process1, process1, 0);/ 用程序father覆盖当前程序 printf(father failed.n);/ execl()执行失败 else close(fd1);/ 关闭管道输出描述符 close(STD_INPUT);/ 关闭标准输入描述符0 dup(fd0);/ 指定标准输入描述符0为管道读指针 close(fd0);/ 关闭原始管道读指针 execl(process2,process2,0);/ 用程序child覆盖当前程序 printf(child failed.n);/ execl()执行失败 exit(2);/ 程序结束清单 4-3 father.c#include #include #include int main()static char string = Parent is using pipe write.;int len;len = sizeof(string); write(1, string, len);/* 将string中的内容写入管道中 */ printf(parent, parent, parent n n n); exit(0);清单4-4 child.c#include #include int main() char output30 ; read (0, output, 30) ;/* 从管道中读数据并存入output中 */ printf(%s n child, child. n, output) ; return(0) ;实验五 Linux线程的使用1、实验目的学习和掌握Linux线程的创建以及同步方法。2、实验内容和步骤步骤1:输入程序5-1,编译并运行写出执行结果。步骤 2:仔细研读代码,描述pthread_create函数中各个参数的意义,并给出线程的入口函数的调用方法,描述两线程间参数传递的方式。int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *),void *arg);第一个参数为指向线程标识符的指针。第二个参数用来设置线程属性。第三个参数是线程运行函数的起始地址。最后一个参数是运行函数的参数。主线程通过pthread_create函数向从线程a_thread传递share_int 而从线程a_thread通过pthread_join向主线程传递thread_result步骤3:打开注释掉的全局变量aa及操作aa的相关代码,回答这跟进程有什么不同。线程共用相同的数据区而各个进程有着自己独立的数据区。步骤4:输入程序5-2,编译并运行,写出运行结果。步骤5:仔细研读代码,描述利用信号量实现线程间同步调用方法。sem_post 以原子操作的方式将信号量加1从而唤醒线程。sem_wait以原子操作将信号量减1当信号量小于0时就会对进程进行等待,从而实现同步调用。步骤6:学习并使用线程间的同步方法,重新改写实验3步骤6要求的程序。要求启用两个线程,其中主线程接受用户对文件的操作命令然后发给子线程,子线程完成对文件的实际操作(注意线程间的同步)。3、 实验结论通过改节实验,让我对线程并发有了初步认识,同时学会用信号量进行线程之间的同步。4、程序清单/程序清单 5-1#include #include #include #include void *thread_function(void *arg);int main()int res;pthread_t a_thread;void *thread_result;int share_int = 10;res = pthread_create(&a_thread,NULL,thread_function,(void *)&share_int);sleep(5);share_int = -10;if(res != 0)perror(Thread creation failed);exit(EXIT_FAILURE);printf(Waiting for thread to finish.n);res = pthread_join(a_thread,&thread_result);if(res != 0)perror(Thread join failed);exit(EXIT_FAILURE);printf(Thread joined,it returned %sn,(char *)thread_result);printf(share_int is now %dn,share_int);exit(EXIT_SUCCESS);void *thread_function(void *arg)printf(thread_function is running.Argument was %dn,*(int *)arg);*(int *)arg = *(int *)arg * 10;pthread_exit(Thank you for the CPU time);/程序清单 5-2#include #include #include #include #include #include #include void *thread_function(void *arg);sem_t bin_sem;#define WORK_SIZE 1024char work_areaWORK_SIZE;int main()int res;pthread_t a_thread;void *thread_result;res = sem_init(&bin_sem,0,0);if (res != 0)perror(Semaphore initialition failed);exit(EXIT_FAILURE);res = pthread_create(&a_thread,NULL,thread_function,NULL);if(res != 0)perror(Thread creation failed);exit(EXIT_FAILURE);printf(Input some text,Enter end to finishn);while(strncmp(end,work_area,3) != 0)fgets(work_area,WORK_SIZE,stdin);sem_post(&bin_sem);printf(nWaiting for thread to finish.n);res = pthread_join(a_thread,&thread_result);if(res != 0)perror(Thread join failed);exit(EXIT_FAILURE);printf(Thread joinedn);sem_destroy(&bin_sem);exit(EXIT_SUCCESS);void *thread_function(void *arg) sem_wait(&bin_sem);while(strncmp(end,work_area,3) != 0)printf(You input %d charactersn,strlen(work_area)-1);sem_wait(&bin_sem);pthread_exit(NULL);实验六 Linux进程间的IPC1、实验目的学习和掌握Linux进程间的IPC及同步方法。2、实验内容和步骤步骤1:输入程序6-1,6-2编译并运行写出执行结果。步骤 2:仔细研读代码,写出程序中实现P、V操作的相关函数和代码,描述程序的同步工作过程。P操作:if (!semaphore_p() exit(EXIT_FAILURE); printf(%c, op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf(%c, op_char); fflush(stdout);V操作: if (!semaphore_v() exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time);P操作后操作当前的临界区占用资源,v操作退出当前的临界区,释放资源。步骤3:输入程序6-3和6-4,编译执行并写出结果。步骤4:程序6-3和6-4中哪段代码实现了共享,描述实现内存共享的主要函数的参数意义和这些函数的使用方法。程序6_3:if(shm = shmat(shmid,NULL,0) = (char *)-1) perror(shmat error);exit(0);s = shm;for(c = a; c = z; c+)*s+ = c;*s = NULL;printf(init over!n);while(*shm != *)sleep(1);程序6_4:if(shm = shmat(shmid,NULL,0) = (char *)-1)perror(shmat error!);exit(0);for(s = shm; *s != NULL; s+)putchar(*s);putchar(n);*shm = *;步骤5:学习并使用IPC中信号量和共享内存的使用方法,重新改写实验3步骤6要求的程序。要求启动多个进程,每一个进程都可以单独对文件进行操作,进程间通过信号量进行同步,对文件的操作映射到共享内存中。3、 实验结论通过对改节实验,让我了解了进程IPC信号量访问临界区,以及进程之间访问共享内存的实现方法和同步机制,掌握了上述一系列函数的使用方法。4、程序清单/程序清单 6-1#if defined(_GNU_LIBRARY_) & !defined(_SEM_SEMUN_UNDEFINED) /* union semun is defined by including */#else /* according to X/OPEN we have to define it ourselves */ union semun int val; /* value for SETVAL */ struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ unsigned short int *array; /* array for GETALL, SETALL */ struct seminfo *_buf; /* buffer for IPC_INFO */ ;#endif/程序清单 6-2#include #include #include #include #include #include #include semun.hstatic int set_semvalue(void);static void del_semvalue(void);static int semaphore_p(void);static int semaphore_v(void);static int sem_id;int main(int argc, char *argv) int i; int pause_time; char op_char = O; srand(unsigned int)getpid(); sem_id = semget(key_t)1234, 1, 0666 | IPC_CREAT); printf(semid = %dn,sem_id); if (argc 1) if (!set_semvalue() fprintf(stderr, Failed to initialize semaphoren); exit(EXIT_FAILURE); op_char = X; sleep(5); /* Then we have a loop which enters and leaves the critical section ten times. There, we first make a call to semaphore_p which sets the semaphore to wait, as this program is about to enter the critical section. */ for(i = 0; i 1) sleep(10); del_semvalue(); exit(EXIT_SUCCESS);/* The function set_semvalue initializes the semaphore using the SETVAL command in a semctl call. We need to do this before we can use the semaphore. */static int set_semvalue(void) union semun sem_union; sem_union.val = 1; if (semctl(sem_id, 0, SETVAL, sem_union) = -1) return(0); return(1);/* The del_semvalue function has almost the same form, except the call to semctl uses the command IPC_RMID to remove the semaphores ID. */static void del_semvalue(void) union semun sem_union; if (semctl(sem_id, 0, IPC_RMID, sem_union) = -1) fprintf(stderr, Failed to delete semaphoren);/* semaphore_p changes the semaphore by -1 (waiting). */static int semaphore_p(void) struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = -1; /* P() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) = -1) fprintf(stderr, semaphore_p failedn); return(0); return(1);/* semaphore_v is similar except for setting the sem_op part of the sembuf structure to 1, so that the semaphore becomes available. */static int semaphore_v(void) struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = 1; /* V() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) = -1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论