簡體   English   中英

子進程未在 c 中正確終止

[英]child processes are not terminating correctly in c

在學習分叉和進程方面仍然是新的,我有這個任務來創建 3 個子進程執行一些操作,然后父進程應該在它們終止時打印退出狀態。

我遇到的問題是孩子 1 提前終止,我認為我沒有正確使用 wait()。

這是我的代碼:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main( int argc, char *argv[] ) {
    pid_t child1,child2, child3,wpid;
    int child1Status,child2Status,child3Status;

child1 = fork();
   if (child1 == 0){
    float marks[8];
    float average = 0.0;
    float sum = 0.0;
    printf("I am child one my pid is %d \n",getpid());
    printf("Please enter 8 marks and I will calcuate the average and highest mark\n");
    for (int i = 0; i < 8; ++i) {
        printf("%i) ",i+1);
    scanf("%f", &marks[i]);
    sum += marks[i];
}
average = sum / 8;
printf("Average = %.2f and highest = %.2f\n", average,highest(&marks));
exit(1);
}
 else{
        
        child2 = fork();
         if (child2 == 0){
    

char *cmd = "wc";
        char *args[4];
    args[0] = "wc";
    args[1] = "-c";
    args[2] = "test.txt";
    args[3] = NULL;
         execvp(cmd, args);
      
         }
         else
        {
           child3 = fork();
            if (child3 == 0){
        

          char *cmd = "wc";
            char *args[4];
            args[0] = "wc";
            args[1] = -c;
            args[2] = "anotherfile.txt";
            args[3] = NULL;
            execvp(cmd, args);
             }
          else
            {

                
                wait(&child1Status);
                printf(" child one has exited with exit status %d \n", (child1Status >> 8));
                wait(&child2Status);
                printf(" child two has exited with exit status %d \n", (child2Status >> 8));
                wait(&child3Status);
                printf(" child three has exited with exit status %d \n", (child3Status  >> 8));
            }

}

在我當前的 output 中,孩子 1 說它在我輸入任何標記之前已經退出,而它應該說它在打印出最高和平均標記后已經退出。

我也知道,因為我在 child2 和 3 中使用 execvp,所以 exit() 代碼不會運行,在這種情況下我如何獲得退出狀態?

一旦所有子進程都終止,我還需要打印“父進程已完成”,如何確保在我打印“父進程已完成”之前所有子進程都已終止

編輯:由於評論,用以下內容替換了最后一個 else 塊

else
            {
                
                waitpid( child1,  &child1Status, 0);
                printf(" child one has exited with exit status %d \n", (child1Status >> 8));
                 waitpid( child2,  &child2Status, 0);
                printf(" child two has exited with exit status %d \n", (child2Status & 0x7F));
                 waitpid( child3,  &child3Status, 0);
                printf(" child three has exited with exit status %d \n", (child3Status & 0x80));
            }

上面所有 3 個子進程在最后一個接一個地退出,這不應該是這種情況

預期 output:

I am child 1 please enter 8 marks and i will find the average
I am child 2 here is the word count 
50
child 2 has exited 
i am child 3 here is the word count 
96 
child 3 has exited 
**enter 8 marks by user**
average is 
child 1 has exited
parent has finished

當前 output:

 I am child 1 please enter 8 marks and i will find the average
    I am child 2 here is the word count 
    50
  i am child 3 here is the word count 
    96 
**enter 8 marks by user**
    average is 
    child 1 has exited
child 2 has exited
child 3 has exited

由於各種錯誤,您的代碼甚至無法編譯...

無論如何,看起來你想在孩子們一出現就顯示它的結束。 為此,您應該循環wait ,直到沒有更多的孩子等待,並使用返回值來知道哪個已經結束:

      ...
      else
        {

        for(;;) {    
            pid_t pid = wait(&child1Status);
            int child_num;
            if (pid == child1) child_num = 1;
            else if (pid == child2) child_num = 2;
            else if (pid == child3) child_num = 3;
            else break;   // no more child to wait...

            printf(" child %d has exited with exit status %d \n",
                   child_num, (child1Status >> 8));
        }
        }
        ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM