fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. // your code goes here
  5. return 0;
  6. }
  7.  
Success #stdin #stdout 0s 5316KB
stdin
#include <stdio.h>
#include <unistd.h>

int main() {
    int p1, p2;
    
    // 第一次fork创建子进程1
    p1 = fork();
    if (p1 < 0) {
        perror("First fork failed");
        return 1;
    }
    
    if (p1 == 0) {
        // 子进程1:输出b
        putchar('b');
    } else {
        // 父进程:第二次fork创建子进程2
        p2 = fork();
        if (p2 < 0) {
            perror("Second fork failed");
            return 1;
        }
        
        if (p2 == 0) {
            // 子进程2:输出c
            putchar('c');
        } else {
            // 父进程:输出a
            putchar('a');
        }
    }
    
    return 0;
}
stdout
Standard output is empty