http://blog.csdn.net/zs634134578/article/details/20475741
Problem Focus: Processes are the foundation of the Linux operating system environment. This article discusses the following elements, but also the interview is often asked some questions: 1 replication process image of the fork system call and replace the process image of the Exec series system call 2 Zombie process 3 interprocess communication one way: Pipeline 4 3 System V Process communication mode : semaphores, Message Queuing, and shared memory
Fork system Call Definition:[CPP]View plain copy #include <sys/types.h> #include <unistd.h> pid_t fork (void); Function Description: This function returns two times in the parent process the PID of the child process each time the call is returned. Returns 0 in the subprocess, which determines whether the current process is a parent or a subprocess (the return value is a subprocess of 0): The code that copies the current process subprocess is identical to the parent process fork function The child process replicates most of the parent process's data (heap data, stack data, static data) The open file descriptor in the replication parent process is opened by default in the child process, so their reference variables are +1.
EXEC Series system calls (6) executing other programs in a child process, replacing the current process image, requires the EXEC series function definition:[CPP]View plain copy #include <unistd.h> extern char** environ; int execl (const char* path, const char* ARG, ...); int EXECLP (const char* file, const char* arg, ...); int execle (Cost 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 (const char* path, char* const envp[]); Parameter Description: path: Specifies the full path to the executable file: accepts the file name, and the location of the file searches for arg in the environment variable path: Receives the variable parameter argv: receives the parameter array, which will be passed to the new program's main function ENVP: The environment variable used to set the new program, and if not set, the new program uses the environment variable specified by the global variable environ
Return: When successful, do not return; error, return-1, and set errno if there is no error, the code after the exec call in the source program will not execute because the source program has been completely replaced by the program specified by the exec parameter (including code and data)
The EXEC function does not close the file descriptor that was opened by the original program unless the file descriptor is set with the type Sock_cloexec attribute.
Zombie Process Zombie Process: Two situations cause a child process to cause zombie if the parent process needs to query the exit state of the subprocess, then the kernel does not immediately release the process table entries for the processes after the child process finishes, to satisfy the parent process's subsequent query for the child process exit information. The child process is in zombie state before the parent process has read its exit state after the subprocess has finished. If the parent process exits or terminates abnormally, and the child process continues to run, the PPID (parent process PID) of the subprocess is set to 1, the Init process, where the init process takes over the process and waits for it to end. After the parent process exits, the child process is in zombie state before the child process exits. That is, the parent process does not correctly handle the return information of the child process, which will cause the child process to be in zombie state, occupy kernel resources, and cause the waste of kernel resources. The function described below is to immediately end the zombie state of the child process.[CPP]View plain copy #include <sys/types.h> #include <sys/wait.h> pid_t Wait (int* stat_loc); pid_t waitpid (pid_t pid, int* stat_loc, int options); Function Description: Wait function: Block the process until a child process finishes running. Returns the PID of a subprocess and stores the exit status information for that child process in the memory pointed to by the Stat_loc parameter. Several macros are defined in the Sys/wait.h header file to explain the exit status information for the child process