简体   繁体   English

如何终止仅子进程?

[英]How to terminate only the child process?

I want print an array of processes with their pid and burst time . 我想打印一系列带有其pidburst time的进程。 For this, I generate the pid using fork() and then get it pid using getpid() . 为此,我使用fork()生成了pid ,然后使用getpid()获得了pid However, since fork creates a child process that runs in isolation from the parent process, I am not getting the expected behavior. 但是,由于fork创建了一个与父进程隔离运行的子进程,所以我没有得到预期的行为。 What the program should do is generate process for given number_of_process and then store pid and random burst time value inside the specific structure element. 程序应该做的是为给定的number_of_process生成进程,然后将pid和随机burst time值存储在特定的结构元素中。 Here is my code:- 这是我的代码:

#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;
}

I tried to kill the child process but that stops the whole program. 我试图杀死子进程,但这会停止整个程序。 The output for number of process = 2 进程数的输出= 2

process 6373 and bt 3
process 6373 and bt 6
process 6374 and bt 3                                                           
process 6376 and bt 6
process 6373 and bt 3
process 6375 and bt 6
process 6374 and bt 3
process 6374 and bt 6

Expected should have been just 2 processes with pid and bt(burst time). 预期应该只有2个进程具有pid和bt(突发时间)。

  • How to kill the child process just after it stores pid and bt(burst time) or it cannot be done ? 如何在存储pid和bt(burst time)后立即杀死子进程,否则无法完成?

You aren't using fork correctly at all. 您根本没有正确使用fork When you call it, the child process continues executing the same code as the parent, but gets a different return value (0) to indicate that it is the child process. 当您调用它时,子进程将继续执行与父进程相同的代码,但会获得不同的返回值(0),以表明它是子进程。 So in your code currently, the child processes are all spawning their own children. 因此,当前在您的代码中,子进程都产生了自己的子进程。

The usual way use fork is to do something akin to 使用fork的通常方法是做类似于

new_process=fork();
if(new_process==0)
  {
  // I am the child
  }
else if(new_process==-1)
  {
  // Something bad happened
  }
else
  {
  // I am the parent
  }

You'll need to pay attention to the return value of fork . 您需要注意fork的返回值。 This small modification to your code probably does what you're looking for. 对代码的小修改可能会满足您的需求。

#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();
        if (new_process == 0) {
            // This is a child process. Just spin until killed.
            while(true);
        }
        p[i].pid = new_process;
        p[i].bt = rand()%10;
        kill(new_process,SIGKILL);
    }
    for(i=0;i<no_of_process;i++){
        printf("process %d and bt %d\n",p[i].pid,p[i].bt);
    }
    return 0;
}

In your code, new_process is either 0 (so it's the child) or it's the pid of the child - no need to call getpid (ignoring -1 for failure) 在您的代码中, new_process要么为0(因此是子代), new_processnew_process无需调用getpid (忽略-1即可获得失败)

so, in the parent (0 return value) call fork no_of_processes times 因此,在父级(返回值0)中调用fork no_of_processes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM