gpt4 book ai didi

c++ - 使用 C++ 在 Linux 中停止并重新开始运行进程

转载 作者:太空狗 更新时间:2023-10-29 23:34:51 25 4
gpt4 key购买 nike

我有两个进程和一个共享内存区,我的工作流程是这样的。进程 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;
}

上面的程序其实是两个程序,一个消费者和一个生产者,取决于你如何编译它。

您通过确保 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,比如信号量。至少你应该做每个循环内都有一个小 sleep 。另外,我认为您并不真正需要共享内存来解决这个问题,使用消息传递技术应该是可行的。

这是一个并行显示的示例 session :

    # ./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 存在(它用于建立共享内存 key (ftok 函数调用)))

关于c++ - 使用 C++ 在 Linux 中停止并重新开始运行进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/422241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com