《Linux课程大作业(共21页).doc》由会员分享,可在线阅读,更多相关《Linux课程大作业(共21页).doc(21页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上Linux课程设计报告 题 目 Linux课程大作业院 系 班 级 姓 名 指导教师 一、基础篇(给出源程序和编译运行的结果)1、 编写一个简单的c语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。源代码(c):#includedouble average( int m,int n)return (m+n)/2.0;int main(void)int m,n=0; printf(请输入两个数,回车分割n); scanf(%d,&m); scanf(%d,&n); printf(%d与%d的平均值是:%lfn,m,n,averag
2、e(m,n);源代码(汇编):.filesum.c.text.globlaverage.typeaverage, functionaverage:.LFB0:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5subl$8, %espmovl12(%ebp), %eaxmovl8(%ebp), %edxaddl%edx, %eaxmovl%eax, -4(%ebp)fildl-4(%ebp)fldl.LC0fdivrp%st, %st(1)leave.
3、cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.sizeaverage, .-average.section.rodata.align 4.LC2:.string350257267350276223345205245344270244344270252346225260357274214345233236350275246345210206345211262.LC3:.string%d.LC4:.string%d344270216%d34723220434527126334523520734520027434623025735727423
4、2%lfn.text.globlmain.typemain, functionmain:.LFB1:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5andl$-16, %espsubl$48, %espmovl$0, 44(%esp)movl$.LC2, (%esp)callputsmovl$.LC3, %eaxleal40(%esp), %edxmovl%edx, 4(%esp)movl%eax, (%esp)call_isoc99_scanfm
5、ovl$.LC3, %eaxleal44(%esp), %edxmovl%edx, 4(%esp)movl%eax, (%esp)call_isoc99_scanfmovl44(%esp), %edxmovl40(%esp), %eaxmovl%edx, 4(%esp)movl%eax, (%esp)callaveragemovl44(%esp), %ecxmovl40(%esp), %edxmovl$.LC4, %eaxfstpl12(%esp)movl%ecx, 8(%esp)movl%edx, 4(%esp)movl%eax, (%esp)callprintfleave.cfi_rest
6、ore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE1:.sizemain, .-main.section.rodata.align 8.LC0:.long0.long.identGCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.section.note.GNU-stack,progbits执行结果:2、 编写一个c语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤,至少十步以上)。所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一水仙花数,因为153=1+5+3。源代码:#includei
7、nt isnumber(int number) int ge=(number%100)%10; int shi=(number/10)%10; int bai=(number/100); printf(%dn,ge); printf(%dn,shi); printf(%dn,bai); int temp=ge*ge*ge+shi*shi*shi+bai*bai*bai; if(temp=number) return 1; elsereturn 0;int main()int number=0;printf(input number!n);scanf(%d,&number);if(number1
8、000&number0)printf(number limits outbounds!n);if(isnumber(number)printf(number is format!n);elseprintf(number is not format!n);return 0;执行结果:3、 设计一个程序,要求输出n个整数(n也由键盘输入)中的最大值,并为它编写makefile文件,用make编译后修成返回最小值,再编译,观察有多少文件不需要重新编译。源代码:max.cdouble max(double m,double n)if(mn) return n; elsereturn m; main.c
9、#include#includedefine.hint main(void)double m,n=0; printf(请输入两个实数、n); scanf(%lf,&m); scanf(%lf,&n); printf(%lf与%lf最大值是:%lfn,m,n,max(m,n);define.hdouble max(double m,double n);makefile文件:test:max.o main.ogcc max.o main.o -o testmain.o:main.c define.hgcc main.c -cmax.o:max.cgcc max.c -c结果图: 改变程序后,只有m
10、ax.c,main.c文件程序需要重新编译;4、 编写一个程序,求2-n间的素数,n由键盘输入,循环变量分别从2到n、2到(int)sqrt(n),分别测出两个循环的所用时间。源代码:#include stdio.h#include math.hint main()int t1=0,t2=0;int n;int j,k,j2,k2;int i,i2;printf(输入的值:);scanf(%d,&n);for(i=2;i=n;i+)t1+;k=sqrt(i);for(j=2;j=k+1)printf(%d是素数n,i);printf(nn循环时间%d,t1);for(i2=2;i2=(int)
11、sqrt(n);i2+)t2+;k2=sqrt(i2);for(j2=2;j2=k+1)printf(%d是素数n,i2);printf(nsqrt(n)循环时间%dn,t2);return 0;编译运行效果:5、设计一个程序,要求将10分别以十进制、八进制和十六进制输出。程序设计:#includeint main()int number=10;printf(十进制值:%dn,number);printf(八进制值:%on,number);printf(十六进制值:%xn,number); 运行结果:一、 提高篇(三选二,划出程序流程图,给出程序源代码和编译运行的结果)1、 设计两个程序,要求
12、用命名管道FIFO,实现简单的文本文件或图片文件的传输功能。流程图: 源代码:send.c#include #include #include #include #include #include #include #include int main()char s128;int fd;FILE *fp;fp = fopen(./a.txt, r);mkfifo(/tmp/fifo.tst, 0644);fd = open(/tmp/fifo.tst, O_WRONLY);while(fgets(s, 127, fp) != NULL) write(fd, s, strlen(s);print
13、f(%s,s);close(fd);fclose(fp);unlink(/tmp/fifo.tst);return 0;get.c/*get.c*/#include #include #include #include #include #include #include #include int main()char s128;int fd = open(/tmp/fifo.tst, O_RDONLY);int fd2 = open(./b.txt, O_CREAT|O_WRONLY);memset(s, 0, 128); while(read(fd, s, 128) 0) printf(%
14、s, s); write(fd2, s, 128); printf(fd2=%dn,fd2);close(fd2);close(fd); return 0;运行结果:2、 设计两个程序,要求用消息队列,实现聊天程序,每次发言后自动在后面增加当前系统时间。增加结束字符,比如最后输入“88”后结束进程。流程图:发送源源代码:#include#include#include#include#includestruct msgbufint type;char ptr0;int main(int argc,char *argv)key_t key;key=ftok(argv1,100);int msgi
15、d;msgid=msgget(key,IPC_CREAT|0600);pid_t pid;pid=fork();if(pid=0)while(1)printf(pls input msg to send:);char buf128;fgets(buf,128,stdin);struct msgbuf *ptr=malloc(sizeof(struct msgbuf)+strlen(buf)+1);ptr-type=1;memcpy(ptr-ptr,buf,strlen(buf)+1);msgsnd(msgid,ptr,strlen(buf)+1,0);free(ptr);elsestruct
16、msgbufint type;char ptr1024;while(1)struct msgbuf mybuf;memset(&mybuf,0,sizeof(mybuf);msgrcv(msgid,&mybuf,1024,2,0);printf(recv msg:%sn,mybuf.ptr);接收端源代码:#include#include#include#include#includestruct msgbufint type;char ptr0;int main(int argc,char *argv)key_t key;key=ftok(argv1,100);int msgid;msgid
17、=msgget(key,IPC_CREAT|0600);pid_t pid;pid=fork();if(pid=0)/sendwhile(1)printf(pls input msg to send:);char buf128;fgets(buf,128,stdin);struct msgbuf *ptr=malloc(sizeof(struct msgbuf)+strlen(buf)+1);ptr-type=2;/send msg type=2memcpy(ptr-ptr,buf,strlen(buf)+1);msgsnd(msgid,ptr,strlen(buf)+1,0);free(pt
18、r);elsestruct msgbufint type;char ptr1024;while(1)struct msgbuf mybuf;memset(&mybuf,0,sizeof(mybuf);msgrcv(msgid,&mybuf,1024,1,0);/recv msg type=2printf(recv msg:%sn,mybuf.ptr);运行结果:3、设计两个程序,要求用mmap系统,实现简单的聊天程序。二、 应用篇给出程序设计思路和程序设计流程图,并给出程序源代码和编译运行的结果。设计思路:要求生产者-消费者在固定的仓库空间条件下,生产者每生产一个产品将占用一个仓库空间,生产者
19、生产的产品库存不能越过仓库的存储量,消费者每消费一个产品将增加一个仓库空间,消费者在仓库产品为零时不能再消费。以下使用了两个信号量,一个用来管理消费者(sem_produce),一个用来管理生产者(sem_custom),sem_produce表示当前仓库可用空间的数量,sem_custom表示当前仓库中产品的数量。对于生产者来说,其需要申请的资源为仓库中的剩余空间,因此,生产者在生产一个产品前需申请sem_produce信号量,当此信号量的值大于零时,即有可用空间,将生产产品,并将sem_produce值减1,同时,当其生产一个产品后,当前仓库的产品数量加1,需要将sem_custom的值自
20、动加1.对于消费者来说,其需要申请的资源为仓库中的产品,因此,消费者在消费一个产品前需申请sem_custom信号量,当此信号量的值大于零时,即有可用产品,将消费一个产品,并将sem_custom值减1,同时,当其消费一个产品后,当前仓库的剩余空间加1,需要sem_produce的值自动加。1、 生产者-消费者问题模拟程序。流程图:生产者源代码:#include #include #include #include #include #include #include int sem_id;void init()key_t key;int ret;unsigned short sem_arra
21、y2;union semun int val;struct semid_ds *buf;unsigned short *array;arg;key=ftok(.,s);sem_id=semget(key,2,IPC_CREAT|0644);sem_array0=0;/identify the productorsem_array1=100;/identify the space/printf(set the productor init value is 0nset the space init value is 100n);arg.array = sem_array;ret = semctl
22、(sem_id, 0, SETALL, arg);if (ret = -1) printf(SETALL failed (%d)n, errno); /printf(nread the numbern);printf(productor init is %dn,semctl(sem_id,0,GETVAL);printf(space init is %dnn,semctl(sem_id,1,GETVAL);void del()semctl(sem_id,IPC_RMID,0);int main(int argc,char *argv)struct sembuf sops2;sops0.sem_
23、num = 0;sops0.sem_op = 1;sops0.sem_flg = 0;sops1.sem_num = 1;sops1.sem_op = -1;sops1.sem_flg = 0;init();printf(this is productorn);while(1)printf(nnbefore produce:n);printf(productor number is %dn,semctl(sem_id,0,GETVAL);printf(space number is %dn,semctl(sem_id,1,GETVAL);semop(sem_id,(struct sembuf
24、*)&sops1,1);/get the space to instore the productorprintf(now producing.n);semop(sem_id,(struct sembuf *)&sops0,1);/now tell the customer can bu cusumeprintf(nafter producen);printf(spaces number is %dn,semctl(sem_id,1,GETVAL);printf(productor number is %dn,semctl(sem_id,0,GETVAL);sleep(4);del();消费者
25、源代码:#include #include #include #include #include #include #include int sem_id;void init()key_t key;key=ftok(.,s);sem_id=semget(key,2,IPC_CREAT|0644);/printf(sem id is %dn,sem_id);int main(int argc,char *argv)init();struct sembuf sops2;sops0.sem_num = 0;sops0.sem_op = -1;sops0.sem_flg = 0;sops1.sem_n
26、um = 1;sops1.sem_op = 1;sops1.sem_flg = 0;init();printf(this is customern);while(1)printf(nnbefore consume:n);printf(productor is %dn,semctl(sem_id,0,GETVAL);printf(space is %dn,semctl(sem_id,1,GETVAL);semop(sem_id,(struct sembuf *)&sops0,1);/get the productor to cusumeprintf(now consuming.n);semop(sem_id,(struct sembuf *)&sops1,1);/now tell the productor can bu produceprintf(nafter consumen);printf(products number is %dn,semctl(sem_id,0,GETVAL);printf(space number is %dn,semctl(sem_id,1,GETVAL);sleep(3);编译运行结果:3、实现基于GTK+的网络聊天室,实现通信双方的实时通信。设计思想:初始化聊天窗口界面,建立socket连接,实现一对一的通信。流程图:专心-专注-专业