资源描述
《操作系统概念》第七版中的实验项目:生产者消费者问题。本程序中,main()函数需要 三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机 的。
程序代码:
#include〈 stdio h>
# include<stdloilh〉
# include<time h>
#include〈windows.h>
#define BUFFER_SIZE 5
typedef int buffer_;item
struct v
{
int ;i
);
buffer_item buffer[BUFFER_SIZE+1
buffer_item front=0,re;ar=0
HANDLE mutex , empty,full;
int insert_i(ebuffer_item item)
{
/*insert item into buffer
return 0 if successful,otherwise
return-1 indicating an error cond/tion
if((rear+1弘(BUFFER_SIZE+1)==front )
return 1;
buffer[rear=item
rear=(rear+1)%(BUFFER_SIZE+1);
return 0;
}
int remove_item(buffer_item *item)
{
/*remove an object from buffer
placing it in item
return 0 if successful,otherwise
reutrn-1 indication an error condition */
if(front == r)ear
return ;1
* item=buffef front];
front=(front+) % (BUFFER_SIZE+1);
return 0;
DWORD WINAPI producer (PVOID Param ) {
int randl
struct v data=*(struct) Waram;
srand((unsigned)time 0));
while (1
{
Sleep(rand()%101*10
WaitForSingleObjectempty, INFINITE );
WaitForSingleObject(mutexINFINITE );
rand1 =ran();
printf "producer has producerd %d Byn%drand1 data i);
if( insert_itemrand1))
printf("insert data n’rrb;r!\
ReleaseMutex(mutex)
ReleaseSemaphore(full, NULL);
}
}
DWORD WINAPI consumer(PVOID Param )
{
int rand1;
struct v dat<a=struct v ) Param;
srand((unsign)edtime(0)) ;
while (1)
{
Sleep (rand()%101* 10);
WaitForSingleObject(f,ulIlNFINITE);
WaitForSingleObjectmutex,INFINITE);
if(remove_item &rand1 ))
printf("remove data err\f,);
else
printf "consumer consumed^ d By % d \nn,rand1,d<ata);
ReleaseMutex(mutex);
ReleaseSemaphore(empty,1,NULL) ;
}
}
int main(int argxhar *argv[]
{
/* Get command line arguments argv)1( the number of producer threads), argtAh/ number of consumer thread,sargv[3] (sleep ti)e*/
/* Initialize buffer int sleeptime,pnun$num; int *ThreadIdP*ThreadIdS,i; struct * countp* counts; HANDLE *ThreadHandleP,*ThreadHandleS; sleeptime=atoi(argv)[[] pnum=atoi ( argv [2]); snum=atoi ( argv [3]); /*srand( time (NULL)); sleeptime=9;0 pnum=3; snum=3 ;*/ ThreadHandleP= ( HANDLE * ) malloc(pnum * sizeof(HANDLE)); ThreadHandleS= ( HANDLE *) malloc ( snum * sizeof(HANDLE ));
ThreadIdP= ( int * )mallOpnum * sizeof in)); ThreadIdS=(int* ) malloc ( pnum * sizeoff int)); mutex=CreateMutex (NULL,FALSE,NULL ); empty=CreateSemaphore(NULL,BUFFER_SIZE,BUFFER_SIZE , NULL ); full=CreateSemaphore(NULL 0,BUFFER_SIZE+1,NULL); /*Create producer thread(s) countp=(struct v)malloc((pnum+1)*sizeof(struqt v)) counts=(struct v *)malloc((sn)um*+1sizeo(f struct))v ; for(i=0 i<pnum; i++)
{
countp[i+L] i = i+1
ThreadHandleP[i]=CreateThreaNULL , 0,producer&countp i+1 ],0,&ThreadIdP[i;]) } /* Create consumer thread(s)*/ for( i=0; i〈 snum;i++) {
counts[i+1]。i=i+1;
ThreadHandleS[i]=CreateThreaNULL , 0, consumer,&count[ i+1 ],0,&ThreadIdS [i])
}
/*Sleep*/
Sleep(sleepti)m;e
/*Exi*t / return 0;
# include "stdafx.h"
#include〈 stdio。h〉
#include〈 stdlib.h>
# include〈 time。h>
#include<windows。 h>
#define BUFFER-SIZE 5 typedef int buffer-item;
struct v
{ int i;
);
buffer-item buffer [BUFFER-SIZE+1];
buffer-item front=0, rear=0;
HANDLE mutex, empty,full;
int insert-item(buffer-item item)
(
/*insert item into buffer
return 0 if successful,otherwise
return -1 indicating an error condition* /
if ((rear+1)% (BUFFER-SIZE + 1 ) ==front)
return 1;
buffer [ rear ] =item;
rear=(rear+1)%(BUFFER-SIZE+1);
return 0;
}
int remove-item(buffer-item *item)
{
/*remove an object from buffer
placing it in item
return 0 if successful, otherwise
reutrn -1 indication an error condition */
if(front == rear)
return 1;
*item=buffer[front];
front= ( front+1) % (BUFFER-SIZE+1);
return 0;
)
DWORD WINAPI producer (PVOID Param) {
int randl;
struct v data=* ( struct v *)Param;
srand (( unsigned) time (0 ));
while ( 1)
{
Sleep ( rand () %101 * 10);
WaitForSingleObject (empty,INFINITE);
WaitForSingleObject ( mutex, INFINITE);
rand1 =rand ();
printf ("producer has producerd %d By %d\n”,rand1,data.i);
if(insert_item(rand1))
printf ("insert data error!\n");
ReleaseMutex (mutex);
ReleaseSemaphore (full, 1, NULL );
}
)
DWORD WINAPI consumer (PVOID Param)
(
int rand1;
struct v data= * (struct v * ) Param;
srand((unsigned)time(0));
while (1)
{
Sleep(rand()%101*10);
WaitForSingleObject (full,INFINITE);
WaitForSingleObject ( mutex, INFINITE );
if(remove_item(&rand1))
printf("remove data error! \n");
else
printf ( "consumer consumed %d By % d \n", rand1, data。i);
ReleaseMutex(mutex);
ReleaseSemaphore(empty,1,NULL);
)
}
int main (int argc, char * argv [])
(
/ * Get command line arguments argv[1] ) (the number of producer threads ) , argv[2 ] (the number of consumer threads ) ,argv[3] ( sleep time )* /
/ * Initialize buffer * /
int sleeptime, pnum, snum;
DWORD *ThreadIdP,*ThreadIdS,i;
struct v *countp,*counts;
HANDLE *ThreadHandleP, * ThreadHandleS; /*sleeptime=atoi ( argv[1]); pnum=atoi ( argv[2]); snum=atoi (argv[3]); */ //srand(time ( NULL)); sleeptime=90; pnum=3; snum=3; ThreadHandleP=(HANDLE * ) malloc (pnum * sizeof(HANDLE)); ThreadHandleS= ( HANDLE * ) malloc (snum * sizeof ( HANDLE )); ThreadIdP=(DWORD * )malloc (pnum * sizeof(DWORD)); ThreadIdS=(DWORD * )malloc ( pnum * sizeof ( DWORD )); mutex=CreateMutex(NULL, FALSE, NULL); empty=CreateSemaphore (NULL,BUFFER_SIZE, BUFFER_SIZE, NULL); full=CreateSemaphore (NULL, 0, BUFFER_SIZE+1,NULL); /*Create producer thread(s)*/ countp= ( struct v *) malloc((pnum+1 ) *sizeof (struct v)); counts=(struct v *)malloc ( ( snum+1)*sizeof ( struct v)); for ( i=0; i<pnum; i++)
{
countp [i+1]. i = i+1;
ThreadHandleP [i]=CreateThread(NULL,0, producer, &countp [i+1 ] ,0,&ThreadIdP[i]);
)
/ * Create consumer thread(s)*/
for ( i=0; i〈 snum; i++)
{
counts [ i+1]. i=i+1;
ThreadHandleS [ i ] =CreateThread ( NULL,0, consumer, &counts [i+1], 0,&ThreadIdS [i]); ) /*Sleep*/
Sleep (sleeptime);
/*Exit*/ return 0;
consitmep conBunecl 32240 By 2 pi'oclucep has prortucei'rt 12873 By 3 consnmcr consuniccl 31438 By 1 vi'Qducep Jias pro clue e pel 17194 By 2 tii'aduGer 如占 produtci&i'd 751 1
comsumeii:' consumed 12229 By 3
licet' has prodiicei^a 11032 By 2 consuinep cnmsuined 23121 "By 1 pi'Qducep has pro luce pi 144L7 By 1 consitmei' consumed 12873 Ely 2 pi'Qclucer has prortucei'rt 18^?? By 3 con a line i* conBumecl 17154 By 1 consumer consumed 751 By 3 consiLmer consumcdl 110B2 "By 1 vi'Qducer las producerd 28138 By 3 tit'aducer has pruduciGt'd 8726 Ey 2 consumer consumed 14417 By 2 kiraiilucef has proflucei^a 6241 Ey 1 板rodiwe略 has: ppoduceFdl. 22192 By 2 consumer consumed 189?? By 1 pi'Ddiicei' has ppodmcei'cl 17198 By 1 Press any hey to continue
展开阅读全文