can somebody tell me what is wrong

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(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); 
     rintf("***********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.

You will get to point A after the 3rd program terminates, what ever happens with the first 2 sinc you specified NOWAIT

Then the two waitpid should block until the first two program terminates, then you could see the printf. Note that for the 3rd program, the stat_loc in your printf is useless.

Also ALWAYS check for validity of return value and if an error is return errno can tell you a lot.

Then

Hi thanks for the reply

I changed all my spawns to NO_WAIT , then I put three waitpid after , but my program is not waiting for the three spawns to finish , it is going to the printfs right away , here is my new 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_NOWAIT ,"/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);
pid = waitpid(pid2,&stat_loc,WEXITED);
rintf("***********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);

what is happening , the three processes starts to run ,then the printf are printing, and the processes continue to run , it takes about 30 seconds to finish all of them …i will go crazy , dont know why my C code doesnt wait.

ED

I modified your program to run sleep instead of Mgr and it works as expected. The code you’ve shown here has a number of obvious errors which I will indicate below. Also note that you need to do a wait() and then a printf() or provide different stat_loc variables. Otherwise the three final printf’s are just printing the same value.