-3
#1 code
    int child_pid;
    for (int i = 0; i < 3; i++)
    {
    child_pid = fork();
    if (child_pid == 0) 
    { sleep(10); }
    else
    { wait(&status); }
    }
    execlp("sleep","sleep","20",(char*)NULL);

#2 code
    int child_pid;
    for (int i = 0; i < 5; i++)
    {
    child_pid = fork();
    if (child_pid == 0) 
    { sleep(1); }
    else
    { wait(&status);
      execlp("sleep","sleep","20",(char*)NULL); }
    }

Can someone explain to me what is happening in these codes? I understand fork() returns a zero to a newly created child process. That's why I don't understand why in #2 code the else branch is also being executed? Execlp replaces the calling process image with a new process image, but what does this really mean?

EDIT: The answer is that the first code will create 8 processes, but I'm not able to understand how? And why will the first code finish 230 seconds? How does exec affect this?

The second code will create 5 processes, but it will finish after (5*2sec+5*20sec). In the second code why does it go to the else branch?

I'm trying to understand how many processes are created as well as why and how long the code will sleep?

Thanks in advance.

ponikoli
  • 25
  • 6
  • 1
    Please format your code correctly – Jabberwocky Apr 18 '18 at 08:05
  • Possible duplicate of [The difference between fork(), vfork(), exec() and clone()](https://stackoverflow.com/questions/4856255/the-difference-between-fork-vfork-exec-and-clone) – Kami Kaze Apr 18 '18 at 08:06
  • "That's why I don't understand why in #2 code the else branch is also being executed?" What do you expect happens to the calling process? – Gerhardh Apr 18 '18 at 08:14
  • Because I expect fork() to return 0, so when does it go the else branch. I've edited my question to clarify – ponikoli Apr 18 '18 at 08:21
  • _"I'm trying to understand how many processes are created as well as why and how long the code will sleep?"_ so run it? or read enough documentation that you can step through the code in your mind and figure it out? IMO, just as SO is not a code-writing service, it should not be a code-explaining service either. – underscore_d Apr 18 '18 at 08:23
  • You should do your execlp into your if (when the pid is equal to 0), and not in the if (parent process, when pid > 0). – Paul-Marie Apr 18 '18 at 08:35
  • You correctly mentioned that `fork` returns "zero to a newly created child process". The caller is not a newly created child process. The caller also returns from `fork` and does not get a zero returned. That is the way to determine if your code that follows `fork` is executed in parent or child process. – Gerhardh Apr 18 '18 at 08:43

2 Answers2

2

fork will return two times, one in the original process, with the return value of the pid of the child process, the other in the child process, with the return value of zero

John Stone
  • 86
  • 3
0

DESCRIPTION

fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.

and:

RETURN VALUE

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

So, after to fork(), you have your newly created process, so you have 2 process, the child (the new process) and the parent (the initial process).

So, what your child (pid == 0) is going into the sleep when you parent (pid > 0) is executing your sleep.

After doing an exec* (execl, execlp, execv, execve, etc...) your process is terminated (try to place a printf or whatever AFTER your execlp, you will see it will never print it (except in case of error (execlp's return value == -1)).

To answer to your initial question: Yes, your program pass in your if AND in your else (put printf to see clearer), because you have in fact 2 process, and you can even try to put a printf after the else statement, at the very end of your program, and you will see that the child pass (print his pid, it should be equal to 0).

Paul-Marie
  • 874
  • 1
  • 6
  • 24