0

I am learning Pthread programming. Here is the question i got at the end of my assignment:

I want to copy each bytes in the source file to a new .txt file by using producer-consumerproblem.
Here is my code structure:

void *producer(....) {
     while(1){
        nsleep();
        read_byte(....);
        nsleep();
        produceToBuffer(....);
     }
}
void *consumer(....) {
     while(1){
        nsleep();
        consumeFromBuffer(....);
        nsleep();
        write_byte(....);
     }
}


int main() {
        pthread_t inThread[nIn];
        pthread_t outThread[nOut];

        //initialize mutex locks for all functions
        pthread_mutex_init(&_mutexConsume, NULL);
        pthread_mutex_init(&_mutexProduce, NULL);
        pthread_mutex_init(&_mutexWrite, NULL);
        pthread_mutex_init(&_mutexRead, NULL);


        sem_init(&empty, 0, size);  //initialize semaphore signal the empty slots available
        sem_init(&full, 0, 0);      //initialize semaphore full signal

        for(i = 0; i < nIn; i++) {
            pthread_create(inThread+i, NULL, producer, null);

        }

        for(j = 0; j < nOut; j++) {
            pthread_create(outThread+i, NULL, consumer, null);

        }
}


My question is: producer thread finish at the end of file, which is pthread_exit(0); when the EOF detected, but for consumer thread, my thought is either finish after sleep or finish when all consumer thread are waiting semaphore full.
Can someone help me with that?

Rob Ye
  • 63
  • 1
  • 7
  • 1
    I'm not sure I understand your question. The situation is made worse with the uncompilable, half-pseudocode that you show. You don't know how to tell the consumer that input is over? One possible approach is to signalize that by inserting a special value in the buffer queue. – Filipe Gonçalves Jul 05 '15 at 20:32

1 Answers1

1

Is there a reason to keep the consumer threads active after the producers are gone?

If there isn't, you can signal the consumer threads there are no more producer threads and use that as a termination signal so your main thread waits for all producer threads.

You can use pthread_join(producer_thread_id) to wait for a producer thread to finish, and once all producer threads are done, use pthread_cancel(consumer_thread_id) for each consumer thread to abort their execution.

You need to save each thread_id to an array when you use pthread_create so you can use them for pthread_cancel and pthread_join

You can read about pthread_join here and pthread_cancel here

Ishay Peled
  • 2,783
  • 1
  • 23
  • 37
  • for my second thought, my idea is initialize a semaphore with value of producer threads number, in the produce function, sem_post(&semaphore), and in the consumer function, sem_wait(&semaphore); and in the main, we check if semaphore is 0, which means no more producer is producing the item to buffer. – Rob Ye Jul 05 '15 at 21:39
  • Not sure I understand entirely - if you `sem_post` in producer and `sem_wait` in the consumer, and you have less consumers than producers, this will not work. Even if you have the same number of consumers and producers it won't work because the semaphore will always be 0 – Ishay Peled Jul 06 '15 at 06:34