c - 使用 switch 语句 fork 两个进程

标签 c linux posix

我正在学习 C 类(class)的介绍,但我对第一个作业有点难过。我们的任务是创建一个父进程和两个子进程。到目前为止,本书向我们展示的所有示例都涉及一个父子关系的 switch 语句。我对如何将其转化为一个父进程和两个子进程感到有点困惑。这是我目前所拥有的:

#include <stdio.h>

int main() {
    int i, pid, status;
    pid = fork();
    switch(pid) {
    case -1:
        /* An error has occurred */
        printf("Fork Error");
        break;
    case 0:
        /* This code is executed by the first parent */
        printf("First child process is born, my pid is %d\n", getpid());
        printf("First child parent process is %d\n", getppid());
        for (i=1; i<=10; i++)
            printf("First child process, iteration: %d\n", i);
        printf("First child dies quietly.\n");
        break;
    default:
        /* This code is executed by the parent process */
        printf("Parent process is born, my pid is %d\n", getpid());
        wait(&status);
        printf("Parent process dies quietly.\n");
    }
}

这非常适合这个过程:

Parent process is born, my pid is 10850
First child process is born, my pid is 10851
First child parent process is 10850
First child process, iteration: 1
First child process, iteration: 2
First child process, iteration: 3
First child process, iteration: 4
First child process, iteration: 5
First child process, iteration: 6
First child process, iteration: 7
First child process, iteration: 8
First child process, iteration: 9
First child process, iteration: 10
First child dies quietly.
Parent process dies quietly.

本质上我只需要用第二个过程做同样的事情......就像:

printf("Second child process is born, my pid is %d\n", getpid());
printf("Second child parent process is %d\n", getppid());
for (k=1; k<=10; k++)
    printf("Second child process, iteration: %d\n", i);
printf("Second child dies quietly.\n");
break;

但我只是不确定如何从我目前所拥有的内容中到达那里。我正在接近这种正确的方式吗?我应该使用更好的方法吗?非常感谢。

最佳答案

有一个通用规则。当您使用 fork(2)您应该始终处理以下三种情况:

  1. fork给了0,你在child进程
  2. fork 给出了一个正的pid_t,你在parent进程中
  3. fork 失败并给了-1

人们(新手)有时往往会忘记上次(失败)的案例。但它确实发生了,您可以使用 setrlimit(2) 轻松测试这种情况。在你的祖父进程中使用 RLIMIT_NPROC 来降低进程的限制,通常那个祖父进程就是你的 shell(例如使用 ulimit Bash builtin-u)。

现在,如何处理这三种情况是编码风格的问题。您可以使用 switch,但也可以使用两个 if。您的代码使用了 switch 并且这样做是正确的。

作为一般规则,大多数 system calls (在 syscalls(2) 中列出)可能会失败,您几乎总是需要处理失败案例(请参阅 errno(3) 并使用 perror(3))。

另请阅读 Advanced Linux Programming (可免费下载)。

I'm a bit confused about how to translate this into one parent and two child processes.

fork 系统调用正在(成功时)恰好创建一个 子进程。所以如果你需要两个 child ,你应该连续调用两次(并在两次调用中测试失败)。如果你需要一个 child 和一个孙子,你应该只在第一个给0时才做第二个fork。当然你需要保留两个(成功和积极)pid_t -例如在两个变量中 - 由您对 fork 的两次调用返回。

避免zombie processes ,每个成功的 fork 稍后都应该有它的等待系统调用(例如 waitpid(2)waitwait4(2)wait3)。你在哪里等待取决于你是否想让两个 child 同时运行。但是每个成功的 fork 都应该有一个相应的成功的 wait 调用。

另请阅读 signal(7) (和 signal-safety(7) )关于 SIGCHLD 如果您想异步通知子进程更改 - 特别是终止。一种常见的方法是安装一个 SIGCHLD 信号处理程序(例如使用 sigaction(2) 或旧的 signal(2) ),它只设置一些全局 volatile sigatomic_t 标志并测试然后清除在代码中方便的地方进行标记(例如,在某些 event loop 中使用 poll(2) )。

注意:请注意,fork 与 C 编程无关(fork C11 标准 n1570 或其标准中定义前身 C99 )。这是一个 POSIX和 Linux 的东西。 Windows 或 z/OSArduino微 Controller 本身没有它,但可以在某些标准 C 中编程。

关于c - 使用 switch 语句 fork 两个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44297208/

相关文章:

c - Socket UDP 客户端内存泄漏

c - 解释这个程序的输出

c - Rust FFI 传递 trait 对象作为调用回调的上下文

android - 如何手动验证 apk 签名并比较签名 key

c# - 从 C#/NET 引用针对 Cygwin 在 GCC 中构建的 GNU C (POSIX) DLL

c - 将 pthread 变量保留在本地

c - 如何使用 NO STDLIB/STDIO 函数将 unsigned long long 转换为字符串

linux - 在 Linux 中的多个线程上阻塞 read() 和 write()

c - sem_wait 未使用 EINTR 解锁

shell - 测试 -d 目录 true - 子目录 false (POSIX)