




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、安工大Linux程序設(shè)計實(shí)驗指導(dǎo)書 部分實(shí)驗完成不全請同學(xué)們自行補(bǔ)充 姓名:* 學(xué)號:* 班級:* 指導(dǎo)老師:阮越 時間:2013年5月31號 實(shí)驗一 Linux基本命令的使用1、實(shí)驗?zāi)康膶W(xué)習(xí)和掌握Linux的基本命令。2、實(shí)驗內(nèi)容和步驟步驟1:以user_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等不同選項并比較不同之處。-a, -all:不隱藏任何以“.”
2、字符開始的項目。-A, -almost-all:列出除了“ . ”及 “. ”以外的任何項目。-F, -classify:加上文件類型的指示符號 (*/=| 其中一個)。-l:使用較長格式列出信息。-lF :即使用長格式又加上類型的指示符號。步驟4:在當(dāng)前目錄下建立一個名為test的新目錄,然后將工作目錄切換到test下,嘗試將/etc目錄下的文件passwd拷貝到該目錄下(cp 源文件 目的目錄)。察看當(dāng)前目錄下的passwd文件的屬主和文件權(quán)限。步驟5:嘗試向當(dāng)前目錄下的passwd文件和/etc/passwd文件分別寫入一些新內(nèi)容(可使用echo “字符串” 文件的命令),看看操作能否成
3、功,如果不能成功,請說明原因。用cat命令瀏覽文件password的內(nèi)容,用more命令進(jìn)行瀏覽翻頁操作,再用less命令瀏覽文件的內(nèi)容。比較這幾個命令的不同之處.對當(dāng)前目錄中成功,因為該目錄下的passwd文件對當(dāng)前用戶具有寫的權(quán)限。對/etc/passwd文件的操作被拒絕因為對當(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、原因。嘗試用chomd將文件權(quán)限為“-rw-”??纯茨芊癯晒Γ怀晒?,請說明原因。用chown和chgrp更改文件的屬主為root、組為root,不成功。因為chown和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é)果。步驟10:使用du命令查看用戶的工作
5、目錄占用了多少空間,記錄結(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í)行會出現(xiàn)什么結(jié)果,解釋一下這條命令。將ls l輸出的結(jié)果輸出重定向到tmp文件中步驟14:嘗試執(zhí)行find /usr/src -name *.c -print | xargs grep “#include”,看看這條命令的執(zhí)行會出
6、現(xiàn)什么結(jié)果,解釋一下這條命令。解釋:匹配查找列表輸出/usr/src下以.c結(jié)尾的文件中含有“#include”的內(nèi)容。步驟15:執(zhí)行cal和date命令,說說這兩條指令的用途。cal:輸出當(dāng)月的日歷,date:輸出當(dāng)前的時間。步驟16:執(zhí)行命令clear和logout,退出系統(tǒng)。(想一想有沒有其他的方法,寫出這種方法)exit步驟17:執(zhí)行命令shutdown,關(guān)閉系統(tǒng)。(想一想有沒有更簡單的命令,寫出這條命令)init 0;3、實(shí)驗結(jié)論通過對linux 的常用命令的學(xué)習(xí),讓我對linux的系統(tǒng)有了初步的認(rèn)識-這是一個完全基于命令操作的操作系統(tǒng),這些命令組成了強(qiáng)大的命令系統(tǒng),提供了完善的功能
7、。這些命令操作將為我以后的linux學(xué)習(xí)打下良好的基礎(chǔ)。實(shí)驗二 簡單Shell程序設(shè)計1、實(shí)驗?zāi)康模?) 學(xué)習(xí)和掌握vi編輯器的基本使用方法。(2) 學(xué)習(xí)和掌握編寫簡單的shell程序。2、實(shí)驗內(nèi)容和步驟2.1 vi的使用步驟1:以root用戶身份登錄進(jìn)入Linux系統(tǒng)。步驟 2:啟動Vi,切換到輸入模式,輸入一段英文:Thisisanexercise!WeuseVitoeditit.Left,down,up,right步驟3:嘗試匹配其中的一串字符,寫出這命令及執(zhí)行結(jié)果。 在命令狀態(tài)下輸入 /Left 即匹配Left這個字符步驟4:嘗試替換其中的一串字符,寫出這命令及執(zhí)行結(jié)果。 在命令狀態(tài)下
8、輸入 1.$/use/123 即把use字符替換為123步驟5:嘗試復(fù)制/刪除其中的一行或幾行文本,寫出命令及執(zhí)行結(jié)果。在命令狀態(tài)下輸入nyy(n為復(fù)制的行數(shù))然后把光標(biāo)移動到要復(fù)制的地方輸入p可以粘貼 同樣輸入ndd即為刪除步驟6:嘗試復(fù)制/刪除其中的一個單詞或幾個字符,寫出命令及執(zhí)行結(jié)果。不斷的退格即可步驟7:嘗試使用鼠標(biāo)復(fù)制文本,寫出命令及執(zhí)行結(jié)果。步驟8:嘗試存盤退出操作,寫出命令??梢杂玫拿?:wq 或:x2.2 簡單的shell程序步驟1:使用Vi將程序清單2-1的程序輸入。步驟 2:嘗試運(yùn)行該程序,如果不能運(yùn)行,原因是什么,怎樣修改。步驟3:執(zhí)行程序,寫出程序的執(zhí)行結(jié)果。步驟4
9、:閱讀程序,掌握shell函數(shù)的編寫方法,補(bǔ)全函數(shù)file_run。for i in ls do if test -x $i;then echo $i fi Done步驟5:修改程序中的函數(shù)uers(),使得顯示在線用戶時不分行。(提示:可以使用循環(huán),對每個在線用戶分別處理。)echo there are who|wc -l users onlinefor i in who are who|awk print $1do echo -n $i echo -n done3、實(shí)驗結(jié)論通過對shell程序的設(shè)計,讓我對shell的語法,結(jié)構(gòu)和命令有了進(jìn)一步的認(rèn)識,同時認(rèn)識到shell程序的強(qiáng)大,我可以
10、比較方便的編寫一些基礎(chǔ)的腳本程序。清單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 aaaT
11、his 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實(shí)驗三 Linux下簡單C程序設(shè)計與文件操作1、實(shí)驗?zāi)康模?) 學(xué)習(xí)和掌握gcc等Linux的開發(fā)調(diào)試環(huán)境。(2) 學(xué)習(xí)并掌握Linux的文件操作。(3) 編寫并實(shí)現(xiàn)實(shí)驗步驟6要求的程序。2、實(shí)驗內(nèi)容和步驟步驟1:使用Vi將程序清單3-1和3-2的程序輸入,并在當(dāng)前
12、目錄下創(chuàng)建文件“file.in”和文件“file.out”,盡可能的使文件“file.in”大一些。步驟 2:利用gcc分別編譯這兩個程序,寫出編譯命令和執(zhí)行結(jié)果,如果不成功,嘗試?yán)胓db調(diào)試。gcc o list3_1 list3_1.cgcc o list3_2 list3_2.c步驟3:仔細(xì)觀察這兩個程序,比較標(biāo)準(zhǔn)C的文件操作和Linux的系統(tǒng)調(diào)用open、read、write等的使用區(qū)別。答:標(biāo)準(zhǔn)c的文件操作一個是庫函數(shù)的調(diào)用,linux中系統(tǒng)調(diào)用文件操是底層的文件調(diào)用,其運(yùn)行效率略有差距,同時參數(shù)也不同,返回的數(shù)據(jù)類型也不同。步驟4:按照說明重新修改程序3-2,并使用time命令察
13、看程序執(zhí)行的時間效率上有何區(qū)別。 修改前:修改后:步驟5:輸入、編譯并運(yùn)行程序3-3和3-4,寫出執(zhí)行結(jié)果,并比較他們fseek和lseek在使用方法上的異同兩者都是對文件的讀寫指針進(jìn)行設(shè)置但兩者的返回類型不同,fseek返回的是int型lseek返回的是off_t型步驟6:學(xué)習(xí)并分別使用標(biāo)準(zhǔn)C的文件操作函數(shù)和Linux的系統(tǒng)調(diào)用創(chuàng)建一個對學(xué)生基本信息進(jìn)行操作(插入、修改和刪除)的C程序,學(xué)生基本信息以結(jié)構(gòu)體的形式存儲在文件中,struct stu_info的定義如下:struct stu_info char stu_num12;char name10;short int se
14、x;/*0為女生,1為男生*/char mobile_phone12;3、 實(shí)驗結(jié)論通過對文件操作的編程,讓我掌握了文件的創(chuàng)建,打開,讀寫,以及特定位置的讀寫關(guān)閉等操作。并且從系統(tǒng)底層調(diào)用和標(biāo)準(zhǔn)c函數(shù)庫兩個角度對文件的操作比較中,認(rèn)識到兩者的異同。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)!=
15、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);/將注釋打開,并將兩條語句的后一句注釋掉,重新編譯執(zhí)行。/ while(nread = read(in,block,sizeof(block) 0) while(nread = read(in,&c,s
16、izeof(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(bu
17、f1,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_e
18、xit(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 wri
19、te buf2 error!);實(shí)驗四 Linux下進(jìn)程操作與進(jìn)程間通信1、實(shí)驗?zāi)康模?) 學(xué)習(xí)和掌握fork等系統(tǒng)調(diào)用的基本使用方法。(2) 利用Linux中的管道實(shí)現(xiàn)父子進(jìn)程間的同步。2、實(shí)驗內(nèi)容與步驟步驟1:使用Vi將程序清單4-1的程序輸入、編譯并運(yùn)行,學(xué)習(xí)和掌握fork的基本調(diào)用方法。步驟 2:使用Vi將程序清單4-2、4-3和4-4的程序輸入、利用gcc分別編譯這三個程序,寫出編譯命令和編譯結(jié)果,如果不成功,嘗試?yán)胓db調(diào)試。gcc -o list4_2 list4_2.cgcc -o father father.cgcc -o child child.c步驟3:寫出編譯這三個程
20、序的makefile,然后利用make進(jìn)行編譯,談?wù)勥@么做的好處??梢怨?jié)省勞動時間,讓三個程序同時編譯,簡化了操作。步驟4:運(yùn)行這三個程序,寫出運(yùn)行結(jié)果。 步驟5:屏幕上顯示出的執(zhí)行結(jié)果是哪一個進(jìn)程執(zhí)行的結(jié)果?father和child步驟6:父進(jìn)程中的printf有向屏幕輸出嗎?為什么?沒有,因為父進(jìn)程的標(biāo)準(zhǔn)輸出已經(jīng)被重定向。步驟7:利用父子進(jìn)程間的管道通信方式,改寫實(shí)驗3步驟6要求的程序。要求啟用兩個進(jìn)程,其中父進(jìn)程接受用戶對文件的操作命令然后通過管道發(fā)給子進(jìn)程,子進(jìn)程完成對文件的實(shí)際操作。3、 實(shí)驗結(jié)論 通過該節(jié)實(shí)驗,我更進(jìn)一步理解了fock函數(shù)dup函數(shù)exercl函數(shù)的
21、工作原理,讓我了解到了進(jìn)程并發(fā)的概念。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 = %d
22、n 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 #inclu
23、de#include #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(char* process1,char* process2)int i;if (i=fork()=-1) / 創(chuàng)建進(jìn)程,失敗退出 perror(p
24、rocess 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);/ execl()執(zhí)行失敗 else close(fd1);/ 關(guān)閉管道輸出描述符 close(STD_INPUT);/ 關(guān)閉標(biāo)準(zhǔn)輸入描述符0 dup(fd0);/ 指定標(biāo)準(zhǔn)輸入
25、描述符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 #include #include int main()static char string = Parent is using pipe write.;int len;len = sizeof(string); write(1, string, len);/* 將string中的內(nèi)容寫入管道
26、中 */ printf(parent, parent, parent n n n); exit(0);清單4-4 child.c#include #include int main() char output30 ; read (0, output, 30) ;/* 從管道中讀數(shù)據(jù)并存入output中 */ printf(%s n child, child. n, output) ; return(0) ;實(shí)驗五 Linux線程的使用1、實(shí)驗?zāi)康膶W(xué)習(xí)和掌握Linux線程的創(chuàng)建以及同步方法。2、實(shí)驗內(nèi)容和步驟步驟1:輸入程序5-1,編譯并運(yùn)行寫出執(zhí)行結(jié)果。步驟 2:仔細(xì)研讀代碼,描述pthrea
27、d_create函數(shù)中各個參數(shù)的意義,并給出線程的入口函數(shù)的調(diào)用方法,描述兩線程間參數(shù)傳遞的方式。int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *),void *arg);第一個參數(shù)為指向線程標(biāo)識符的指針。第二個參數(shù)用來設(shè)置線程屬性。第三個參數(shù)是線程運(yùn)行函數(shù)的起始地址。最后一個參數(shù)是運(yùn)行函數(shù)的參數(shù)。主線程通過pthread_create函數(shù)向從線程a_thread傳遞share_int 而從線程a_thread通過pthread_join向主線程傳遞th
28、read_result步驟3:打開注釋掉的全局變量aa及操作aa的相關(guān)代碼,回答這跟進(jìn)程有什么不同。線程共用相同的數(shù)據(jù)區(qū)而各個進(jìn)程有著自己獨(dú)立的數(shù)據(jù)區(qū)。步驟4:輸入程序5-2,編譯并運(yùn)行,寫出運(yùn)行結(jié)果。步驟5:仔細(xì)研讀代碼,描述利用信號量實(shí)現(xiàn)線程間同步調(diào)用方法。sem_post 以原子操作的方式將信號量加1從而喚醒線程。sem_wait以原子操作將信號量減1當(dāng)信號量小于0時就會對進(jìn)程進(jìn)行等待,從而實(shí)現(xiàn)同步調(diào)用。步驟6:學(xué)習(xí)并使用線程間的同步方法,重新改寫實(shí)驗3步驟6要求的程序。要求啟用兩個線程,其中主線程接受用戶對文件的操作命令然后發(fā)給子線程,子線程完成對文件的實(shí)際操作(注意線
29、程間的同步)。3、 實(shí)驗結(jié)論通過改節(jié)實(shí)驗,讓我對線程并發(fā)有了初步認(rèn)識,同時學(xué)會用信號量進(jìn)行線程之間的同步。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_
30、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
31、,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 *a
32、rg);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(Thr
33、ead 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
34、(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);實(shí)驗六 Linux進(jìn)程間的IPC1、實(shí)驗?zāi)康膶W(xué)習(xí)和掌握Linux進(jìn)程間的IPC及同步
35、方法。2、實(shí)驗內(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_char); fflush(stdout);V操作: if (!semaphore_v() exit(EXIT_FAILURE); pause_time =
36、 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);exit(0);s = shm;for(c = a; c = z; c+)*s+ = c;*s = NULL;printf(init over!n);while(*shm !
37、= *)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:學(xué)習(xí)并使用IPC中信號量和共享內(nèi)存的使用方法,重新改寫實(shí)驗3步驟6要求的程序。要求啟動多個進(jìn)程,每一個進(jìn)程都可以單獨(dú)對文件進(jìn)行操作,進(jìn)程間通過信號量進(jìn)行同步,對文件的操作映射到共享內(nèi)存中。3、 實(shí)驗結(jié)論通過對改節(jié)實(shí)驗,讓我了解了進(jìn)程IPC信號量訪問臨界區(qū),以及進(jìn)程之間訪問共享內(nèi)存的實(shí)現(xiàn)方法和
38、同步機(jī)制,掌握了上述一系列函數(shù)的使用方法。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 */
39、 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 semaph
40、ore_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
41、_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(EXI
42、T_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(
43、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);/* semaphor
44、e_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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 設(shè)備管道清洗管理制度
- 設(shè)計中心日常管理制度
- 設(shè)計公司簽單管理制度
- 設(shè)計班級績效管理制度
- 診室人員健康管理制度
- 診所張貼中藥管理制度
- 診斷證明規(guī)范管理制度
- 調(diào)度考核獎勵管理制度
- 財政信息安全管理制度
- 貨到付款績效管理制度
- XXXXX智能化弱電施工組織計劃
- TK校驗儀的使用
- 北京市中小學(xué)教師崗位考核登記表(表樣)
- 血栓性淺靜脈炎
- 棄渣場施工及方案
- SolidWorks入門教程(很全面)PPT課件
- 工作聯(lián)系函-087,088關(guān)于鋁合金窗門安裝滯后影響工期等相關(guān)事宜
- 叉車租賃合同
- 投資公司股權(quán)投資管理制度
- 意大利西姆公司雙D石灰窯簡介
- 四川監(jiān)理日志封面及表格(共3頁)
評論
0/150
提交評論