fork(1) download
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<sys/types.h>
  4. #include<sys/wait.h>
  5.  
  6. int main()
  7. {
  8. pid_t pid;
  9. int n;
  10.  
  11. printf("Enter nice value for child:");
  12. scanf("%d",&n);
  13. pid=fork();
  14.  
  15. if(pid<0)
  16. {
  17. printf("Fork Failed\n");
  18. }
  19. else if(pid==0)
  20. {
  21. printf("Child Process\n");
  22. printf("Child PID:%d\n",getpid());
  23. nice(n);
  24. printf("Child value increased by %d\n",n);
  25. }
  26. else
  27. {
  28. wait(NULL);
  29. printf("Parent Process\n");
  30. printf("Parent PID:%d",getpid());
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter nice value for child:Child Process
Child PID:1086921
Child value increased by 32765
Enter nice value for child:Parent Process
Parent PID:1086878