- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
用 C 语言创建一个程序,该程序创建两个进程并通过管道连接它们。
第一个后代将其标准输出重定向到管道中,并将(空格分隔的)随机数对写入其中(函数 rand)。将数字的输出延迟 1 秒。
第二个后代将管道输出重定向到它的标准输入,将它的标准输出重定向到当前目录中名为 out.txt 的文件。
父进程等待 5 秒,然后向第一个进程(数字生成器)发送 SIGUSR1。这应该正确终止两个进程。它等待子进程终止(等待函数)并自行终止。
我确实需要帮助:第一个后代必须处理 SIGUSR1 信号(sigaction 函数),并且在接收到此类信号的情况下,它会向其 stderr 打印字符串“TERMINATED”并终止。
FILE *file;
file = fopen(NAZEV, "a+");
int pipefd[2];
pipe(pipefd);
pid_t pid1;
int retcode;
pid1=fork();
if(pid1 == 0) // child 1
{
close(roura[0]);
printf("child1...\n");
dup2(roura[1], STDOUT_FILENO);
int i = 0;
while(i < 6)
{
i++;
int a = rand();
int b = rand();
sleep(1);
printf("%d %d\n", a, b);
}
close(roura[1]);
exit(45);
}
else if (pid1 < 0)
{
printf("Fork selhal\n");
exit(2);
}
else
{
pid_t pid2;
pid2 = fork();
if (pid2 == 0) //child 2
{
close(roura[1]);
dup2(roura[0], STDIN_FILENO);
printf("child2...\n");
int i = 0;
while(i < 5)
{
i++;
int c;
int d;
scanf("%d %d", &c, &d);
printf("%d %d\n", c, d);
fprintf(file,"%d %d\n", c, d);
}
printf("child2 end\n");
exit(0);
}
else if (pid2 < 0)
{
printf("Fork error\n");
exit(2);
}else
{
sleep(5);
kill(pid1, SIGUSR1);
wait(&pid1); //wait for child 1
wait(&pid2); //wait for child 2
printf("parent end\n");
exit(0);
}
}
exit(0);
}
最佳答案
向 sigusr1 添加信号处理程序,打印到 stderr 并退出。试试这个,适合在cygwin中编译:
#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
# define STDOUT_FILENO 1
#endif
void sig_handler(){
fprintf(stderr,"TERMINATED");
exit(0);
}
void main(int argc, char ** argv){
FILE *file;
file = fopen("NAZEV", "a+");
int pipefd[2];
int roura[2] ;
pipe(pipefd);
pid_t pid1;
int retcode;
pid1=fork();
if(pid1 == 0) // child 1
{
close(roura[0]);
printf("child1...\n");
dup2(roura[1], STDOUT_FILENO);
if (signal(SIGUSR1, sig_handler) == SIG_ERR){
printf("\ncan't catch SIGUSR1\n");
exit(13);
}
int i = 0;
while(i < 6)
{
i++;
int a = rand();
int b = rand();
sleep(1);
printf("%d %d\n", a, b);
}
close(roura[1]);
exit(45);
}
else if (pid1 < 0)
{
printf("Fork selhal\n");
exit(2);
}
else
{
pid_t pid2;
pid2 = fork();
if (pid2 == 0) //child 2
{
close(roura[1]);
dup2(roura[0], STDIN_FILENO);
printf("child2...\n");
int i = 0;
while(i < 5)
{
i++;
int c;
int d;
scanf("%d %d", &c, &d);
printf("%d %d\n", c, d);
fprintf(file,"%d %d\n", c, d);
}
printf("child2 end\n");
exit(0);
}
else if (pid2 < 0)
{
printf("Fork error\n");
exit(2);
}else
{
sleep(5);
kill(pid1, SIGUSR1);
wait(&pid1); //wait for child 1
wait(&pid2); //wait for child 2
printf("parent end\n");
exit(0);
}
}
exit(0);
}
关于c - SIGUSR1 - 杀死 C 中的 sigaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27113347/
我在一些代码中看到以下用法: struct sigaction term_handler; // global variable without initialization ... sigacti
我正在做简单的 sigaction 示例来练习 C,但是当我尝试编译代码时,它声称 struct sigaction 不存在 [1]。 当我检查我生成的一些旧代码时,我发现我在文件的最顶部添加了一些
如果我们使用 sigaction 来定义一个信号处理程序,那为什么我们不需要重置处理程序呢?如果我们使用 signal(sig_no,handler_func)那么我们必须重置它。为什么是这样? #i
我正在学习信号并编写了一个简单的程序来处理它们。 所以我输入一个数字,然后使用 fork 创建一个进程。父进程应该将数字作为信号发送给子进程,然后 child_signal 处理程序应该将数字的平方作
我在 C++ 中对此类使用 sigaction 时遇到问题: class SerialHandler{ private: ... /* Action Handling
我有以下 sigaction 处理程序代码 void signal_term_handler(int sig) { int rc = async_lockf(pid_file, F_UNLCK
我正在使用 sigaction 作为信号,我正在为此使用一个简单的结构。我实际上是从手册页中获取的。有人可以向我解释结构中的第二行是做什么的吗?还有一个错误: error: expected decl
有没有办法用sigaction结构和函数只捕获一次信号?更具体地说,我想简单地重置为默认特定信号 (SIGINT)。是否有可能在处理程序中实现这一点? 编辑 所以,这样的事情是正确的: void si
我正在将程序的信号处理从 signal() 转换为 sigaction()。 根据UNIX spec struct sigaction 应该至少有 4 个成员; sa_handler、sa_mask、
我很难理解 sigaction() 的方式有效。 在 , sigaction 定义为 int sigaction(int sig, const struct sigaction *act, struc
我正在查看 sigaction 的手册页,最后我看到了以下行。 sigaction(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
在我下面的代码中,如果我将 old_act 声明为全局变量,那么程序可以正常工作。如果在 main 中声明: 如果使用 SA_RESTART,它工作正常 如果不使用 SA_RESTART,则会导致段错
我必须向进程发送两个信号,SIGUSR1 和 SIGUSR2,以便修改程序中的特定 bool 变量 (SIGUSR1 将其设置为 true,SIGUSR2 将其设置为 false)。所以我写了一个 s
我正要向我们这里的应用程序添加一个额外的信号处理程序,我注意到作者使用了 sigaction() 来设置其他信号处理程序。我打算使用 signal()。按照惯例,我应该使用 sigaction(),但
编译以下代码, struct sigaction sa; memset (&sa, 0, sizeof (sa)); sa.sa_handler = &handler; sigaction (SIGR
我有一个程序,它为 SIGSEGV 安装信号处理程序。在信号处理程序中(我 try catch 崩溃)我重新启动我的应用程序。 但是当我的应用程序复活时,它不再处理 SIGSEGV。 这是一个例子:
我注意到 sigaction 被定义为结构和函数(http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html):
每次收到 SIGINT 时,我都使用 sigaction() 执行操作。我见过的所有教程都使用这个原型(prototype)作为信号处理程序 void sig_handler(int sig); 有没
这个问题在这里已经有了答案: What is it with printf() sending output to buffer? (3 个答案) 关闭 3 年前。 我是 Unix 中信号的新手,这
我正在尝试以下信号处理程序,引用在线教程,但它似乎不起作用,我的代码有什么问题: #include #include #include #include #include typedef void (
我是一名优秀的程序员,十分优秀!