fork() system call in c

前端 未结 5 1701
孤城傲影
孤城傲影 2021-01-01 06:17
    #include 
    #include 
    int main()
    {
       fork();
       fork() && fork() || fork();
       fork();

     printf         


        
5条回答
  •  礼貌的吻别
    2021-01-01 06:41

    You should not use fork() like this. Never. And, nevertheless, you won't need to do so in real life. How to use it:

    int main() {
        /* code */
        pid_t pid = fork();
        if (pid < 0) {
            /* error, no child process spawned */
        }
        if (pid > 0) {
            /* we are the parent process, pid is the process ID of the ONE child process spawned */
        }
        /* else, we are the child process, running exactly one command later the fork() was called in the parent. */
        /* some more code */
        return 0;
    }
    

提交回复
热议问题