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. FILE * fp;
  18. fp = fopen ("abc.txt", "w+"); // create empty file
  19. fclose(fp);
  20. }
  21. //Parent Class
  22. else{
  23. printf("I am Parent \n");
  24. wait(NULL); //Wait for child process
  25. printf("The PID of child process is %d \n", returnValue);
  26. }
  27. return 0;
  28. }
Success #stdin #stdout 0s 4568KB
stdin
Standard input is empty
stdout
I am Parent 
The PID of child process is 4066