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

int main() {
    pid_t pid1, pid2;

    pid1 = fork();
    if (pid1 < 0) {
        fprintf(stderr, "Fork Failed\n");
        return 1;
    } else if (pid1 == 0) {
        lockf(1, 1, 0);
        printf("我是第一个子进程，我的PID是：19870905\n");
        fflush(stdout);
        lockf(1, 0, 0);
        return 0;
    }

    // 父进程先等子进程1结束，再创建子进程2
    waitpid(pid1, NULL, 0);

    pid2 = fork();
    if (pid2 < 0) {
        fprintf(stderr, "Fork Failed\n");
        return 1;
    } else if (pid2 == 0) {
        lockf(1, 1, 0);
        printf("我是第二个子进程，我的PID是：20060928\n");
        fflush(stdout);
        lockf(1, 0, 0);
        return 0;
    }

    // 再等子进程2结束
    waitpid(pid2, NULL, 0);

    lockf(1, 1, 0);
    printf("我是父进程，我的PID是：1787075\n");
    fflush(stdout);
    lockf(1, 0, 0);

    return 0;
}