操作系統(tǒng)實驗二Mutex_第1頁
操作系統(tǒng)實驗二Mutex_第2頁
操作系統(tǒng)實驗二Mutex_第3頁
操作系統(tǒng)實驗二Mutex_第4頁
操作系統(tǒng)實驗二Mutex_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、操作系統(tǒng) 實驗一姓名:廖桉冬 學號:09012431日期:15/4/29實驗內(nèi)容:在Windows和Linux操作系統(tǒng)上,利用各自操作系統(tǒng)提供的Mutex和信號量機制(Win32 API或Pthreads),實現(xiàn)生產(chǎn)者/消費者問題。具體要求見”O(jiān)perating System Concepts(Seventh Edition)” Chapter 6后的Project(P236-241)。實驗目的:通過實驗,理解Win32 API、Pthreads中mutex locks、semaphores等使用方法,并掌握如何利用它們實現(xiàn)進程(線程)間的同步和互斥。設(shè)計思路和流程圖生產(chǎn)者/消費者主要考慮兩個

2、同步問題:1、 產(chǎn)品隊列被兩者共同訪問,因此需要保持產(chǎn)品變化的互斥性;2、 產(chǎn)品有上限,因此生產(chǎn)者不能無限生產(chǎn),多個生產(chǎn)者應該保持同步;消費者亦然。/生產(chǎn)者Do/produce an itemWait(empty);/隊列有空位才能將產(chǎn)品放入Wait(mutex);/互斥訪問隊列/add productSignal(mutex);Signal(full);/告訴消費者有產(chǎn)品可以取用/消費者DoWait(full);/隊列有產(chǎn)品才能消費Wait(mutex);/互斥訪問隊列/consume productSignal(mutex);Signal(full);/告訴生產(chǎn)者隊列有空位主要數(shù)據(jù)結(jié)構(gòu)及其

3、說明int BuffM:有限緩沖隊列HANDLE/sem_t Mutex:互斥鎖HANDLE/sem_t Full/Empty:隊列有物品/空位的信號源程序并附上注釋/WIN32#include <iostream>#include<stdio.h>#include <stdlib.h>#include <time.h>/系統(tǒng)調(diào)用#include<windows.h>#include<process.h>using namespace std;#define M 10#define Random_Max 50/插入隨機數(shù)最

4、大值int buffM=0; /緩沖區(qū)大小為10int in=0;/生產(chǎn)者下標int out=0;/消費者下標int prod_id=0,proc_id=0;/初始化idbool con=true;HANDLE Empty=NULL; /空緩沖信號量HANDLE Full=NULL;/滿緩沖信號量HANDLE Mutex=NULL;/緩沖區(qū)互斥鎖void produce();void consume();UINT _stdcall ThreadProd(LPVOID);UINT _stdcall ThreadCons(LPVOID);/生產(chǎn)void produce()/隨機產(chǎn)生“產(chǎn)品”放入隊列

5、srand(time(0);int j = rand() % Random_Max;buffin=j;/打印生產(chǎn)信息cout<<" produce "<<j<<" into buffer"<<endl;/指針移動,下一個空項in = (in+1)%M;/消費void consume()cout<<" consume "<<buffout<<" from buffer"<<endl;/指針移動,下一個滿項out = (ou

6、t+1)%M;/生產(chǎn)者入口函數(shù)UINT _stdcall ThreadProd(LPVOID)int id=+prod_id;while(1) /初始化srand(time(0);int i = rand() % 1000; Sleep(i+3000);/等待隊列空WaitForSingleObject(Empty,INFINITE);/等待互斥鎖WaitForSingleObject(Mutex,INFINITE);/打印生產(chǎn)者信息cout<<"Producer:"<<id;/生產(chǎn)produce();/釋放semaphoreReleaseSemap

7、hore(Full,1,NULL);ReleaseSemaphore(Mutex,1,NULL);return 0;/消費者入口函數(shù)UINT _stdcall ThreadCons(LPVOID)int id=+proc_id;while(1)/初始化srand(time(0);int i = rand() % 1000;Sleep(i);/等待隊列WaitForSingleObject(Full,INFINITE);/等待互斥鎖WaitForSingleObject(Mutex,INFINITE);cout<<"Consumer:"<<id;/消費

8、consume();/釋放ReleaseSemaphore(Empty,1,NULL);ReleaseSemaphore(Mutex,1,NULL);return 0;void main()/標識主進程UINT uId;int prod_num,cons_num,runtime;cout<<"請輸入生產(chǎn)者個數(shù)和消費者個數(shù):"cin>>prod_num>>cons_num;cout<<"請輸入運行時間"cin>>runtime;/線程標識HANDLE *h_producer = new HANDL

9、Eprod_num;HANDLE *h_consumer = new HANDLEcons_num;/初始化Empty = CreateSemaphore(NULL,M,M,NULL);/ 緩沖區(qū)滿,無法生產(chǎn)Full = CreateSemaphore(NULL,0,M,NULL);/緩沖區(qū)空,無法消費Mutex = CreateSemaphore(NULL,1,1,NULL);/ 線程互斥/創(chuàng)建N個生產(chǎn)者int i;for(i=0;i<prod_num;i+) h_produceri=(HANDLE):_beginthreadex(NULL,0,ThreadProd,NULL,0,&a

10、mp;uId);/創(chuàng)建N個消費者for(i=0;i<cons_num;i+) h_consumeri=(HANDLE):_beginthreadex(NULL,0,ThreadCons,NULL,0,&uId);Sleep(runtime);/LINUX(Pthread)#include <pthread.h>#include <semaphore.h>#include <iostream>#include <stdlib.h>#include <unistd.h>#include <time.h>#incl

11、ude <stdio.h>using namespace std;#define M 10#define Random_Max 50int buffM=0; int prod_id=0;sem_t Empty;sem_t Full;sem_t Mutex; void produce();void consume();void *producer(void *);void *consumer(void *);void produce()srand(time(0);int j = rand() % Random_Max;buffin=j;cout<<" produ

12、ce "<<j<<" into buffer"<<endl;in = (in+1)%M;void consume()cout<<" consume "<<buffout<<" from buffer"<<endl;out = (out+1)%M;void *producer(void *arg)int id=+prod_id;while(1)srand(time(0);int i = rand() % 5; sleep(i);sem_wait

13、(&Empty);sem_wait(&Mutex);cout<<"Producer"<<id;produce();sem_post(&Full);sem_post(&Mutex);cout<<"end"<<endl;return 0;void *consumer(void *arg)int id=+prod_id;while(1)srand(time(0);int i = rand() % 5;sleep(i);sem_wait(&Full);sem_wait(&am

14、p;Mutex);cout<<"Consumer "<<id;consume();sem_post(&Empty);sem_post(&Mutex);return 0;int main()int prod_num,cons_num,runtime;cout<<"input producer and consumer number:"cin>>prod_num>>cons_num;cout<<"input how long main sleep:"c

15、in>>runtime;pthread_t *h_producer = new pthread_tprod_num;pthread_t *h_consumer = new pthread_tcons_num;sem_init(&Empty,0,M);sem_init(&Full,0,0);sem_init(&Mutex,0,1); int i;for(i=0;i<prod_num;i+)pthread_create(&h_produceri, NULL,producer, NULL);for(i=0;i<cons_num;i+)pthread_create(&h_consumeri, NULL,consumer, NULL);sleep(runtime);程序運行時的初值和運行結(jié)果消費者:10 生產(chǎn)者:10 運行時間:6000

溫馨提示

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

評論

0/150

提交評論