linux 学习笔记之 Process Control

本文是关于Linux进程控制的学习笔记,重点介绍了fork函数及其采用的copy-on-write技术,以及子进程如何继承父进程的属性。同时,提到了vfork函数,它用于创建新进程并保证子进程先于父进程运行,主要用于exec新程序。此外,还讨论了当父进程在子进程之前终止时,init如何接管孤儿进程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Process Identifiers

Every process has unique process ID, a non-negative integer. Process ID 0 is swapper. Process ID 1 is usually the init process and is invoked by the kernel at the end of the bootstrap procedure. The init process never dies. It is a normal user process, not a system process within the kernel, like the swapper, although it does run with super user privileges.

  • Fork Function
    pid_t fork(void)

A technique called copy-on-write (COW) is used. These regions are shared by the parent and child and have their protection changed by the kernel to read-only. If either process tries to modify these regions, the kernel then makes a copy of that piece of memory only, typically a “page” in a virtual memory system.

The parent and child share a file table entry for every open descriptor. It is import that the parent and child share the same of file offset.

  • Child process inherited from parent process:
  1. Real user ID, real group ID, effective user ID, effective group ID
  2. Supplementary group ID
  3. Process group ID
  4. Session ID
  5. Controlling terminal
  6. The set-user-ID and set-group-ID flags
  7. Current working directory
  8. Root directory
  9. File mode creation mask
  10. Signal mask and dispositions
  11. The close-on exec flag from any open file descriptors
  12. Environment
  13. Attached shared memory segments
  14. Memory mappings
  15. Resource limits
  • The differences between child and parent are:
  1. The return value from fork
  2. The process IDs are different
  3. The two processes have different parent process IDs
  4. The child’s tms values are set to 0
  5. File lock set by the parent are not inherited by the child
  6. Pending alarms are cleared for the child
  7. The set of pending signals for the child is set to the empty set
  • vfork Function

The vfork function is intended to create a new process when the purpose of the new process is to exec a new program. The vfork function creates the new process, just like fork, without copying the address space of the parent into the child, as the child will reference that address space; the child simply call exec(or exit) right after the vfork. Instead, while the child is running and until it calls either exec or exit, the child runs in the address space of the parent.

Another difference between the two functions is that vfork guarantees that the child runs first, until the child calls exec or exit.

What happens if the parent terminates before the child? The answer is that the init process becomes the parent process of any process whose parent terminates. We say that the process has been inherited by init.

  • Change User IDs and Group IDs
    int setuid(uid_t uid);
    int setgid(gid_t gid);

Below table show ways to change the three user IDs:

ID

exec

setuid(uid)

set-user-ID bit off

set-user-ID bit on

super user

unprivileged user

real user ID

effective user ID

 

saved set-user-ID

unchanged

unchanged

 

copied form effective user ID

unchanged

set from user ID of grogram file

copied from effective user ID

set to uid

set to uid

 

set to uid

unchaged

set to uid

 

unchaged

  • User Identification
    char * getlogin(void)

The getlogin function provides a way to fetch that login name. This function cal fail if the process is not attached to a terminal that a user longed in to. We normally call these processes daemons.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值