fork download
  1. /****************************************************************
  2.  *
  3.  * Example: to demonstrate fork() and execl() and system calls
  4.  *
  5.  ***************************************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <sys/wait.h>
  11.  
  12. int main( int argc, char *argv[], char *env[] )
  13. {
  14. pid_t my_pid, parent_pid, child_pid;
  15. int status;
  16.  
  17. /* get and print my pid and my parent's pid. */
  18.  
  19. my_pid = getpid(); parent_pid = getppid();
  20. printf("\n Parent: my pid is %d\n\n", my_pid);
  21. printf("Parent: my parent's pid is %d\n\n", parent_pid);
  22.  
  23. /* print error message if fork() fails */
  24. if((child_pid = fork()) < 0 )
  25. {
  26. perror("fork failure");
  27. exit(1);
  28. }
  29.  
  30. /* fork() == 0 for child process */
  31.  
  32. if(child_pid == 0)
  33. { printf("\nChild: I am a new-born process!\n\n");
  34. my_pid = getpid(); parent_pid = getppid();
  35. printf("Child: my pid is: %d\n\n", my_pid);
  36. printf("Child: my parent's pid is: %d\n\n", parent_pid);
  37. printf("Child: I will sleep 3 seconds and then execute - date - command \n\n");
  38.  
  39. sleep(3);
  40. printf("Child: Now, I woke up and am executing date command \n\n");
  41. //execl("/bin/date", "date", NULL);/* execl("/bin/date","date",0,0); */
  42. execlp("date", "date",NULL); /* execlp("date","date",0,0);
  43.   perror("execl() failure!\n\n");
  44.  
  45.   printf("This print is after execl() and should not have been executed if execl were successful! \n\n");
  46.  
  47.   _exit(1);*/
  48. }
  49. /*
  50.  * parent process
  51.  */
  52. else
  53. {
  54. printf("\nParent: I created a child process.\n\n");
  55. printf("Parent: my child's pid is: %d\n\n", child_pid);
  56. system("ps -el | grep wait"); printf("\n \n");
  57. wait(&status); /* can use wait(NULL) since exit status
  58.   from child is not used. */
  59. printf("\n Parent: my child is dead. I am going to leave.\n \n ");
  60. }
  61.  
  62. return 0;
  63. }
  64. /*
  65.  * Parent: my pid is 2020
  66.  *
  67.  * Parent: my parent's pid is 1740
  68.  *
  69.  *
  70.  * Parent: I created a child process.
  71.  *
  72.  * Parent: my child's pid is: 2021
  73.  *
  74.  *
  75.  * Child: I am a new-born process!
  76.  *
  77.  * Child: my pid is: 2021
  78.  *
  79.  * Child: my parent's pid is: 2020
  80.  *
  81.  * Child: I will sleep 3 seconds and then execute - date - command
  82.  *
  83.  * 1 S 0 996 1 0 80 0 - 29038 wait ? 00:00:00 ksmtuned
  84.  * 0 S 500 1740 1734 0 80 0 - 29105 wait pts/0 00:00:00 bash
  85.  * 0 S 500 2020 1740 0 80 0 - 998 wait pts/0 00:00:00 a.out
  86.  * 0 S 500 2022 2020 0 80 0 - 28512 wait pts/0 00:00:00 sh
  87.  *
  88.  *
  89.  * Child: Now, I woke up and am executing date command
  90.  *
  91.  * Tue Aug 27 09:07:37 IST 2013
  92.  *
  93.  * Parent: my child is dead. I am going to leave.
  94.  */
  95.  
  96.  
Success #stdin #stdout 0.02s 5284KB
stdin
Standard input is empty
stdout
Wed 11 Sep 2024 04:46:28 PM UTC

 Parent: my pid is 358759

Parent: my parent's pid is 358758


Parent: I created a child process.

Parent: my child's pid is: 358762


 

 Parent: my child is dead. I am going to leave.