c - 使用 fork 打印

标签 c

我无法理解打印输出的顺序...

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

int main(void)
{
    int index;
    for (index = 1; index < 4; index++)
    {
        printf("HI\n"); 
        fork();
    }
    printf("Unix System Programming\n");
    exit(0);
}

当我只打印 unix 编程系统时,很容易理解 fork 工作 2^n 次......但是当我用它打印 HI 时......我不明白为什么这个顺序? 输出:

HI
HI
HI
HI
HI
Unix System Programming
HI
HI
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming
Unix System Programming

最佳答案

For better visibility i have just modified the code

int main(void)   
    {  
        int index;  
        for (index = 1; index < 4; index++)  
        {
            printf("We are in loop with  process id=%d\n",getpid());
            if(!fork()) { //Child process
               printf("Came inside chid with process id=%d\n",getpid());
              // _exit(0);
            } else {     //Parent process
              printf("Came inside parent with process id=%d\n",getpid());
           }
        }
        printf("We are out of loop with  process id=%d\n",getpid());
        exit(0);
    }

    [OP]   
    We are in loop with  process id=2258   -----> Main Parent   
    Came inside parent with process id=2258   
    We are in loop with  process id=2258  
    Came inside child with process id=2259  
    We are in loop with  process id=2259  
    Came inside parent with process id=2258  
    We are in loop with  process id=2258  
    Came inside parent with process id=2259  
    We are in loop with  process id=2259  
    Came inside parent with process id=2258  
    We are out of loop with  process id=2258 ----> "Outer block of loop referring main Parent  Here exit(0) will rip parent process of pid 2258 "      
    Came inside parent with process id=2259     
    We are out of loop with  process id=2259   
    Came inside child with process id=2263   
    We are out of loop with  process id=2263   
    Came inside child with process id=2261   
    We are in loop with  process id=2261   
    Came inside parent with process id=2261   
    We are out of loop with  process id=2261   
    Came inside child with process id=2264   
    We are out of loop with  process id=2264   
    Came inside child with process id=2262   
    Came inside child with process id=2260   
    We are out of loop with  process id=2262   
    We are in loop with  process id=2260   
    Came inside parent with process id=2260   
    We are out of loop with  process id=2260   
    Came inside child with process id=2265   
    We are out of loop with  process id=2265   

首先我们要考虑要执行3次的循环 每次 fork 都会产生 1 个 child 并且肯定会有一个 parent 并且执行将完全 依赖于调度可以是 child 也可以是 parent 。

一旦你有更多的迭代值,这将变得非常令人头疼。

Now let the child be cleaned

    int main(void)   
            {  
                int index;  
                for (index = 1; index < 4; index++)  
                {
                    printf("We are in loop with  process id=%d\n",getpid());
                    if(!fork()) { //Child process
                       printf("Came inside chid with process id=%d\n",getpid());
                       _exit(0);
                    } else {     //Parent process
                      printf("Came inside parent with process id=%d\n",getpid());
                   }
                }
                printf("We are out of loop with  process id=%d\n",getpid());
                exit(0);
            }

    We are in loop with  process id=2393
Came inside parent with process id=2393
We are in loop with  process id=2393
Came inside chid with process id=2394
Came inside parent with process id=2393
We are in loop with  process id=2393
Came inside parent with process id=2393
We are out of loop with  process id=2393
Came inside chid with process id=2396
Came inside chid with process id=2395

所以上面的 OP 现在更清楚了 3 次 parent 和 3 次 child execution 。

关于c - 使用 fork 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15173883/

相关文章:

c - 在 Linux 中实现零页的最快方法

c++ - Windows API,__declspec(thread) vs CreateThread?

c++ - 为什么我应该在 scanf() 系列成员中加入一个长度修饰符作为参数?有什么好处?带有长度修饰符的 scanf 有什么作用?

c - 它是如何制作从 poweron 到用户界面的 digicoder vcr dvd 播放器图形用户界面的?

c - 宏参数不会接受传递的参数(nvcc)

c - 头文件中的值结构导致 "duplicate symbol"链接器错误

c - fclose 返回值检查

c - 所有对 struct doxygen 的引用列表

c - 字符串数组 : Segmentation fault(core dumped)

c - 为什么 c 中的整型变量在将字符值传递给它后会打印其旧值?