Difference between “system” and “exec” in Linux?

前端 未结 12 1884
天命终不由人
天命终不由人 2020-11-28 05:18

What is the difference between system and exec family commands? Especially I want to know which one of them creates child process to work?

相关标签:
12条回答
  • 2020-11-28 05:31

    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.

    0 讨论(0)
  • 2020-11-28 05:32

    int system(const char *cmdstring);
    

    Ex: system("date > file");


    In general, system is implemented by calling fork, exec, and waitpid, there are three types of return values.

    • If either the fork fails or waitpid returns an error other than EINTR, system returns –1 with errno set to indicate the error.
    • If the exec fails, implying that the shell can't be executed, the return value is as if the shell had executed exit(127).
    • Otherwise, all three functions—fork, exec, and waitpid—succeed, and the return value from system is the termination status of the shell, in the format specified for waitpid.

    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,


    int execl(const char *pathname, const char *arg0, ... /* (char *)0 */ );
    
    int execv(const char *pathname, char *const argv []);
    
    int execle(const char *pathname, const char *arg0, .../* (char *)0, char *const envp[] */ );
    
    int execve(const char *pathname, char *const argv[], char *const envp []);
    
    int execlp(const char *filename, const char *arg0,... /* (char *)0 */ );
    
    int execvp(const char *filename, char *const argv []);
    

    0 讨论(0)
  • 2020-11-28 05:38

    system() will invoke your systems default command shell, which will execute the command string passed as an argument, that itself may or may not create further processes, that would depend on the command and the system. Either way, at least a command shell process will be created.

    With system() you can invoke any command, whereas with exec(), you can only invoke an executable file. Shell scripts and batch files must be executed by the command shell.

    Basically they are entirely different used for different purposes. Moreover exec() replaces the calling process, and does not return. A more useful comparison would be between system() and spawn(). While system may be simpler to invoke, it returns a value that tells you whether the command shell was invoked, and tells you nothing about the success of the command itself. With spawn() you can get the process's exit code; by convention non-zero is used to indicate error conditions. Like exec() spawn() must invoke an executable, not a shell script or built-in command.

    0 讨论(0)
  • 2020-11-28 05:43

    The exec function replace the currently running process image when successful, no child is created (unless you do that yourself previously with fork()). The system() function does fork a child process and returns when the command supplied is finished executing or an error occurs.

    0 讨论(0)
  • 2020-11-28 05:44

    system() calls out to sh 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. With exec(), 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 combination fork(), exec(), and wait().

    To answer your final question, system() causes a child process to be created, and the exec() family do not. You would need to use fork() for that.

    0 讨论(0)
  • 2020-11-28 05:46

    To create a process:

    • fork(2), a system call directly to the kernel

    To execute a program, replacing the current image:

    • execve(2), a system call directly to the kernel, usually just called exec

    To wait for a child process to finish:

    • wait(2), a system call directly to the kernel

    To run a program in a shell in a child process and wait for it to finish:

    • system(3), a library function

    To get the man pages for all of the above:

       $ man 2 fork execve wait
       $ man 3 system
    
    0 讨论(0)
提交回复
热议问题