fork 子进程

2022-08-09  本文已影响0人  michael_jia

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

fork()

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()

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.

execvp()

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 僵尸进程 <defunct>

上一篇 下一篇

猜你喜欢

热点阅读