I wonder are there any other ways to create a new process in C.
I know about fork
which creates a child process.
Also, with exec
I can replace the current executing program, but yet it does not create a new process.
Asked
Active
Viewed 834 times
0

vanderwaal
- 336
- 1
- 3
- 10
-
Exec doesn't *create* a process. `posix_spawn()` and `vfork()` come to mind. Linux also has `clone()`, Windows has `CreateProcess()`... – Shawn Jan 19 '22 at 06:41
-
There's also `system`. But unlike the others, it's not a system call. – ikegami Jan 19 '22 at 06:43
-
`exec` doesn't replace the process; it basically replaces the *program* being executed by the current process. – ikegami Jan 19 '22 at 06:44
-
Some ways have shared [here](https://stackoverflow.com/a/1698608/8328237). You can read other answers of the [question](https://stackoverflow.com/questions/1697440/difference-between-system-and-exec-in-linux). – karaketir16 Jan 19 '22 at 07:01
-
What are you trying to do, that you cannot do with `fork`, `clone` (fine tuned fork) and the `exec` (family)? – Erdal Küçük Jan 19 '22 at 09:47
-
Thank you all, guys. I don't have a particular task. I was just interested. – vanderwaal Jan 21 '22 at 18:40
1 Answers
1
As far as I'm aware only fork
, vfork
and clone
are used to create new processes in linux
. (posix_spawn
is not a syscall, but rather a library function which uses one of the syscalls above to create the process).
exec
(and its sibling functions) is used to "mutate" a process, changing its memory layout according to the given ELF file.
(the example below is a very simplified version of the process, that leaves a lot of things out)
For example, when you run a command from your bash
terminal, it uses fork
(or clone). After the fork
syscall, a new process is created and its memory layout is exactly the same as your bash
terminal (To understand it better, read about Virtualization
& COW, Copy-On-Write
). Afterwards, the new process calls execv
(or a similar function) to execute the ELF given to it by the command.

Amit Sides
- 274
- 2
- 6