




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、安工大Linux程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)書 部分實(shí)驗(yàn)完成不全請(qǐng)同學(xué)們自行補(bǔ)充 姓名:* 學(xué)號(hào):* 班級(jí):* 指導(dǎo)老師:阮越 時(shí)間:2013年5月31號(hào) 實(shí)驗(yàn)一 Linux基本命令的使用1、實(shí)驗(yàn)?zāi)康?#160;學(xué)習(xí)和掌握Linux的基本命令。2、實(shí)驗(yàn)內(nèi)容和步驟步驟1:以u(píng)ser_login用戶身份并使用telnet登錄Linux服務(wù)器,按照提示創(chuàng)建自己的賬戶和口令。步驟 2:使用新創(chuàng)建的用戶賬戶和口令登錄Linux系統(tǒng),察看登錄后的界面。步驟3:使用pwd命令察看當(dāng)前的工作目錄,然后用ls命令查看當(dāng)前目錄下的內(nèi)容,嘗試使用-a,-l,-F,-A,-lF等不同選項(xiàng)并比較不同之處。-a, -all:不隱藏任
2、何以“.” 字符開始的項(xiàng)目。-A, -almost-all:列出除了“ . ”及 “. ”以外的任何項(xiàng)目。-F, -classify:加上文件類型的指示符號(hào) (*/=| 其中一個(gè))。-l:使用較長格式列出信息。-lF :即使用長格式又加上類型的指示符號(hào)。步驟4:在當(dāng)前目錄下建立一個(gè)名為test的新目錄,然后將工作目錄切換到test下,嘗試將/etc目錄下的文件passwd拷貝到該目錄下(cp 源文件 目的目錄)。察看當(dāng)前目錄下的passwd文件的屬主和文件權(quán)限。步驟5:嘗試向當(dāng)前目錄下的passwd文件和/etc/passwd文件分別寫入一些新內(nèi)容(可使用echo “字符串” >>
3、文件的命令),看看操作能否成功,如果不能成功,請(qǐng)說明原因。用cat命令瀏覽文件password的內(nèi)容,用more命令進(jìn)行瀏覽翻頁操作,再用less命令瀏覽文件的內(nèi)容。比較這幾個(gè)命令的不同之處.對(duì)當(dāng)前目錄中成功,因?yàn)樵撃夸浵碌膒asswd文件對(duì)當(dāng)前用戶具有寫的權(quán)限。對(duì)/etc/passwd文件的操作被拒絕因?yàn)閷?duì)當(dāng)前用戶不具有寫的權(quán)限cat命令將文件的內(nèi)容全部顯示,more命令將文件內(nèi)容分屏顯示,less命令進(jìn)行刷新的全部顯示步驟6:用ls命令查看test下文件的權(quán)限,用mv命令更改文件password的文件名為test.txt,嘗試用chown和chgrp更改文件的屬主為root、組為root,
4、看看能否成功,不成功,請(qǐng)說明原因。嘗試用chomd將文件權(quán)限為“-rw-”??纯茨芊癯晒Γ怀晒?,請(qǐng)說明原因。用chown和chgrp更改文件的屬主為root、組為root,不成功。因?yàn)閏hown和chgrp一般是root的權(quán)限步驟7:用rm命令刪除test目錄下的所有文件,再用rmdir命令刪除test目錄。 (想一想有沒有一條命令將目錄及目錄下的所有文件刪除,寫出這條命令)rm -rf test步驟8:使用ps命令查看當(dāng)前系統(tǒng)內(nèi)的進(jìn)程,并利用man命令獲取ps命令的參數(shù),寫出獲取當(dāng)前終端進(jìn)程執(zhí)行情況的ps命令。步驟9:使用df命令查看當(dāng)前系統(tǒng)已安裝的文件系統(tǒng)的空間使用情況,記錄結(jié)
5、果。步驟10:使用du命令查看用戶的工作目錄占用了多少空間,記錄結(jié)果。步驟11:使用free命令查看內(nèi)存資源的使用情況,記錄結(jié)果。步驟12:使用man獲取tar和gzip的幫助信息,嘗試將test目錄下的文件打包并壓縮,然后到另外一目錄tmp下解包,寫出這幾條命令。tar zcvf /tmp/test.tar.gz /home/seasker/test步驟13:嘗試執(zhí)行“l(fā)s l > tmp”,看看這條命令的執(zhí)行會(huì)出現(xiàn)什么結(jié)果,解釋一下這條命令。將ls l輸出的結(jié)果輸出重定向到tmp文件中步驟14:嘗試執(zhí)行find /usr/src -name *.c -print | xargs gr
6、ep “#include”,看看這條命令的執(zhí)行會(huì)出現(xiàn)什么結(jié)果,解釋一下這條命令。解釋:匹配查找列表輸出/usr/src下以.c結(jié)尾的文件中含有“#include”的內(nèi)容。步驟15:執(zhí)行cal和date命令,說說這兩條指令的用途。cal:輸出當(dāng)月的日歷,date:輸出當(dāng)前的時(shí)間。步驟16:執(zhí)行命令clear和logout,退出系統(tǒng)。(想一想有沒有其他的方法,寫出這種方法)exit步驟17:執(zhí)行命令shutdown,關(guān)閉系統(tǒng)。(想一想有沒有更簡單的命令,寫出這條命令)init 0;3、實(shí)驗(yàn)結(jié)論通過對(duì)linux 的常用命令的學(xué)習(xí),讓我對(duì)linux的系統(tǒng)有了初步的認(rèn)識(shí)-這是一個(gè)完全基于命令操作的操作系
7、統(tǒng),這些命令組成了強(qiáng)大的命令系統(tǒng),提供了完善的功能。這些命令操作將為我以后的linux學(xué)習(xí)打下良好的基礎(chǔ)。實(shí)驗(yàn)二 簡單Shell程序設(shè)計(jì) 1、實(shí)驗(yàn)?zāi)康?#160;(1) 學(xué)習(xí)和掌握vi編輯器的基本使用方法。(2) 學(xué)習(xí)和掌握編寫簡單的shell程序。2、實(shí)驗(yàn)內(nèi)容和步驟2.1 vi的使用步驟1:以root用戶身份登錄進(jìn)入Linux 系統(tǒng)。步驟 2:啟動(dòng)Vi,切換到輸入模式,輸入一段英文:This is an exercise! We use Vi to edit it.
8、60;Left,down,up,right 步驟3:嘗試匹配其中的一串字符,寫出這命令及執(zhí)行結(jié)果。 在命令狀態(tài)下輸入 /Left 即匹配Left這個(gè)字符步驟4:嘗試替換其中的一串字符,寫出這命令及執(zhí)行結(jié)果。 在命令狀態(tài)下輸入 1.$/use/123 即把use字符替換為123步驟5:嘗試復(fù)制/刪除其中的一行或幾行文本,寫出命令及執(zhí)行結(jié)果。在命令狀態(tài)下輸入nyy(n為復(fù)制的行數(shù))然后把光標(biāo)移動(dòng)到要復(fù)制的地方輸入p可以粘貼 同樣輸入ndd即為刪除步驟6:嘗試復(fù)制/刪除其中的一個(gè)單詞或幾個(gè)字符,寫出命令及執(zhí)行結(jié)果。不斷的退格即可步驟7:嘗試使用鼠標(biāo)復(fù)制文本,寫出命令及執(zhí)行結(jié)果。步驟8:嘗試
9、存盤退出操作,寫出命令??梢杂玫拿?:wq 或:x2.2 簡單的shell程序步驟1:使用Vi將程序清單2-1的程序輸入。步驟 2:嘗試運(yùn)行該程序,如果不能運(yùn)行,原因是什么,怎樣修改。步驟3:執(zhí)行程序,寫出程序的執(zhí)行結(jié)果。步驟4:閱讀程序,掌握shell函數(shù)的編寫方法,補(bǔ)全函數(shù)file_run。for i in ls do if test -x $i;then echo $i fi Done步驟5:修改程序中的函數(shù)uers(),使得顯示在線用戶時(shí)不分行。(提示:可以使用循環(huán),對(duì)每個(gè)在線用戶分別處理。)echo "there are who|wc -l users online&qu
10、ot;for i in "who are who|awk 'print $1'"do echo -n $i echo -n " "done3、實(shí)驗(yàn)結(jié)論通過對(duì)shell程序的設(shè)計(jì),讓我對(duì)shell的語法,結(jié)構(gòu)和命令有了進(jìn)一步的認(rèn)識(shí),同時(shí)認(rèn)識(shí)到shell程序的強(qiáng)大,我可以比較方便的編寫一些基礎(chǔ)的腳本程序。清單2-1 menuMYDATE=date +%d/%m/%yTHIS_HOST=hostname -sUSER=whoamifile_run()echo "you must complete it"uers()echo
11、"there are who|wc -l users online"echo -e "who arenwho|awk 'print $1'" while :dotput clearcat <<aaa-User:$USERHost:$THIS_HOSTDate:$MYDATE-1 : List subdirectory in current directory 2 : List files in current directory which can run3 : See who is on the systemH : Help
12、 screenQ : Exit Menu-aaaecho -e -n "tYour Choice 1,2,3,H,Q >"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"esacec
13、ho -e -n "nntHit the return key to continue"read DUMMYDone實(shí)驗(yàn)三 Linux下簡單C程序設(shè)計(jì)與文件操作1、實(shí)驗(yàn)?zāi)康?#160;(1) 學(xué)習(xí)和掌握gcc等Linux的開發(fā)調(diào)試環(huán)境。(2) 學(xué)習(xí)并掌握Linux的文件操作。(3) 編寫并實(shí)現(xiàn)實(shí)驗(yàn)步驟6要求的程序。2、實(shí)驗(yàn)內(nèi)容和步驟步驟1:使用Vi將程序清單3-1和3-2的程序輸入,并在當(dāng)前目錄下創(chuàng)建文件“file.in”和文件“file.out”,盡可能的使文件“file.in”大一些。步驟 2:利用gcc分別編譯這兩個(gè)程序,寫出編譯命令和執(zhí)行結(jié)果,如果不成功,嘗試?yán)胓
14、db調(diào)試。gcc o list3_1 list3_1.cgcc o list3_2 list3_2.c步驟3:仔細(xì)觀察這兩個(gè)程序,比較標(biāo)準(zhǔn)C的文件操作和Linux的系統(tǒng)調(diào)用open、read、write等的使用區(qū)別。答:標(biāo)準(zhǔn)c的文件操作一個(gè)是庫函數(shù)的調(diào)用,linux中系統(tǒng)調(diào)用文件操是底層的文件調(diào)用,其運(yùn)行效率略有差距,同時(shí)參數(shù)也不同,返回的數(shù)據(jù)類型也不同。步驟4:按照說明重新修改程序3-2,并使用time命令察看程序執(zhí)行的時(shí)間效率上有何區(qū)別。 修改前:修改后:步驟5:輸入、編譯并運(yùn)行程序3-3和3-4,寫出執(zhí)行結(jié)果,并比較他們fseek和lseek在使用方法上的異同兩者都是對(duì)文件的讀寫指針進(jìn)行
15、設(shè)置但兩者的返回類型不同,fseek返回的是int型lseek返回的是off_t型步驟6:學(xué)習(xí)并分別使用標(biāo)準(zhǔn)C的文件操作函數(shù)和Linux的系統(tǒng)調(diào)用創(chuàng)建一個(gè)對(duì)學(xué)生基本信息進(jìn)行操作(插入、修改和刪除)的C程序,學(xué)生基本信息以結(jié)構(gòu)體的形式存儲(chǔ)在文件中,struct stu_info的定義如下:struct stu_info char stu_num12;char name10;short int sex;/*0為女生,1為男生*/char mobile_phone12;3、 實(shí)驗(yàn)結(jié)論通過對(duì)文件操作的編程,讓我掌握了文件的創(chuàng)建,打開,讀寫,以及特定位置的讀寫關(guān)閉等操作。并且從系統(tǒng)底層調(diào)
16、用和標(biāo)準(zhǔn)c函數(shù)庫兩個(gè)角度對(duì)文件的操作比較中,認(rèn)識(shí)到兩者的異同。4、程序清單/程序清單 3-1#include <stdio.h>#include <stdlib.h>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)!=EO
17、F)fputc(c,out);/程序清單 3-2#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>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);/將注釋打開,并將兩條語句的后一句注釋掉,重新編譯執(zhí)行。/
18、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 <stdio.h>#include <unistd.h>#include <fcntl.h>#include <sys/stat.h>#include <sys/types.h>char buf1 = "
19、;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!");
20、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 <stdio.h>#include <unistd.h>#include <fcntl.h>#include <sys/stat.h>#include <sys/types.h>char bu
21、f1 = "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
22、error!");if(lseek(fd,40,SEEK_SET)=-1)err_exit("lseek error!");if(write(fd,buf2,10)!=10)err_exit("file write buf2 error!");實(shí)驗(yàn)四 Linux下進(jìn)程操作與進(jìn)程間通信1、實(shí)驗(yàn)?zāi)康?#160;(1) 學(xué)習(xí)和掌握fork等系統(tǒng)調(diào)用的基本使用方法。(2) 利用Linux中的管道實(shí)現(xiàn)父子進(jìn)程間的同步。2、實(shí)驗(yàn)內(nèi)容與步驟步驟1:使用Vi將程序清單4-1的程序輸入、編譯并運(yùn)行,學(xué)習(xí)和掌握fork的基本調(diào)用方法。步驟 2:使用Vi將程序清單4
23、-2、4-3和4-4的程序輸入、利用gcc分別編譯這三個(gè)程序,寫出編譯命令和編譯結(jié)果,如果不成功,嘗試?yán)胓db調(diào)試。gcc -o list4_2 list4_2.cgcc -o father father.cgcc -o child child.c步驟3:寫出編譯這三個(gè)程序的makefile,然后利用make進(jìn)行編譯,談?wù)勥@么做的好處。可以節(jié)省勞動(dòng)時(shí)間,讓三個(gè)程序同時(shí)編譯,簡化了操作。步驟4:運(yùn)行這三個(gè)程序,寫出運(yùn)行結(jié)果。 步驟5:屏幕上顯示出的執(zhí)行結(jié)果是哪一個(gè)進(jìn)程執(zhí)行的結(jié)果?father和child步驟6:父進(jìn)程中的printf有向屏幕輸出嗎?為什么?沒有,因?yàn)楦高M(jìn)程的標(biāo)準(zhǔn)輸出已經(jīng)被重定向
24、。步驟7:利用父子進(jìn)程間的管道通信方式,改寫實(shí)驗(yàn)3步驟6要求的程序。要求啟用兩個(gè)進(jìn)程,其中父進(jìn)程接受用戶對(duì)文件的操作命令然后通過管道發(fā)給子進(jìn)程,子進(jìn)程完成對(duì)文件的實(shí)際操作。3、 實(shí)驗(yàn)結(jié)論 通過該節(jié)實(shí)驗(yàn),我更進(jìn)一步理解了fock函數(shù)dup函數(shù)exercl函數(shù)的工作原理,讓我了解到了進(jìn)程并發(fā)的概念。4、程序清單/程序清單 4-1#include <unistd.h>#include <stdio.h>#include "err_exit.h"int global = 5;int main(void)pid_t pid;char *stri
25、ng = "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,%sn""pid = %dn global = %dn local = %dn",getpid(),string,pid
26、,global,local);global+;elsestring = "I am parent."printf("nMy pid is %d,%sn""pid = %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 <u
27、nistd.h>#include<stdlib.h>#include <stdio.h>#define STD_INPUT 0/ 定義標(biāo)準(zhǔn)輸入設(shè)備描述符# define STD_OUTPUT 1/ 定義標(biāo)準(zhǔn)輸出設(shè)備描述符int fd2;void main()static char process1="father",process2="child" pipe(fd);/ 定義管道 pipeline(process1,process2);/ 調(diào)用自定義函數(shù)pipeline() exit(1);/ 程序結(jié)束pipeline(ch
28、ar* process1,char* process2)int i;if (i=fork()=-1) / 創(chuàng)建進(jìn)程,失敗退出 perror("process fork error!");exit(1);if (i) close(fd0);/ 關(guān)閉管道輸入描述符 close(STD_OUTPUT);/ 關(guān)閉標(biāo)準(zhǔn)輸出描述符1 dup(fd1);/ 指定標(biāo)準(zhǔn)輸出描述符1為管道寫指針 close(fd1);/ 關(guān)閉原始管道寫指針 execl(process1, process1, 0);/ 用程序father覆蓋當(dāng)前程序 printf("father failed.n&q
29、uot;);/ execl()執(zhí)行失敗 else close(fd1);/ 關(guān)閉管道輸出描述符 close(STD_INPUT);/ 關(guān)閉標(biāo)準(zhǔn)輸入描述符0 dup(fd0);/ 指定標(biāo)準(zhǔn)輸入描述符0為管道讀指針 close(fd0);/ 關(guān)閉原始管道讀指針 execl(process2,process2,0);/ 用程序child覆蓋當(dāng)前程序 printf("child failed.n");/ execl()執(zhí)行失敗 exit(2);/ 程序結(jié)束清單 4-3 father.c#include <unistd.h>#include <stdlib.h>
30、;#include <stdio.h>int main()static char string = "Parent is using pipe write."int len;len = sizeof(string); write(1, string, len);/* 將string中的內(nèi)容寫入管道中 */ printf("parent, parent, parent n n n"); exit(0);清單4-4 child.c#include <unistd.h>#include <stdio.h>int main()
31、 char output30 ; read (0, output, 30) ;/* 從管道中讀數(shù)據(jù)并存入output中 */ printf("%s n child, child. n", output) ; return(0) ;實(shí)驗(yàn)五 Linux線程的使用1、實(shí)驗(yàn)?zāi)康?#160;學(xué)習(xí)和掌握Linux線程的創(chuàng)建以及同步方法。2、實(shí)驗(yàn)內(nèi)容和步驟步驟1:輸入程序5-1,編譯并運(yùn)行寫出執(zhí)行結(jié)果。步驟 2:仔細(xì)研讀代碼,描述pthread_create函數(shù)中各個(gè)參數(shù)的意義,并給出線程的入口函數(shù)的調(diào)用方法,描述兩線程間參數(shù)傳遞的方式。int pthread_create(pthrea
32、d_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *),void *arg);第一個(gè)參數(shù)為指向線程標(biāo)識(shí)符的指針。第二個(gè)參數(shù)用來設(shè)置線程屬性。第三個(gè)參數(shù)是線程運(yùn)行函數(shù)的起始地址。最后一個(gè)參數(shù)是運(yùn)行函數(shù)的參數(shù)。主線程通過pthread_create函數(shù)向從線程a_thread傳遞share_int 而從線程a_thread通過pthread_join向主線程傳遞thread_result步驟3:打開注釋掉的全局變量aa及操作aa的相關(guān)代碼,回答這跟進(jìn)程有什么不同。線程共用相同的數(shù)據(jù)區(qū)而各個(gè)進(jìn)程有著自己獨(dú)立的數(shù)據(jù)
33、區(qū)。步驟4:輸入程序5-2,編譯并運(yùn)行,寫出運(yùn)行結(jié)果。步驟5:仔細(xì)研讀代碼,描述利用信號(hào)量實(shí)現(xiàn)線程間同步調(diào)用方法。sem_post 以原子操作的方式將信號(hào)量加1從而喚醒線程。sem_wait以原子操作將信號(hào)量減1當(dāng)信號(hào)量小于0時(shí)就會(huì)對(duì)進(jìn)程進(jìn)行等待,從而實(shí)現(xiàn)同步調(diào)用。步驟6:學(xué)習(xí)并使用線程間的同步方法,重新改寫實(shí)驗(yàn)3步驟6要求的程序。要求啟用兩個(gè)線程,其中主線程接受用戶對(duì)文件的操作命令然后發(fā)給子線程,子線程完成對(duì)文件的實(shí)際操作(注意線程間的同步)。3、 實(shí)驗(yàn)結(jié)論通過改節(jié)實(shí)驗(yàn),讓我對(duì)線程并發(fā)有了初步認(rèn)識(shí),同時(shí)學(xué)會(huì)用信號(hào)量進(jìn)行線程之間的同步。4、程序清單/程序清單 5-1#inclu
34、de <stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>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_in
35、t = -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",
36、(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#inclu
37、de <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <string.h>#include <pthread.h>#include <semaphore.h>void *thread_function(void *arg);sem_t bin_sem;#define WORK_SIZE 1024char work_areaWORK_SIZE;int main()int res;pthread_t a_threa
38、d;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 tex
39、t,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(
40、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(N
41、ULL);實(shí)驗(yàn)六 Linux進(jìn)程間的IPC1、實(shí)驗(yàn)?zāi)康?#160;學(xué)習(xí)和掌握Linux進(jìn)程間的IPC及同步方法。2、實(shí)驗(yàn)內(nèi)容和步驟步驟1:輸入程序6-1,6-2編譯并運(yùn)行寫出執(zhí)行結(jié)果。步驟 2:仔細(xì)研讀代碼,寫出程序中實(shí)現(xiàn)P、V操作的相關(guān)函數(shù)和代碼,描述程序的同步工作過程。P操作:if (!semaphore_p() exit(EXIT_FAILURE); printf("%c", op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_c
42、har); fflush(stdout);V操作: if (!semaphore_v() exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time);P操作后操作當(dāng)前的臨界區(qū)占用資源,v操作退出當(dāng)前的臨界區(qū),釋放資源。步驟3:輸入程序6-3和6-4,編譯執(zhí)行并寫出結(jié)果。步驟4:程序6-3和6-4中哪段代碼實(shí)現(xiàn)了共享,描述實(shí)現(xiàn)內(nèi)存共享的主要函數(shù)的參數(shù)意義和這些函數(shù)的使用方法。程序6_3:if(shm = shmat(shmid,NULL,0) = (char *)-1) perror("shmat error"
43、);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
44、 = '*'步驟5:學(xué)習(xí)并使用IPC中信號(hào)量和共享內(nèi)存的使用方法,重新改寫實(shí)驗(yàn)3步驟6要求的程序。要求啟動(dòng)多個(gè)進(jìn)程,每一個(gè)進(jìn)程都可以單獨(dú)對(duì)文件進(jìn)行操作,進(jìn)程間通過信號(hào)量進(jìn)行同步,對(duì)文件的操作映射到共享內(nèi)存中。3、 實(shí)驗(yàn)結(jié)論通過對(duì)改節(jié)實(shí)驗(yàn),讓我了解了進(jìn)程IPC信號(hào)量訪問臨界區(qū),以及進(jìn)程之間訪問共享內(nèi)存的實(shí)現(xiàn)方法和同步機(jī)制,掌握了上述一系列函數(shù)的使用方法。4、程序清單/程序清單 6-1#if defined(_GNU_LIBRARY_) && !defined(_SEM_SEMUN_UNDEFINED) /* union semun is defined by in
45、cluding <sys/sem.h> */#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 */ ;#
46、endif/程序清單 6-2#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/sem.h>#include "semun.h"static int set_semvalue(void);static void del_semvalue(void);static int semaphore_p(void);static int sem
47、aphore_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 t
48、o 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. */ f
49、or(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 goi
50、ng 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 - finishedn", getpid(); if (argc > 1) sleep(10); del_semvalue(); ex
51、it(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); r
52、eturn(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 semaph
53、oren");/* 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
54、);/* 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 failedn"); return(0); ret
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 南通智能裝備制造基地建設(shè)合作合同
- 報(bào)警器安裝施工合同
- 個(gè)人居間合同
- 借款合同抵押與擔(dān)保
- 民間借貸抵押合同年
- 冬季水果供應(yīng)鏈管理服務(wù)合同
- 森林資源培育與利用合作合同
- 沈陽科技學(xué)院《推銷理論與實(shí)務(wù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣州城市理工學(xué)院《科研實(shí)訓(xùn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 雙手向后拋實(shí)心球的練習(xí) 教學(xué)設(shè)計(jì)-2023-2024學(xué)年高一上學(xué)期體育與健康人教版必修第一冊(cè)
- 寧夏回族自治區(qū)地圖矢量動(dòng)態(tài)PPT模板(圖文)
- 教育測(cè)量與評(píng)價(jià)第三版PPT完整全套教學(xué)課件
- 中小學(xué)教師教育法律法規(guī)培訓(xùn)PPT頁
- 醫(yī)療器械可用性工程文檔
- 非遺文化介紹推廣課件
- 火電機(jī)組整套啟動(dòng)前安全技術(shù)交底卡
- 菲斯特轉(zhuǎn)子秤的
- 藥學(xué)專業(yè)教學(xué)資源庫建設(shè)申報(bào)書
- 解讀《泰州市市區(qū)城市排水管理辦法》
- 人教版五年級(jí)下冊(cè)口算題大全(全冊(cè)齊全)
- 林則徐課件完整版
評(píng)論
0/150
提交評(píng)論