OS-(I) Assignment 1: Set A- 1)

(1) Implement the C Program to create a child process using fork(), display parent and child process id. Child process will display the message “I am Child Process” and the parent process should display “I am Parent Process”.

Solution:
#include<stdio.h>
#include<sys/types.h>
void ChildProcess();
void ParentProcess();
int main()
{
        pid_t pid;
        printf("\nMain Process Id: %dn",getpid());
        pid=fork();
       if(pid < 0)
        {   
                printf("\nFork Failed\n");
                return 1;
        }
        else if (pid==0)
        {
            ChildProcess();
            printf("\nChild Process Id: %d\n",(int)getpid());
        }
        else 
        {
wait(NULL);
     printf("\nIn parent Process...child completed\n");
  ParentProcess();
          printf("\nParent Process Id: %d\n",(int)getpid());      
        }
return 0;
}
void ChildProcess()
printf("I am child process..");
}
void ParentProcess()
printf("I am parent process..");
}

0 Comments:

Post a Comment