Bhuvanesh
Bhuvanesh

Reputation: 1279

What is the output of the parent process atfer terminating a child?

 #include<stdio.h>
 #include<stdlib.h>
    #include<unistd.h>
 main()
 {
    printf("Parent pid=%d\n",getpid());
    int a=2,b;
    pid_t pid;
    pid=vfork();
    if(pid!=0)
            sleep(5);
    a=a+2;

    printf("%d\t%d\n",getpid(),a);
                    if(getpid()==0)                                                                                         
            exit(0);
  }

when i am printing it shows like

 Parent pid=10696
 10697  4
 10696  3
a.out: cxa_atexit.c:100: __new_exitfn: Assertion `l != ((void *)0)' failed.
Aborted (core dumped)

Upvotes: 1

Views: 615

Answers (2)

Rajalakshmi
Rajalakshmi

Reputation: 681

 #include<stdio.h>
#include<stdlib.h>
 #include<unistd.h>
 main()
    {
    printf("Parent pid=%d\n",getpid());
    int a=2,b;
    pid_t pid;
    pid=vfork();
    if(pid!=0)
            sleep(5);
    a=a+2;
    printf("%d\t%d\n",getpid(),a);
  if(pid == 0)  
  exit(0);
}

change that if condition

Upvotes: 2

Chandru
Chandru

Reputation: 1334

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
        printf("Parent pid=%d\n",getpid());
        int a=2,b;
        pid_t pid;
        pid=vfork();
        if(pid!=0)
                sleep(5);
        a=a+2;
        printf("%d\t%d\n",getpid(),a);
        exit(0);
}

Remove the line if(getpid() == 0).

getpid() will return process of the executing process not 0.

The return value of 0 in fork() or vfork() is different from getting the pid.

Upvotes: 2

Related Questions