實驗1-進程同步_第1頁
實驗1-進程同步_第2頁
實驗1-進程同步_第3頁
實驗1-進程同步_第4頁
實驗1-進程同步_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費閱讀

付費下載

VIP免費下載

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

實驗1進程同步一、實驗要求模擬生產(chǎn)者與消費者問題分析代碼,給出注釋給出實驗結(jié)果二、實驗內(nèi)容

數(shù)據(jù)結(jié)構(gòu):

每個進程有一個進程控制塊(PCB)表示。進程控制塊可以包含如下信息:進程類型標號、進程系統(tǒng)號、進程狀態(tài)(本程序未用)、進程產(chǎn)品(字符)、進程鏈指針等等。

系統(tǒng)開辟了一個緩沖區(qū),大小由buffersize指定。

程序中有三個鏈隊列,一個鏈表。一個就緒隊列(ready),兩個等待隊列:生產(chǎn)者等待隊列(producer);消費者隊列(consumer)。一個鏈表(over),用于收集已經(jīng)運行結(jié)束的進程

本程序通過函數(shù)模擬信號量的原子操作。

算法的文字描述:

①由用戶指定要產(chǎn)生的進程及其類別,存入進入就緒隊列。

②調(diào)度程序從就緒隊列中提取一個就緒進程運行。如果申請的資源不存在則進入響應(yīng)的等待隊列,調(diào)度程序調(diào)度就緒隊列中的下一個進程。進程運行結(jié)束時,會檢查對應(yīng)的等待隊列,激活隊列中的進程進入就緒隊列。運行結(jié)束的進程進入over鏈表。重復(fù)這一過程直至就緒隊列為空。

③程序詢問是否要繼續(xù)?如果要轉(zhuǎn)直①開始執(zhí)行,否則退出程序。源程序代碼部分:#include<stdio.h>#include<malloc.h>

//Canonlybeusedinindependentsituation;

//#definegetmem(type)(type*)malloc(sizeof(type))#definebuffersize5

intprocessnum=0;//thenumofprocessesstructpcb{/*定義進程控制塊PCB*/

intflag;//flag=1denoteproducer;flag=2denoteconsumer;

intnumlabel;

charproduct;

charstate;

structpcb*processlink;

}*exe=NULL,*over=NULL;

typedefstructpcbPCB;PCB*readyhead=NULL,*readytail=NULL;

PCB*consumerhead=NULL,*consumertail=NULL;

PCB*producerhead=NULL,*producertail=NULL;//產(chǎn)品數(shù)量

intproductnum=0;

intfull=0,empty=buffersize;//semaphore

charbuffer[buffersize];//緩沖區(qū)

intbufferpoint=0;//緩沖區(qū)指針voidlinkqueue(PCB*process,PCB**tail);

PCB*getq(PCB*head,PCB**tail);

boolhasElement(PCB*pro);

voiddisplay(PCB*p);

voidlinklist(PCB*p,PCB*listhead);

voidfreelink(PCB*linkhead);

boolprocessproc();

boolwaitempty();

boolwaitfull();

voidsignalempty();

voidsignalfull();

voidproducerrun();

voidcomsuerrun();

boolhasElement(PCB*pro);voidlinklist(PCB*p,PCB*listhead)

{

PCB*cursor=listhead;

while(cursor->processlink!=NULL){

cursor=cursor->processlink;

}

cursor->processlink=p;

}voidfreelink(PCB*linkhead)

{

PCB*p;

while(linkhead!=NULL){

p=linkhead;

linkhead=linkhead->processlink;

free(p);

}

}voidlinkqueue(PCB*process,PCB**

tail)

{

if((*tail)!=NULL){

(*tail)->processlink=process;

(*tail)=process;

}

else{

printf("隊列未初始化!");

}

}PCB*getq(PCB*head,PCB**tail)

{

PCB*p;

p=head->processlink;

if(p!=NULL){

head->processlink=p->processlink;

p->processlink=NULL;

if(head->processlink==NULL)

(*tail)=head;

}

else

returnNULL;

returnp;

}boolprocessproc()

{

inti,f,num;

charch;

PCB*p=NULL;

PCB**p1=NULL;

printf("\n請輸入希望產(chǎn)生的進程個數(shù)?");

scanf("%d",&num);

getchar();

//

if(num>=100){

//

printf("您怎么要產(chǎn)生這么多進程!DemandsDenied!");

//

returnfalse;

//

}

for(i=0;i<NUM;I++){

printf("\n請輸入您要產(chǎn)生的進程:輸入1為生產(chǎn)者進程;輸入2為消費者進程\n");

scanf("%d",&f);

getchar();

p=(PCB*)malloc(sizeof(PCB));

if(!p){

printf("內(nèi)存分配失敗");

returnfalse;

}

p->flag=f;

processnum++;

p->numlabel=processnum;

p->state='w';

p->processlink=NULL;

if(p->flag==1){

printf("您要產(chǎn)生的進程是生產(chǎn)者,它是第%d個進程。請您輸入您要該進程產(chǎn)生的字符!\n",processnum);

scanf("%c",&ch);

getchar();

p->product=ch;

productnum++;

printf("您要該進程產(chǎn)生的字符是%c\n",p->product);

}

else{

printf("您要產(chǎn)生的進程是消費者,它是第%d個進程。\n",p->numlabel);

}

linkqueue(p,&readytail);

}

returntrue;

}boolwaitempty()

{

if(empty<=0)

{

printf("進程%d:緩沖區(qū)存數(shù),緩沖區(qū)滿,該進程進入生產(chǎn)者等待隊列\(zhòng)n",exe->numlabel);

linkqueue(exe,&producertail);

returnfalse;

}

else{

empty--;

returntrue;

}

}voidsignalempty()

{

PCB*p;

if(hasElement(producerhead)){

p=getq(producerhead,&producertail);

linkqueue(p,&readytail);

printf("等待中的生產(chǎn)者進程進入就緒隊列,它的進程號是%d\n",p->numlabel);

}

empty++;

}

boolwaitfull()

{

if(full<=0)

{

printf("進程%d:緩沖區(qū)取數(shù),緩沖區(qū)空,該進程進入消費者等待隊列\(zhòng)n",exe->numlabel);

linkqueue(exe,&consumertail);

returnfalse;

}

else{

full--;

returntrue;}

}voidsignalfull()

{

PCB*p;

if(hasElement(consumerhead)){

p=getq(consumerhead,&consumertail);

linkqueue(p,&readytail);

printf("等待中的消費者進程進入就緒隊列,它的進程號是%d\n",p->numlabel);

}

full++;

}voidproducerrun()

{

if(!waitempty())

return;

printf("進程%d開始向緩沖區(qū)存數(shù)%c\n",exe->numlabel,exe->product);

buffer[bufferpoint]=exe->product;

bufferpoint++;

printf("進程%d向緩沖區(qū)存數(shù)操作結(jié)束\n",exe->numlabel);

signalfull();

linklist(exe,over);

}voidcomsuerrun()

{

if(!waitfull())

return;

printf("進程%d開始向緩沖區(qū)取數(shù)\n",exe->numlabel);

exe->product=buffer[bufferpoint-1];

bufferpoint--;

printf("進程%d向緩沖區(qū)取數(shù)操作結(jié)束,取數(shù)是%c\n",exe->numlabel,exe->product);

signalempty();

linklist(exe,over);

}voiddisplay(PCB*p)

{

p=p->processlink;

while(p!=NULL){

printf("進程%d,它是一個",p->numlabel);

p->flag==1?printf("生產(chǎn)者\n"):printf("消費者\n");

p=p->processlink;

}

}boolhasElement(PCB*pro)

{

if(pro->processlink==NULL)

returnfalse;

else

returntrue;

}voidmain()

{

charterminate;

boolelement;

printf("你想開始程序嗎?(y/n)");

scanf("%c",&terminate);

getchar();

//Queueinitialize;

readyhead=(PCB*)malloc(sizeof(PCB));

if(readyhead==NULL)return;

readytail=readyhead;

readyhead->flag=3;

readyhead->numlabel=processnum;

readyhead->state='w';

readyhead->processlink=NULL;

consumerhead=(PCB*)malloc(sizeof(PCB));

if(consumerhead==NULL)return;

consumertail=consumerhead;

consumerhead->processlink=NULL;

consumerhead->flag=4;

consumerhead->numlabel=processnum;

consumerhead->state='w';

consumerhead->processlink=NULL;

producerhead=(PCB*)malloc(sizeof(PCB));

if(producerhead==NULL)return;

producertail=producerhead;

producerhead->processlink=NULL;

producerhead->flag=5;

producerhead->numlabel=processnum;

producerhead->state='w';

producerhead->processlink=NULL;

over=(PCB*)malloc(sizeof(PCB));

if(over==NULL)return;

over->processlink=NULL;

while(terminate=='y')

{

if(!processproc())

break;

element=hasElement(readyhead);

while(element){

exe=getq(readyhead,&readytail);

printf("進程%d申請運行,它是一個",exe->numlabel);

exe->flag==1?printf("生產(chǎn)者\n"):printf("消費者\n");

if(exe->flag==1)

producerrun();

else

comsuerrun();

element=hasElement(readyhead);

}

printf("就緒隊列沒有進程\n");

if(hasElement(consumerhead))

{

p

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論