Linux environment Programming--waitpid and fork and EXECLP

Source: Internet
Author: User
Tags define function function definition

Waitpid Waitpid (Waiting for the child process to break or end)
Table header File
#include <sys/types.h>
#include <sys/wait.h>
Define function pid_t waitpid (pid_t pid,int * status,int options);
Function description
Waitpid () will temporarily stop the execution of the current process until a signal arrives or a child process
End. If the child process has ended when you call Wait (), wait () immediately
Returns the child process end status value. The end state value of the child process is returned by the parameter status,
The process identification code of the child process is also returned together. If the end state value is not
The parameter status can be set to NULL. Parameter PID is to wait for the child process identification Code,
Other numerical meanings are as follows:
Pid<-1 waits for the process group identifier to be the PID absolute value of any child processes.
Pid=-1 waits for any child process, equivalent to wait ().
Pid=0 waits for the process group identifier to be the same as the current process for any child processes.
Pid>0 waits for any child process identification code to be a child of PID.
The parameter option can be 0 or the following or combination:
Wnohang If there are no completed sub-processes then return immediately, not to wait.
wuntraced If a child process enters a paused execution, it returns immediately, but the end state is ignored.
The end state of the child process is returned and stored in status, with a few macros at the bottom to determine the end condition:
Wifexited (status) is a non-0 value if the child process ends normally.
Wexitstatus (status) Gets the end code returned by the child process exit (), typically using wifexited to determine whether the macro ends properly before it can be used.
wifsignaled (status) This macro value is true if the child process ends because of a signal
Wtermsig (status) Gets the signal code that the child process aborts because of the signal, generally uses wifsignaled to judge before using this macro.
wifstopped (status) This macro value is true if the child process is in a paused execution condition. This is generally the case only if you are using wuntraced.
Wstopsig (status) Gets the signal code that causes the child process to pause, usually using wifstopped to judge before the macro is used.
Returns the child process identifier (PID) if the execution succeeds, or returns if an error occurs
return value-1. The reason for failure is stored in errno.
 
 
 /** * * * waitpid.c-simple wait usage *********/#include<unistd.h>#include<sys/types.h>#include<sys/wait.h>#include<stdio.h>#include<stdlib.h>intMainvoid) {pid_t childpid; intstatus; Childpid=Fork (); if( -1==childpid) {Perror ("Fork ()" );  Exit (Exit_failure); }Else if(0==childpid) {Puts ("In child process" ); Sleep (3);//let the child process sleep for 3 seconds to see the behavior of the parent processprintf"\tchild pid =%d\n", Getpid ()); printf ("\tchild ppid =%d\n", Getppid ());  Exit (exit_success); }Else{waitpid (childpid,&status,0 ); Puts ("In parent" ); printf ("\tparent pid =%d\n", Getpid ()); printf ("\tparent ppid =%d\n", Getppid ()); printf ("\tchild process exited with status%d \ n", status);  } exit (exit_success); }


[Email protected] src]# gcc WAITPID.C
[Email protected] src]#./a.out
In child process
Child PID = 4469
Child Ppid = 4468
In parent
Parent PID = 4468
Parent Ppid = 4379
Child process exited with status 0
[Email protected] src]#
If it will be above "waitpid (Childpid, &status, 0);" Line comment out, the program execution effect is as follows:
[Email protected] src]#./a.out
In child process
In parent
Parent PID = 4481
Parent Ppid = 4379
Child process exited with status 1331234400
[[Email protected] src]# child PID = 4482
Child Ppid = 1
The child process has not exited and the parent process has exited. Fork Fork () function, Linux system call
Header file:
#include <unistd.h>
function definition:
int fork (void);
return value:
The child process returns 0, the parent process returns the child process ID, and an error returns-1
Function Description:
An existing process can call the fork function to create a new process. A new process created by Fork is called a subprocess (child process). The fork function 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.
Example code:
  
#include <unistd.h>#include<stdio.h>intMainintargcvoid**argv) {intPID =Fork (); if(PID <0) {//print ("error!");}Else if(PID = =0) {//print ("This was the child process!");}Else{//print ("This is the parent process! Child process id =%d ", pid);}return 0; }

EXECLP EXECLP (find files from the PATH environment variable and execute)
Related functions:
Fork,execl,execle,execv,execve,execvp
Table header file:
#include <unistd.h>
To define a function:
int EXECLP (const char * file,const char * arg,......) ;
Function Description:
EXECLP () finds the file name in the directory referred to by the PATH environment variable, executes the file after it is found, and then takes the second later parameter as the file's argv[0], argv[1] ...., the last parameter must be terminated with a null pointer (NULL).
return value:
If the execution succeeds, the function does not return, and the execution fails to return 1 directly, and the reason for the failure is stored in errno.
Error code reference EXECVE ().
Example:
  
   /* / #include<unistd.h> main () {EXECLP ("ls", "ls", "-al", "/etc/  passwd ", (char *)0); }


Perform:
-rw-r--r--1 root root 705 Sep 3 13:52/etc/passwd
———————————————————————————————— Add by Love_aiqiu
NAME
Execl, EXECLP, Execle, EXECV, execvp-execute a file
Synopsis
#include <unistd.h>
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[]);

Linux environment Programming--waitpid and fork and EXECLP

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.