我想知道如何杀死进程

时间:2017-11-30 12:56:16

标签: c linux fork

我使用fork创建了child,我尝试每隔3秒杀死生成的孩子。我也试图使用"抚养或杀死"来杀死我的父母。 我不知道如何杀死父处理器。 当我运行我的代码,除了杀死父母,不像我的期望,这么多的孩子杀了出来。

代码:

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>    

int main()
{

   int ch[3];
   int i;
   for (i = 0; i < 3; i++) {
       ch[i] = fork();
       if (ch[i] == 0){
             printf("child[%d]=%d\n",i,getpid());
             exit(0); }        
   }

   for(i = 0; i<3; i++) {
       sleep(3);
       kill(ch[i],SIGKILL);
       printf("Killch[%d]=%d\n",i,ch[i]); 
   }

      /* KILL or raise() parent kill */

}

如何更正此代码?

1 个答案:

答案 0 :(得分:1)

root.linux.gtk.x86_64=filesToCopy不是收集儿童身份的正确解决方案,请在父级中使用sleep()

当父母处于睡眠状态时

wait() or waitpid()
代码中的

for(i = 0; i<3; i++) { sleep(3); kill(ch[i],SIGKILL); printf("Killch[%d]=%d\n",i,ch[i]); } 是不是在等待父母杀人?孩子被child声明自行杀死。

exit(0)需要使用(child)向父母和父母发送exit status孩子的collect状态wait() or waitpid()然后杀了?

如果你想观察父母是否在杀孩子,请在孩子身上使用延迟并观察。

&#34;我试图杀死生成的孩子&#34;一世 ?假设父母,这是我的代码

int a[3];
int temp[3]; //to set flag=1 , when child completes instruction and become zombie
//collect status in wait() in parent, so no need to further process in my_isr
void my_isr(int n) //if child has not completed instruction, i.e not removed by wait() in parent
{ //then remove it using my_isr
        printf("in isr..\n");
        static int i;
        for(;i<3;i++)
                if((temp[i]!=1) )//if first child "not turned into zombie and removed by parent" then kill it
                {
                        printf("child %d killed \n",i+1);
                        kill(a[i],SIGKILL);
                }
                else
                {
                        printf("zombie child %d has been terminated normally \n",i+1);
                }
}
int main()
{
        if( (a[0]=fork()) == 0)
        {
                int r;
                srand(getpid());
                r=rand()%10+1;
                printf("child %d is going for sleep of %d sec\n",getpid(),r);
                sleep(r);
                exit(1);
        }
        else
        {
                if( (a[1]=fork()) == 0)
                {
                        int r;
                        srand(getpid());
                        r=rand()%10+1;
                        printf("child %d is going for sleep of  %d sec\n",getpid(),r);
                        sleep(r);
                        exit(2);
                }
                else
                {
                        if( (a[3]=fork()) == 0)
                        {
                                int r;
                                srand(getpid());
                                r=rand()%10+1;
                                printf("in child %d is going for sleep of  %d sec\n",getpid(),r);
                                sleep(r);
                                exit(3);
                        }
                        else
                        {
                                int s;
                                printf("in parent : %d \n",getpid());
                                signal(SIGALRM,my_isr);
                                //setting timer to tell child's that you need to completes within this duration
                                alarm(5);
                                while(wait(&s) != -1)//when there is no child left , wait returns  -1
                                {
                                        if( s>>8 == 1 )
                                                temp[0]=1; //set the flag when exit status is received
                                        else if( s>>8 == 2)
                                                temp[1]=1; //set the flag when child completed work before

                                        else if( s>>8 ==3)
                                                temp[2]=1; //set the flags when zombies are removed
                                }
                        }
                }
        }
        return 0;
}

我希望对你有帮助。