Search code examples
cpthreadssemaphoremutual-exclusion

Larry, Moe, Curly Mutual Exclusion


Problem Statement:

"Larry, Moe, and Curly are planting seeds. Larry digs the holes. Moe then places a seed in each hole. Curly then fills the hole up. There are several synchronization constraints:

  • Moe cannot plant a seed unless at least one empty hole exists, but Moe does not care how far Larry gets ahead of Moe.
  • Curly cannot fill a hole unless at least one hole exists in which Moe has planted a seed, but the hole has not yet been filled. Curly does not care how far Moe gets ahead of Curly.
  • Curly does care that Larry does not get more than MAX holes ahead of Curly. Thus, if there are MAX unfilled holes, Larry has to wait.
  • There is only one shovel with which both Larry and Curly need to dig and fill the holes, respectively.

Design, implement and test a solution for this IPC problem, which represent Larry, Curly, and Moe. Use semaphores as the synchronization mechanism."

I've typed up a program from some pseudocode that I was given, but I'm getting the error:

project2part3.c:13:13: warning: initialization makes pointer from integer without a cast [-Wint-conversion] 
 #define MAX 5
             ^
project2part3.c:22:18: note: in expansion of macro 'MAX'
 sem_t unfilled = MAX;
                   ^
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#define MAX 5

void *larry();
void *moe();
void *curly();

pthread_mutex_t shovel = PTHREAD_MUTEX_INITIALIZER;
sem_t empty;
sem_t seeded;
sem_t unfilled;

int main(){
    pthread_t ltid;
    pthread_t mtid;
    pthread_t ctid;
    //initializing the semaphores
    sem_init(&empty, 0, 0);
    sem_init(&seeded, 0, 0);
    sem_init(&unfilled, 0, 0);

    pthread_create(&ltid, NULL, larry, NULL);  //create the larry thread
    pthread_create(&mtid, NULL, moe, NULL);  //create the moe thread
    pthread_create(&ctid, NULL, curly, NULL);  //create the curly thread

    pthread_join(ltid,NULL);
    pthread_join(mtid,NULL);
    pthread_join(ctid,NULL);
}

void *larry(){
    while(1){
        sem_wait(unfilled);
        sem_wait(shovel);
        //Dig the hole
        printf("Digging");
        sem_post(shovel);
        sem_post(empty);
    }
}

void *moe(){
    while(1){
        sem_wait(empty);
        //Seed the hole
        printf("Seeding");
        sem_post(seeded);
    }
}

void *curly(){
    while(1){
        sem_wait(seeded);
        sem_wait(shovel);
        //Fill the hole
        printf("Filling");
        sem_post(shovel);
        sem_post(unfilled);
    }
}

Solution

  • sem_t shovel = 1; // Note: Is a lock in the updated question code
    sem_t empty = 0;
    sem_t seeded = 0;
    sem_t unfilled = MAX;
    

    That's not how you initialise semaphores. They're complex things, not just counters that can be assigned to. That's the reason there's the function sem_init, which you're using, but not correctly. Read up on it in a reference of your choice.

    IIRC sem_t is a typedef similar to

    typedef struct whatever * sem_t;
    

    Thus each of the above lines initialises a pointer from an integer.

    Furthermore, your functions should return what you declare them to return. This is especially true for the functions used for the threads: They (need to) declare that they return a void pointer, so (if you don't have anything meaningful to return) just use

    return NULL;
    

    at the end of reach of these.

    Moreover:

    pthread_create(&btid, NULL, larry, NULL);  //create the larry thread
    pthread_create(&btid, NULL, moe, NULL);  //create the moe thread
    pthread_create(&btid, NULL, curly, NULL);  //create the curly thread
    

    You want these calls to different thread_ts, not all to the same (btid).