C正确退出程序

时间:2017-04-19 20:51:27

标签: c

您好我正在编写一个运行的程序然后等待它完成然后执行if条件。但是,在子进程完成后,它不会执行父进程中的其余代码。任何建议都会很棒。感谢

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;



}

2 个答案:

答案 0 :(得分:1)

你没有调用wait()而你的execv()不正确...... ...将其与您的版本进行比较以检查差异......

#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;

}

答案 1 :(得分:1)

我尝试使用waitpid()而不是wait()来提出解决方案。 为什么这不起作用?

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;
}

观察:   如果我将waitid()语句放在子进程之外,这是有效的。   以下代码有效:

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);

这意味着在这种情况下,父进程能够检测到子进程的终止。我的理解是waitpid()应该工作 在孩子或父母的过程中。我坚信它确实有效。但是,由于子进程终止,子进程中的后续printf()语句没有执行。

相关问题