1. Execute a new program
In Linux, execute a new program using the exec function. This function searches for the execution file in the file system and copies the file content to the address space that calls the exec function to replace the content of the original process, that is, data that is rewritten from the parent process is overwritten.
# Include <unistd. h>
Int execl (const char * pathname, const char * arg0 ,......);
Int execle (const char * pathname, const char * arg0 ,...... /* Char * const envp [] */);
Int execv (const char * pathname, char * const argv []);
Int execve (const char * pathname, char * const argv [], char * const envp []);
Execlp (const char * filename, const char * arg0 ,......);
Int execvp (const char * filename, char * const argv []);
Here, l indicates list, indicating that the parameters are provided in the form of a list and end with null, but there is no limit on the number of parameters.
Eg:
Char * arg0, char * arg1, char * arg2, null
V indicates the vecter, which is provided in the form of a two-dimensional array of parameters. Each row of this array is a parameter
E indicates the list of environment variables passed to the program. This is a two-dimensional array. Each row of this array is an environment variable. If explicit transfer is not performed, the new program copies the environment table of the parent process.
Ending with P indicates that the first parameter is not a complete path name, but a program name. In this way, Yuyao extracts each item from the PATH variable in the Environment table and combines the first parameter into a complete path.
Eg:
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
Int main (void)
{
Pid_t PID;
PID = fork ();
If (PID <0 ){
Printf ("fail to fork/N ");
Exit (1 );
} Else if (pid = 0 ){
If (execvp ("ls", null) <0 ){
Printf ("fail to Exec/N ");
Exit (0 );
}
/* It should never be executed here, because it has been replaced by the LS program code segment */
Printf ("the child is not Hello/N ");
Exit (0 );
}
Printf ("the parent/N ");
Return 0;
}
Result:
Alei @ Alei-desktop :~ /Linux/code/chapter_11 (done) $ GCC exec. C-o Exec
Alei @ Alei-desktop :~ /Linux/code/chapter_11 (done) $./exec
The parent
Err_vfork exec. c fork. c nozombie. c vfork. c zombie. c
Err_vfork.c exec. C ~ Mul_process.c system. c wait3.c
Exec fork mul_process.c ~ Vfork wait. c
2. Execute the interpreter File
The interpreter file is a text file. The initial form of this file is:
#! Parameters required by the interpreter program's path interpreter program
For example, the shell script starts:
#! /Bin/sh
If the program is a general program. This interpreter can only contain the first line
For example, to run the test file, the interpreter file can only have one row
#! /Home/demo/test
Using the exec function to execute this script is the same as inputting "/home/demo/test" in shell.
The following is an example:
(1) Compile the following program:
// Echoargs. c
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Int I;
For (I = 0; I <argc; I ++ ){
Printf ("argv [% d]: % s/n", I, argv [I]);
}
Return 0;
}
(2) Compile the program
GCC echoargs. C-o echoargs
(3) Check the executable file server in the/home/demo/directory.
CP echoargs/home/demo
(4) edit the interpreter script and save it as interp. Sh.
#! /Home/demo/echoargs testarg
# Interpreter file interp, out of the first line, all other lines are comments
# The second parameter is a test parameter intentionally added, which has no effect on the program.
(5) change the document to executable.
Chmod U + x interp. Sh
(6) write a program that calls the script.
// Run_script.c
# Include <stdio. h>
# Include <unistd. h>
# Include <stdlib. h>
Int main ()
{
Pid_t PID;
PID = fork ();
If (PID <0 ){
Perror ("fail to fork ");
Exit (1 );
} Else if (pid = 0 ){
Execl ("/home/demo/interp. Sh", "interp", "arg1", "arg2", null );
Exit (0 );
}
Exit (0 );
}
(7) Compile the program
GCC run_script.c-O script
(8) run the program
Argv [0]:/home/Alei/demo/echoargs
Argv [1]: testarg
Argv [2]:/home/Alei/demo/interp. Sh
Argv [3]: arg1
Argv [4]: arg2
The first line is the path of the entire interpreter file, the second line is the useless parameters used for testing, and the subsequent call of exec is the passed parameter. It can be seen that the command line parameters passed to the new program in the exec function and the command line parameters in the interpreter file are used as the parameter ship song interpreter program. The order is that the command line parameters in the interpreter file are in the front, the command line parameters passed to the new program in the exec function are later.
3. execute shell commands in the program
In Linux, shell commands are called using system functions. The function prototype is as follows:
# Include <stdlib. h>
INT system (const char * character string );
Eg:
// System. c
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <fcntl. h>
# Deprecision Max 1024
Int main (void)
{
Int FD;
Char Buf [Max];
Int N;
If (system ("ls> temp.txt") =-1 ){
Perror ("fail to exec command ");
Exit (1 );
}
If (FD = open ("temp.txt", o_rdwr) =-1 ){
Perror ("fail to open ");
Exit (1 );
}
If (n = read (FD, Buf, max) =-1 ){
Perror ("fail to read ");
Exit (1 );
}
Buf [N] = '/0 ';
Printf ("% s", Buf );
Return 0;
}