fork 子进程

  • Fork, exec, wait and exit system call explained in Linux: 这篇文章比较系统。

waitpid() IBM 系列文章精粹,W开头的系列宏定义函数

  • Notes on Wait & Waitpid;
  • WIFEXITED–Query status to see if a child process ended normally;
  • wait–Wait for status information from a Child process;
  • waitpid–Obtain status information from a child process;
  • WEXITSTATUS–Obtain exit status of a child process;
  • Exit status of a child process in Linux;
  • Process Completion Status;

fork()

  • Zombie process is not cleanup with waitpid call
  • Does a KILL signal exit a process immediately?

Some of the important points on fork() are as follows:
The parent will get the child process ID with non-zero value.
Zero Value is returned to the child.
If there will be any system or hardware errors while creating the child, -1 is returned to the fork().
With the unique process ID obtained by the child process, it does not match the ID of any existing process group.

pipe()

  • pipe() System call
    fd0 read, fd1 write
int pipe(int fds[2]);
Parameters :
fd[0] will be the fd(file descriptor) for the read end of pipe.
fd[1] will be the fd for the write end of pipe.
Returns : 0 on Success, -1 on error.
call fork after creating a pipe, then the parent and child can communicate via the pipe.
  • dup2() Linux system call;

execvp()

  • How to use the execvp() function in C/C++;
What happens to our C program now?
This function will give the control of the current process (C program) to the command. So, the C program is instantly replaced with the actual command.
So, anything that comes after execvp() will NOT execute, since our program is taken over completely!
However, if the command fails for some reason, execvp() will return -1.
So, whenever you use execvp(), if you want to maintain your C program, you generally use fork() to first spawn a new process, and then use execvp() on that new process.
This is called the “fork-exec” model, and is the standard practice for running multiple processes using C.

zombie 僵尸进程

  • Zombies can be identified in the output from the Unix ps command by the presence of a "Z" in the "STAT" column.
    父子进程关系
  • kill 掉僵尸进程的父进程即可杀死僵尸进程。
  • How to kill zombie process @stackoverflow;
  • What Is a “Zombie Process” on Linux?:说的清楚。

你可能感兴趣的:(fork 子进程)