Thursday, February 2, 2012

Fork - Program to fork a child process 2

 /*:) fork_test :) have fun */  
 // header files  
 #include<unistd.h>  
 #include<stdio.h>  
 #include<sys/types.h>  
 //main func  
 int main(int argc, char *argv[])  
 {  
  pid_t new_pid;  
  char *message;  
  int n;  
  printf("fork program starting");  
  new_pid = fork();  
  switch(new_pid)  
   {  
   case -1:  
    printf("fork failed");  
    exit(1);  
   case 0:  
    message = "this is child";  
    n=5;  
    break;  
   default:  
    message = "this is parent";  
    n=3;  
    break;  
   }  
  for(;n>0;n--)  
   {  
    puts(message);  
    sleep(1);  
   }  
  exit(0);  
 }  

No comments:

Post a Comment