Process Control in Linux-Use of exec function family

Source: Internet
Author: User

The exec function can replace the current process with a new process. The new process is specified by path or file. You can use the exec function to switch the execution of the program from one program to another, after the new program is started, the original program will no longer run.

# Include <unistd. h>

 

Char ** environ;

 

Int execl (const char * path, const char * arg0,..., (char *) 0 );

Int execlp (const char * file, const char * arg0,..., (char *) 0 );

Int execle (const char * path, const char * arg0,..., (char *) 0, char * const envp []);

 

Int execv (const char * path, char * const argv []);

Int execvp (const char * file, char * const argv []);

Int execve (const char * path, char * const argv [], char * const envp []);

 

These functions are divided into two categories. The number of parameters for execl, execlp, and execle is variable, and the parameter ends with a null pointer.

The second parameter of execv and execvp is a string array. Both parameters in the argv array are passed to the main function when the new program starts.

The above five functions are usually implemented by the execve function.

The function ending with P searches for the PATH environment variable to find the path of the executable file of the new program. If the executable file is not in the path defined by path, you need to pass the absolute path file name, including the directory, as a parameter to the function.

The execle and execve functions can pass the string array through the envp parameter as the environment variable of the new program.

 

The following is an example program:

# Include <unistd. h>

# Include <stdio. h>

# Include <stdlib. h>

 

Int main ()
{
Printf ("running ls with execl/N ");
Execl ("/bin/ls", "ls", "-l", null );
Printf ("done./N ");
Exit (0 );
}

 

Save as pexec. c

Compile: gcc-O pexec. c

Then run:./pexec

The following results are obtained on the author's computer:

 

Running ps with execl
Total 36

-Rwxr-XR-x 1 Deng 8371 2010-08-13 21:06 pexec
-RW-r -- 1 Deng 177 2010-08-13 20:11 pexec. c
-RW-r -- 1 Deng 170 2010-08-13 07:54 pexec. C ~
-Rwxr-XR-x 1 Deng 8374 2010-08-13 07:35 system1
-RW-r -- 1 Deng 143 2010-08-13 07:35 system1.c

 

The above is the output result of the normal LS command, but the string done does not appear at all, indicating that the current program has been replaced by the execl function.

 

Another example:

# Include <unistd. h>
# Include <stdio. h>
# Include <stdlib. h>

Int main ()
{
Printf ("Running ps with execlp/N ");
Execlp ("Ps", "Ps", "ax", null );
Printf ("done./N ");
Exit (0 );
}
This example program is used to print the current process and save it as pexecp. C.

Compilation: gcc-O pexecp. c

Run:./pexecp

 

The results of my computer operation are as follows:

Running ps with execlp
PID tty stat Time Command
1? SS 0: 00/sbin/init
2? S <0: 00 [kthreadd]
3? S <0: 00 [migration/0]
4? S <0: 00 [ksoftirqd/0]
5? S <0: 00 [watchdog/0]
6? S <0: 00 [migration/1]
7? S <0: 00 [ksoftirqd/1]
8? S <0: 00 [watchdog/1]
9? S <0: 00 [events/0]
10? S <0: 00 [events/1]
11? S <0: 00 [cpuset]
12? S <0: 00 [khelper]

.................. There are many more in the middle, which are omitted here

 

4698? SL/usr/lib/firefox-3.6.8/Plugin-container/usr/lib/flas
4787? S 0: 01 gedit/home/Deng/workspace/linux_test/chapter11/pexec
4975? S 0: 00/usr/lib/gvfs/gvfsd-computer -- spawner: 1.9/org/GTK/
4997? RL 0: 00 gnome-Terminal
4998? S 0: 00 gnome-Pty-helper
4999 pts/0 SS 0: 00 bash
5106 pts/0 r + PS ax

 

Here we can see that the process PS ax started using the execlp () function in the program is equivalent to the ps ax command in shell, with the same effect.

However, we still do not see the last done being printed.

 

In general, the exec function will not return unless an error occurs. When an error occurs, the exec function returns-1 and sets the error variable errno.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.