The exec family of Linux processes

Today, I also learned about the exec family functions of the Linux process (execl and execlp execv and execvp).
Here I also write my own understanding and summary.

1. Function:
You can run another program when running a program, halfway through it, or when it reaches a certain place in the program.

2. Header file:

 #include <unistd.h>

3. Prototype:

**1.execl**   int execl(const char *path, const char *arg, ...);
**2.execlp**  int execlp(const char *file, const char *arg, ...);
**3.execv**   int execv(const char *path, char *const argv[]);
**4.execvp**  int execvp(const char *file, char *const argv[]);

**5.execle**  int execle(const char *path, const char *arg,
                  ..., char * const envp[]);
**6.execvpe** int execvpe(const char *file, char *const argv[],
                  char *const envp[]);         

4. Parameters
**path:**Path name of the executable file
**arg:** The parameters of the executable program. The first parameter is the name of the executable file. There is no path and arg must end with NULL.
**file:** If the parameter file contains /, it will be regarded as a path name, otherwise, it will search for executable files in the directories specified by the PATH environment variable.

Fifth, the return value The
function of the exec function family will not return after successful execution, nor will it continue to execute below the call point in the original program. When the call fails, errno will be set and return -1, and then from the original program The call point is then executed.

1. First use the first prototype execl

 int execl(const char *path, const char *arg, ...);

For example, I want to print 5, 4, 3, 2, 1, and then run date to display the system time.
We directly enter date in Linux and press Enter will display the system time. But we want to run the program in demo.c and print 5.4.3.2.1 before running date. So we use execl.
First of all, we have to know the path of date
Insert picture description here
whereis + object enter to know that the path of
date is in /bin/date.

Code:
Insert picture description here
Run result:
Insert picture description here
Note: After our execl is successful, we will not continue to run the following after execl. If unsuccessful, the following will be executed.

But we will find that every time we run another program we need to use whereis to find the path. If we write date without writing the path, the result is as follows:

Insert picture description here
Operation result:
Insert picture description here
Obviously, our execl is not found.

But we can do it by adding a p and using another prototype. That is execlp

**2.execlp** int execlp(const char *file, const char *arg, ...);

Code:
Insert picture description here
Running result:
Insert picture description here
You can use execlp without adding absolute path.
One more p than execl can find the executable file date
PAYH through the environment variable PATH ?
PATH refers to environment variables. Use the command echo PATH to view the current environment variables. If you need to modify it, you can add it later. For example, if you want to add / etc / apache 2 / bin as an environment variable, you can write it as: PATH = PATH to view the current environment variable. If you need to modify it, you can add it later. For example, if you want to add /etc/apache2/bin as an environment variable, you can write it as: PATH=P A T H i.e. can check to see if the front ring environment variable amount ,As if required to repair the change may be to the rear surface of the intake line chase added . Ratio as like the / E T C / A P A C H E 2 / B I n- add was added for the ring environment variable amount ,Can be written as:PATH= PATH;/etc/apache2/bin and then press Enter.
We can configure environment variables ourselves and useexport

Current environment variables:
Insert picture description herecurrent path:

Insert picture description here
export splicing the two:
Insert picture description here
now the environment variables:
Insert picture description here
spliced ​​together.

Run the program without the ./ name and press Enter:
Now you can run executable programs in other paths under other paths.
Insert picture description here

**3.execv** int execv(const char *path, char *const argv[]);

In fact, this is almost the same as the above is to wrap the following parameters and put them in the char* array.
Code:
Insert picture description here
Run results:
Insert picture description here

**4.execvp** int execvp(const char *file, char *const argv[]);

This is one more P than the above. In fact, it is still not necessary to write the absolute path.

Code:
Insert picture description here
Run results:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47457689/article/details/107692424