fork(1) download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5.  
  6. void sum_of_digits()
  7. {
  8. int num, sum = 0, rem;
  9.  
  10. printf("Enter the number: ");
  11. fflush(stdout); // Flush output before input
  12. scanf("%d", &num);
  13.  
  14. while (num != 0) {
  15. rem = num % 10;
  16. sum += rem;
  17. num /= 10;
  18. }
  19.  
  20. printf("Sum of Digits: %d\n", sum);
  21. }
  22.  
  23. int main()
  24. {
  25. pid_t c1, c2;
  26.  
  27. c1 = fork();
  28.  
  29. if (c1 == 0) {
  30. printf("Happy new year\n");
  31. return 0;
  32. }
  33. else {
  34. c2 = fork();
  35.  
  36. if (c2 == 0) {
  37. sum_of_digits();
  38. return 0;
  39. }
  40. else {
  41. wait(NULL);
  42. wait(NULL);
  43. printf("Parent exiting …good bye....\n");
  44. }
  45. }
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5320KB
stdin
123
stdout
Enter the number: Sum of Digits: 6
Happy new year
Parent exiting …good bye....