C:How Process communicate in linux -


i want count number of processes created 1,10 , fork() si executed. program executed in linux. don't how use wait or wexitstatus , i've spent hours on forums , still don't it. can me, please?

thanks, dragos

#include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h>  int nr = 1;  int main() {       int pid;     int i;     int stare;     for(i = 1; i<=10 ; i++)     {          pid = fork();          if( pid !=0 )         {              //parent             wait(&stare);             nr = nr + stare;           }         else         {             //child             nr++;             stare = wexitstatus(nr);             exit(nr);          }     }      printf("\nnr: %d\n", nr);  }                

the macros wexitstatus used in parent process exit status after wait call.

in child process, it's enough return nr (or call exit argument).

in parent use wexitstatus this:

if (wait(&stare) > 0) {     if (wifexited(stare))         nr += wexitstatus(stare); } 

we must use wifexited check because otherwise exit status not valid.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -