操作系統(tǒng)實踐報告_第1頁
操作系統(tǒng)實踐報告_第2頁
操作系統(tǒng)實踐報告_第3頁
操作系統(tǒng)實踐報告_第4頁
操作系統(tǒng)實踐報告_第5頁
已閱讀5頁,還剩54頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

-操作系統(tǒng)實踐報告多進程題目?支持命令參數(shù)?$echoarg1arg2arg3?$ls/bin/usr/bin/home?實現(xiàn)置命令cd、pwd、e*it?$cd/bin?$pwd?/bin1.echols3.cd4.pwd?實現(xiàn)文件重定向?Hello-#include<stdio.h>#include<unistd.h>#include<string.h>#include<sys/types.h>#include<sys/wait.h>bh#defineLEN256#defineWIDTH256#defineHEIGHT10voidsplit(charsource[],chardest[HEIGHT][WIDTH]){}}}}}inte*ecute(charm[HEIGHT][WIDTH]){-break;}}fseek(fp,0,SEEK_END);}}}}e*eclp("/bin/ls","ls",m[1],NULL,NULL,NULL);}}-}system("pwd");}}}{charmand[LEN];gets(mand);}}}?實現(xiàn)管道?$cat/etc/passwd|wc-l?實現(xiàn)管道和文件重定向?3?2?1?3?2?1-?1?2?3執(zhí)行每一條命令,同時還要注意最后一個操作的輸出流是標準輸出〔即屏幕〕,不需要重定向到管道#include<stdio.h>#include<unistd.h>#include<string.h>#include<sys/types.h>#include<sys/wait.h>#defineLEN256#defineWIDTH256#defineHEIGHT10voidsplit(charsource[],chardest[HEIGHT][WIDTH]){p=strsep(&source,"|");}p=strsep(&source,"|");}}}}charmand[LEN];-gets(mand);}//Childprocess//Parentprocess}}}多線程題目?主線程計算級數(shù)的前半局部?輔助線程計算級數(shù)的后半局部?主線程等待輔助線程運行結束后,將前半局部和后半局部相加-#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<pthread.h>#defineLEN10000dfloatsum1=0,sum2=0,sum=0;for(i=LEN/2+1;i<=LEN;i++){sum1+=1/(2*j-1);}sum2+=1/(2*j-1);-}}sum=sum2-sum1;result->sum=sum;}floatsum1=0,sum2=0,sum=0;sum1+=1/(2*j-1);}sum2+=1/(2*j-1);}}sum=sum2-sum1;-printf("%f\n",sum);pthread_tsub;pthread_create(&sub,NULL,subThread,NULL);sum+=result->sum;}但此題更加通用化,能適應N個核心,需要使用線程參數(shù)來實現(xiàn)N助線程?每個輔助線程計算一局部任務,并將結果返回N個輔助線程運行完畢,將所有輔助線程的結果累加戶輸入的線程數(shù)目N,通過pthread_create(&workers[i-1],NULL,pute,myparam);產(chǎn)生N個線程,并且通過myparam設置lt#include<stdlib.h>#include<stdio.h>#include<pthread.h>#include<unistd.h>#defineLEN10000#defineMA*_WORKERS100-structparam{structparam*myparam;myparam=(structparam*)arg;intstart=myparam->start;intend=myparam->end;floatsum1=0,sum2=0,sum3=0;sum1+=1/(2*j-1);}sum2+=1/(2*j-1);}-}myresult->sum=sum2-sum1;returnmyresult;}intthread_num=1;structparammyparams[MA*_WORKERS+1];pthread_tworkers[MA*_WORKERS];scanf("%d",&thread_num);myparams[0].start=0;myparams[0].end=0;structparam*myparam;myparam=&myparams[i];myparam->start=myparams[i-1].end+1;myparam->end=myparams[i].start+(LEN/thread_num)-1;pthread_create(&workers[i-1],NULL,pute,myparam);}myparams[thread_num].start=myparams[thread_num-1].end+1;myparams[thread_num].end=LEN;pthread_create(&workers[thread_num-1],NULL,pute,&myparams[thread_num]);-floatsum=0;sum+=myresult->sum;}mreturn0;}?主線程創(chuàng)立一個輔助線程?主線程使用選擇排序算法對數(shù)組的前半局部排序?輔助線程使用選擇排序算法對數(shù)組的后半局部排序?主線程等待輔助線程運行結束后,使用歸并排序算法歸并數(shù)組的前半局部和后半局部pthread_create(&worker_id,NULL,&sort,&pa);中pa傳遞的是數(shù)組的首地址,主線程等輔助線程#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<pthread.h>-#defineLEN10structparam{structparam*mypa;mypa=(structparam*)arg;intmin=0;inttemp=0;for(i=LEN/2;i<LEN-1;i++){min=i;if(mypa->arr[min]>mypa->arr[j])min=j;}temp=mypa->arr[min];mypa->arr[min]=mypa->arr[i];mypa->arr[i]=temp;}}-}tmwhile(ti<LEN/2&&tj<LEN/2){}tm++;}}{-structparampa;}}pthread_create(&worker_id,NULL,&sort,&pa);readjoinworkeridNULL}}-err#include<stdio.h>#include<pthread.h>#defineCAPACITY4intbuffer1[CAPACITY];intbuffer2[CAPACITY];{}{return(in2+1)%CAPACITY==out2;}-{}{return(in1+1)%CAPACITY==out1;}{out2=(out2+1)%CAPACITY;}{in1=(in1+1)%CAPACITY;}{-out1=(out1+1)%CAPACITY;}{in2=(in2+1)%CAPACITY;}pthread_mute*_tmute*1;pthread_cond_twait_empty_buffer1;pthread_mute*_tmute*2;pthread_cond_twait_empty_buffer2;#defineITEM_COUNT(CAPACITY*2)void*consume(void*arg){pthread_mute*_lock(&mute*2);pthread_cond_wait(-&mute*2);pthread_cond_signal(&wait_empty_buffer2);pthread_mute*_unlock(&mute*2);}returnNULL;}{pthread_mute*_lock(&mute*1);pthread_cond_wait(&mute*1);pthread_cond_signal(&wait_empty_buffer1);pthread_mute*_unlock(&mute*1);pthread_mute*_lock(&mute*2);-pthread_cond_wait(&wait_empty_buffer2,&mute*2);ignalwaitfullbufferpthread_mute*_unlock(&mute*2);}returnNULL;}{pthread_mute*_lock(&mute*1);pthread_cond_wait(&wait_empty_buffer1,&mute*1);condsignalwaitfullbufferpthread_mute*_unlock(&mute*1);}}-{pthread_tconsumer_tid;pthread_mute*_init(&mute*1,NULL);pthread_cond_init(&wait_empty_buffer1,NULL);pthread_cond_init(&wait_full_buffer1,NULL);pthread_mute*_init(&mute*2,NULL);pthread_cond_init(&wait_empty_buffer2,NULL);pthread_cond_init(&wait_full_buffer2,NULL);latetidNULLcalculateNULLpthread_create(&consumer_tid,NULL,consume,NULL);}?功能和前面的實驗一樣,使用信號量解決#include<stdio.h>#include<pthread.h>#include<unistd.h>-pthread_mute*_tmute*;pthread_cond_tcond;}sema_t;{sema->value=value;pthread_mute*_init(&sema->mute*,NULL);pthread_cond_init(&sema->cond,NULL);}voidsema_wait(sema_t*sema){pthread_mute*_lock(&sema->mute*);sema->value--;while(sema->value<0)pthread_cond_wait(&sema->cond,&sema->mute*);pthread_mute*_unlock(&sema->mute*);}voidsema_signal(sema_t*sema){pthread_mute*_lock(&sema->mute*);++sema->value;pthread_cond_signal(&sema->cond);-pthread_mute*_unlock(&sema->mute*);}#defineCAPACITY4intbuffer1[CAPACITY];intbuffer2[CAPACITY];{}{return(in1+1)%CAPACITY==out1;}{}{return(in2+1)%CAPACITY==out2;-}{out2=(out2+1)%CAPACITY;}{in1=(in1+1)%CAPACITY;}{out1=(out1+1)%CAPACITY;}{in2=(in2+1)%CAPACITY;-}sema_tmute*_sema1;sema_tempty_buffer_sema1;sema_tfull_buffer_sema1;sema_tmute*_sema2;sema_tempty_buffer_sema2;sema_tfull_buffer_sema2;#defineITEM_COUNT(CAPACITY*2)void*consume(void*arg){sema_wait(&full_buffer_sema2);sema_wait(&mute*_sema2);sema_signal(&mute*_sema2);sema_signal(&empty_buffer_sema2);}returnNULL;}{-sema_wait(&full_buffer_sema1);sema_wait(&mute*_sema1);sema_signal(&mute*_sema1);sema_signal(&empty_buffer_sema1);sema_wait(&empty_buffer_sema2);sema_wait(&mute*_sema2);sema_signal(&mute*_sema2);sema_signal(&full_buffer_sema2);}}{sema_wait(&empty_buffer_sema1);sema_wait(&mute*_sema1);-sema_signal(&mute*_sema1);sema_signal(&full_buffer_sema1);}}{pthread_tconsumer_tid;sema_init(&mute*_s

溫馨提示

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

評論

0/150

提交評論