#include #include #include #include "putNget.h" #include "sem_ops.h" #define NUM_CONS 7 #define NUM_PROD 9 int full, empty; void *producer(void * arg1){ int *i = (int *) arg1; /* acquire right to a slot */ P( empty ); /* wait until some are empty */ printf("Producer %d running\n", *i ); put_item( (*i) * (*i) ); V( full ); /* increment full count */ pthread_exit(NULL); } void *consumer(void *arg1){ int *i = (int *) arg1; int *thisitem = (int *)malloc( sizeof(int)); /* wait until a slot is full */ P( full ); get_item(thisitem); printf("Consumer %d got %d. \n", *i, *thisitem ); V( empty ); /* increment empty count */ pthread_exit(thisitem); } int max( int i, int j ){ if( i >= j ) return i; return j; } int main(void){ pthread_t prodtid[NUM_PROD], constid[NUM_CONS]; int slots, i, id[NUM_PROD]; int *returned_item_ptr; empty = semtran( IPC_PRIVATE ); full = semtran( IPC_PRIVATE ); slots = get_buffersize(); for( i = 0; i < slots; i++ ) V( empty ); /* set empty = BUFSIZE, full = 0 */ for( i=0; i