Linux.com

mlfever

mlfever

  • Linux.com Member
  • Posts: 1
  • Member Since: 19 Nov 09
  • Last Logged In: 20 Nov 09

Latest Posts

Posted by
Topic
Post Preview
Posted
  • mlfever
    about fork()
    i am learning the kernel,the fork() ,i have some questions,and read some from the net,but not very know about it the source: #include #include #include void main() { int i; int p_id; printf("aaa"); p_id=fork(); if(p_id==0) { for(i=1;i<3;i++) { printf("this is child process\n"); } } if(p_id==-1) { printf("fork new process error!\n"); } if(p_id==0) { printf("iii"); for(i=1;i<3;i++) { printf("this is parent process\n" ); } } printf("bbb"); } yanj@yanj-desktop:~/lianxi/chapter2$ ./forktest aaathis is child process this is child process iiithis is parent process this is parent process I know fork() copy the two same program to child,parent process, why can show "aaa",cannot "bbb"
    Link to this post 20 Nov 09

    i am learning the kernel,the fork() ,i have some questions,and read some from the net,but not very know about it
    the source:

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

    void main()
    {
    int i;
    int p_id;
    printf("aaa");
    p_id=fork();
    if(p_id==0)
    {
    for(i=1;i<3;i++)
    { printf("this is child process\n"); }
    }
    if(p_id==-1)
    {
    printf("fork new process error!\n");
    }
    if(p_id==0)
    {
    printf("iii");
    for(i=1;i<3;i++)
    { printf("this is parent process\n" ); }
    }
    printf("bbb");
    }

    yanj@yanj-desktop:~/lianxi/chapter2$ ./forktest
    aaathis is child process
    this is child process
    iiithis is parent process
    this is parent process


    I know fork() copy the two same program to child,parent process,
    why can show "aaa",cannot "bbb"

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board