C语言waitpid函数怎么使用

C语言中的waitpid函数是一个非常实用的函数,它可以让父进程在等待子进程结束时继续执行其他任务,waitpid函数的原型如下:

include <sys/types.h>
include <sys/wait.h>
pid_t waitpid(pid_t pid, int *status, int options);

pid参数表示要等待的子进程ID,status参数用于接收子进程的退出状态,options参数用于设置函数的行为。

C语言waitpid函数怎么使用

下面我们详细介绍一下waitpid函数的使用。

waitpid函数的基本用法

1、创建子进程

我们需要创建一个子进程,可以使用fork()函数来创建子进程。

C语言waitpid函数怎么使用

include <stdio.h>
include <unistd.h>
include <sys/types.h>
include <sys/wait.h>
int main() {
    pid_t pid = fork();
    if (pid == 0) { // 子进程
        printf("I am the child process, my PID is %d
", getpid());
    } else if (pid > 0) { // 父进程
        printf("I am the parent process, my PID is %d and my child's PID is %d
", getpid(), pid);
    } else { // fork失败
        perror("fork failed");
        return 1;
    }
    return 0;
}

2、使用waitpid等待子进程结束并获取其退出状态

在父进程中,我们可以使用waitpid函数来等待子进程结束并获取其退出状态。

include <stdio.h>
include <unistd.h>
include <sys/types.h>
include <sys/wait.h>
int main() {
    pid_t pid = fork(); // 创建子进程
    if (pid == 0) { // 子进程
        printf("I am the child process, my PID is %d
", getpid());
    } else if (pid > 0) { // 父进程
        int status; // 用于接收子进程的退出状态
        pid_t ret = waitpid(pid, &status, 0); // 等待子进程结束并获取其退出状态
        if (ret == -1) { // waitpid失败
            perror("waitpid failed");
            return 1;
        } else if (ret == pid) { // 成功获取到子进程的退出状态
            printf("The child process with PID %d exited with status %d
", pid, WEXITSTATUS(status));
        } else { // waitpid返回了错误的PID(可能是因为信号中断)
            printf("waitpid returned an invalid PID (%d) when waiting for the child process with PID %d to exit. This is most likely due to a signal interruption. The child process may have already exited or may not be running at all. Please check the system logs for more information.
", ret, pid);
        }
    } else { // fork失败(返回值为负数)
        perror("fork failed");
        return 1;
    }
    return 0;
}

waitpid函数的其他选项参数

1、WNOHANG选项:如果没有子进程结束,waitpid函数将立即返回0,而不是阻塞等待,这样可以提高程序的响应速度。

C语言waitpid函数怎么使用

include <stdio.h>
include <unistd.h>
include <sys/types.h>
include <sys/wait.h>
include <sys/time.h>
include <errno.h>
include <string.h>
include <signal.h>
include <pthread.h>
include <assert.h>
include <stdbool.h> // for bool type definition in C99 and later versions of C standard library header files (not available in some older compilers)
include <stdlib.h> // for NULL macro definition (not available in some older compilers) and other utility functions such as atoi() and strtol() that are used in this example but not defined in POSIX standard library headers (which are included by sys/types.h and sys/wait.h) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating system) (but not available in some older compilers) and which are required for this example to compile and run on a modern Unix-like operating系统) (but not available in some

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/132184.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seo的头像K-seoSEO优化员
Previous 2023-12-16 01:15
Next 2023-12-16 01:18

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入