简体   繁体   中英

Fork Program Output

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

int main(){
    int i;
    for(i=0;i<2;i++){
        fork();
        printf("%d\n", i);
    }
    return(0);
}

I am a little confused on the way fork works, to my understanding fork duplicates the memory including the stack, heap, static data, and text. I thought the program would return: 0 0 1 1 1 1 But I compiled and ran the program and the output is as follows: 0 1 0 1 1 1 Why is this?

fork返回两次的同时 ,即在同一时间

输出将是随机的,因为最终答案取决于在哪个时间完成哪个过程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM