114 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			114 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | #include "main.h"
 | ||
|  | 
 | ||
|  | 
 | ||
|  | #define SIZE 1
 | ||
|  | pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER; | ||
|  | pthread_cond_t myCond1, myCond2; | ||
|  | int myCount; | ||
|  | 
 | ||
|  | int isProduced = 0; | ||
|  | 
 | ||
|  | int main() { | ||
|  | 
 | ||
|  |   printf("PROGRAM START\n"); | ||
|  |   fflush(stdout); | ||
|  | 
 | ||
|  |   // Create the consumer thread
 | ||
|  |   pthread_t consumerID; | ||
|  |   int resultInt = pthread_create(&consumerID, NULL, consumer, NULL); | ||
|  | 
 | ||
|  |   producer(); | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | void producer() { | ||
|  | 
 | ||
|  |   while (1) { | ||
|  | 
 | ||
|  |   pthread_mutex_lock(&myMutex); | ||
|  |   print_mutex("PRODUCER", "locked"); | ||
|  | 
 | ||
|  | 
 | ||
|  |   while (isProduced == SIZE) { | ||
|  |     print_wait("PRODUCER", "myCond2"); | ||
|  |     pthread_cond_wait(&myCond2, &myMutex); | ||
|  |   } | ||
|  | 
 | ||
|  |   if(myCount == 10) | ||
|  |     end_program(); | ||
|  | 
 | ||
|  |   myCount++; | ||
|  |   isProduced++; | ||
|  | 
 | ||
|  |   pthread_mutex_unlock(&myMutex); | ||
|  |   print_mutex("PRODUCER", "unlocked"); | ||
|  |   pthread_cond_signal(&myCond1);  | ||
|  |   print_signal("PRODUCER", "myCond1"); | ||
|  |   } | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | void* consumer() { | ||
|  | 
 | ||
|  |   printf("CONSUMER THREAD CREATED\n"); | ||
|  |   fflush(stdout); | ||
|  | 
 | ||
|  |   while (1) { | ||
|  | 
 | ||
|  |   pthread_mutex_lock(&myMutex); | ||
|  |   print_mutex("CONSUMER", "locked"); | ||
|  | 
 | ||
|  |   while (isProduced == 0) { | ||
|  |     print_wait("CONSUMER", "myCond1"); | ||
|  |     pthread_cond_wait(&myCond1, &myMutex); | ||
|  |   } | ||
|  | 
 | ||
|  |   print_count(); | ||
|  |   isProduced--; | ||
|  |    | ||
|  |   pthread_mutex_unlock(&myMutex);  | ||
|  |   print_mutex("CONSUMER", "unlocked"); | ||
|  |   pthread_cond_signal(&myCond2); | ||
|  |   print_signal("CONSUMER", "myCond1"); | ||
|  |   } | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | void end_program() { | ||
|  | 
 | ||
|  |   printf("PROGRAM END\n"); | ||
|  |   fflush(stdout); | ||
|  |   exit(0); | ||
|  | 
 | ||
|  | } | ||
|  | void print_mutex(char* type, char* lockstate) { | ||
|  | 
 | ||
|  |   printf("%s: myMutex %s\n", type, lockstate); | ||
|  |   fflush(stdout); | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | void print_count() { | ||
|  | 
 | ||
|  |   printf("myCount: %d -> %d\n", myCount-1, myCount); | ||
|  |   fflush(stdout); | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | void print_wait(char* type, char* var) { | ||
|  | 
 | ||
|  |   printf("%s: waiting on %s\n", type, var); | ||
|  |   fflush(stdout); | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | void print_signal(char* type, char* var) { | ||
|  | 
 | ||
|  |   printf("%s: signaling %s\n", type, var); | ||
|  |   fflush(stdout); | ||
|  | 
 | ||
|  | } |