hello friends :)

well can anybody tell me why does this code

for (i = 0; i < 10; i++){
    childpid = fork();

    if (0 == childpid){  /* This is the line that is different in parts A and B. */
        execlp("some_prog", "some_prog", (char *)0);
       
        exit(2);
    }
    else if (childpid < 0){
        fprintf(stderr, "Error with fork.\n");
        exit(1);
    }

create children from the same parent ( the main method i assume)

and this code:

for (i = 0; i < 10; i++){
    childpid = fork();

    if (childpid > 0){  /* This is the line that is different in parts A and B. */
        execlp("some_prog", "some_prog", (char *)0);
        fprintf(stderr, "Error with exec.\n");
        exit(2);
    }
    else if (childpid < 0){
        fprintf(stderr, "Error with fork.\n");
        exit(1);
    }
}

Create this a child that will create a child and we will have a relation like this

Parent-->Child-->Child-->Child-->....and so on.

thank you :)

If you google the fork() command you'll learn why.

t_pid pid;

pid = fork();

if (pid > 0) {
  printf( "I am the parent. My child's PID is %u\n", pid );
  }
else if (pid == 0) {
  puts( "I am the child." );
  }
else {
  puts( "I am the parent. I lost my fork." );
  }

Now you can look back and see what the difference is.

Hope this helps.

i did google it ...i know it return either 0 for the child...-1 error and childip for the parent..but still i cannot know why .. can u help?

you need to read the description for fork()

On success, the PID of the child process is returned in
the parent's thread of execution, and a 0 is returned in
the child's thread of execution. On failure, a -1 will be
returned in the parent's context, no child process will be
created, and errno will be set appropriately.

The second program you posted never checks for existance of the child so it just continues to create children of children etc. My guess is that when i == 0 the program will create 9 * 8* 7 * 6 ... *1, or 9! children.

execlp("some_prog", "some_prog", (char *)0);

This is because of the exec functions replace the program running in a process with another program.
When a program calls an exec function, that process immediately ceases executing that
program and begins executing a new program from the beginning, assuming that the
exec call doesn’t encounter an error.

So now if you consider the firstcase:

for (i = 0; i < 10; i++){
    childpid = fork();

    if (0 == childpid){  /* This is the line that is different in parts A and B. */
        execlp("some_prog", "some_prog", (char *)0);
       
        exit(2);
    }
    else if (childpid < 0){
        fprintf(stderr, "Error with fork.\n");
        exit(1);
    }

Here exec function is called in the child process, so whenever a new child is created in the for loop, that child goes on to run as new program("some prog") and will not return unless it encounters an error and the for loop is being run in the parent process.

Where as in the second case:

for (i = 0; i < 10; i++){
    childpid = fork();

    if (childpid > 0){  /* This is the line that is different in parts A and B. */
        execlp("some_prog", "some_prog", (char *)0);
        fprintf(stderr, "Error with exec.\n");
        exit(2);
    }
    else if (childpid < 0){
        fprintf(stderr, "Error with fork.\n");
        exit(1);
    }
}

Here exec function is called in the parent process, so parent goes on to run new program ("some prog") and the child will continue the loop.

Hope you got a better understanding of the issue at hand.

Happy Coding,
/Sukhmal

This article has been dead for over six months. Start a new discussion instead.