Friday, July 22, 2022

[FIXED] What are the ways to create a new process in C?

Issue

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.


Solution

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.



Answered By - Amit Sides
Answer Checked By - Senaida (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.