0
votes

The server is intended to be a multithreaded server which spawns new threads upon either a producer or consumer connection via TCP. My problem is that I'm getting stuck in a wait state once a producer client fills the queue. This snippet shows the server's handling of a producer connection. The format of the request the producer sends is PUT (item)

    reqline[0] = strtok (mesg, " \t\n");


 if ( strncmp(reqline[0], "PUT\0", 4)==0 )
           {
            item[0]=strtok(NULL," \t\n");
            pthread_create (&pro, NULL, producer, fifo);
            pthread_join (pro, NULL);


            }

So you can see I'm creating a new thread which handles the job of filling the queue/detecting when it is empty. The code within producer:

    queue *fifo;
    int i=atoi(item[0]);
    char*fullmsg="Full\n";

    fifo = (queue *)q;

    pthread_mutex_lock (fifo->mut);

         while (fifo->full) { //the problem  block

              printf ("producer: queue FULL.\n");
              send(conn_s, fullmsg,strlen(fullmsg),0);
              pthread_cond_wait (fifo->notFull, fifo->mut);
           } 

           queueAdd (fifo, 0);
           pthread_mutex_unlock (fifo->mut);
           pthread_cond_signal (fifo->notEmpty);

I believe the wait condition is the problem. The server is obviously waiting for a condition which can never be met, considering a consumer thread was not started to eat up the queue. I was thinking that I should change the condition to wait for an incoming consumer connection and then start a consumer thread. But then it seems silly to do so within this method. If I send a consumer request while in this state nothing happens.

Any suggestions are greatly appreciated. I'm not sure if this design is feasible.

1
"Producer" and "consumer" are supposed to be named as "server" and "client", don't they?user529758

1 Answers

0
votes

Your problem seems to be that you're calling pthread_join() immediately after pthread_create(), which means that the main thread stops here until the producer exits - it can't ever accept a consumer connection, so the producer can't make progress once the queue is full.