文档详情

安工大Linux实验指导书

仙***
实名认证
店铺
DOC
472.50KB
约34页
文档ID:172171074
安工大Linux实验指导书_第1页
1/34

安工大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,切换到输入模式,输入一段英文:This is an exercise!  We use Vi to edit it. 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 online"for i in "who are `who|awk '{print $1}'`"do echo -n $i echo -n " "done3、实验结论通过对shell程序的设计,让我对shell的语法,结构和命令有了进一步的认识,同时认识到shell程序的强大,我可以比较方便的编写一些基础的脚本程序清单2-1 menuMYDATE=`date +%d/%m/%y`THIS_HOST=`hostname -s`USER=`whoami`file_run(){echo "you must complete it"}uers(){echo "there are `who|wc -l` users online"echo -e "who are\n`who|awk '{print $1}'`" }while :do tput clear cat <" read CHOICE case $CHOICE in 1) ls -l|awk '/^d/ {print $8}' ;; 2) file_run ;; 3) uers ;; H|h) cat <

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程序,学生基本信息以结构体的形式存储在文件stu.info中,struct stu_info的定义如下:struct stu_info { char stu_num[12]; char name[10]; short int sex;/*0为女生,1为男生*/ char mobile_phone[12];};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 block[1024]; 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要求的程序要求启用两个进程,其中父进程接受用户对文件stu.info的操作命令然后通过管道发给子进程,子进程完成对文件的实际操作3、 实验结论 通过该节实验,我更进一步理解了fock函数dup函数exercl函数的工作原理,让我了解到了进程并发的概念。

4、程序清单//程序清单 4-1#include #include #include "err_exit.h"int global = 5;int main(void){ pid_t pid; char *string = "these are values before fork:"; int local = 10; printf("before fork * * *\n\n"); if((pid = fork())<0) err_exit("fork error"); if(pid == 0){ string = "I am child."; printf("\nMy pid is %d,%s\n" "pid = %d\n global = %d\n local = %d\n", getpid(),string,pid,global,local); global++; } else{ string = "I am parent."; printf("\nMy pid is %d,%s\n" "pid = %d\n global = %d\n local = %d\n", getpid(),string,pid,global,local); local++; } printf("%s\n Now,global = %d,local = %d\n",string,global,local); exit(EXIT_SUCCESS);}//清单4-2 管道程序#include #include#include #define STD_INPUT 0 // 定义标准输入设备描述符# define STD_OUTPUT 1 // 定义标准输出设备描述符int fd[2];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(fd[0]); // 关闭管道输入描述符 close(STD_OUTPUT); // 关闭标准输出描述符1 dup(fd[1]); // 指定标准输出描述符1为管道写指针 close(fd[1]); // 关闭原始管道写指针 execl(process1, process1, 0); // 用程序father覆盖当前程序 printf("father failed.\n"); // execl()执行失败 } else { close(fd[1]); // 关闭管道输出描述符 close(STD_INPUT); // 关闭标准输入描述符0 dup(fd[0]); // 指定标准输入描述符0为管道读指针 close(fd[0]); // 关闭原始管道读指针 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 output[30] ; 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要求的程序要求启用两个线程,其中主线程接受用户对文件stu.info的操作命令然后发给子线程,子线程完成对文件的实际操作(注意线程间的同步)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 %s\n",(char *)thread_result); printf("share_int is now %d\n",share_int); exit(EXIT_SUCCESS);}void *thread_function(void *arg){ printf("thread_function is running.Argument was %d\n",*(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_area[WORK_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 finish\n"); 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 joined\n"); 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 characters\n",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.h"static 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 = %d\n",sem_id); if (argc > 1) { if (!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore\n"); 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 < 10; i++) { 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);/* After the critical section, we call semaphore_v, setting the semaphore available, before going through the for loop again after a random wait. After the loop, the call to del_semvalue is made to clean up the code. */ if (!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time); } printf("\n%d - finished\n", getpid()); if (argc > 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 semaphore's 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 semaphore\n");}/* 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 failed\n"); 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) { fprintf(stderr, "semaphore_v failed\n"); return(0); } return(1);}//程序清单 6-3#include #include #include #include #include #include #define SHMSZ 27int main(void){ char c; int shmid; key_t key; char *shm,*s; key = 1234; if((shmid = shmget(key,SHMSZ,IPC_CREAT|0666))<0) { perror("shmget error"); exit(0); } 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); exit(0);}//程序清单 6-4#include #include #include #include #include #include #define SHMSZ 27int main(void){ int shmid; key_t key; char *shm,*s; key = 1234; if((shmid = shmget(key,SHMSZ,0666)) < 0){ perror("shmget error!"); exit(0); } if((shm = shmat(shmid,NULL,0)) == (char *)-1){ perror("shmat error!"); exit(0); } for(s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); *shm = '*'; exit(0);}实验七 Linux下访问Mysql数据库1、实验目的 学习和掌握Mysql数据库及编程访问方法。

2、实验内容和步骤步骤1:在Mysql中使用sql命令create database foo命令创建数据库foo步骤 2:在数据库foo中创建表childrenCreate table children { Childno int(11) default ‘0’not null auto_increment, Fname varchar(30), Age int(11), Primary key (childno));步骤3:向表children中插入若干记录Insert into children values (1,’Jenny’,17);Insert into children values (2,’Andrew’,13);Insert into children values (3,’Alex’,12);Insert into children values (4,’Tom’,11);Insert into children values (5,’Jack’,7);Insert into children values (6,’Ella’,14);步骤4:输入、编译并运行程序7-1、7-2和7-3,写出运行结果。

步骤5:学习并使用C语言访问Mysql的基本方法,重新改写实验3步骤6要求的程序要求使用数据库保存stu.info结构3、 实验结论通过改节实验我掌握了linux下怎么用c语言进行简单的数据库方面的编程4、程序清单//程序清单 7-1#include #include #include "mysql.h"。

下载提示
相关文档
正为您匹配相似的精品文档