Zameer  Haque
Zameer Haque

Reputation: 414

How to terminate only the child process?

I want print an array of processes with their pid and burst time. For this, I generate the pid using fork() and then get it pid using getpid(). However, since fork creates a child process that runs in isolation from the parent process, I am not getting the expected behavior. What the program should do is generate process for given number_of_process and then store pid and randomburst time value inside the specific structure element. 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

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).

  • How to kill the child process just after it stores pid and bt(burst time) or it cannot be done ?

Upvotes: 0

Views: 10391

Answers (3)

Chris Turner
Chris Turner

Reputation: 8142

You aren't using fork correctly at all. 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. 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

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

Upvotes: 7

KevinDTimm
KevinDTimm

Reputation: 14376

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)

so, in the parent (0 return value) call fork no_of_processes times

Upvotes: 2

Jonesinator
Jonesinator

Reputation: 4216

You'll need to pay attention to the return value of 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;
}

Upvotes: 3

Related Questions