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(<id, 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);
}
}