Welcome to the Linux Foundation Forum!

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++)<br />
{ 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++)<br />
{ 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"

Comments

  • I dunno help you with your problem but learn to use the BB codes. The code you put into bb code, then it would be readable, dude.
  • Posts: 501
    You have two blocks that are testing for a child process and none that check for parent process. Try this:
    1. if(p_id==0)
    2. {
    3. for(i=1;i<3;i++)
    4. { printf("this is child process\\n"); }
    5. }
    6. else if(p_id==-1)
    7. {
    8. printf("fork new process error!\\n")
    9. }
    10. else
    11. {
    12. for(i=1;i<3;i++)
    13. { printf("this is parent process\\n" ); }
    14. }

    Cute! The code processor strips out the backslashes in the code example! Let's try doubling them to see if they display correctly...
    Ok, that works. Note to forum moderator(s) - the code block processor is incorrectly stripping out new-line (backslash-n) in C code. Not good.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training