Larry, Moe, Curly 互斥
Larry, Moe, Curly Mutual Exclusion
问题陈述:
"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."
我已经根据收到的一些伪代码输入了一个程序,但出现错误:
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);
}
}
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;
那是不是初始化信号量的方式。它们是复杂的东西,而不仅仅是可以分配给的计数器。这就是您正在使用但不正确的函数 sem_init
的原因。 Read up on it in a reference of your choice.
IIRC sem_t
是类似于
的类型定义
typedef struct whatever * sem_t;
因此,上面的每一行都从一个整数初始化了一个指针。
此外,您的函数应该 return 您声明它们的内容 return。对于用于线程的函数尤其如此:它们(需要)声明它们 return 一个空指针,所以(如果你对 return 没有任何意义)只需使用
return NULL;
在这些到达的尽头。
此外:
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
您希望对不同的 thread_t
进行这些调用,而不是对同一个 (btid
)。
问题陈述:
"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."
我已经根据收到的一些伪代码输入了一个程序,但出现错误:
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);
}
}
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;
那是不是初始化信号量的方式。它们是复杂的东西,而不仅仅是可以分配给的计数器。这就是您正在使用但不正确的函数 sem_init
的原因。 Read up on it in a reference of your choice.
IIRC sem_t
是类似于
typedef struct whatever * sem_t;
因此,上面的每一行都从一个整数初始化了一个指针。
此外,您的函数应该 return 您声明它们的内容 return。对于用于线程的函数尤其如此:它们(需要)声明它们 return 一个空指针,所以(如果你对 return 没有任何意义)只需使用
return NULL;
在这些到达的尽头。
此外:
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
您希望对不同的 thread_t
进行这些调用,而不是对同一个 (btid
)。