阅读 103

Process API

fork

when call fork(), OS create a new process.
the two process is all same, and return from fork() function.

but according the man fork(), when it create success.

  • the child process receives a return code of 0
  • the parent process receives the PID of the newly-created child process

wait

call wait() to delay its execution, until target proccess is completed

wait for any child process

wait(NULL) will block parent process until any of its children has finished

wait for all child process completed

POSIX defines a function: wait(NULL);. It‘s the shorthand for waitpid(-1, NULL, 0);
while ((wpid = wait(&status)) > 0);

exec

work with fork()

the exec family function will replace the current processs image with new process image.
so the rest codes of original process, will not execution.

execvp

The first argument, by convention, should point to the file name associated with the file being exe-cuted. The list of arguments must be terminated by a NULL pointer.

why

the separation of fork() and exec() is essential in building a UNIX shell

shell work flow

cmd: fork ------------------------ -> wait -> output
\ -> pre-env -> exec -> exit /

redirected

Specifically, UNIX systems start looking for free file descriptors at zero. In this case, STDOUT FILENO will be the first available one and thus get assigned when open() is called. Subsequent writes by the child process to the standard output file descriptor, for example by routines such as printf(), will then be routed transpar- ently to the newly-opened file instead of the screen.

extension

init

The process 1, and PID=1

the very first process (called init) is started by the kernel at booting time and never terminates

Zombie Process

A child that terminates, but has not been waited for becomes a "zombie".

Orphan Process

running process whose parent process has finished or terminated

Re-parenting

In most cases, the new parent is the init process, one with the PID 1.

Daemon Process

is an intintionally orphaned process

A daemon process is usually created by a process forking a child process and then immediately exiting, thus causing init to adopt the child process. In a Unix environment, the parent process of a daemon is often, but not always, the init process.

references

原文:https://www.cnblogs.com/Gilfoyle/p/15227380.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐