小编Zam*_*que的帖子

如何只终止子进程?

我想用他们的pid和打印一系列进程burst time.为此,我生成pid使用fork()然后pid使用它getpid().但是,由于fork创建了一个与父进程隔离运行的子进程,因此我没有得到预期的行为.程序应该做的是生成给定的进程number_of_process,然后在特定的结构元素内存储pid和随机burst time值.这是我的代码: -

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

struct process{
    int pid;
    int bt;
};

int main()
{   
    int no_of_process,i,new_process;
    printf("Enter the number of process\n");
    scanf("%d",&no_of_process);
    struct process p[no_of_process];
    for(i=0;i<no_of_process;i++){
        new_process=fork();
        p[i].pid = getpid();
        p[i].bt = rand()%10;
        //kill(getpid(),SIGKILL);
    }
    for(i=0;i<no_of_process;i++){
        printf("process %d and bt %d\n",p[i].pid,p[i].bt);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我试图杀死子进程,但这会停止整个程序.进程数的输出= 2

process 6373 and bt 3
process 6373 and bt 6
process …
Run Code Online (Sandbox Code Playgroud)

c fork

0
推荐指数
1
解决办法
524
查看次数

标签 统计

c ×1

fork ×1