Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does system() exactly work in linux?

Tags:

c

linux

I've been reading its man page but haven't yet been successful in figuring out how it works. On calling system(), is a new child process forked and the shell binary exec()-ed in it? That may be a stupid guess though.

like image 521
user108127 Avatar asked May 16 '09 11:05

user108127


People also ask

How does the system command work in Linux?

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

How does system () work in C?

In the C Programming Language, the system function allows a C program to run another program by passing a command line (pointed to by string) to the operating system's command processor that will then be executed.

How does system command work?

System() Function in C/C++It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib. h> or <cstdlib> should be included to call this function.

What shell does system () use?

On any POSIX OS (including Linux), the shell used by std::system is /bin/sh .


2 Answers

Yes, system() is essentially a fork() and exec() "sh -c" for the passed command string. An example implementation (from eglibc, recently forked from glibc) can be found here.

like image 199
Lance Richardson Avatar answered Oct 06 '22 01:10

Lance Richardson


Yes, system("foo bar") is equivalent to execv("/bin/sh", ["sh", "-c", "foo bar"]).

like image 31
daf Avatar answered Oct 05 '22 23:10

daf