Friday, 19 September 2014

C PROGRAM TO CREATE A CHILD PROCESS AND PRINT THE ID

C program to create a child process using fork(). Print its own PID and ID of its parent and then exits. The parent process waits for child process to complete and prints its own ID and its child process ID and then exits.

program:
 

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
void main()
{
  pid_t pid;
  pid=fork();
  if(d==0)
      {
        printf("this is a child process \n");
        printf("child pid: %d \n",getpid());
        printf("parent pid: %d \n",getppid());
        exit(0);
      }
wait(NULL);
printf("this is parent process \n");
printf("parent pid : %d \n",getpid());
printf("child pid: %d \n",pid);
exit(0);


-----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Total Pageviews