fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. int main()
  5. {
  6. int x = 1;
  7. //create new process
  8. int returnValue = fork();
  9. //Error handling
  10. if(returnValue < 0){
  11. perror("fork() error");
  12. exit(-1); //terminate calling process
  13. }
  14. //Child Class
  15. if (returnValue == 0) {
  16. printf("I am Child \n");
  17. }
  18. //Parent Class
  19. else{
  20. printf("I am Parent \n");
  21. wait(NULL); //Wait for child process
  22. printf("The PID of child process is %d \n", returnValue);
  23. }
  24. return 0;
  25. }
Success #stdin #stdout 0s 4204KB
stdin
Standard input is empty
stdout
I am Child 
I am Parent 
The PID of child process is 11182