-1
我该如何重写这段代码来使用进程而不是线程?我尝试用C语言学习过程编程。我不知道我该怎么做。这段代码使用线程。关于这个algoritmus更多信息是这里第153页 - 158:线程处理
http://www.greenteapress.com/semaphores/downey08semaphores.pdf
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#define MAX_PASSENGERS 500
sem_t loading, loaded, unloading, unloaded;
int n, c;
void *passenger (void *tid) {
int i = *((int*) tid);
while (1) {
sem_wait(&loading);
printf("pass(i=%d).board()\n", i);
sem_post(&loaded);
sem_wait(&unloading);
printf("pass(i=%d).unboard()\n", i);
sem_post(&unloaded);
}
}
void *roller_coaster() {
while (1) {
printf("car.load(c=%d)\n", c);
for (int i = 0; i < c; i++) {
sem_post(&loading);
}
// wait for c passengers to load
for (int i = 0; i < c; i++) {
sem_wait(&loaded);
}
printf("car.run()\n");
sleep(1);
printf("car.unload(c=%d)\n", c);
for (int i = 0; i < c; i++) {
sem_post(&unloading);
}
// wait for c passengers to unload
for (int i = 0; i < c; i++) {
sem_wait(&unloaded);
}
}
}
int main() {
printf("Number of passengers(n, n <= 500): ");
scanf("%d", &n);
printf("Number of passengers per cart(c, c < n): ");
scanf("%d", &c);
sem_init(&loading, 0, 0);
sem_init(&unloading, 0, 0);
sem_init(&loaded, 0, 0);
sem_init(&unloaded, 0, 0);
pthread_t car;
pthread_t tid[MAX_PASSENGERS];
int my_ids[MAX_PASSENGERS];
pthread_create(&car, NULL, roller_coaster, NULL);
for (int i = 0; i < n; i++) {
my_ids[i] = i;
pthread_create(&tid[i], NULL, passenger, &my_ids[i]);
}
pthread_join(car, NULL);
return 0;
}
感谢所有帮助球员
您可以使用任何形式的[IPC](https://en.wikipedia.org/wiki/Inter-process_communication)来实现消息传递,这是一个好的开始。您可以使用套接字或命名/匿名管道等来完成此操作。阅读链接以开始使用(; – Linuxxon
您可以重构代码并使用[fork](http://linux.die.net/man/2/fork)a子进程以及发送/接收数据之间的管道。请记住,在孩子们工作的时候,你需要保持父进程的某种循环。 – alexJones