الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

طلب مساعدة في برنامج C pthreads

مغلق
بدأه almalak4 في 24 مايو 2005 · 0 رد · 389 مشاهدة · في نظام تشغيل Unix & Linux
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم

عندي مشكلة في برنامج بلغة C

المشكلة ان الثريدز يشتغلوا بالترتيب

يعني: thread1 , thread2 ,thread3, thread4 , thread5

مع انه المفروض ان الترتيب يكون عشوائي

 
#include <stdio.h> 

#include <stdlib.h> 

#include <pthread.h> 



/* define your constants */ 

#define NUM_BOXES 5 

#define NUM_CYCLE 10 



/* define a random number form 1 to x */ 

#define RND(x) ((int)(((float)rand()/RAND_MAX)*(x)) + 1) 



/* Global variable box */ 

int box[NUM_BOXES];
pthread_mutex_t  mutex1;
pthread_mutex_t  mutex2;
pthread_cond_t  cond_producers_done;
pthread_cond_t  cond_consumer_done;
int producers_done = 0;
int consumer_done = 0;

int areProducersDone( int array[] )
{
int i;
for(i =0; i<= 4; i++)
if (array == 0) return 0;

return 1;
} 

int isConsumerDone( int array[] )
{
int i;
for(i =0; i<= 4; i++)
if (array != 0) return 0;

return 1;
} 

int calculateSum( int array[] )
{
int i;
int sum = 0;
for(i =0; i<= 4; i++)
sum += array;

return sum;
} 

void setBoxEmpty( int array[] )
{
int i;
for(i =0; i<= 4; i++)
array = 0;
} 


/* the produce function */ 

void *produce(void *arg) 

{ 

 int * pointer;
 int threadNumber;
 int random;
 int i;


 pointer = (int *) arg;
 threadNumber = *pointer;




 for (i=0; i<NUM_CYCLE; i++) 

 { 
  
	pthread_mutex_lock(&mutex1);
	random = RND(10);

	/*if(i !=0)
	pthread_cond_wait(&cond_consumer_done,&mutex2);*/
	if( box[threadNumber] == 0)
	box[threadNumber] = random;
	printf("Thread%d puts %d on its box\n",threadNumber+1,random );
	if(areProducersDone(box) == 1){
  pthread_cond_signal(&cond_producers_done);
	/*	printf("producers_done signal is sent\n");*/
  }
	pthread_mutex_unlock(&mutex1);
	sleep(1);
	/*pthread_exit(NULL);*/


 } 

} 



/* the consume function */ 

void *consume(void *arg) 

{ 
/*printf("*** Consumer ***\n");*/
int sum = 0;
int i; 



 for (i=0; i<NUM_CYCLE; i++) 

 {
  printf("***** Cycle #%d *****\n\n",i+1); 
 /* pthread_mutex_lock(&mutex2);*/
  pthread_cond_wait(&cond_producers_done,&mutex1);
  sum = calculateSum(box);
  setBoxEmpty(box);
  pthread_cond_broadcast(&cond_consumer_done);
  printf("\nSum of all boxes is %d\n\n",sum);

 /* pthread_mutex_unlock(&mutex2);*/
  sleep(1);
 } 

} 



/* the main function */ 

int main() 

{ 

pthread_t producer[NUM_BOXES];

pthread_t consumer; 



pthread_mutex_init (&mutex1, NULL);
pthread_mutex_init (&mutex2, NULL);
pthread_cond_init(&cond_producers_done, NULL);
pthread_cond_init(&cond_consumer_done, NULL);



 /* set seed number for random numbers */ 

 srand(0); 


 /* create your threads */

pthread_create(&consumer,NULL,consume,NULL);


int j;
int rc;
int *index;
index = (int *) malloc(sizeof(int));

for (j=0; j < NUM_BOXES; j++) {
	*index = j;
	rc = pthread_create(&producer[j],NULL,produce,(void *) index);
	/*pthread_join(producer[j],NULL);*/

}



 /* wait for them */ 

pthread_join(consumer,NULL);




pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
pthread_cond_destroy(&cond_producers_done);
pthread_cond_destroy(&cond_consumer_done);
printf("All Done.\n");
exit(0);


}

تم تعديل هذه المشاركة بواسطة برق في 25 مايو 2005 في 08:30

هذا الموضوع مغلق.

مواضيع مشابهة