Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - meaning of wait(NULL) when executing fork() in parallel

In the code below, do the forks actually run in parallel or one after another?

What is the meaning of wait(NULL) ?

(The program creates an n number of child processes, n is supplied via command line)

int main ( int argc, char *argv[] ) {
    int i, pid;

    for(i = 0; i < atoi(argv[1]); i++) {
        pid = fork();
        if(pid < 0) {
            printf("Error occured");
            exit(1);
        } else if (pid == 0) {
            printf("Child (%d): %d\n", i + 1, getpid());
            exit(0); 
        } else  {
            wait(NULL);
        }
    }
}
like image 229
YemSalat Avatar asked Oct 21 '14 22:10

YemSalat


People also ask

What does wait NULL do in C?

wait(NULL) will block the parent process until any of its children has finished. If the child terminates before the parent process reaches wait(NULL) then the child process turns to a zombie process until its parent waits on it and its released from memory.

When a process calls fork What is the return value the process should receive on success?

RETURN VALUE Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.

How does a fork work in C?

fork() in C. Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

What is the purpose of fork function give the syntax and explain with a program?

In the computing field, fork() is the primary method of process creation on Unix-like operating systems. This function creates a new copy called the child out of the original process, that is called the parent. When the parent process closes or crashes for some reason, it also kills the child process.


Video Answer


1 Answers

They do run in parallel, up until the point that one of them waits.

wait(NULL) or more accurately wait(0) means wait until a state change in the child process. To find out about Unix / Linux C api calls, type man <function Name> on the command line. You'll need to get used to reading those pages, so better start now.

In your case

man wait

would have given you what you needed.

Since you've only fork(...)ed once, you have only one child. The parent waits until it changes state, and since the child's state during the fork is the same as the parent's state prior to the fork (that of running), the likely outcome is that the parent waits until the child dies. Then the parent will continue executing, but since it doesn't have much to do after the wait(...) call, it will quickly exit too.

like image 145
Edwin Buck Avatar answered Oct 16 '22 08:10

Edwin Buck