Note that there are some explanatory texts on larger screens.

plurals
  1. POPthread awake and signal advice
    primarykey
    data
    text
    <p>This is homework so I'm just looking for some tips or at least if someone can point out where I'm going wrong, which is in quite a few places obviously. I'm trying to simulate a checkin line at the airport. Now the main doesn't create the threads but the methods do and they function similarly to producers and consumers. My issue seems to be finding the right place to signaling the other method to continue or maybe I've misunderstood.</p> <pre><code>void *producer(void *arg) { int i = 0; // Acquire the mutex pthread_mutex_lock(&amp;items_lock); // Check items; sleep if buffer is full if (items == MAX) { printf("Producer goes to sleep\n"); pthread_cond_wait(&amp;cp, &amp;items_lock); printf("Producer wakes up\n"); // The lock is automatically reacquired // Only one thread is waken up by one signal call } clock_t start=clock(); sleep(checkInTime); buf[pi] = i++; items++; pi = (pi + 1) % MAX; printf("Traveler %d has entered the check-in line at time %d; he/she is at position %d.\n", tId, checkInTime,pi); // Wake up a potentally sleeping consumer pthread_cond_signal(&amp;cc); printf("Traveler %d is now checked in at %f\n", tId, ((double)clock() - start) / CLOCKS_PER_SEC); pthread_mutex_unlock(&amp;items_lock); //sleep(1); } int Traveler(int id, int numBags) { tId=id; bags=numBags; int ret; pthread_t ptid; //pthread_cond_init(&amp;cc, NULL); //pthread_cond_init(&amp;cp, NULL); // create producer thread ret = pthread_create(&amp;ptid, NULL, producer, NULL); if (ret != 0) perror("Producer thread could not be created\n"); pthread_join(ptid, NULL); return 0; } //------------------------------- char* curName; void *checkIn(void* args) { int i; int count=0; printf("Staff %s's service has begun!\n",curName); while(count!=20) { // Acquire the lock pthread_mutex_lock(&amp;items_lock); // Check items; sleep if buffer is empty if (items == 0) { printf("No travelers\n"); pthread_cond_wait(&amp;cc, &amp;items_lock); printf("A traveler approaches.\n"); } if(count==5) { printf("Staff %s is taking a break.\n",curName); sleep(5); printf("Staff %s break is over.\n",curName); } sleep(1); i = buf[ci]; items--; ci = (ci + 1) % MAX; int bags; printf("Consumed at #%d (total %d)\n", ci, items); // signal a potentially sleeping producer pthread_cond_signal(&amp;cp); pthread_mutex_unlock(&amp;items_lock); count++; } printf("Staff %s service is complete!\n",curName); } int AirlineStaff(char *name) { curName=name; pthread_t ctid; int ret; // Create consumer thread ret = pthread_create(&amp;ctid, NULL, checkIn, NULL); //int bags; if(ret != 0) perror("Consumer thread could not be created\n"); pthread_join(ctid, NULL); return 0; } </code></pre> <p>I have a basic producer and consumer running so I understand the concept behind it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload