编辑代码

#include <stdio.h>
#include <unistd.h>
#if 0
int main() {
    pid_t pid;

    // 调用 fork() 创建一个新的进程
    pid = fork();

    if (pid < 0) {
        // 错误处理
        fprintf(stderr, "Fork failed!\n");
        return 1;
    } else if (pid == 0) {
        // 子进程代码
        printf("This is the child process. PID: %d\n", getpid());
    } else {
        // 父进程代码
        printf("This is the parent process. PID: %d, Child PID: %d\n", getpid(), pid);
    }

    return 0;
}
#else
/*
int main()
{
    pid_t pid;
    int i;
    pid=fork();
    if(pid==0)
    {
        for(i=0;i<500;i++){
            printf("This is the child process. PID: %d\n", getpid());
        }
    }
    else{
        for(i=0;i<500;i++){
            printf("This is the parent process. PID: %d, Child PID: %d\n", getpid(), pid);
        }
    }
}
*/
int main(){
    int i;
    for(i=0;i<3;i++){
        if(fork()==0){
            printf("This is the child process\r\n");
        }
        else{
            printf("This is the parent process\r\n");
        }
    }
}
#endif