Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fork() why not infinite output

Tags:

c

fork

process

why does

int main(...) {  
    fork();  
    printf("hello again\n");  
    exit(0);  
}

not create an infinte number of processess? I understood it as follows, the main process creates a childprocess, the child an other child, and so on.

like image 542
user1324258 Avatar asked Jan 27 '13 20:01

user1324258


People also ask

What happens when we fork a process?

When a process calls fork, it is deemed the parent process and the newly created process is its child. After the fork, both processes not only run the same program, but they resume execution as though both had called the system call.

What happens if you fork without exec?

It creates a copy of the current process, allowing tasks to read the same memory, without needing fine-grained synchronization. Unfortunately, you need to use it extremely carefully in large programs because fork in a multi-threaded program can easily cause deadlocks in the child process.

How does forking affect the parent process?

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. The child process and the parent process run in separate memory spaces.


1 Answers

The execution continues after the fork in both the parent and the child, it doesn't restart the program.

like image 172
Femaref Avatar answered Sep 19 '22 14:09

Femaref