What is the difference between system
and exec
family commands? Especially I want to know which one of them creates child process to work?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
exec() replaces the current running process with the process image of the function being performed..only executable files can be invoked using this.
system() forks off a new process implicitly to service the request and returns the value it obtained through the child process it forked initially.It uses the system's default shell to carry out the operation.
Ex:
system("date > file");
In general, system is implemented by calling fork, exec, and waitpid, there are three types of return values.
The fork function is to create a new process (the child) that then causes another program to be executed by calling one of the exec functions. When a process calls one of the exec functions, that process is completely replaced by the new program, and the new program starts executing at its main function. The process ID does not change across an exec, because a new process is not created; exec merely replaces the current process—its text, data, heap, and stack segments—with a brand new program from disk.
There are six different exec functions,
system()
calls out tosh
to handle your command line, so you can get wildcard expansion, etc.exec()
and its friends replace the current process image with a new process image.With
system()
, your program continues running and you get back some status about the external command you called. Withexec()
, your process is obliterated.In general, I guess you could think of
system()
as a higher-level interface. You could duplicate its functionality yourself using some combinationfork()
,exec()
, andwait()
.To answer your final question,
system()
causes a child process to be created, and theexec()
family do not. You would need to usefork()
for that.system() invokes the desired program or built-in command using a shell, this is an inefficient way because a shell is started before the program is started.
In the case of the exec family of system calls, a whole new image is being created, that is, they replace the current process with a new process specified by the path or file or whatever argument you are mentioning.
The thing to be kept in mind is that, when the exec family of system calls are used, the original program will no longer be running after the new one is started.
There are some significant differences between
exec(2)
andsystem(3)
that should be kept in mind.system()
returns to the caller, whereasexec()
replaces the existing code with the new image. This has been explained above.However, the not so subtle difference comes when you want to run a procedure and then return to your existing code, receiving the return code from the invoked procedure.
system()
does provide a return code, but the return code can only be used to detect an error condition, and cannot be used to recover a return code.One possible proper sequence of system calls is:
There are other subtleties to this sequence which can be determined by a careful reading of the relevant man pages, but this code will work fine in the absence of signals, multiple child processes, etc. Also, the inline declarations may limit the scope of the variables, but are included to allow this code to be used as a template that works (you may use a different coding style :-).
To create a process:
fork(2)
, a system call directly to the kernelTo execute a program, replacing the current image:
execve(2)
, a system call directly to the kernel, usually just calledexec
To wait for a child process to finish:
wait(2)
, a system call directly to the kernelTo run a program in a shell in a child process and wait for it to finish:
system(3)
, a library functionTo get the man pages for all of the above: