




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上安工大Linux程序設(shè)計實驗指導(dǎo)書 部分實驗完成不全請同學(xué)們自行補充 姓名:* 學(xué)號:* 班級:* 指導(dǎo)老師:阮越 時間:2013年5月31號 實驗一 Linux基本命令的使用1、實驗?zāi)康?#160;學(xué)習(xí)和掌握Linux的基本命令。2、實驗內(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等不同選項并比較不同之處。
2、-a, -all:不隱藏任何以“.” 字符開始的項目。-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命令進行瀏覽翻頁操作,再用less命令瀏覽文件的內(nèi)容。比較這幾個命令的不同之處.對當(dāng)前目錄中成功,因為該目錄下的passwd文件對當(dāng)前用戶具有寫的權(quán)限。對/etc/passwd文件的操作被拒絕因為對當(dāng)前用戶不具有寫的權(quán)限cat命令將文件的內(nèi)容全部顯示,more命令將文件內(nèi)容分屏顯示,less命令進行刷新的全部顯示步驟6:用ls命令查看test下文件的權(quán)限,用mv命令更改文件password的文件名為test.txt,嘗試用chown和chgrp更改文件的屬主
4、為root、組為root,看看能否成功,不成功,請說明原因。嘗試用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)的進程,并利用man命令獲取ps命令的參數(shù),寫出獲取當(dāng)前終端進程執(zhí)行情況的ps命令。步驟9:使用df命令查看當(dāng)前系統(tǒng)已安裝的文件
5、系統(tǒng)的空間使用情況,記錄結(jié)果。步驟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í)行會出現(xiàn)什么結(jié)果,解釋一下這條命令。將ls l輸出的結(jié)果輸出重定向到tmp文件中步驟14:嘗試執(zhí)行find /usr/src -name *.c -pri
6、nt | xargs grep “#include”,看看這條命令的執(zhí)行會出現(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、實驗結(jié)論通過對linux 的常用命令的學(xué)習(xí),讓我對linux的系統(tǒng)有了初步的認識-這是一
7、個完全基于命令操作的操作系統(tǒng),這些命令組成了強大的命令系統(tǒng),提供了完善的功能。這些命令操作將為我以后的linux學(xué)習(xí)打下良好的基礎(chǔ)。實驗二 簡單Shell程序設(shè)計 1、實驗?zāi)康?#160;(1) 學(xué)習(xí)和掌握vi編輯器的基本使用方法。(2) 學(xué)習(xí)和掌握編寫簡單的shell程序。2、實驗內(nèi)容和步驟2.1 vi的使用步驟1:以root用戶身份登錄進入Linux 系統(tǒng)。步驟 2:啟動Vi,切換到輸入模式,輸入一段英文:This is an exercise! We use Vi to edi
8、t it. Left,down,up,right 步驟3:嘗試匹配其中的一串字符,寫出這命令及執(zhí)行結(jié)果。 在命令狀態(tài)下輸入 /Left 即匹配Left這個字符步驟4:嘗試替換其中的一串字符,寫出這命令及執(zhí)行結(jié)果。 在命令狀態(tài)下輸入 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ù)制文本,寫出命
9、令及執(zhí)行結(jié)果。步驟8:嘗試存盤退出操作,寫出命令??梢杂玫拿?:wq 或:x2.2 簡單的shell程序步驟1:使用Vi將程序清單2-1的程序輸入。步驟 2:嘗試運行該程序,如果不能運行,原因是什么,怎樣修改。步驟3:執(zhí)行程序,寫出程序的執(zhí)行結(jié)果。步驟4:閱讀程序,掌握shell函數(shù)的編寫方法,補全函數(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 us
10、ers online"for i in "who are who|awk 'print $1'"do echo -n $i echo -n " "done3、實驗結(jié)論通過對shell程序的設(shè)計,讓我對shell的語法,結(jié)構(gòu)和命令有了進一步的認識,同時認識到shell程序的強大,我可以比較方便的編寫一些基礎(chǔ)的腳本程序。清單2-1 menuMYDATE=date +%d/%m/%yTHIS_HOST=hostname -sUSER=whoamifile_run()echo "you must complete it&quo
11、t;uers()echo "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 s
12、ystemH : Help 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 respons
13、e"esacecho -e -n "nntHit the return key to continue"read DUMMYDone實驗三 Linux下簡單C程序設(shè)計與文件操作1、實驗?zāi)康?#160;(1) 學(xué)習(xí)和掌握gcc等Linux的開發(fā)調(diào)試環(huán)境。(2) 學(xué)習(xí)并掌握Linux的文件操作。(3) 編寫并實現(xiàn)實驗步驟6要求的程序。2、實驗內(nèi)容和步驟步驟1:使用Vi將程序清單3-1和3-2的程序輸入,并在當(dāng)前目錄下創(chuàng)建文件“file.in”和文件“file.out”,盡可能的使文件“file.in”大一些。步驟 2:利用gcc分別編譯這兩個程序,寫出編譯命令和執(zhí)行結(jié)
14、果,如果不成功,嘗試利用gdb調(diào)試。gcc o list3_1 list3_1.cgcc o list3_2 list3_2.c步驟3:仔細觀察這兩個程序,比較標(biāo)準C的文件操作和Linux的系統(tǒng)調(diào)用open、read、write等的使用區(qū)別。答:標(biāo)準c的文件操作一個是庫函數(shù)的調(diào)用,linux中系統(tǒng)調(diào)用文件操是底層的文件調(diào)用,其運行效率略有差距,同時參數(shù)也不同,返回的數(shù)據(jù)類型也不同。步驟4:按照說明重新修改程序3-2,并使用time命令察看程序執(zhí)行的時間效率上有何區(qū)別。 修改前:修改后:步驟5:輸入、編譯并運行程序3-3和3-4,寫出執(zhí)行結(jié)果,并比較他們fseek和lseek在使用方法上的異同兩
15、者都是對文件的讀寫指針進行設(shè)置但兩者的返回類型不同,fseek返回的是int型lseek返回的是off_t型步驟6:學(xué)習(xí)并分別使用標(biāo)準C的文件操作函數(shù)和Linux的系統(tǒng)調(diào)用創(chuàng)建一個對學(xué)生基本信息進行操作(插入、修改和刪除)的C程序,學(xué)生基本信息以結(jié)構(gòu)體的形式存儲在文件中,struct stu_info的定義如下:struct stu_info char stu_num12;char name10;short int sex;/*0為女生,1為男生*/char mobile_phone12;3、 實驗結(jié)論通過對文件操作的編程,讓我掌握了文件的創(chuàng)建,打開,讀寫,以及特定位置的讀寫關(guān)
16、閉等操作。并且從系統(tǒng)底層調(diào)用和標(biāo)準c函數(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 =
17、fgetc(in)!=EOF)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);/將注釋打開,并將兩條語句的后一句
18、注釋掉,重新編譯執(zhí)行。/ 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
19、 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 e
20、rror!");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
21、.h>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("fil
22、e 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、實驗?zāi)康?#160;(1) 學(xué)習(xí)和掌握fork等系統(tǒng)調(diào)用的基本使用方法。(2) 利用Linux中的管道實現(xiàn)父子進程間的同步。2、實驗內(nèi)容與步驟步驟1:使用Vi將程序清單4-1的程序輸入、編譯并運行,學(xué)習(xí)和掌握fork的基本調(diào)用方法。步驟
23、 2:使用Vi將程序清單4-2、4-3和4-4的程序輸入、利用gcc分別編譯這三個程序,寫出編譯命令和編譯結(jié)果,如果不成功,嘗試利用gdb調(diào)試。gcc -o list4_2 list4_2.cgcc -o father father.cgcc -o child child.c步驟3:寫出編譯這三個程序的makefile,然后利用make進行編譯,談?wù)勥@么做的好處。可以節(jié)省勞動時間,讓三個程序同時編譯,簡化了操作。步驟4:運行這三個程序,寫出運行結(jié)果。 步驟5:屏幕上顯示出的執(zhí)行結(jié)果是哪一個進程執(zhí)行的結(jié)果?father和child步驟6:父進程中的printf有向屏幕輸出嗎?為什么?沒有,因為父
24、進程的標(biāo)準輸出已經(jīng)被重定向。步驟7:利用父子進程間的管道通信方式,改寫實驗3步驟6要求的程序。要求啟用兩個進程,其中父進程接受用戶對文件的操作命令然后通過管道發(fā)給子進程,子進程完成對文件的實際操作。3、 實驗結(jié)論 通過該節(jié)實驗,我更進一步理解了fock函數(shù)dup函數(shù)exercl函數(shù)的工作原理,讓我了解到了進程并發(fā)的概念。4、程序清單/程序清單 4-1#include <unistd.h>#include <stdio.h>#include "err_exit.h"int global = 5;int main(void)pid_t p
25、id;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,%sn""pid = %dn global = %dn local = %dn",getpid
26、(),string,pid,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 管道程序#
27、include <unistd.h>#include<stdlib.h>#include <stdio.h>#define STD_INPUT 0/ 定義標(biāo)準輸入設(shè)備描述符# define STD_OUTPUT 1/ 定義標(biāo)準輸出設(shè)備描述符int fd2;void main()static char process1="father",process2="child" pipe(fd);/ 定義管道 pipeline(process1,process2);/ 調(diào)用自定義函數(shù)pipeline() exit(1);/ 程序
28、結(jié)束pipeline(char* process1,char* process2)int i;if (i=fork()=-1) / 創(chuàng)建進程,失敗退出 perror("process fork error!");exit(1);if (i) close(fd0);/ 關(guān)閉管道輸入描述符 close(STD_OUTPUT);/ 關(guān)閉標(biāo)準輸出描述符1 dup(fd1);/ 指定標(biāo)準輸出描述符1為管道寫指針 close(fd1);/ 關(guān)閉原始管道寫指針 execl(process1, process1, 0);/ 用程序father覆蓋當(dāng)前程序 printf("fath
29、er failed.n");/ execl()執(zhí)行失敗 else close(fd1);/ 關(guān)閉管道輸出描述符 close(STD_INPUT);/ 關(guān)閉標(biāo)準輸入描述符0 dup(fd0);/ 指定標(biāo)準輸入描述符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 &l
30、t;stdlib.h>#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&
31、gt;int main() char output30 ; read (0, output, 30) ;/* 從管道中讀數(shù)據(jù)并存入output中 */ printf("%s n child, child. n", output) ; return(0) ;實驗五 Linux線程的使用1、實驗?zāi)康?#160;學(xué)習(xí)和掌握Linux線程的創(chuàng)建以及同步方法。2、實驗內(nèi)容和步驟步驟1:輸入程序5-1,編譯并運行寫出執(zhí)行結(jié)果。步驟 2:仔細研讀代碼,描述pthread_create函數(shù)中各個參數(shù)的意義,并給出線程的入口函數(shù)的調(diào)用方法,描述兩線程間參數(shù)傳遞的方式。int pthread_
32、create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *),void *arg);第一個參數(shù)為指向線程的。第二個參數(shù)用來設(shè)置線程屬性。第三個參數(shù)是線程運行函數(shù)的起始地址。最后一個參數(shù)是運行函數(shù)的參數(shù)。主線程通過pthread_create函數(shù)向從線程a_thread傳遞share_int 而從線程a_thread通過pthread_join向主線程傳遞thread_result步驟3:打開注釋掉的全局變量aa及操作aa的相關(guān)代碼,回答這跟進程有什么不同。線程共用相同的數(shù)據(jù)區(qū)而各個進程有
33、著自己獨立的數(shù)據(jù)區(qū)。步驟4:輸入程序5-2,編譯并運行,寫出運行結(jié)果。步驟5:仔細研讀代碼,描述利用信號量實現(xiàn)線程間同步調(diào)用方法。sem_post 以原子操作的方式將信號量加1從而喚醒線程。sem_wait以原子操作將信號量減1當(dāng)信號量小于0時就會對進程進行等待,從而實現(xiàn)同步調(diào)用。步驟6:學(xué)習(xí)并使用線程間的同步方法,重新改寫實驗3步驟6要求的程序。要求啟用兩個線程,其中主線程接受用戶對文件的操作命令然后發(fā)給子線程,子線程完成對文件的實際操作(注意線程間的同步)。3、 實驗結(jié)論通過改節(jié)實驗,讓我對線程并發(fā)有了初步認識,同時學(xué)會用信號量進行線程之間的同步。4、程序清單/程序清單 5
34、-1#include <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);
35、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
36、n",(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
37、-2#include <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
38、 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
39、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"
40、;);exit(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);pthrea
41、d_exit(NULL);實驗六 Linux進程間的IPC1、實驗?zāi)康?#160;學(xué)習(xí)和掌握Linux進程間的IPC及同步方法。2、實驗內(nèi)容和步驟步驟1:輸入程序6-1,6-2編譯并運行寫出執(zhí)行結(jié)果。步驟 2:仔細研讀代碼,寫出程序中實現(xiàn)P、V操作的相關(guān)函數(shù)和代碼,描述程序的同步工作過程。專心-專注-專業(yè)P操作:if (!semaphore_p() exit(EXIT_FAILURE); printf("%c", op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf(&quo
42、t;%c", op_char); 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中哪段代碼實現(xiàn)了共享,描述實現(xiàn)內(nèi)存共享的主要函數(shù)的參數(shù)意義和這些函數(shù)的使用方法。程序6_3:if(shm = shmat(shmid,NULL,0) = (char *)-1) perror("s
43、hmat 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(&
44、#39;n');*shm = '*'步驟5:學(xué)習(xí)并使用IPC中信號量和共享內(nèi)存的使用方法,重新改寫實驗3步驟6要求的程序。要求啟動多個進程,每一個進程都可以單獨對文件進行操作,進程間通過信號量進行同步,對文件的操作映射到共享內(nèi)存中。3、 實驗結(jié)論通過對改節(jié)實驗,讓我了解了進程IPC信號量訪問臨界區(qū),以及進程之間訪問共享內(nèi)存的實現(xiàn)方法和同步機制,掌握了上述一系列函數(shù)的使用方法。4、程序清單/程序清單 6-1#if defined(_GNU_LIBRARY_) && !defined(_SEM_SEMUN_UNDEFINED) /* union semun
45、is defined by including <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 fo
46、r IPC_INFO */ ;#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
47、);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 = %dn",sem_id); if (argc > 1) if (!set_semvalue() fprintf(stderr
48、, "Failed to 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 critic
49、al 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 avail
50、able, 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 - finishedn", getpid(); if (argc > 1) sleep(10); de
51、l_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) =
52、-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
53、to delete semaphoren");/* 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"); ret
54、urn(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 f
溫馨提示
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年稷山社工面試試題及答案
- 2025年運籌學(xué)對策論試題及答案
- 2025年零售媒體行業(yè)研究報告
- 2025年課程標(biāo)準考試題及答案
- 鋼結(jié)構(gòu)拆除專項施工方案
- 5f的徑向分布函數(shù)極大值
- c++多線程同步原子操作原理
- 住宅水電施工方案
- 水罐施工方案
- 加熱涂料施工方案
- 2025年呼和浩特職業(yè)學(xué)院單招職業(yè)傾向性測試題庫及參考答案
- 在泰居留90天移民局報到表格(TM47)
- 銅陵職業(yè)技術(shù)學(xué)院“十三五”發(fā)展規(guī)劃編制工作方案
- EDTA絡(luò)合滴定法測定銀合金中的銀
- 某屠宰場廢水處理工藝設(shè)計_畢業(yè)設(shè)計(論文)
- 江蘇省無錫市2020年中考語文真題試題(含解析)
- 癌癥患者生命質(zhì)量量表FACT-G v4
- 李清照詞修辭現(xiàn)象探析畢業(yè)論文
- 新教科版四年級科學(xué)下冊第一單元《植物的生長變化》知識點整理
- 礦床成礦模式(PPT頁)
- SMT 供應(yīng)商出貨檢驗報告書
評論
0/150
提交評論