Hello I am writing ac program which runs then waits for it to complete then executes an if condition. However after the child process completes it does not execute the rest of code in parent. Any advice would be great. Thanks
AA
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
int main() {
int D, waitVal3, waitVal4;
D = fork();
if(D == 0)
{
execv("DD", 0);
}
if(D != 0)
{
printf("\nPid = %d Code AA: created proccess Pid = %d (code DD)\n", getpid(), D);
}
waitVal3 = (waitVal4);
//NEVER ENTERS THIS CONDITION
printf("WAIT VAL: %d", waitVal3);
if(waitVal3 == D)
{
printf("\nPid = %d Code AA: process Pid = %d terminated\n", getpid(), D);
}
return 0;
}
DD
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
int main (int argc, char *argBB[]) {
int C3, waitVal, waitVal2, ps;
C3 = fork();
if(C3 != 0)
{
printf("\nPid = %d Code DD: created proccess Pid = %d (code CC)\n", getpid(), C3);
}
if( C3 == 0 )
{
execv("CC", 0);
printf("\nexecv failed\n");
exit(0);
}
if(C3 < 0)
{
printf("Fork failed");
exit(1);
}
ps = fork();
if(ps != 0)
{
printf("\nPid = %d Code DD: created proccess Pid = %d (code ps)\n", getpid(), ps);
}
if( ps == 0 )
{
char command[50];
strcpy(command, "ps -u username");
system(command);
exit(11);
kill(ps, SIGKILL);//KILL PROCCESS PS HERE
}
waitVal = wait(waitVal2);
if(waitVal == ps)
{
printf("\nPid= %d Code DD: process Pid = %d terminated\n", getpid(), ps);
printf("\nPid = %d Code DD: killing process Pid = %d\n", getpid(), C3);
kill(C3, SIGKILL);
printf("\nPid= %d Code DD: process Pid = %d terminated\n", getpid(), C3);
printf("\nPid = %d Code DD: terminating\n", getpid());
exit(7);
}
return 0;
}
You didn't call wait() and your execv() was incorrect... ... compare this with your version to check the differences...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
int main() {
char *args[2] = {"DD", NULL};
int D, waitVal3, waitVal4, rc, waitStatus;
D = fork();
if ( D == -1 )
{
printf("fork failed\n");
exit(1);
}
if(D == 0)
{
// ORIG: execv("DD", args);
rc = execv("/some/path/to/DD", args);
printf("execv failed: errno: %d\n", errno);
exit(1);
}
if(D != 0)
{
printf("\nPid = %d Code AA: created proccess Pid = %d (code DD)\n", getpid(), D);
}
waitVal4 = wait(&waitStatus);
waitVal3 = (waitVal4);
//ORIGINALLY - NEVER ENTERS THIS CONDITION
printf("WAIT VAL: %d", waitVal3);
if(waitVal3 == D)
{
printf("\nPid = %d Code AA: process Pid = %d terminated\n", getpid(), D);
}
return 0;
}
I tried to come up with a solution using waitpid() instead of wait(). Why this didn't work?
int main() {
char *args[2] = {"DD", NULL};
int waitVal3, waitVal4, rc, waitStatus;
pid_t D;
D = fork();
if ( D == -1 )
{
printf("fork failed\n");
exit(1);
}
if(D == 0)
{
// ORIG: execv("DD", args);
if( execv("/home/ubuntu/workspace/C/DD", args) == -1 ){
printf("execv failed: errno: %d\n", errno);
exit(1);
}
if( waitpid( D, &waitStatus, 0 ) == -1 ){
printf("Error waiting child process.\n");
exit(1);
};
printf("\nPid = %d Code AA: process Pid = %d terminated\n", getpid(), D);
}
return 0;
}
Observation: If I put the waitid() statement outside the child process, this works. The following code worked:
if(D == 0)
{
// ORIG: execv("DD", args);
if( execv("/home/ubuntu/workspace/C/DD", args) == -1 ){
printf("execv failed: errno: %d\n", errno);
exit(1);
}
}
if( waitpid( D, &waitStatus, 0 ) == -1 ){
printf("Error waiting child process.\n");
exit(1);
};
printf("\nPid = %d Code AA: process Pid = %d terminated\n", getpid(), D);
This means that the parent process was able detect the termination of child process in this case. My understanding is that waitpid() should work whether in child or parent process. I am firm believer that it did work. However, the consequent printf() statement in the child process didn't get executed since the child process got terminated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.