. For example, you can check the credentials of a user before starting another program that has a restricted usage policy. The EXEC function is more efficient than the system function because the original program is no longer running after the new program starts.#include 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 EXECLP (const char *file, const char *arg, ...);int execle (const char *path, const char *ARG,..., char * const envp[]);int execv (const char *path, char *const argv[]);int EXECVP (const char *file, char *const argv[]);int execvpe (const char *file, char *const argv[], char *const envp[]);where path is the full path name that includes the name of the running file. Multiple parameters. Note that the last parameter that follows must be Null,a
Running new code in the processThe EXECX series function can run a new program in the current child process. When a process invokes any function in the series, the process's user-space resources are completely replaced by the new program.The difference between these functions: Indicates whether the location of the new program uses a path or a file name, and if the file name searches for the program in the path described by the system's $PATH environment variable.How to use the parameter list or
succeeds, but the exit () function must be used to exit the child if the execution fails!
Here are the examples:
2.1 execv Function
int childpid;
int i;
if (fork () = = 0) {
//child process
char * execv_str[] = {"echo", "executed by Execv", NULL};
if (Execv ("/usr/bin/echo", Execv_str) Note the definition and assignment of the string pointer array
process, and then creates a new child process.
How to use:
The code is as follows
Copy Code
#./portmap the path of the program that needs to be monitored#args parameters required by Portmap$/supervisor./portmap args ..... The code is as follows: /**** Supervisor** Author:liyangguang (liyangguang@software.ict.ac.cn)** Date:2011-01-21 21:04:01** Changes* 1, Execl to EXECV*/#include #include #include #include #include #incl
command line parameters, so we have three command line parameters. This may be a little different from what you usually get used to. Be careful not to make a mistake.
Now let's look at the exec function family and focus on execve:
int execve(const char *path, char *const argv[], char *const envp[]);
Compare the complete form of the main function. Do you see the problem? Yes, the argv and envp in these two functions have a one-to-one relationship. The execve 1st parameter path is
1. Exec replacement process image
On Process Creation, Unix adopts a unique method, which separates Process Creation from loading a new process image. The advantage is that there is more room to manage the two operations. After we create a process, we usually replace the child process with a new process image. This can be done using exec functions. Of course, the exec series functions can also replace the current process.
2. Exec Associated Function Group
Include header file The exec function ca
define a qtextedit object to display the execution information, A custom mythread class object is defined to execute external programs. In this thread's run function, I call the fork and execv functions to execute external programs, redirects the standard output of the executable program to the pipeline, reads the information from the pipeline, and then transmits the information back to the main thread using the qapplication: postevent () function, t
addition, you can set some switches during compilation, so that the optimization during compilation is generally better than the optimization of running time.
However, one disadvantage of source code release is that it has high requirements on users. If you run configue and make command smoothly, it will be fine. If the problem persists, you must change the header file. In addition, the dependency between libraries is also very troublesome for manual processing. Fortunately, in the Linux World,
function in the sub-process, execute the program that needs to be automatically restarted. 3) execute wait in the parent process to wait for the completion of the Child process, and then create a new child process.
Usage:
#./Portmap the path of the program to be monitored # parameters required by ARGs Portmap$./Supervisor./Portmap ARGs .....
The Code is as follows:
/* * ** Supervisor ** Author: liyangguang (liyangguang@software.ict.ac.cn) ** Date: 21:04:01 ** changes * 1, execl to
exec function family.
Let yourself be reborn;
· If a process wants to execute another program, it can call the fork function to create a new process, and then
Call any exec, which looks like a new process is generated by executing the application. (In this case
Very common) (2) exec function family syntax
In fact, there is no exec () function in Linux, but there are 6 Function Families starting with exec.
There are nuances in the method, which will be explained in detail below. Header file requ
, the returned value of the sub-process is 0. fork creates a sub-process to completely replicate the parent process, and the buffer also copies the meta-process and the sub-capital are returned from the fork function, and continues the circle in each of them, however, the fork return value of the meta-function is the pid of the sub-process, while the fork returns 0 in the sub-process, and the return value-1 indicates that the creation failed.
# Include
2.3exec: the exec function uses the progr
is called once but returns two times. The only difference of two returns is that the child process ID is returned in the parent process with a value of 0.A child process is a copy of the parent process that obtains a copy of the parent process's data space, heap, stack, and so on. Note that the child process holds a "copy" of the above storage space, which means that the storage space is not shared between parent and child processes, and that only the code snippets are shared between them.Examp
);
int execle (const Char *path, const char *arg0, ..., (char*) 0, char *const envp[]);
int Execv (const Char *path, char *const argv[]);
int EXECVP (cosnt char *file, char *const argv[]);
int Execve (const Char *path, char *const argv[], char *const envp[]);
This type of function can be divided into two categories, execl, EXECLP, and execle parameters are mutable, ending with a null pointer, and the second argument of
MakefileDevelop a Makefile, employing variable definitions and automatic variables, to compile and link the project's files.Step 1. Execute Simple external commands1.execute commands without arguments, such as/bin/ls2.execute commands with arguments, such as/usr/bin/cal-yHelpful C99 and POSIX functions:fork (), EXECV (), exit (), wait ().The Functions:system (), Popen (), EXECLP (), or EXECVP () anywhere in your project.Step 2. Search PathThe global
0.Fork creates a child process that is fully replicating the parent process, and the buffer is also copiedboth the meta-process and the sub-capital are returned from the function fork and continue round in their respective circles, but the fork return value of the META functionis the PID of the child process, and the fork returns 0 in the child process, and 1 indicates that the creation failed#include 2.3exec functionThe EXEC function overwrites the existing process space with a program develop
1. Introduction
In Linux, there is no exec () function, exec refers to a set of functions, a total of 6, respectively:
#include
extern char **environ;
int execl (const char *path, const char *arg, ...);
int EXECLP (const char *file, const char *arg, ...);
int execle (const char *path, const char *arg, ..., char * const envp[]);
int execv (const char *path, char *const argv[]);
int EXECVP (const char *file, char *const argv[]);
int Execve (co
based on the zip package information we obtained in the previous step and the absolute path of the upgrade package. For later use.
⑥ Pipe (): creates an MPS queue for communication between the following sub-processes and the parent process.
7. Fork (): create a sub-process. The sub-processes are mainly responsible for executing binary (execv (binary, argS), that is, executing our installation Command Script ), the parent process is responsible for up
.
Exec is short for a family of functions, including
The prototype is as follows:
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 * constEnvp[] */);
Int execv (const char *Path, Char * constArgv[]);
Int execvp (const char *File, Char * constArgv[]);
Int execve (const
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.