子进程如何杀死其他子进程然后终止?
How a child process kill other child process and then terminate?
这是代码,父进程在管道中写入一个字符串输入,子进程从管道中读取它。如果子进程从管道中读取单词 "end",那么我想终止所有进程,然后自行终止,如果读取单词 "finish",我想向父亲发出信号以终止所有进程然后退出。我 运行 代码和我有分段错误。为什么错了?
#define _POSIX_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
void measure_time(int sig)
{
printf("child [%d] received signal %d\n", getpid(), sig);
}
int main(int argc, char *argv[])
{
int n_task = 4;
pid_t pid;
pid_t pid_array[n_task];
int fd[2];
for (int i = 0; i < n_task; i++)
{
pid = fork();
if (pipe(fd) == -1)
{
perror(" pipe ");
exit(1);
}
if (pid < 0)
{
perror("fork");
exit(1);
}
if (pid == 0) //child
{
char *buf;
close(fd[1]);
read(fd[0], buf, 10);
printf("I read: %s", buf);
if (strcmp(buf, "end") == 0)
{
for (int i = 0; i < n_task; i++)
kill(pid_array[i], SIGUSR1);
}else if(strcmp(buf,"finish") == 0){
/*Here i want father to kill all children and then exit.*/
}
exit(0);
}
close(fd[0]);
char *buf;
printf("Give the input string: \n");
scanf("%s", buf);
write(fd[1], buf, strlen(buf));
close(fd[1]);
pid_array[i] = pid;
}
sleep(1);
for (int i = 0; i < n_task; i++)
wait(NULL);
return (0);
}
您正在声明一个指针 buf
,但没有对其进行初始化。对 read()
和 scanf()
的后续调用将失败,因为指针无效。
您需要确保 buf
已初始化并指向有效内存。修复代码的一种简单方法是:
char buf[10];
read(fd[0], buf, 10);
如果您使用 -Wall
启用编译器警告,那么编译器将警告您有关已初始化变量的信息。
注意潜在的缓冲区溢出:如果声明 char buf[10]
,请确保永远不会向其中写入超过 10 个字节。此外,检查 read()
、write()
、scanf()
等函数的 return 值以确保没有遇到错误,否则缓冲区或输出文件的内容可能不会像预期。
除了@G指出的未初始化buf
的问题。 Sliepen,需要在 fork()
之前调用 pipe()
,因为在分叉子进程时文件描述符保持打开状态。这也是管道的工作原理。
您可以尝试更改您的代码片段,将 pipe()
放在 fork()
之前。
...
if (pipe(fd) == -1)
{
perror(" pipe ");
exit(1);
}
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
...
请阅读 pipe(2) 的手册页,其中提供了示例。
所以 post fork() and pipes() in c 也对此进行了解释。
终止进程更新
此子进程不知道其兄弟进程的存在,但其父进程知道。如果没有明确要求,您可以让父进程这样做,即 "end" 所有子进程。
顺便说一句,与其发送信号 SIGUSR1,不如发送 SIGTERM 信号。尽管 SIGUSSR1 会导致目标进程默认终止(参见 signal(7))。
至"finish",即杀死(或终止)所有子进程以及父进程,您可以简单地杀死父进程。它的所有后代也都被杀了。或者,您可以向同一个进程组发送信号。参见 kill(2)。
这是代码,父进程在管道中写入一个字符串输入,子进程从管道中读取它。如果子进程从管道中读取单词 "end",那么我想终止所有进程,然后自行终止,如果读取单词 "finish",我想向父亲发出信号以终止所有进程然后退出。我 运行 代码和我有分段错误。为什么错了?
#define _POSIX_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
void measure_time(int sig)
{
printf("child [%d] received signal %d\n", getpid(), sig);
}
int main(int argc, char *argv[])
{
int n_task = 4;
pid_t pid;
pid_t pid_array[n_task];
int fd[2];
for (int i = 0; i < n_task; i++)
{
pid = fork();
if (pipe(fd) == -1)
{
perror(" pipe ");
exit(1);
}
if (pid < 0)
{
perror("fork");
exit(1);
}
if (pid == 0) //child
{
char *buf;
close(fd[1]);
read(fd[0], buf, 10);
printf("I read: %s", buf);
if (strcmp(buf, "end") == 0)
{
for (int i = 0; i < n_task; i++)
kill(pid_array[i], SIGUSR1);
}else if(strcmp(buf,"finish") == 0){
/*Here i want father to kill all children and then exit.*/
}
exit(0);
}
close(fd[0]);
char *buf;
printf("Give the input string: \n");
scanf("%s", buf);
write(fd[1], buf, strlen(buf));
close(fd[1]);
pid_array[i] = pid;
}
sleep(1);
for (int i = 0; i < n_task; i++)
wait(NULL);
return (0);
}
您正在声明一个指针 buf
,但没有对其进行初始化。对 read()
和 scanf()
的后续调用将失败,因为指针无效。
您需要确保 buf
已初始化并指向有效内存。修复代码的一种简单方法是:
char buf[10];
read(fd[0], buf, 10);
如果您使用 -Wall
启用编译器警告,那么编译器将警告您有关已初始化变量的信息。
注意潜在的缓冲区溢出:如果声明 char buf[10]
,请确保永远不会向其中写入超过 10 个字节。此外,检查 read()
、write()
、scanf()
等函数的 return 值以确保没有遇到错误,否则缓冲区或输出文件的内容可能不会像预期。
除了@G指出的未初始化buf
的问题。 Sliepen,需要在 fork()
之前调用 pipe()
,因为在分叉子进程时文件描述符保持打开状态。这也是管道的工作原理。
您可以尝试更改您的代码片段,将 pipe()
放在 fork()
之前。
...
if (pipe(fd) == -1)
{
perror(" pipe ");
exit(1);
}
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
...
请阅读 pipe(2) 的手册页,其中提供了示例。
所以 post fork() and pipes() in c 也对此进行了解释。
终止进程更新
此子进程不知道其兄弟进程的存在,但其父进程知道。如果没有明确要求,您可以让父进程这样做,即 "end" 所有子进程。
顺便说一句,与其发送信号 SIGUSR1,不如发送 SIGTERM 信号。尽管 SIGUSSR1 会导致目标进程默认终止(参见 signal(7))。
至"finish",即杀死(或终止)所有子进程以及父进程,您可以简单地杀死父进程。它的所有后代也都被杀了。或者,您可以向同一个进程组发送信号。参见 kill(2)。