Ladies and gentlemen, crossing, the last time we were talking about the "memory details when creating threads" example, this time we say the example is: EXEC series functions. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!
Crossing, we mentioned the exec series functions earlier in the chapter on creating process memory details, which was not described in detail at the time. Today we are going to look at the Exec series functions with you.
The EXEC Series function can import a process into the process calling the Exec series function, and replace the current process with a new one, and in the previous chapter we also introduced that the EXEC series function triggers a write-time copy, so the new process has its own code area, data area, heap area, and stack area.
Let's start by selecting a function from the EXEC series function to give you an intuitive impression. Here is a function EXECLP in the EXEC series function, whose function prototype is as follows:
...)
- The first parameter in the function is the file name, which indicates the executable file name or the script file name;
- The second parameter in the function is the argument passed to the executable file, which is passed to the argv[of the main function in the executable file];
- The number of arguments in the function is variable, but the last parameter is always a null pointer;
- When the function runs successfully, there is no return value, and 1 is returned when the error is run.
Next, we use the actual code to illustrate how the function is used. Here is the detailed code, please refer to:
#include <unistd.h>#include <stdio.h>intMain () {pid_t pid; pid_t Pid_res;intStat_value; PID =Fork();if(PID >0) {printf("PID: %d -Father Process is running \ n", Getpid ()); Pid_res =wait(&stat_value);if(Pid_res >0) {printf("Son Process finished:pid = %d \ n", pid_res); } }Else if(PID = =0) {printf("PID: Son Process is running \ %d ", Getpid ()); EXECLP ("echo","echo","Hello", NULL);//Run the EXECLP function}Else{printf("Create process failed \ n");return 1; }return 0;}
The above code creates a child process by fork and then executes the EXECLP function in the child process. Let's focus on the use of the EXECLP function.
The first argument passed to the function is the executable file name Echo, and the second and third parameters are echo and hello. Both parameters are passed to the argv[of the executable file (this is the Echo) main function). The last parameter is a null pointer, which is also passed to argv[]. At this point, let's look at what's in argv[].
- The content in argv[0] is "echo";
- ARGV[1] is "Hello" in the content;
- The content in argv[2] is "NULL", which is the command end flag.
Executable file has, the parameters of the executable file also have, really everything is ready, only owe East wind AH. Crossing, the east wind, the EXECLP function is the east wind, it will go to the system environment variable to find the executable file "echo", and the parameters in argv[] passed to it, and then run the executable file.
The following is the results of the operation of the program, please refer to:
4194Processis4195Processis running hello process4195
We can also run the echo command separately in the terminal, the following is the result of running the command:
echo‘hello‘ //在终端中单独运行echo命令hello //显示命令运行的结果
From the results above, we can see that it is consistent with the running results of the program just now.
You crossing, here's an example of the EXEC series function. I want to know what the following example, and listen to tell.
Talk C Chestnut bar (135th back: C language Instance--exec series function one)