//Esempio i due processi somma e sottrai si contendono la risorsa num #include #include #include #include #include #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) DWORD finito (LPDWORD lpdwParam) {int *fine; fine=(int *) lpdwParam; _getch(); *fine=1; return 0; } DWORD Somma (LPDWORD lpdwParam) {int *num,fine=0; num=(int *)lpdwParam; HANDLE hMutex; hMutex=OpenMutex(EVENT_MODIFY_STATE|SYNCHRONIZE, FALSE,"SommaMutex"); printf ("Somma start\n"); while(fine==0) {WaitForSingleObject(hMutex,INFINITE); num[0]+=num[1]; printf ("Somma %d \n",num[0]); ReleaseMutex(hMutex); Sleep(10); } return 0; } ///////////////////////////////////////// DWORD Prodotto (LPDWORD lpdwParam) {int *num,fine=0; num=(int *)lpdwParam; HANDLE hMutex; hMutex=OpenMutex(EVENT_MODIFY_STATE|SYNCHRONIZE, FALSE,"ProdottoMutex"); printf ("Prodotto start\n"); while(fine==0) {WaitForSingleObject(hMutex,INFINITE); num[0]*=num[2]; printf ("Prodotto %d \n",num[0]); ReleaseMutex(hMutex); Sleep(10); } return 0; } ///////////////////////////////////////// int main () {int i,fine=0; int numeri[3]; HANDLE hMutexsomma,hMutexprodotto, hThreads[2],hfineThreads; DWORD dwThreadId0,dwThreadId1, dwThreadId2; hMutexsomma=CreateMutex(NULL,TRUE,"SommaMutex"); hMutexprodotto=CreateMutex(NULL,TRUE,"ProdottoMutex"); if ( (hMutexsomma==NULL) || (hMutexprodotto==NULL) ) fprintf(stderr,"Errore CreateMutex\n"); else {hfineThreads=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) finito, (LPVOID) &fine,0, &dwThreadId0); if (hfineThreads==NULL) fprintf(stderr,"Errore CreateThread finito error\n"); else {hThreads[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Somma, (LPVOID) numeri,CREATE_SUSPENDED, &dwThreadId1); if (hThreads[0]==NULL) fprintf(stderr,"Errore CreateThread Somma error\n"); else {hThreads[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Prodotto, (LPVOID) numeri, CREATE_SUSPENDED, &dwThreadId2); if (hThreads[1]==NULL) fprintf(stderr,"Errore CreateThread Prodotto error\n"); else {i=1; while(fine==0) {srand( (unsigned)time( NULL ) ); WaitForSingleObject(hMutexsomma,INFINITE); /* aspetta che il mutexsomma sia libero inizialmente il mutex è libero*/ WaitForSingleObject(hMutexprodotto,INFINITE); /* aspetta che il mutexprodotto sia libero inizialmente il mutex è libero*/ numeri[0]=GetRandom( 1, 100 ); printf("primo numero della terna Numero %d e':%d\n",i,numeri[0]); numeri[1]=GetRandom( 1, 100 ); printf("secondo numero della terna Numero %d e':%d\n",i,numeri[1]); numeri[2]=GetRandom( 1, 100 ); printf("terzo numero della terna Numero %d e':%d\n",i++,numeri[2]); ResumeThread( hThreads[0]); ReleaseMutex(hMutexsomma); Sleep(5); WaitForSingleObject(hMutexsomma,INFINITE); /* aspetta che il mutexsomma sia libero*/ SuspendThread( hThreads[0]); ReleaseMutex(hMutexprodotto); ResumeThread( hThreads[1]); Sleep(5); WaitForSingleObject(hMutexprodotto,INFINITE); /* aspetta che il mutexsomma sia libero*/ SuspendThread( hThreads[1]); ReleaseMutex(hMutexsomma); } TerminateThread(hThreads[0],0); TerminateThread(hThreads[0],1); } } } } }