立刻打电话给每个孩子的过程杀人?

时间:2015-04-21 07:09:30

标签: 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;
}

3 个答案:

答案 0 :(得分:1)

代码中的一些问题:

1)正如@Peter Schneider指出的那样,

  

父进程等待子进程退出,以便它不会找到会杀死子进程的代码

首先,你必须摆脱:

if(pid != 0){
    wait(NULL);
} 

2)杀死孩子的for循环只能由父进程执行,因此if子句包含for

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

3)子进程必须等待做某事直到父进程杀死它们,所以将以下else子句附加到上面的if

else{
    while(1){
        printf("I am a child process %d. Will sleep for 2 senconds\n",getpid());
        sleep(2);
    }
}

4)以下代码毫无意义,因为当孩子被杀时,他们就会停止工作。

if(pidarr[(i-1)] != 0){
    printf("Hello I am a child process %d, I will terminate now.\n",getpid());
}

如果您希望孩子在kill()发出信号时做某事,则必须使用signals

答案 1 :(得分:1)

以下代码显示了如何处理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

答案 2 :(得分:0)

我不确定逻辑的其他部分(例如fork循环中的if子句),但是

if(pid != 0){
   wait(NULL);
}

看起来很可疑,因为父进程等待一个孩子退出,所以它根本没有找到会杀死孩子的代码(除非他们自己退出,但是杀戮似乎毫无意义)