You are on page 1of 6

1. PETERSON # include <stdio.h> # include <windows.h> #include <conio.

h> # define bufsize 5 # define N 20 int BUFFER[bufsize]; int in=0, out=0, count=0; int turn=0; int flag[2]={0,0}; DWORD WINAPI Producer(LPVOID p) { int * pa=(int *)p; int i=0; while(i<N) { while(count==bufsize); flag [0]=1; turn =1; while(flag[1] && (turn==1)); BUFFER[in]=pa[i++]; in=(in+1) % bufsize ; count=count+1; flag[0]=0; } return 0; } DWORD WINAPI Consumer(LPVOID p) { int * pb=(int *)p; int j=0; while(j<N) { while(count==0); flag[1]=1; turn=0; while(flag[0] && (turn==0)); pb[j++]=BUFFER[out]; out=(out+1) % bufsize; count=count-1; flag[1]=0; } return 0; } void Xuatmang(int a[],int n) { int i; for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n"); }

void main() { int a[N],b[N],i; HANDLE h[2]; DWORD id[2]; for(i=0;i<N;i++) a[i]=i; h[0]=CreateThread(NULL,0,Producer, (LPVOID)a,0,&id[0]); h[1]=CreateThread(NULL,0,Consumer, (LPVOID)b,0,&id[1]); WaitForMultipleObjects(2,h,true,INFINIT E); Xuatmang(a,N); Xuatmang(b,N); getch(); }

2. SEMAPHORE #include <stdio.h> #include <windows.h> #include <conio.h> #define N 20 #define BUFSIZE 5 int buffer[BUFSIZE]; int in=0,out=0,count=0; HANDLE hSem; DWORD WINAPI Producer(LPVOID p) { int *pa=(int*)p; int i=0; while(i<N) { while(count==BUFSIZE); WaitForSingleObject(hSem,INFINITE); buffer[in]=pa[i++]; in=(in+1)%BUFSIZE; count=count+1; ReleaseSemaphore(hSem,1,NULL); } return 0; } DWORD WINAPI Consumer(LPVOID p) { int *pb=(int*)p; int j=0; while(j<N) { while(count==0); WaitForSingleObject(hSem,INFINITE); pb[j++]=buffer[out]; out=(out+1)%BUFSIZE; count=count-1; ReleaseSemaphore(hSem,1,NULL); } return 0; } void xuatmang(int a[],int n) { int i; printf("\n"); for(i=0;i<n;i++) printf("%d ",a[i]); }

void main() { int a[N],b[N],i; HANDLE h[2]; DWORD id[2]; for(i=0;i<N;i++) a[i]=i; hSem=CreateSemaphore(NULL,1,1,NU LL); h[0]=CreateThread(NULL,0,Producer, (LPVOID)a,0,&id[0]); h[1]=CreateThread(NULL,0,Consumer, (LPVOID)b,0,&id[1]); WaitForMultipleObjects (2,h,true,INFINITE); xuatmang(a,N); xuatmang (b,N); CloseHandle(hSem); getch(); }

3. CRITICALSECTION #include <stdio.h> #include <windows.h> #include <conio.h> #define N 20 #define BUFSIZE 5 int buffer[BUFSIZE]; int in=0,out=0,count=0; CRITICAL_SECTION cs; DWORD WINAPI Producer(LPVOID p) { int *pa=(int*)p; int i=0; while(i<N) { while(count==BUFSIZE); EnterCriticalSection(&cs); buffer[in]=pa[i++]; in=(in+1)%BUFSIZE; count=count+1; LeaveCriticalSection(&cs); } return 0; } DWORD WINAPI Consumer(LPVOID p) { int *pb=(int*)p; int j=0; while(j<N) { while(count==0); EnterCriticalSection(&cs); pb[j++]=buffer[out]; out=(out+1)%BUFSIZE; count=count-1; LeaveCriticalSection(&cs); } return 0; } void xuatmang(int a[],int n) { int i; printf("\n"); for(i=0;i<n;i++) printf("%d ",a[i]); }

void main() { int a[N],b[N],i; HANDLE h[2]; DWORD id[2]; for(i=0;i<N;i++) a[i]=i; InitializeCriticalSection(&cs); h[0]=CreateThread(NULL,0,Producer, (LPVOID)a,0,&id[0]); h[1]=CreateThread(NULL,0,Consumer, (LPVOID)b,0,&id[1]); WaitForMultipleObjects(2,h,true,INFINIT E); xuatmang(a,N); xuatmang (b,N); DeleteCriticalSection(&cs); getch(); }

4. MUTEX #include <stdio.h> #include <windows.h> #include <conio.h> #define N 20 #define BUFSIZE 5 int buffer[BUFSIZE]; int in=0,out=0,count=0; HANDLE hMutex; DWORD WINAPI Producer(LPVOID p) { int *pa=(int*)p; int i=0; while(i<N) { while(count==BUFSIZE); WaitForSingleObject(hMutex,INFINITE); buffer[in]=pa[i++]; in=(in+1)%BUFSIZE; count=count+1; ReleaseMutex(hMutex); } return 0; } DWORD WINAPI Consumer(LPVOID p) { int *pb=(int*)p; int j=0; while(j<N) { while(count==0); WaitForSingleObject(hMutex,INFINITE); pb[j++]=buffer[out]; out=(out+1)%BUFSIZE; count=count-1; ReleaseMutex(hMutex); } return 0; } void xuatmang(int a[],int n) { int i; printf("\n"); for(i=0;i<n;i++) printf("%d ",a[i]); }

void main() { int a[N],b[N],i; HANDLE h[2]; DWORD id[2]; for(i=0;i<N;i++) a[i]=i; hMutex=CreateMutex(NULL,false,NULL ); h[0]=CreateThread(NULL,0,Producer, (LPVOID)a,0,&id[0]); h[1]=CreateThread(NULL,0,Consumer, (LPVOID)b,0,&id[1]); WaitForMultipleObjects (2,h,true,INFINITE); xuatmang(a,N); xuatmang (b,N); CloseHandle(hMutex); getch(); }

5. PETERSON dung STACK


#include<stdio.h> #include<windows.h> #include <conio.h> #define BUFSIZE 5 #define N 20 int buffer [BUFSIZE]; int top=0,count=0; int turn=0; int flag[2]={0,0}; DWORD WINAPI producer(LPVOID p) { int *pa=(int *)p; int i=0; while (i<N) { while(count==BUFSIZE); flag[0]=1; turn=1; while((flag[1])&&(turn==1)); buffer[top]=pa[i++]; top=top+1; count=count+1; flag[0]=0; } return 0; } DWORD WINAPI consumer(LPVOID p) { int *pb=(int *)p; int j=0; while(j<N) { while(count==0); flag[1]=1; turn=0; while((flag[0])&&(turn==0)); top=top-1; pb[j++]=buffer[top]; count=count-1; flag[1]=false; } return 0; } void xuatmang(int a[],int n) { int i; printf("\n"); for(i=0;i<n;i++) printf(" %d",a[i]); }

void main() { int a[N],b[N],i; HANDLE h[2]; DWORD id[2]; for(i=0;i<N;i++) a[i]=i; h[0]=CreateThread(NULL,0,producer, (LPVOID)a,0,&id[0]); h[1]=CreateThread(NULL,0,consumer , (LPVOID)b,0,&id[1]); WaitForMultipleObjects(2,h,TRUE,INFIN ITE); xuatmang(a,N); xuatmang(b,N); getch(); }

6. UD SEMAPHORE int b=20; Process P1() { while(1) { ............. b=b-2; ............. } } Process P2() { while(1) { ............. b=b+2; ............. } }

Voi b>0: Semaphore s=9; Process P1() { while(1) { wait(s); b=b-2; ............. } } Process P2() { while(1) { ............. b=b+2; signal(s); } }

You might also like