Linux課程大作業(yè)_第1頁
Linux課程大作業(yè)_第2頁
Linux課程大作業(yè)_第3頁
Linux課程大作業(yè)_第4頁
Linux課程大作業(yè)_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Linux課程設(shè)計報告 題 目 Linux課程大作業(yè)院 系 班 級 姓 名 指導(dǎo)教師 一、基礎(chǔ)篇(給出源程序和編譯運行的結(jié)果)1、 編寫一個簡單的c語言程序:根據(jù)輸入的兩個整數(shù)求平均值并且在終端輸出,通過gcc編譯器得到它的匯編程序文件。源代碼(c):#includedouble average( int m,int n)return (m+n)/2.0;int main(void)int m,n=0; printf(請輸入兩個數(shù),回車分割n); scanf(%d,&m); scanf(%d,&n); printf(%d與%d的平均值是:%lfn,m,n,average(m,n);源代碼(匯編

2、):.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.cfi_restore 5

3、.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.sizeaverage, .-average.section.rodata.align 4.LC2:.string350257267350276223345205245344270244344270252346225260357274214345233236350275246345210206345211262.LC3:.string%d.LC4:.string%d344270216%d347232204345271263345235207345200274346230257357274232%lfn.text.gl

4、oblmain.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_scanfmovl$.LC3, %ea

5、xleal44(%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_restore 5.cfi_def

6、_cfa 4, 4ret.cfi_endproc.LFE1:.sizemain, .-main.section.rodata.align 8.LC0:.long0.long1073741824.identGCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.section.note.GNU-stack,progbits執(zhí)行結(jié)果:2、 編寫一個c語言程序:打印輸出所有“水仙花數(shù)”,用gdb調(diào)試程序(給出步驟,至少十步以上)。所謂“水仙花數(shù)”是指一個3位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如,153是一水仙花數(shù),因為153=1+5+3。源代碼:#includeint

7、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(number1000

8、&number0)printf(number limits outbounds!n);if(isnumber(number)printf(number is format!n);elseprintf(number is not format!n);return 0;執(zhí)行結(jié)果:3、 設(shè)計一個程序,要求輸出n個整數(shù)(n也由鍵盤輸入)中的最大值,并為它編寫makefile文件,用make編譯后修成返回最小值,再編譯,觀察有多少文件不需要重新編譯。源代碼:max.cdouble max(double m,double n)if(mn) return n; elsereturn m; main.c#in

9、clude#includedefine.hint main(void)double m,n=0; printf(請輸入兩個實數(shù)、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結(jié)果圖: 改變程序后,只有max.

10、c,main.c文件程序需要重新編譯;4、 編寫一個程序,求2-n間的素數(shù),n由鍵盤輸入,循環(huán)變量分別從2到n、2到(int)sqrt(n),分別測出兩個循環(huá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是素數(shù)n,i);printf(nn循環(huán)時間%d,t1);for(i2=2;i2=(int)sqr

11、t(n);i2+)t2+;k2=sqrt(i2);for(j2=2;j2=k+1)printf(%d是素數(shù)n,i2);printf(nsqrt(n)循環(huán)時間%dn,t2);return 0;編譯運行效果:5、設(shè)計一個程序,要求將10分別以十進制、八進制和十六進制輸出。程序設(shè)計:#includeint main()int number=10;printf(十進制值:%dn,number);printf(八進制值:%on,number);printf(十六進制值:%xn,number); 運行結(jié)果:一、 提高篇(三選二,劃出程序流程圖,給出程序源代碼和編譯運行的結(jié)果)1、 設(shè)計兩個程序,要求用命名

12、管道FIFO,實現(xiàn)簡單的文本文件或圖片文件的傳輸功能。流程圖: 源代碼: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);printf(%

13、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(%s,

14、s); write(fd2, s, 128); printf(fd2=%dn,fd2);close(fd2);close(fd); return 0;運行結(jié)果:2、 設(shè)計兩個程序,要求用消息隊列,實現(xiàn)聊天程序,每次發(fā)言后自動在后面增加當(dāng)前系統(tǒng)時間。增加結(jié)束字符,比如最后輸入“88”后結(jié)束進程。流程圖:發(fā)送源源代碼:#include#include#include#include#includestruct msgbufint type;char ptr0;int main(int argc,char *argv)key_t key;key=ftok(argv1,100);int msgid;m

15、sgid=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 msg

16、bufint 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=ms

17、gget(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(ptr);

18、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);運行結(jié)果:3、設(shè)計兩個程序,要求用mmap系統(tǒng),實現(xiàn)簡單的聊天程序。二、 應(yīng)用篇給出程序設(shè)計思路和程序設(shè)計流程圖,并給出程序源代碼和編譯運行的結(jié)果。設(shè)計思路:要求生產(chǎn)者-消費者在固定的倉庫空間條件下,生產(chǎn)者每生產(chǎn)一個產(chǎn)品將占用一個倉庫空間,生產(chǎn)者生產(chǎn)的

19、產(chǎn)品庫存不能越過倉庫的存儲量,消費者每消費一個產(chǎn)品將增加一個倉庫空間,消費者在倉庫產(chǎn)品為零時不能再消費。以下使用了兩個信號量,一個用來管理消費者(sem_produce),一個用來管理生產(chǎn)者(sem_custom),sem_produce表示當(dāng)前倉庫可用空間的數(shù)量,sem_custom表示當(dāng)前倉庫中產(chǎn)品的數(shù)量。對于生產(chǎn)者來說,其需要申請的資源為倉庫中的剩余空間,因此,生產(chǎn)者在生產(chǎn)一個產(chǎn)品前需申請sem_produce信號量,當(dāng)此信號量的值大于零時,即有可用空間,將生產(chǎn)產(chǎn)品,并將sem_produce值減1,同時,當(dāng)其生產(chǎn)一個產(chǎn)品后,當(dāng)前倉庫的產(chǎn)品數(shù)量加1,需要將sem_custom的值自動加1

20、.對于消費者來說,其需要申請的資源為倉庫中的產(chǎn)品,因此,消費者在消費一個產(chǎn)品前需申請sem_custom信號量,當(dāng)此信號量的值大于零時,即有可用產(chǎn)品,將消費一個產(chǎn)品,并將sem_custom值減1,同時,當(dāng)其消費一個產(chǎn)品后,當(dāng)前倉庫的剩余空間加1,需要sem_produce的值自動加。1、 生產(chǎn)者-消費者問題模擬程序。流程圖:生產(chǎn)者源代碼:#include #include #include #include #include #include #include int sem_id;void init()key_t key;int ret;unsigned short sem_array2;

21、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(se

22、m_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_num

23、 = 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();消費者源代碼:#include #include #include #include #include #include #include int sem_id

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論