版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)操作系統(tǒng)實踐報告多進(jìn)程題目sh1.c: 實現(xiàn)shell程序,要求具備如下功能支持命令參數(shù)$ echo arg1 arg2 arg3$ ls /bin /usr/bin /home實現(xiàn)內(nèi)置命令cd、pwd、exit$ cd /bin$ pwd/bin思路:說明:首先設(shè)置一個死循環(huán)模仿shell終端,讀取用戶的輸入,并且根據(jù)空格將輸入拆分成字符串?dāng)?shù)組,然后調(diào)用excute這個子函數(shù)進(jìn)行處理。echo根據(jù)數(shù)組第一個元素來判斷命令是什么,判斷出是ehco后,fork一個新的進(jìn)程
2、,將其后的內(nèi)容一個個輸出出來,并且父進(jìn)程等待子進(jìn)程退出后再執(zhí)行,確保輸出在屏幕上時不被打斷。ls讀取用戶輸入并且根據(jù)拆分的數(shù)組判斷出是ls命令后,fork一個新的進(jìn)程,調(diào)用execlp函數(shù)將/bin/ls下的ls程序裝入子進(jìn)程并將拆分的數(shù)組參數(shù)部分傳遞給ls即可,同樣的,父進(jìn)程等待子進(jìn)程退出,確保輸出在屏幕上不被打斷。cd同樣是根據(jù)輸入并拆分成數(shù)組后判斷出是cd命令后,fork一個新的進(jìn)程,然后調(diào)用chdir并將拆分?jǐn)?shù)組的參數(shù)部分傳遞給chdir作為實參即可。pwd同樣是根據(jù)輸入并拆分成數(shù)組后判斷出是pwd命令后,fork一個新的進(jìn)程,然后調(diào)用system(pwd)即可,此命令也可以用來驗證上
3、面的cd命令是否正確執(zhí)行。exit根據(jù)用戶輸入逼格拆分的數(shù)組判斷出是exit命令后,excute子函數(shù)返回-1,在循環(huán)中檢測excute的返回值,如果是-1則直接return,退出模仿的shell終端。sh2.c: 實現(xiàn)shell程序,要求在第1版的基礎(chǔ)上,添加如下功能實現(xiàn)文件重定向$ echo hello log$ cat logHello思路: 接sh1.c的描述,若判斷出是echo命令后,要再次判斷拆分的字符串?dāng)?shù)組中有無“”出現(xiàn),如果 有,則把“”之前、echo之后的內(nèi)容作為輸出,把“”之后到“”之后的第一個空白字符作為文件名,fopen創(chuàng)建文件并fwrite將輸出內(nèi)容輸出到該文件中,并
4、關(guān)閉文件。sh1.c和sh2.c的源代碼:#include #include #include #include #include #include #include #define LEN 256#define WIDTH 256#define HEIGHT 10void split(char source,char destHEIGHTWIDTH)char *p;p=strsep(&source, );int i=0;for(i=0;pi!=0;i+)dest0i=pi;dest0i=0;int j=1;while(p)p=strsep(&source, );if(p)for(i=0;pi
5、!=0;i+)destji=pi;destji=0;j+;int execute(char commHEIGHTWIDTH)if(strcmp(comm0,echo)=0)int pid=fork();if(pid=0)int i=0;int is=0;for(i=1;commi0!=0;i+)if(commi0=)is=1;break;if(is=1)puts(commi+1);FILE *fp=fopen(commi+1,w+);int j=0;for(j=1;j);gets(command);split(command,splitArray);int i=0;if(-1=execute(
6、splitArray)return 0;sh3.c: 實現(xiàn)shell程序,要求在第2版的基礎(chǔ)上,添加如下功能實現(xiàn)管道$ cat /etc/passwd | wc -l實現(xiàn)管道和文件重定向$ cat input.txt321321$ cat output.txt$ cat output.txt123思路:首先讀取用戶輸入,以“|”為分隔符將輸入分割成字符串?dāng)?shù)組,然后在一個while循環(huán)中依次執(zhí)行下面的動作:代碼中通過pipe()函數(shù)來創(chuàng)建管道,創(chuàng)建之后父進(jìn)程和子進(jìn)程一個只能向管道寫內(nèi)容,一個只能向管道讀內(nèi)容。然后利用dup()函數(shù)來把進(jìn)程的輸入流或者輸出流重定向到管道里,這樣就能實現(xiàn)管道的操作。
7、實現(xiàn)的時候注意可以使用多個“|”來迭代進(jìn)行管道操作,需要使用一個循環(huán)來處理。用system執(zhí)行每一條命令,同時還要注意最后一個操作的輸出流是標(biāo)準(zhǔn)輸出(即屏幕),不需要重定向到管道里,需要特殊處理一下。源代碼:#include #include #include #include #include #include #include #define LEN 256#define WIDTH 256#define HEIGHT 10void split(char source,char destHEIGHTWIDTH)char *p;p=strsep(&source,|);int i=0;for(
8、i=0;pi!=0;i+)dest0i=pi;dest0i=0;int j=1;while(p)p=strsep(&source,|);if(p)for(i=0;pi!=0;i+)destji=pi;destji=0;j+;main()char commandLEN;char splitArrayHEIGHTWIDTH=0;printf(%s,);gets(command); split(command,splitArray); int i=0; for(i=0;splitArrayi0!=0;i+) puts(splitArrayi); int p2; pipe(p); int j=0; f
9、or(j=0;splitArrayj+10!=0;j+) if (fork() = 0) / Child process close(0); close(p0) close(p1); dup(p0); system(splitArrayj); else /Parent process close(1); close(p0) close(p1); dup(p1); system(splitArrayj+1); 多線程題目pi1.c: 使用2個線程根據(jù)萊布尼茲級數(shù)計算PI萊布尼茲級數(shù)公式: 1 - 1/3 + 1/5 - 1/7 + 1/9 - . = PI/4主線程創(chuàng)建1個輔助線程主線程計算級數(shù)
10、的前半部分輔助線程計算級數(shù)的后半部分主線程等待輔助線程運行結(jié)束后,將前半部分和后半部分相加思路:計算公式前1000項,主線程計算前5000項,子線程計算后5000項,主進(jìn)程等待子進(jìn)程結(jié)束,通過pthread_join(sub,(void *)&result);的result參數(shù)獲取子進(jìn)程計算結(jié)果再相加即可。源代碼:#include#include#include#include#define LEN 10000 struct result float sum;void *subThread() int i; float j; struct result *result; float sum1=
11、0,sum2=0,sum=0; for(i=LEN/2+1;isum=sum; return result;int main() int i; float j; float sum1=0,sum2=0,sum=0; for(i=1;isum; printf(%fn,sum); return 0; pi2.c: 使用N個線程根據(jù)萊布尼茲級數(shù)計算PI與上一題類似,但本題更加通用化,能適應(yīng)N個核心,需要使用線程參數(shù)來實現(xiàn)主線程創(chuàng)建N個輔助線程每個輔助線程計算一部分任務(wù),并將結(jié)果返回主線程等待N個輔助線程運行結(jié)束,將所有輔助線程的結(jié)果累加思路:設(shè)計算公式前1000項,讀取用戶輸入的線程數(shù)目N,通過pt
12、hread_create(&workersi-1,NULL,compute,myparam);產(chǎn)生N個線程,并且通過myparam設(shè)置每一個線程計算的起始項和終止項,通過pthread_join(workersj,(void *)&myresult);等待每個線程結(jié)束并通過result獲取結(jié)果,將結(jié)果相加即可。源代碼:#include#include#include#include#define LEN 10000#define MAX_WORKERS 100struct param int start; int end;struct result float sum;void *comput
13、e(void *arg) int i; float j; struct param *myparam; myparam=(struct param *)arg; int start=myparam-start; int end=myparam-end; struct result *myresult; float sum1=0,sum2=0,sum3=0; for(i=start;isum=sum2-sum1; return myresult;int main() int thread_num=1; struct param myparamsMAX_WORKERS+1; pthread_t w
14、orkersMAX_WORKERS; printf(please input thread number:); scanf(%d,&thread_num); int i; myparams0.start=0; myparams0.end=0; for(i=1;istart=myparamsi-1.end+1; myparam-end=myparamsi.start+(LEN/thread_num)-1; pthread_create(&workersi-1,NULL,compute,myparam); myparamsthread_num.start=myparamsthread_num-1.
15、end+1; myparamsthread_num.end=LEN; pthread_create(&workersthread_num-1,NULL,compute,&myparamsthread_num); int j; float sum=0; for(j=0;jsum; free(myresult); printf(%fn,sum); return 0;sort.c: 多線程排序主線程創(chuàng)建一個輔助線程主線程使用選擇排序算法對數(shù)組的前半部分排序輔助線程使用選擇排序算法對數(shù)組的后半部分排序主線程等待輔助線程運行結(jié)束后,使用歸并排序算法歸并數(shù)組的前半部分和后半部分思路:主線程排序數(shù)組的前半部
16、分,輔助線程排序后半部分,pthread_create(&worker_id,NULL,&sort,&pa);中pa傳遞的是數(shù)組的首地址,主線程等輔助線程結(jié)束后,再調(diào)用merge將數(shù)組合并為有序。源代碼:#include#include#include#include#define LEN 10int arrayLEN=0,3,8,6,2,9,5,4,1,7;struct param int *arr;void *sort(void *arg) struct param *mypa; mypa=(struct param *)arg; int i=0; int j=0; int min=0;
17、int temp=0; for(i=LEN/2;iLEN-1;i+) min=i; for(j=i;jarrminmypa-arrj)min=j; temp=mypa-arrmin; mypa-arrmin=mypa-arri; mypa-arri=temp; void merge() int i=0; int aLEN/2; int bLEN/2; for(i=0;iLEN/2;i+) ai=arrayi; bi=arrayi+LEN/2; /* for(i=0;iLEN/2;i+) printf(%dn,ai); */ int tm=0; int ti=0,tj=0; while(tiLE
18、N/2&tjLEN/2) if(atibtj) arraytm=ati; ti+; else arraytm=btj; tj+; tm+; int main() struct param pa; pa.arr=array; int ti=0,tj=0,tmin=0; for(ti=0;tiLEN/2-1;ti+) tmin=ti; for(tj=ti;tjarraytj)tmin=tj; int temp=arraytmin; arraytmin=arrayti; arrayti=temp; pthread_t worker_id; pthread_create(&worker_id,NULL
19、,&sort,&pa); pthread_join(worker_id,NULL); merge(); int i=0; for(i=0;iLEN;i+) printf(%dn,arrayi); pc1.c: 使用條件變量解決生產(chǎn)者、計算者、消費者問題系統(tǒng)中有3個線程:生產(chǎn)者、計算者、消費者系統(tǒng)中有2個容量為4的緩沖區(qū):buffer1、buffer2生產(chǎn)者生產(chǎn)a、b、c、d、e、f、g、h八個字符,放入到buffer1計算者從buffer1取出字符,將小寫字符轉(zhuǎn)換為大寫字符,放入到buffer2消費者從buffer2取出字符,將其打印到屏幕上思路:類似于生產(chǎn)者和消費者,在問題中,生產(chǎn)者、計算者
20、相對應(yīng)buffer1是生產(chǎn)者、消費者,二者互斥的進(jìn)入buffer1,并且當(dāng)buffer1滿時,生產(chǎn)者等待,當(dāng)buffer1空時,且計算值要從中取數(shù)據(jù)時,計算者等待。同理,計算者、消費者相對應(yīng)buffer2是生產(chǎn)者和消費者,二者互斥的進(jìn)入buffer2,當(dāng)buffer2滿時,且計算者要向其中放入數(shù)據(jù)時,計算者應(yīng)等待,當(dāng)buffer2空時,消費者應(yīng)等待。源代碼:#include #include #define CAPACITY 4int buffer1CAPACITY;int buffer2CAPACITY;int in1;int out1;int in2;int out2;int buffer
21、2_is_empty() return in2 = out2;int buffer2_is_full() return (in2 + 1) % CAPACITY = out2;int buffer1_is_empty() return in1 = out1;int buffer1_is_full() return (in1 + 1) % CAPACITY = out1;int get_item() int item; item = buffer2out2; out2 = (out2 + 1) % CAPACITY; return item;void put_item(int item) buf
22、fer1in1 = item; in1 = (in1 + 1) % CAPACITY;int cal_get_item() int item; item = buffer1out1; out1 = (out1 + 1) % CAPACITY; return item;void cal_put_item(int item) buffer2in2 = item; in2 = (in2 + 1) % CAPACITY;pthread_mutex_t mutex1;pthread_cond_t wait_empty_buffer1;pthread_cond_t wait_full_buffer1;pt
23、hread_mutex_t mutex2;pthread_cond_t wait_empty_buffer2;pthread_cond_t wait_full_buffer2;#define ITEM_COUNT (CAPACITY * 2)void *consume(void *arg) int i; int item; for (i = 0; i ITEM_COUNT; i+) pthread_mutex_lock(&mutex2); while (buffer2_is_empty() pthread_cond_wait( &wait_full_buffer2, &mutex2); ite
24、m = get_item(); printf( consume item: %cn, item); sleep(1); pthread_cond_signal(&wait_empty_buffer2); pthread_mutex_unlock(&mutex2); return NULL;void *calculate(void *arg) int i; int item; for (i = 0; i ITEM_COUNT; i+) pthread_mutex_lock(&mutex1); while (buffer1_is_empty() pthread_cond_wait( &wait_f
25、ull_buffer1, &mutex1); item = cal_get_item(); item+=A-a; pthread_cond_signal(&wait_empty_buffer1); pthread_mutex_unlock(&mutex1); pthread_mutex_lock(&mutex2); while (buffer2_is_full() pthread_cond_wait(&wait_empty_buffer2, &mutex2); cal_put_item(item); pthread_cond_signal(&wait_full_buffer2); pthrea
26、d_mutex_unlock(&mutex2); return NULL;void produce() int i; int item; for (i = 0; i ITEM_COUNT; i+) pthread_mutex_lock(&mutex1); while (buffer1_is_full() pthread_cond_wait(&wait_empty_buffer1, &mutex1); item = i + a; printf(produce item: %cn, item); put_item(item); sleep(1); pthread_cond_signal(&wait
27、_full_buffer1); pthread_mutex_unlock(&mutex1); int main() pthread_t consumer_tid; pthread_t calculate_tid; pthread_mutex_init(&mutex1, NULL); pthread_cond_init(&wait_empty_buffer1, NULL); pthread_cond_init(&wait_full_buffer1, NULL); pthread_mutex_init(&mutex2, NULL); pthread_cond_init(&wait_empty_bu
28、ffer2, NULL); pthread_cond_init(&wait_full_buffer2, NULL); pthread_create(&calculate_tid, NULL, calculate, NULL); pthread_create(&consumer_tid, NULL, consume, NULL); produce(); return 0;pc2.c: 使用信號量解決生產(chǎn)者、計算者、消費者問題功能和前面的實驗相同,使用信號量解決思路:類似于pc1.c源代碼:#include #include #include typedef struct int value; p
29、thread_mutex_t mutex; pthread_cond_t cond; sema_t;void sema_init(sema_t *sema, int value) sema-value = value; pthread_mutex_init(&sema-mutex, NULL); pthread_cond_init(&sema-cond, NULL);void sema_wait(sema_t *sema) pthread_mutex_lock(&sema-mutex); sema-value-; while (sema-value cond, &sema-mutex); pt
30、hread_mutex_unlock(&sema-mutex);void sema_signal(sema_t *sema) pthread_mutex_lock(&sema-mutex); +sema-value; pthread_cond_signal(&sema-cond); pthread_mutex_unlock(&sema-mutex);#define CAPACITY 4int buffer1CAPACITY;int buffer2CAPACITY;int in1;int out1;int in2;int out2;int buffer1_is_empty() return in
31、1 = out1;int buffer1_is_full() return (in1 + 1) % CAPACITY = out1;int buffer2_is_empty() return in2 = out2;int buffer2_is_full() return (in2 + 1) % CAPACITY = out2;int get_item() int item; item = buffer2out2; out2 = (out2 + 1) % CAPACITY; return item;void put_item(int item) buffer1in1 = item; in1 =
32、(in1 + 1) % CAPACITY;int cal_get_item() int item; item = buffer1out1; out1 = (out1 + 1) % CAPACITY; return item;void cal_put_item(int item) buffer2in2 = item; in2 = (in2 + 1) % CAPACITY;sema_t mutex_sema1;sema_t empty_buffer_sema1;sema_t full_buffer_sema1;sema_t mutex_sema2;sema_t empty_buffer_sema2
33、;sema_t full_buffer_sema2;#define ITEM_COUNT (CAPACITY * 2)void *consume(void *arg) int i; int item; for (i = 0; i ITEM_COUNT; i+) sema_wait(&full_buffer_sema2); sema_wait(&mutex_sema2); item = get_item(); sema_signal(&mutex_sema2); sema_signal(&empty_buffer_sema2); printf( consume item: %cn, item);
34、 return NULL;void *calculate() int i; int item; for (i = 0; i ITEM_COUNT; i+) sema_wait(&full_buffer_sema1); sema_wait(&mutex_sema1); item = cal_get_item(); item+=A-a; sema_signal(&mutex_sema1); sema_signal(&empty_buffer_sema1); sema_wait(&empty_buffer_sema2); sema_wait(&mutex_sema2); cal_put_item(i
35、tem); sema_signal(&mutex_sema2); sema_signal(&full_buffer_sema2); void produce() int i; int item; for (i = 0; i ITEM_COUNT; i+) sema_wait(&empty_buffer_sema1); sema_wait(&mutex_sema1); item = i + a; put_item(item); sema_signal(&mutex_sema1); sema_signal(&full_buffer_sema1); printf(produce item: %cn, item); int main() pthread_t consumer_tid; pthread_t calculate_tid; sema_init(&mutex_sema1, 1); sema_init(&empty_buffer_se
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 樓房加固施工方案(3篇)
- 2025年山西省職教高考《語文》核心考點必刷必練試題庫(含答案)
- 《國防動員法》考試題庫100題(含答案)
- 2025年池州職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試近5年??及鎱⒖碱}庫含答案解析
- 2025年武威職業(yè)學(xué)院高職單招職業(yè)技能測試近5年??及鎱⒖碱}庫含答案解析
- 2025年棗莊科技職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性測試近5年常考版參考題庫含答案解析
- 專題05 名句名篇默寫(第3期)
- 消防工程維修合同書
- 廣西二手房買賣合同
- 建材購銷合同格式范本
- 2025年度院感管理工作計劃(后附表格版)
- 勵志課件-如何做好本職工作
- 2024年山東省濟(jì)南市中考英語試題卷(含答案解析)
- 2024年社區(qū)警務(wù)規(guī)范考試題庫
- 暑假作業(yè) 10 高二英語完形填空20篇(原卷版)-【暑假分層作業(yè)】2024年高二英語暑假培優(yōu)練(人教版2019)
- 武強縣華浩數(shù)控設(shè)備科技有限公司年產(chǎn)9000把(只)提琴、吉他、薩克斯等樂器及80臺(套)數(shù)控雕刻設(shè)備項目環(huán)評報告
- 安全生產(chǎn)法律法規(guī)匯編(2024年4月)
- DB11∕T 882-2023 房屋建筑安全評估技術(shù)規(guī)程
- 華為員工股權(quán)激勵方案
- 衛(wèi)生院安全生產(chǎn)知識培訓(xùn)課件
- 兒童尿道黏膜脫垂介紹演示培訓(xùn)課件
評論
0/150
提交評論