A little bit of eye
Exc replaces the program that calls it with the exec uted program (the new program). Relative to the fork function, a new process is created and a new PID is generated. Exc starts a new program to replace the current process, and the PID is unchanged.
Reference to the usage of exec function family: https://blog.csdn.net/amoscykl/article/details/80354052
The following is the practical application of execl() function in the family of functions.
2. Using execl to execute the command program pwd without options
1 code
#include <unistd.h> int main(int argc, char* argv[]) { // Execute pwd in / bin directory, note that argv[0] must have execl("/bin/pwd", "asdfaf", NULL); return 0; }
2 operation
[root@localhost test]# g++ test.cpp -o test [root@localhost test]# ./test /root/C++/ch05/5.5/test
3 description
After the program runs, it prints the current path, which is the same as the PWD command. Although the PWD command does not have options, when execl is used to execute it, the argv[0] parameter is still required. If "asdfaf" is removed, an error will be reported. However, this kind of scribbling seems not good to look at. Generally, it is the name of the writing command, such as execl("/bin/pwd", "pwd", "NULL);.
3. Executing command programs with options using execl
1 code
#include <unistd.h> int main(void) { //ls in the execution / bin directory //The first parameter is the program name ls, the second parameter is - al, and the third parameter is / etc/passwd. execl("/bin/ls", "ls", "-al", "/etc/passwd", NULL); return 0; }
2 operation
[root@localhost test]# g++ test.cpp -o test [root@localhost test]# ./test -rw-r--r--. 1 root root 1110 Mar 3 16:01 /etc/passwd
3 description
passwd is a file under etc. You can view it directly under the shell with the ls command
[root@localhost test]# ls -la /etc/passwd -rw-r--r--. 1 root root 1110 Mar 3 16:01 /etc/passwd
In the program, the second parameter of execl (relative to argv[0]) is actually useless, even if we enter a string casually, the effect is the same.
For execl functions, you only need the full path of the provider and the parameter information starting with argv[1].
Fourth, execl is used to execute programs written by ourselves
1. Write our own programs
#include <string.h> using namespace std; #include <iostream> int main(int argc, char* argv[]) { int i; cout <<"argc=" << argc << endl; //Print the number of parameters passed in for(i=0;i<argc;i++) //Print each parameter cout<<argv[i]<<endl; if (argc == 2&&strcmp(argv[1], "-p")==0) //Determine if the parameter - p is present cout << "will print all" << endl; else cout << "will print little" << endl; cout << "my program over" << endl; return 0; }
2. Compile and run our own programs
[root@localhost test]# g++ mytest.cpp -o mytest [root@localhost test]# ./mytest argc=1 ./mytest will print little my program over
3. Call the program we wrote through execl
#include<unistd.h> using namespace std; #include <iostream> int main(int argc, char* argv[]) { execl("mytest",NULL); //No parameters are passed to mytest cout << "-----------------main is over" << endl; //If execl executes successfully, this line will not be executed return 0; }
4. Compile and run the program
[root@localhost test]# g++ test.cpp -o test [root@localhost test]# ./test argc=0 will print little my program over
When execl is invoked, no parameters are passed to mytest, so 0 is printed, which means that argv[0] can not be passed when executing the program written by oneself, which is different from executing the system command.
5. Excl passes in two parameters to call the program we wrote
[root@localhost test]# cat test.cpp #include<unistd.h> using namespace std; #include <iostream> int main(int argc, char* argv[]) { execl("mytest","two params","-p",NULL); cout << "-----------------main is over" << endl; return 0; }
6. Compile and run
[root@localhost test]# g++ test.cpp -o test [root@localhost test]# ./test argc=2 two params -p will print all my program over