need to detect the result of a process

Hi All
I have the following lines in my C code , these lines call certain shell programs and execute them , I want a way to wait for those programs to finish then check the status to see if they passed or failed.

by the way all those processes have to work together (i e same time)

i1=system(“Mgr 1 0 &”);
i2=system(“Mgr 2 0 &”);
i3=system(“Mgr 3 0 &”);

how can i check which one failed or passed …

Thanks All

Use spawn() and wait().

You also have to setup a SIGCHLD to detect when the spawned process have terminated.

I think you just need the signal catcher if you have better things to do than sit around and wait.

can you guys please just give me an example how to use spawn with wait() , and also how to setup SIGCHLD.
my problem is that the three processes I want to run and detect they have to run same time , I cant spawn one , then wait for it , then do the other , I have to spawn three times , then figure out to detect which one of them fails , so does the spawn , wait method work here.

thanks

Note there is a whole family of spawn…() routines to choose from,
depending on what suits you best.

example1.c: without a signal handler
#include <libc.h>
int main()
{
int i;
pid_t pid1;
pid_t pid2;
pid_t pid3;
pid_t pid;
int stat_loc;

pid1 = spawnlp(P_NOWAIT,"sleep","sleep","3",NULL);
fprintf(stderr,"Spawned process %d\n",pid1);

pid2 = spawnlp(P_NOWAIT,"sleep","sleep","3",NULL);
fprintf(stderr,"Spawned process %d\n",pid2);

pid3 = spawnlp(P_NOWAIT,"sleep","sleep","3",NULL);
fprintf(stderr,"Spawned process %d\n",pid3);

for(i=0;i<3;i++)
{
	pid = wait(&stat_loc);
	fprintf(stderr,"PID %d ended with rc %d\n",pid,stat_loc);
}

}

example2.c: with a signal handler
#include <libc.h>
#include <signal.h>
void my_handler(int sig);
int main(void);
int main()
{
int i;
pid_t pid1;
pid_t pid2;
pid_t pid3;

signal(SIGCHLD, my_handler);

pid1 = spawnlp(P_NOWAIT,"sleep","sleep","3",NULL);
fprintf(stderr,"Spawned process %d\n",pid1);

pid2 = spawnlp(P_NOWAIT,"sleep","sleep","3",NULL);
fprintf(stderr,"Spawned process %d\n",pid2);

pid3 = spawnlp(P_NOWAIT,"sleep","sleep","3",NULL);
fprintf(stderr,"Spawned process %d\n",pid3);

sleep(5);

for(i=0;i<3;i++)
{
	sleep(4);
}

}
void my_handler(int sig)
{
pid_t pid;
int stat_loc;

fprintf(stderr,"In Signal Handler signal %d\n",sig);
pid = wait(&stat_loc);
fprintf(stderr,"PID %d ended with rc %d\n",pid,stat_loc);

}

shouldn’t use stderr or fprintf in a signal handler. File handle are not signal safe.

Are you sure? I would never put an fprintf in a real signal handler, but this is just for demo, and I tried it out before posting. The signal should be masked while in the handler. Where is the danger?

The signal could be involved in the middle of an fprintf using stderr. It’s not that fprintf is not signal safe, it’s the usage of the same file handle that isn’t. You get the kind of bug that only happens once every six months ;-)

I personaly have had such a bug, it was with printf though and the program would only crash on termination, after the call to exit() ;-)

Hi guys
Thank you very much for the help so far ,Iam new to QNX programming , just started in last three weeks, so forgive me if I ask some simple questions , but some stuff it doesnt seems to click for me right away :

iam confused now , I have the following code :

             pid1=spawnl(P_NOWAIT ,"/fs0p0/bin/Mgr","/fs0p0/bin/Mgr", "1","0",NULL);
 printf( "pid of running process is %d\r\n",pid1);
 pid2=spawnl(P_NOWAIT ,"/fs0p0/bin/Mgr","/fs0p0/bin/Mgr", "2","0",NULL);
 printf( "pid of running process is %d\r\n",pid2);
 pid3=spawnl(P_WAIT ,"/fs0p0/bin/Mgr","/fs0p0/bin/Mgr", "3","0",NULL);
 printf( "pid of running process is %d\r\n",pid3);

             // *****************POINT A*****************
             pid4 = waitpid(pid1,&stat_loc,WEXITED);
             pid5 = waitpid(pid2,&stat_loc,WEXITED); 
             printf("***********P1 ID %d ended with rc %d\r\n",pid4,stat_loc); 
             printf("***********P2 ID %d ended with rc %d\r\n",pid5,stat_loc); 
             printf("***********P3 ID %d ended with rc %d\r\n",pid3,stat_loc);

from what I understood of the documentation , that my C program , should not go to the line (POINT A) unless the three child processes end execution, but what happening here is that the (printf) lines after (POINT A) is getting executed , even before the three processes above it finish.

my goal is that my program to wait for the three processes to finish then continue of anything iam doing. can somebody explain what is the problem.

Answered in other post.

answered in another topic