Process-related functions-exec, etc.

Source: Internet
Author: User

1.

Exec Function

#include<unistd.h>int execl(const char *pathname,const char *arg,...);int execlp(const char *filename,const char *arg,...);int execle(const char *pathname,const char *arg,...,char *const envp[]);int execv(const char *pathname,char *const argv[]);int execvp(const char *filename,char *const argv[]);int execve(const char *filename,char *const argv[],char *const envp[]);

Parameters with L are not fixed,

The path with P is automatically searched in the environment variable,

Argv [] is used with V,

One more envp [] with E specifies the environment variable.

If the call is successful, no value is returned. Otherwise,-1 is returned.

 

Note: The standard form of the main function may be

Int main (INT argc, char * argv [], char * envp []);

Example:

 1 #include<stdio.h> 2  3 int main(int argc,char *argv[],char *envp[]) 4 { 5     printf("###  ARGC ###\n%d\n",argc); 6     printf("###  ARGV ###\n"); 7     while(*argv) 8     { 9         printf("%s\n",*(argv++));10     }11     printf("###  ENVP ###\n");12     while(*envp)13     {14         printf("%s\n",*(envp++));15     }16     return 0;17 }

 

Usage notes,

When a variable with P is used, the complete path is not required because it will be found in the original path.

Use the envp array when using E, char * envp [] = {"path1",..., null}

When using a V, you need to use the argv array, char * argv [] = {"cmd",..., null };

Parameters must be listed one by one when l is used. Absolute address, command name, parameter, and end null

 

 

2.

Exit Function

#include<stdlib.h>void exit(int status);

Terminate the process.

You can also use _ exit () instead of exit. The difference is that _ exit does not perform on-site cleaning and directly enters the kernel (kernel), which is defined by posix.1 and is not in ansi c.

Generally, using exit ensures data security and prevents data loss in the cache area.

 

3.

Wait and waitpid Functions

#include<sys/types.h>#include<sys/wait.h>pid_t wait(int *status);pid_t waitpid(pid_t pid,int *status,int options);

 

Through wait, you can collect information about zombie processes and then destroy process residues.

Example:

 1 #include<sys/types.h> 2 #include<sys/wait.h> 3  4 #include<unistd.h> 5 #include<stdio.h> 6 #include<stdlib.h> 7  8 int main(void) 9 {10     pid_t pc,pr;11     if((pc=fork())<0)12     {13         exit(1);14     }15     else if(pc == 0)16     {17         printf("child pid is %d\n",getpid());18         sleep(3);19     }20     else21     {22         pr=wait(NULL);23         printf("parent pid is %d\n",getpid());24     }25     return 0;26 }

 

At this time, the parent process will continue to be executed only after the sub-process has completed sleep, because wait calls will cause blocking and only zombie processes will continue to be executed. Otherwise, the system blocks the current Code until the zombie process is captured.

 

For waitpid, if a process is captured, the PID Number is returned; otherwise, 0 is returned.

 

If the PID parameter is greater than 0, the value waits for this process.

If pid =-1, wait for any sub-process to exit.

If pid = 0, wait for the sub-process in the same process group,

If PID <-1 waits for any sub-process of the Process Group of the PID absolute value ID.

 

If you do not want to use options, you can use 0.

Otherwise, only

Wnohang does not block the use of wait, that is, it collects and destroys zombie processes before the time of the call.

Wuntraced, generally not used, mainly used in debugging.

Both can be used | both can be used.

 

In fact, wait is packaged by waitpid. In this case, pid =-1., Options = 0

 

Common macro functions for and status

Wifexited (Status );

When the sub-process Exits normally, a non-0 value is returned.

 

Wexitstatus (Status );

Returns the code for exiting the process. The preceding macro function is required to return the code for success.

 

There are also two functions called wait3 and wait4, which provide the same usage as wait and waitpid, and finally provide a struct parameter, struct rusage * rusage;

When the resources occupied by the process are not null, the information is written to the buffer zone pointed to by the struct pointer.

 

Process-related functions-exec, etc.

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.