c-试图制作一个处理SIGUSR1的程序,Child应该执行另一个可执行文件



正如标题所说,我正在尝试制作一个处理SIGUSR1的程序,该程序的Child需要执行另一个可执行文件。这可以是硬编码的,也可以来自命令行,我现在选择了硬编码。

这是用vim的C代码编写的(一定喜欢Linux类)。

我的父函数似乎至少在printf()末尾运行。

我有两个.c文件,一个叫parent.c,另一个叫child.c

我的父程序的代码如下:

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>

int signalHandle(int signo)
{
if(signo == SIGUSR1)
{
printf("Signal recived from child.n");
}
else
printf("Unexpected signal received");
return;
}

int main()
{
pid_t child = fork();
if (child == 0)
{
execvp("child", signalHandle(child));
}
sleep(10);
printf("Now finishing the parent process pid of %dn", getpid());

}

Child计划如下:

#include <stdio.h>                                                                                         
#include <stdlib.h>                                                                                        

int main()                                                                                                 
{                                                                                                          
printf("This is the child, with pid %d and parent pid %dnHello World", getpid(), getppid());      
sleep(5);                                                                                          
printf("Now exiting the child program...n");                                                      
return 0;                                                                                          
}                                                                                                          

我有一个makefile,看起来像:

all: parent child                        
parent: parent.c                         
gcc -o parent parent.c           
child: child.c                           
gcc -o child child.c             

当我运行Makefile时,我会收到一些警告:

gcc -o parent parent.c
parent.c: In function ‘signalHandle’:
parent.c:14:2: warning: ‘return’ with no value, in function returning non-void
return;
^
parent.c: In function ‘main’:
parent.c:21:16: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
pid_t child = fork();
^
parent.c:24:3: warning: implicit declaration of function ‘execvp’ [-Wimplicit-function-declaration]
execvp("child", signalHandle(child));
^
parent.c:27:2: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
sleep(10);
^
parent.c:28:57: warning: implicit declaration of function ‘getpid’ [-Wimplicit-function-declaration]
printf("Now finishing the parent process pid of %dn", getpid());
^
gcc -o child child.c
child.c: In function ‘main’:
child.c:8:74: warning: implicit declaration of function ‘getpid’ [-Wimplicit-function-declaration]
printf("This is the child, with pid %d and parent pid %dnHello World", getpid(), getppid());
    ^
child.c:8:84: warning: implicit declaration of function ‘getppid’ [-Wimplicit-function-declaration]
printf("This is the child, with pid %d and parent pid %dnHello World", getpid(), getppid());
              ^
child.c:9:2: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
sleep(5);
^

它确实运行,但我的程序的当前输出是:

zerkereod@zerkereod-VirtualBox:~/Class/admin/asullivan/lab7$ ./parent
Now finishing the parent process pid of 9763
zerkereod@zerkereod-VirtualBox:~/Class/admin/asullivan/lab7$ Unexpected signal receivedNow finishing the parent process pid of 9764

我查阅了大量的代码,但我看到的每个使用exec的人都使用argv或argv[],没有人解释为什么。我不知道信号处理程序是用来做什么的。扩展表达式和信号让我为这门课伤透了脑筋。不管我读了多少书,我都没能弄清楚。

parent.c:14:2: warning:这是因为int signalHandle(int signo);应该返回int,并且当您键入return;时,您试图不返回任何内容。您应该更改void signalHandle(int signo);的原型,因为此函数不返回值。

CCD_ 5是来自CCD_ 6的函数。由于您没有包含此lib,编译器无法识别它。"execvp"、"sleep"、"getpid"one_answers"getppid"的Idem。你应该添加

#include <sys/types.h>
#include <unistd.h>

到您的代码,这样您就可以使用这些函数。

要声明信号处理程序,必须包含signal.h库。要将函数声明为信号处理程序,必须为此使用signal函数。处理程序应该只接收一个参数(信号)并返回void。

您的父进程捕获SIGUSR1信号的正确方法是:

void signalHandle(int signo)
{
printf("Signal SIGUSR1 received from child.n");
}

在parent.c main中,您必须调用:

signal(SIGUSR1, signalHandle);

要使子进程向父进程发送信号,您必须使用"kill"功能:

kill(getppid(), SIGUSR1);

你把错误的论点传递给了execvp。第一个参数应该是.exe文件的名称。第二个参数是字符串数组,其中第一个元素也应该是文件名,最后一个必须为NULL:

char *argv[2];
argv[0] = "child";
argv[1] = NULL;
execvp(argv[0], argv);

您应该在以下链接中了解更多信息:http://man7.org/linux/man-pages/man2/signal.2.htmlhttp://man7.org/linux/man-pages/man2/kill.2.html

相关内容

最新更新