fork download
  1.  
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. main() {
  6. int num[10]={1,2,3,4,5,6,7,8,9,10};
  7. int totalOdd = 0, totalEven = 0;
  8.  
  9. /* child process */
  10. if(fork() == 0) {
  11. for(int i = 0; i <10; i++){
  12. if(num[i]%2!=0){
  13. totalOdd+=num[i];
  14. }
  15. }
  16. printf("\nChild sum: %d", totalOdd);
  17. }
  18. /* parent process */
  19. else {
  20. for(int i = 0; i <10; i++){
  21. if(num[i]%2==0){
  22. totalEven+=num[i];
  23. }
  24. }
  25.  
  26. printf("\nParent sum: %d", totalEven);
  27. } /* end if */
  28. }
Success #stdin #stdout 0s 4448KB
stdin
Standard input is empty
stdout
Parent sum: 30
Child sum: 25