- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有两个进程和一个共享内存区,我的工作流程是这样的。进程 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/
我是 Linux 的新手,并且继承了保持我们的单一 Linux 服务器运行的职责。这是我们的SVN服务器,所以比较重要。 原来在我之前维护它的人有一个 cron 任务,当有太多 svnserve 进程
Node 虽然自身存在多个线程,但是运行在 v8 上的 JavaScript 是单线程的。Node 的 child_process 模块用于创建子进程,我们可以通过子进程充分利用 CPU。范例:
Jenkins 有这么多进程处于事件状态是否正常? 我检查了我的设置,我只配置了 2 个“执行者”... htop http://d.pr/i/RZzG+ 最佳答案 您不仅要限制 Master 中的执
我正在尝试在 scala 中运行这样的 bash 命令: cat "example file.txt" | grep abc Scala 有一个特殊的流程管道语法,所以这是我的第一个方法: val f
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我需要一些帮助来理解并发编程的基础知识。事实上,我读得越多,就越感到困惑。因此,我理解进程是顺序执行的程序的一个实例,并且它可以由一个或多个线程组成。在单核CPU中,一次只能执行一个线程,而在多核CP
我的问题是在上一次集成测试后服务器进程没有关闭。 在integration.rs中,我有: lazy_static! { static ref SERVER: Arc> = {
我正在使用 Scala scala.sys.process图书馆。 我知道我可以用 ! 捕获退出代码和输出 !!但是如果我想同时捕获两者呢? 我看过这个答案 https://stackoverflow
我正在开发一个C++类(MyClass.cpp),将其编译为动态共享库(MyClass.so)。 同一台Linux计算机上运行的两个不同应用程序将使用此共享库。 它们是两个不同的应用程序。它不是多线程
我在我的 C 程序中使用 recvfrom() 从多个客户端接收 UDP 数据包,这些客户端可以使用自定义用户名登录。一旦他们登录,我希望他们的用户名与唯一的客户端进程配对,这样服务器就可以通过数据包
如何更改程序,以便函数 function_delayed_1 和 function_delayed_2 仅同时执行一次: int main(int argc, char *argv[]) {
考虑这两个程序: //in #define MAX 50 int main(int argc, char* argv[]) { int *count; int fd=shm
请告诉我如何一次打开三个终端,这样我的项目就可以轻松执行,而不必打开三个终端三次然后运行三个exe文件。请问我们如何通过脚本来做到这一点,即打开三个终端并执行三个 exe 文件。 最佳答案 在后台运行
我编写了一个监控服务来跟踪一组进程,并在服务行为异常、内存使用率高、超出 CPU 运行时间等时发出通知。 这在我的本地计算机上运行良好,但我需要它指向远程机器并获取这些机器上的进程信息。 我的方法,在
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 8年前关闭。 Improve this qu
我有一个允许用户上传文件的应用程序。上传完成后,必须在服务器上完成许多处理步骤(解压、存储、验证等...),因此稍后会在一切完成后通过电子邮件通知用户。 我见过很多示例,其中 System.Compo
这个问题对很多人来说可能听起来很愚蠢,但我想对这个话题有一个清晰的理解。例如:当我们在 linux(ubuntu, x86) 上构建一个 C 程序时,它会在成功编译和链接过程后生成 a.out。 a.
ps -eaf | grep java 命令在这里不是识别进程是否是 java 进程的解决方案,因为执行此命令后我的许多 java 进程未在输出中列出。 最佳答案 简答(希望有人写一个更全面的): 获
我有几个与内核态和用户态的 Windows 进程相关的问题。 如果我有一个 hello world 应用程序和一个暴露新系统调用 foo() 的 hello world 驱动程序,我很好奇在内核模式下
我找不到很多关于 Windows 中不受信任的完整性级别的信息,对此有一些疑问: 是否有不受信任的完整性级别进程可以创建命名对象的地方? (互斥锁、事件等) 不受信任的完整性级别进程是否应该能够打开一
我是一名优秀的程序员,十分优秀!