Edu*_*rdo 2 c++ linux controls process
我有两个进程和一个共享内存区域,我的工作流程是这样的.进程A在共享内存中写入一些数据,之后它应该等待并向其他进程B发送信号以开始运行.进程B应该从共享内存中读取一些数据做一些东西写入结果,然后向进程A发送一个信号继续运行,此进程B应该等待.
任何人都可以提供一个示例或地方,我可以找到如何停止进程以及如何重新开始运行该进程?我在Linux和C++工作.
我已经有了信号量,但是我不喜欢它,它是一个进程停止从共享内存中一直读取一堆秒,直到它检测到它可以运行.这就是为什么我只是想在恰当的时刻发出信号
更新解决方案
我选择了stefan.ciobaca的答案作为最喜欢的,因为它是一个完整的解决方案,它有效,它有一个非常好的解释.但在所有其他答案中还有其他有趣的选择.
以下是如何完成的概念验证:
#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;
}
Run Code Online (Sandbox Code Playgroud)
上面的程序实际上是两个程序,一个消费者和一个生产者,取决于你如何编译它.
您通过确保定义PRODUCER_MODE宏来编译生成器:
# gcc -Wall -DPRODUCER_MODE -o producer test_concur.c
在不定义PRODUCER_MODE宏的情况下编译使用者:
# gcc -Wall -o consumer test_concur.c
消费者和生产者共享一些全局内存(数据指向8个字节); 生产者的角色是从stdin读取两个32位整数并将它们写入共享内存.消费者从共享内存中读取整数并计算它们的总和.
在将数据写入共享存储器之后,生产者向消费者发信号(通过SIGUSR1)它可以开始计算.在计算完成之后,消费者向生产者发信号(再次通过SIGUSR1)它可以继续.
当总和为0时,两个进程都会停止.
目前,每个程序首先输出其pid并从stdin读取其他程序的pid.这应该可能:D取而代之的是更聪明的东西,具体取决于你正在做什么.
此外,在实践中,"while(!cancontinue);" - 类似的循环应该被其他东西替换:D,就像信号量一样.至少你应该在每个循环中做一个小睡眠.另外,我认为你并不真正需要共享内存来解决这个问题,它应该可以使用消息传递技术.
这是一个示例会话,并行显示:
# ./producer # ./consumer
Hi, I am the writer process and my pid is 11357 Hi, I am the reader process and my pid is 11358
Please give me the pid of the other process: 11358 Please give me the pid of the other process: 11357
Enter two integers: 2 Waiting for producer to signal me to do my work
3
Sending signal to consumer process Received signal
Waiting for consumer to allow me to continue Pretending to do a long calculation
... some times passes ...
The sum of the ints in the shared memory is 5
Signaling producer I'm done
Enter two integers: 0 Waiting for producer to signal me to do my work
0
Sending signal to consumer process Received signal
Waiting for consumer to allow me to continue Pretending to do a long calculation
... some times passes ...
The sum of the ints in the shared memory is 0
Signaling producer I'm done
Sum was 0, exiting...
我希望这有帮助.(当你运行程序时,确保文件test_concur.c存在(它用于建立共享内存密钥(ftok函数调用)))