void *operators(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, operator_thread #%ld!\n", tid); queue require; queue tools; while(true)//for (int i=0; i<10; i++)//change to while(true) { pthread_mutex_lock(&mutex_pause); while (isPaused == 1) { pthread_cond_wait(&cond_pause, &mutex_pause); } //else //{ pthread_mutex_unlock(&mutex_pause); int rand_time=rand() % 10000; usleep(rand_time); pthread_mutex_lock(&M);//lock mutex //critical section if (fix_buffer.size()==0) { cout<<"Empty"<<endl; dead_count++; cout<<"Deadlock due to Empty!!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; } while(fix_buffer.size()==0)//wait if fixed size buffer is 0 if (pthread_cond_wait (&C1, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); } if (require.size()==0)//get 1st material { int temp=fix_buffer.dequeue(); pthread_cond_signal (&C); require.enqueue(temp); cout<<"get "<<temp<<"by "<<tid<<endl; cout<<"require: "<<endl; require.show(); } else if (require.size()==1)//get 2nd material { int temp1=fix_buffer.dequeue(); pthread_cond_signal(&C); if (temp1 != require.front()) { require.enqueue(temp1); cout<<"get "<<temp1<<"by "<<tid<<endl; cout<<"require: "<<endl; require.show(); int check_product=require.check_p(); if (output_Q.size()==0) //starting queue no need to check for previous product { int random_tool1; int random_tool2; if(pthread_mutex_trylock(&M1)==0) { while(tools.size()<2) { if (tools.size()==0) { random_tool1=rand() % tool_num + 1; tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } else if (tools.size()==1) { random_tool1=rand() % tool_num + 1; if (!(random_tool1==random_tool2)) { tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } } } } int product=require.dequeue(); product=product+require.dequeue(); output_Q.enqueue(product); cout<<"output_Q: "<<endl; output_Q.show(); if (product==3) product1_count++; else if (product==4) product2_count++; else product3_count++; cout<<product1_count<<" of product 3 are generated\n"; cout<<product2_count<<" of product 4 are generated\n"; cout<<product3_count<<" of product 5 are generated\n"; } else// if it is not 1st product { if ((!output_Q.recent(check_product)) || (!output_Q.check_entire(check_product))) { dead_count++; cout<<"Deadlock due to recent product is same as new product or different number of products are more than 10 !!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; while(fix_buffer.size() >=10) //check if there are many of those products if (pthread_cond_wait (&C1, &M))//producer of the material goes to wait { fprintf (stdout, "pthread_cond_wait: producer\n"); exit (-1); } //for (int i=0;i<2;i++) //{ int put_back=require.dequeue(); fix_buffer.enqueue(put_back); pthread_cond_signal (&C1); cout<<"Prevent Deadlock put : "<<put_back<<" to buffer"<<endl; fix_buffer.show(); cout<<"current size of fix_buffer is "<<fix_buffer.size()<<endl; //} } /*while(!output_Q.recent(product))//if recent is of same kind then wait if (pthread_cond_wait (&C2, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); } while(!output_Q.check_entire(product))// wait if the difference are 10 if (pthread_cond_wait (&C2, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); }*/ else { int random_tool1; int random_tool2; if(pthread_mutex_trylock(&M1)==0) { while(tools.size()<2) { if (tools.size()==0) { random_tool1=rand() % tool_num + 1; tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } else if (tools.size()==1) { random_tool1=rand() % tool_num + 1; if (!(random_tool1==random_tool2)) { tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } } } int product=require.dequeue(); product=product+require.dequeue(); cout<<"product is "<<product<<" by tools"<<tools.dequeue()<<" and "<<tools.dequeue()<<endl; output_Q.enqueue(product); cout<<"output_Q: "<<endl; output_Q.show(); cout<<"current size of output queue is "<<output_Q.size()<<endl; if (product==3) product1_count++; else if (product==4) product2_count++; else product3_count++; cout<<product1_count<<" of product 3 are generated\n"; cout<<product2_count<<" of product 4 are generated\n"; cout<<product3_count<<" of product 5 are generated\n"; } } }//end of not 1st output }// end of if (temp1 ! = require.front()) else if ((temp1 == require.front())) { dead_count++; cout<<"Deadlock due to both materials are same!!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; fix_buffer.enqueue(temp1); pthread_cond_signal (&C1); } } pthread_mutex_unlock ( &M); pthread_mutex_unlock ( &M1); pthread_mutex_unlock ( &M2); pthread_mutex_unlock ( &M3); //non critical section pthread_cond_signal (&C); //}//if pause } pthread_exit(NULL); }
void * generators(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, generator_thread #%ld!\n", tid); while(true)//for (int i=0; i<10; i++)//change to while(true) { pthread_mutex_lock(&mutex_pause); while (isPaused == 1) { pthread_cond_wait(&cond_pause, &mutex_pause); } //else //{ pthread_mutex_unlock(&mutex_pause); //int rand_time=rand() % 10000; //usleep(rand_time); pthread_mutex_lock(&M);//lock mutex //critical section int material=tid; if ((!fix_buffer.check(tid)) || (!fix_buffer.check_buffer(material)) || (fix_buffer.size() >=10) ) { dead_count++; cout<<"Deadlock due to full or too many of the material in the buffer!!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; } while ((!fix_buffer.check(tid)) || (!fix_buffer.check_buffer(material)) || (fix_buffer.size() >=10) )//check if there are many of those products if (pthread_cond_wait (&C, &M))//producer of the material goes to wait { fprintf (stdout, "pthread_cond_wait: producer\n"); exit (-1); } /*int material=tid; while(!fix_buffer.check_buffer(material))// wait if the difference are 10 if (pthread_cond_wait (&C, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); }*/ printf(" thread #%ld produces %ld\n", tid, material); if (fix_buffer.size() >= 10) { cout<<"Full Wait"<<endl; } /*while (fix_buffer.size() >=10)//if its full wait if (pthread_cond_wait ( &C1 , &M)) { fprintf (stdout, "pthread_cond_wait: producer\n"); exit (-1); }*/ fix_buffer.enqueue(material); //pthread_cond_signal (&C1); if (material==1) material1_count++; else if (material==2) material2_count++; else material3_count++; cout<<"current size of fix_buffer is "<<fix_buffer.size()<<endl; fix_buffer.show(); cout<<material1_count<<" of material 1 are generated\n"; cout<<material2_count<<" of material 2 are generated\n"; cout<<material3_count<<" of material 3 are generated\n"; pthread_mutex_unlock (&M); // noncritical section pthread_cond_signal (&C1); } //} pthread_exit(NULL); }