子进程如何发出执行完成的信号?

问题描述 投票:0回答:1

这里是宿主程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <wait.h>

int main(int argc, char* argv[])
{
        printf("Starting the main function.\n");
        int retval=1;
        pid_t pid=fork();

        if (pid==0)
        {
                printf("In the child process now. The PID of the child is %d and the PID of its parent is %d.\n", getpid(), getppid());
                printf("Child process now, retval is %d\n", retval);
                execl("./binsearch", argv[1], NULL);
        }
        else
        {
                printf("Back to the parent process now, here is its PID: %d.\n", getpid());
                printf("Parent process now, retval is %d\n", retval);

                wait(&retval);
                if(WIFEXITED(retval)==1)
                {
                        printf("Process terminated normally.\n");
                }
                else
                {
                        printf("Process termniated abnormally.\n");
                        exit(42);
                }
        }
        return 0;
}

这是可执行文件为

./binsearch
的程序:

#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return 1;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}

void swap(int *xp, int *yp) 
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void sort(int arr[], int n) 
{
    int i, j;
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                swap(&arr[j], &arr[j+1]);
            }
        }
    }
}

int main(void)
{
    int n, key, arr[10];
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    
    printf("Enter the elements: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    sort(arr, n);
    
    printf("Enter element to be searched: ");
    scanf("%d", &key);
    
    int result = binarySearch(arr, 0, n - 1, key);
    if (result == -1) {
        printf("Element is not present in array\n");
    } else {
        printf("Element is present\n");
    }
    
    return 0;
}

这是我执行主机程序时的输出:

Starting the main function.
Back to the parent process now, here is its PID: 1734.
Parent process now, retval is 1
In the child process now. The PID of the child is 1735 and the PID of its parent is 1734.
Child process now, retval is 1
Enter the number of elements in the array: 3
Enter the elements: 1 2 3
Enter element to be searched: 2
Element is present
Process terminated normally.

我无法理解这里执行的指令的顺序。第一行之后,

Starting the main function.
为什么父进程的else块会执行?既然 fork 调用是下一条指令,子进程不应该执行吗?

另外,retval 如何反映宿主程序的 else 块中子进程的完成情况?据我所知,它是一个二进制信号量,子进程在开始执行时获取并在完成时释放。但是,我无法弄清楚子进程是如何完成这些工作的。

c process operating-system
1个回答
0
投票

fork
函数,正如大多数教程和书籍所说,“返回两次”。

在子进程中返回一次,返回值

0
。并且它在父进程中返回一次,带有子进程的pid。

fork
返回开始,您就有两个独立的进程,它们同时运行。

但是您也无法控制哪个进程将首先继续,这取决于操作系统。

在您的情况下,父进程似乎继续运行,直到它到达

wait
调用。然后,该调用将等待第一个(或仅在您的情况下)子进程退出。然后才恢复运行。

当父进程陷入

wait
调用时,子进程可以一直运行到完成。此时它将不再存在,操作系统将通过从其
wait
调用返回来继续父进程。

© www.soinside.com 2019 - 2024. All rights reserved.