10、adIdP[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〈 ti
11、me。h>
#include
#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
12、 -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 in
13、dication 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);
14、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 consume
15、r (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 c
16、onsumed %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 )* /
/ * Initial
17、ize 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 * ) mal
18、loc (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); ful
19、l=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; i20、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'o
21、clucep 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 128
22、73 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