立即调用每个子进程来杀死?

标签 c process fork kill

我必须编写一个程序,该程序将生成随机数量的进程,然后在它们全部创建后将它们一个接一个地杀死。

我的问题是子进程创建后无法停止。

此外,我尝试从子进程调用终止输出到 stdout,但并不真正知道如何解决它(因为 pid = 0 适用于每个子进程)。

#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{
    //int status;
    srand(time(NULL));
    int amount = (rand())%9+1;
    pid_t fatherid = getpid();
    printf("Hello I am a parent process, my PID is %d and I will now create %d children.\n",fatherid,amount);
    pid_t pid = 1;
    pid_t pidarr[amount];
    for(int i = 0;i<amount;i++){
        if(pid != 0){
            pid = fork();
            pidarr[i] = pid;
            if(pid ==0){
                printf("Hello I am a child process, my PID is %d and my parent has the PID %d.\n",getpid(),fatherid);
            }
            sleep(1);
        }
    }
    if(pid != 0){
        wait(NULL);
    }
    for(int i = (amount-1);i >= 0;i--){
        if(pidarr[(i-1)] != 0){
            printf("Hello I am a child process %d, I will terminate now.\n",getpid());
        }
        sleep(rand()%4);
        if(pid != 0){
            kill(pidarr[i],SIGKILL);
            printf("Child Process %d was terminated.\n",pidarr[i]);
        }
    }
    if(pid != 0){
        printf("All child processes were terminated. I will terminate myself now.\n");
    }
    return EXIT_SUCCESS;
}

最佳答案

下面的代码展示了如何处理fork和子进程。

代码编译干净,经过测试并且可以工作

#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>

int main( void )
{
    //int status;
    srand(time(NULL));

    int amount = (rand())%9+1;

    pid_t fatherid = getpid();

    printf("Hello I am a parent process, my PID is %d and I will now create %d children.\n",fatherid,amount);

    pid_t pid;
    pid_t pidarr[amount];

    for(int i = 0;i<amount;i++)
    {

            pid = fork();
            if( -1 == pid )
            { //then, fork() error
                perror( "fork() failed" );
                exit(1);
            }

            // implied else, fork() successful

            //pidarr[i] = pid;
            if(!pid )
            { // then child process
                printf("Hello I am a child process, my PID is %d and my parent has the PID %d.\n",getpid(),fatherid);
                exit(0);  // exit child process
            }

            // implied else, parent process

            pidarr[i] = pid;           
            sleep(1);
    } // end for

    for(int i = (amount-1); i >= 0; i--)
    {
            kill(pidarr[i],SIGKILL);
            printf("Child Process %d was terminated.\n",pidarr[i]);
    }


    printf("All child processes were terminated. I will terminate myself now.\n");

    return(0);
} // end function: main

关于立即调用每个子进程来杀死?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765266/

相关文章:

C 指针 : Assignment from incompatible pointer type

c - 在等式中使用 bool 值 (C)

java - 如何从 Java 中查找并杀死正在运行的 Win-Processes?

c++ - 减慢 Windows 进程?

c - 尝试学习如何在受控循环中用 C 实现信号量

c - 管道/ fork 有问题。说明书看不懂一半(man)

c - 向家长发送信号不起作用

c - 函数调用期间指针到指针不起作用?

C - 在二叉搜索树中打印字符串的有序 3-Anagrams

process - NSIS 检测正在运行的后台进程