waitpid (waiting for child process interrupt or end) header file #include<sys/types.h> #include<sys/wait.h> defining function pid_t waitpid(pid_t pid,int * status,int options); function Explain that waitpid() will temporarily stop the execution of the current process until a signal arrives or the child process ends. If the child process has ended when wait() is called, wait() will immediately return the child process end state value. The end state value of the child process is returned by the parameter status, and the process ID of the child process is returned together. If you do not care about ending the status value, the parameter status can be set to NULL. The parameter pid is the subprocess ID to be waited for. The other values have the following meaning: pid<-1 Any child process waiting for the process group ID to be the absolute value of pid. Pid=-1 Wait for any child process, equivalent to wait(). Pid=0 Any child process waiting for the process group ID to be the same as the current process. Pid>0 waits for any child process whose child process ID is pid. The parameter option can be 0 or the following OR combination: WNOHANG If there are no child processes that have ended, they will return immediately without waiting. WUNTRACED Returns immediately if the child process enters a suspended execution, but the end state is ignored. The end state of the child process is returned to status and there are several macros to determine the end condition: WIFEXITED(status) is a non-zero value if the child process ends normally. WEXITSTATUS(status) gets the end code returned by the child process exit(). Generally, WIFEXITED is used to judge whether it ends normally before using this macro. WIFSIGNALED (status) If the sub-process ends because of the signal, the macro value is true. WTERMSIG(status) Obtains the signal code that the sub-process aborts due to the signal. Generally, it is judged by WIFSIGNALED before using this macro. WIFSTOPPED(status) This macro value is true if the child process is in a suspended execution. This is usually the case only when using WUNTRACED. WSTOPSIG(status) Gets the signal code that causes the child process to pause. Generally, it is judged by WIFSTOPPED before using this macro. If the execution is successful, the child process ID (PID) is returned, and if an error occurs, the return value is -1. The reason for the failure is in errno. /****** * waitpid.c - Simple wait usage *********/#include <unistd.h> #include <sys/types.h> #include <sys/wait .h> #include <stdio.h> #include <stdlib.h> int main( void ) { pid_t childpid; int status; 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, see the parent process The behavior of printf("\\tchild pid = %d\ ", getpid()); printf("\\tchild ppid = %d\ ", getppid()); exit(EXIT_SUCCESS); } else { waitpid( Childpid, &status, 0 ); puts( "in parent" ); printf( "\\tparent pid = %d\ ", getpid() ); printf( "\\tparent ppid = %d\ " , getppid() ); printf( "\\tchild process exited with status %d \ ", status } } exit(EXIT_SUCCESS); } [root@localhost src]# gcc waitpid.c [root@localhost 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 [root@localhost src]# If you comment out the above “waitpid( childpid, &status, 0 );” line, the program execution effect is as follows: [root@localhost src]# ./a.out In child process in parent parent pid = 4481 parent ppid = 4379 child process exited with status 1331234400 [root@localhost 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: 0 in the child process, The child process ID is returned in the parent process, and the error returns -1. Function Description: An existing process can call the fork function to create a new process. The new process created by fork is called a child process. fork function is called once but returns twice. The only difference between the two is the return of the child process returns a value of 0 is returned in the child and the parent process ID. The child process is a copy of the parent process, it will get the parent process data space, heap, stack copy of the resource and so on. Note that the child is held by the storage space of the & ldquo; do not share storage space between copy & rdquo ;, which means that the parent and child, sharing between their storage space only the code segment. Sample code: #include <unistd.h> #include <stdio.h> int main(int argc, void ** argv ) { int pid = fork(); if(pid < 0 ) { //print( "error!"); } else if( pid == 0 ) { //print("This is 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 header file: #include<unistd.h> Define function: int execlp(const char * file,const char * arg,……); Function description: execlp() will Find the file name that matches the parameter file from the directory pointed to by the PATH environment variable. After the file is found, execute the file, and then use the second and subsequent parameters as argv[0], argv[1]…… of the file. The last parameter must use a null pointer (NULL) End. Return Value: If the execution is successful, the function will not return. If the execution fails, it will return -1 directly. The reason for the failure is stored in errno. Error code Refer to execve(). Example: /* Executing ls -al /etc/passwd execlp() will find /bin/ls */#include<unistd.h> main() { execlp(“ls”,” depending on /bin in the PATH variable Ls”,”-al”,”/etc/passwd”,(char *)0); } Execution: -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd &mdash ; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash; & mdash ;——————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[]);
Using a Linux terminal is more than just entering commands. After learning these b
1. /The root partition is a mandatory partition, partitioned according to your actual situation. 2.
Git as the worlds most advanced distributed version control system (no one), presumably many people
If you are a Linux user, you may have heard that you dont need to defrag your Linux files system. Yo
Linux wc command parameters and usage details
AMH VPS control panel always fails to log in. The password is not registered.
The samba service is not installed and the dual-system boot fails.
Scp copying between Linux hosts without entering a password
Batch replacement of file contents under Linux system
Reduce the TIME of too many Linux servers
VPS disk full full led to ngnix can not start
Linux-based new mobile operating system Taize
Two "BBC" recovery corrupted Linux systems
Linux regularly performs tasks. Implementation Tutorial
Registration of the s3c2410 device
XXX is not in the sudoers file. This incident will be report
How to set up wireless network connection in Windows 8 system
Software application Q&A: Double-click can't open "My Computer"
Win8 system enables Microsoft Pinyin new experience input method
Problem Win 8 wireless network card can not be used 1.
How to set up Win10 boot greeting? Win10 set the way to boot greetings
Arduino driver installation method for Windows 8
Win8 upgrade to Win8.1 black screen, blue screen solution
Win10 build 10041 installation upgrade to build 10049 version to get started experience
Win8 open sharing prompts need to enter network credentials 0x000000709 how to do?
Win8 can not use the proxy server to access the Internet and the solution