You are on page 1of 7

Bi 3: Tm hiu lp trnh Process, Thread trong mi trng window

b. Bi ton Producer ! "onsumer.


#. $%ng mute&'
/* AP 18-apr-05 producer-consumer using mutexes for synch */
# include <stdioh!
# include <stdli"h!
# include <#indo#sh!
# include <conioh!
static $A%&'( mutex)
static int counter)
static int max*counter)
static int min*counter)
&+,-& +.%AP. Producer/'P0,.& arg1 2
int iter 3 0)
do 2
+ait4or5ingle,"6ect/ mutex7 .%4.%.8( 1)
counter99)
if /counter ! max*counter1 max*counter 3 counter)
printf/:Producer; counter 3 <d=n:7 counter1)
-elease>utex/ mutex 1)
iter99)
? #hile /iter < 10001)
return 0)
?
&+,-& +.%AP. @onsumer/'P0,.& arg1 2
int iter 3 0)
int i)
&+,-& ret)
do 2
ret 3 +ait4or5ingle,"6ect/ mutex7 .%4.%.8( 1)
counter--)
if/ counter < min*counter1 min*counter 3 counter)
printf/:@onsumer; counter 3 <d=n:7 counter1)
iter99)
-elease>utex/ mutex 1)
? #hile /iter < 10001)
return 0)
?
main /1 2
$A%&'( hProducer7 h@onsumer)
$A%&'( mutex 3 @reate>utex/ %A''7 4A'5(7 %A'' 1)
printf/:Bet@urrentProcess.d 3 <d:7 Bet@urrentProcess.d/11)
hProducer 3 @reate8hread/ %A''7 07 Producer7 %A''7 07 %A'' 1)
h@onsumer 3 @reate8hread/ %A''7 07 @onsumer7 %A''7 07 %A'' 1)
+ait4or5ingle,"6ect/ hProducer7 .%4.%.8( 1)
+ait4or5ingle,"6ect/ h@onsumer7 .%4.%.8( 1)
printf/:<d <3 counter <3 <d=n:7 min*counter7 max*counter1)
@lose$andle/ mutex 1)
*getch/1)
?
(. $%ng semaphore'
"ode'
# include <stdioh!
# include <stdli"h!
# include <#indo#sh!
# include <conioh!
static $A%&'( semaphore)
static int counter)
static int max*counter)
static int min*counter)
&+,-& +.%AP. Producer/'P0,.& arg1 2
int iter 3 0)
do 2
+ait4or5ingle,"6ect/ semaphore7 .%4.%.8( 1)
counter99) // tang count
if /counter ! max*counter1 max*counter 3 counter)
printf/:Producer; counter 3 <d=n:7 counter1)
-elease5emaphore/ semaphore7 17 %A'' 1) // nha semaphore
iter99)
? #hile /iter < 10001)
return 0)
?
&+,-& +.%AP. @onsumer/'P0,.& arg1 2
int iter 3 0)
int i)
&+,-& ret)
do 2
ret 3 +ait4or5ingle,"6ect/ semaphore7 .%4.%.8( 1)
counter--) // giam count
if/ counter < min*counter1 min*counter 3 counter)
printf/:@onsumer; counter 3 <d=n:7 counter1)
iter99)
-elease5emaphore/ semaphore7 17 %A'' 1) // nha semaphore
? #hile /iter < 10001)
return 0)
?
int main /1 2
$A%&'( hProducer7 h@onsumer)
$A%&'( semaphore 3 @reate5emaphore/ %A''7 4A'5(7 4A'5(7 %A'' 1) //
tao semaphore
printf/:Bet@urrentProcess.d 3 <d:7 Bet@urrentProcess.d/11)
hProducer 3 @reate8hread/ %A''7 07 Producer7 %A''7 07 %A'' 1)
h@onsumer 3 @reate8hread/ %A''7 07 @onsumer7 %A''7 07 %A'' 1)
+ait4or5ingle,"6ect/ hProducer7 .%4.%.8( 1)
+ait4or5ingle,"6ect/ h@onsumer7 .%4.%.8( 1)
printf/:<d <3 counter <3 <d=n:7 min*counter7 max*counter1)
@lose$andle/ semaphore 1)
*getch/1)
return 0)
?
). $%ng mute& trong trng h*p s+ d,ng tranh ch-p ring bu..er'
"ode'
# include <stdioh!
# include <stdli"h!
# include <#indo#sh!
# include <conioh!
static $A%&'( mutex)
int %7 in 3 07 out 3 07 num*e)
Coid **ta"le)
char *p7 strDEFG)
Coid *px)
Coid fifoinit /int siHe1)
Coid fifodestroy/1)
int fifoempty/1)
int fifoput/Coid *next1)
int fifoget/1)
&+,-& +.%AP. Producer/'P0,.& arg1 2
int iter 3 0)
do
2
+ait4or5ingle,"6ect/ mutex7 .%4.%.8( 1)
// them du lieu
printf/: .nsert elements=n:1)
printf/: BiCe string =n:1)
scanf/:<s:7str1)
p 3 strdup/str1)
if /I/fifoput//Coid*1 p111
2
free/p1)
printf/: 8a"le is full=n:1)
?
else
2
printf/: .nsert successful=n:1)
?
-elease>utex/ mutex 1)
iter99)
? #hile /iter < 10001)
return 0)
?
&+,-& +.%AP. @onsumer/'P0,.& arg1 2
int iter 3 0)
int i)
&+,-& ret)
#hile /iter < 10001
2
ret 3 +ait4or5ingle,"6ect/ mutex7 .%4.%.8( 1) // dung de Jiem
tra co the ghi du lieu hay Jhong
s#itch /Iret1
2
// ghi du lieu
case +A.8*,KL(@8*0;
printf/:+riting data"ase=n:1)
if /fifoget/1 33 01
2
printf/: 8a"le is null=n:1)
?
else
2
printf/: gia tri la; <s=n:7 px1)
?
5leep/51)

// nha mutex
if /I -elease>utex/ mutex 11
2
// handle error
?
*getch/1)
"reaJ)
case +A.8*AKA%&,%(&;
return 4A'5()
?
iter99)
?
return 0)
?
int main /1 2
int y 3 1)
$A%&'( hProducer7 h@onsumer)
$A%&'( mutex 3 @reate>utex/ %A''7 4A'5(7 %A''1) // tao mutex
printf/: BiCe an integer for siHe of fifo; :1) // lay Jich thuoc cua
ring "uffer
scanf/:<d:7 My1)
fifoinit/y1) // Jhoi tao ring "uffer
printf/:Bet@urrentProcess.d 3 <d =n:7 Bet@urrentProcess.d/11)
hProducer 3 @reate8hread/ %A''7 07 Producer7 %A''7 07 %A'' 1)
h@onsumer 3 @reate8hread/ %A''7 07 @onsumer7 %A''7 07 %A'' 1)
+ait4or5ingle,"6ect/ hProducer7 .%4.%.8( 1)
+ait4or5ingle,"6ect/ h@onsumer7 .%4.%.8( 1)
fifodestroy/1) // huy ring "uffer
@lose$andle/ mutex 1)
*getch/1)
return 0)
?
//-------------------------------------------------//
/*init Nueue*/
Coid fifoinit /int siHe1
2
num*e30) in30)out30)
%3siHe)
printf/:fifo init=n:1)
ta"le3/Coid**1malloc/%*siHeof/Coid*11)
?
/*free memmory*/
Coid fifodestroy/1
2
int i)
if/Ififoempty/11
free/ta"le1)
else
2
for/i3out)i<in)i991
2
free/ta"leDiG1)
?
free/ta"le1)
?
?
/*empty Nueue 3 1 else 0*/
int fifoempty/1
2
return/num*e3301)
?

/*insert element*/
int fifoput/Coid *next1
2
if/num*e 33 %1
return/01)
else
2
ta"leDinG 3 next)
num*e99)
in3/in911<%)
return/11)
?
?
int fifoget/1
2
if/num*e 33 01
return %A'')
else
2
px 3 ta"leDoutG)
num*e--)
out 3 /out911<%)
return/11)
?
?

You might also like