Skip to the content.

wait syscall

wait 和 exit 都在 kernel/exit.c 下

阅读 man

The wait() system call suspends execution of the calling thread until one of its children terminates. The call wait(&wstatus) is equivalent to:

waitpid(-1, &wstatus, 0);

waitid()

The waitid() system call (available since Linux 2.6.9) provides more precise control over which child state changes to wait for.

The wait3() and wait4() system calls are similar to waitpid(2), but additionally re‐ turn resource usage information about the child in the structure pointed to by rusage.

Other than the use of the rusage argument, the following wait3() call:

wait3(wstatus, options, rusage);

is equivalent to:

waitpid(-1, wstatus, options);

Similarly, the following wait4() call:

wait4(pid, wstatus, options, rusage);

is equivalent to:

waitpid(pid, wstatus, options);

和 fork 系列类似,三个是逐级递增的,wait / waitpid / wait

/* Wait for a child matching PID to die.
   If PID is greater than 0, match any process whose process ID is PID.
   If PID is (pid_t) -1, match any process.
   If PID is (pid_t) 0, match any process with the
   same process group as the current process.
   If PID is less than -1, match any process whose
   process group is the absolute value of PID.
   If the WNOHANG bit is set in OPTIONS, and that child
   is not already dead, return (pid_t) 0.  If successful,
   return PID and store the dead child's status in STAT_LOC.
   Return (pid_t) -1 for errors.  If the WUNTRACED bit is
   set in OPTIONS, return status for stopped children; otherwise don't.

   This function is a cancellation point and therefore not marked with
   __THROW.  */
extern __pid_t waitpid (__pid_t __pid, int *__stat_loc, int __options);

试图读读代码

void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
{
	__wake_up_sync_key(&parent->signal->wait_chldexit,
			   TASK_INTERRUPTIBLE, p);
}

waitpid vs pthread_join

参考: https://stackoverflow.com/questions/11295298/waiting-for-threads-of-another-process-using-waitpid

等待整理

kernel/sys.c

  1. pid
  2. rlimit
  3. prctl

A process can set and reset the keep_capabilities flag by means of the Linux-specific prctl() system call.

本站所有文章转发 CSDN 将按侵权追究法律责任,其它情况随意。