以下是如何完成的概念验证:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <assert.h>
typedef void (*sighandler_t)(int);
#define SHM_SIZE 8 /* size of shared memory: enough for two 32 bit integers */
volatile int cancontinue = 0;
void halt(char *err) { perror(err); exit(1); }
void handler(int signum) { assert(signum == SIGUSR1); cancontinue = 1; }
int main(void)
{
key_t key;
int id;
int *data;
pid_t otherpid;
printf("Hi, I am the %s process and my pid is %d\n",
#ifdef PRODUCER_MODE
"writer"
#else
"reader"
#endif
, getpid());
printf("Please give me the pid of the other process: ");
scanf("%d", &otherpid);
// get a pointer to the shared memory
if ((key = ftok("test_concur.c", 'R')) == -1) halt("ftok");
if ((id = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) halt("shmget");
if ((data = shmat(id, (void *)0, 0)) == (int *)(-1)) halt("shmat");
sighandler_t oldhandler = signal(SIGUSR1, handler);
while (1) {
#ifdef PRODUCER_MODE
printf("Enter two integers: ");
scanf("%d %d", data, data + 1);
printf("Sending signal to consumer process\n");
kill(otherpid, SIGUSR1);
printf("Waiting for consumer to allow me to continue\n");
while (!cancontinue);
cancontinue = 0;
if (*data + *(data + 1) == 0) { printf("Sum was 0, exiting...\n"); break; }
#else
printf("Waiting for producer to signal me to do my work\n");
while (!cancontinue);
cancontinue = 0;
printf("Received signal\n");
printf("Pretending to do a long calculation\n");
sleep(1);
int sum = *data + *(data + 1);
printf("The sum of the ints in the shared memory is %d\n", sum);
printf("Signaling producer I'm done\n");
kill(otherpid, SIGUSR1);
if (sum == 0) break;
#endif
}
signal(SIGUSR1, oldhandler);
/* detach from the segment: */
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
// don't forget to remove the shared segment from the command line with
// #sudo ipcs
// ... and look for the key of the shared memory segment
// #ipcrm -m <key>
return 0;
}
上面的程序其实是两个程序,一个消费者和一个生产者,
取决于你如何编译它。
您通过确保 PRODUCER_MODE 宏来编译生产者
已定义:
# gcc -Wall -DPRODUCER_MODE -o 生产者 test_concur.c
consumer 编译时没有定义 PRODUCER_MODE 宏:
# gcc -Wall -o 消费者 test_concur.c
消费者和生产者共享一些全局内存(数据指向的8个字节);生产者的角色是从标准输入中读取两个 32 位整数并将它们写入共享
记忆。消费者从共享内存中读取整数,然后
计算它们的总和。
将数据写入共享内存后,生产者向
消费者(通过 SIGUSR1)它可以开始计算。之后
计算完成后,消费者向生产者发出信号(通过 SIGUSR1
再次)它可能会继续。
当总和为 0 时,两个进程都停止。
目前,每个程序首先输出它的 pid 并从
stdin 另一个程序的 pid。这应该可能 :D 替换为
更智能的东西,具体取决于您在做什么。
此外,在实践中,“while (!cancontinue);”-like 循环应该是
被其他东西取代:D,比如信号量。至少你应该做
每个循环内都有一个小睡眠。另外,我认为您并不真的需要共享内存来解决这个问题,使用消息传递技术应该是可行的。
这是一个示例会话,并行显示:
# ./producer # ./consumer
嗨,我是编写器进程,我的 pid 是 11357 嗨,我是读取器进程,我的 pid 是 11358
请给我其他进程的pid:11358 请给我其他进程的pid:11357
输入两个整数:2 等待生产者通知我做我的工作
3
向消费者进程发送信号接收到的信号
等待消费者允许我继续 假装做一个很长的计算
...有些时候过去了...
共享内存中的整数之和为 5
信号制作人 我完成了
输入两个整数:0 等待生产者通知我做我的工作
0
向消费者进程发送信号接收到的信号
等待消费者允许我继续 假装做一个很长的计算
...有些时候过去了...
共享内存中的整数之和为0
信号制作人 我完成了
总和为 0,正在退出...
我希望这会有所帮助。 (运行程序时,确保文件test_concur.c存在(用于建立共享内存密钥(ftok函数调用))