How many ways can we create a process in linux using c

I have a confusion about creating a process on Linux. So far, I thought we could create a process by calling the following system calls.

  • system()
  • exec() system call family
  • fork() system call

a

  • system (): But since the "system" system call executing the input executable on the shell and the shell creates a child process to execute the input .here shell calls the child process, then we can say that fork creates a process for this.

  • exec is a family of system calls . Because this family of system calls overwrites the currently running process with the new process. Thus, it also creates a new process, but uses the same address space. As I think it also calls a call fork to create a process.

I am confused by the fact that all of the above is a possible way to create a new process or just a fork system.

+4
source share
5 answers

execthe system call family does not call fork, and it does not create a new process. It only overwrites the existing process with the new binary.

linux fork - . fork clone .

system fork exec. fork system. system .

+7

fork() . POSIX, Linux. , fork() - .

, exec() , (, , fork()) , exec() , fork(), , .

system() fork(), exec(), .

+3

POSIX fork - . .

exec ( exec()).

system() fork(), exec().

+3

: fork clone.

:

  • exec() family: . exec() fork clone, . a bash gcc, , forks, bash gcc, exec().

  • system() family: fork/clone exec(), , , stdin stdout ..

, fork(), clone(), exec(), system() .. - , C ( ), . , counterintuitively, fork() clone . , . C , .

, fork - . , : ( , ) , - , exec. , fork . - clone, , , , , pthreads clone.

+1

, fork (2) ( vfork (2)...) clone (2) syscalls ( , execve (2) syscall , , ), "" , :

  • /sbin/init ( , , /bin/sh....); pid 1 at is ...

  • ( ) , kswapd, kworker (. ) .. 50

  • Linux , hotplug (8), modprobe .. . udev ....

  • fork ( clone...) /sbin/init ( pid 1). ( modprobe hotplug , fork ).

( fork ..) . ( , cd ulimit...); clone ( fork...)

, system (3), popen (3) - ( , syscalls (2)...), fork, execve ( /bin/sh...) daemon (3) - , fork () ..

strace (1) ( , ) Linux

Libc clone , fork ( syscall fork, clone); libc, MUSL libc ( ) GNU libc

+1
source

All Articles