Use and difference of wait and waitpid

Source: Internet
Author: User

Yesterday saw a news, talking about a game development danale death of the news, the company issued an obituary, intends to go to his father and mother every month 10000 alimony, it is feeling that the game industry in China and foreign countries on the gap, days jealous talent ah. Really want to those game company leaders say, do not for the progress for the market, torture programmers. Although the market is very important, but a good game, not to speed up the progress can be done. It is the game experience, the goal of the game, the rules, the player's interaction and so on is the most important. It would be worthwhile to develop a good game in 10. I think, maybe I'm too young to understand the market. But I always have an idea: the body is the capital of revolution. If the company always let you work overtime, or the early return, appropriate overtime, it doesn't matter. Often overtime to the early hours of this, the body will be unbearable one day.

Pulled away.

The wait and WAITPID functions are used to handle zombie processes. They have some differences, they always forget when they are used, they are going to do an experimental test. Also for beginners like me in UNIX network programming learning reference.

-1. Using the background

--2, function prototype

--3, wait and waitpid differences

=========================================================================

 -->1, using the background

   What is a zombie process (zombie)?

The role of zombie is to maintain the information of the subroutine so that the parent program can invoke it later. This information includes: ID, termination status, resource utilization (CPU utilization, memory usage, and so on). ("UNIX Network Programming" P106)

However, the operating system limits the maximum number of processes that can exist at a time, although they do not occupy the CPU and do not occupy memory, but the next time a new process is generated, an unknown error is generated. Therefore, it is necessary to deal with the zombie process effectively.

At this time, we used the wait and waitpid two functions.

  The role of wait and waitpid?

Returns the process ID number of the child process that has been terminated, and clears the zombie process.

  -->2, Function prototypes

#include <sys/wait.h>pid_t wait (int * statloc);p id_t waitpid (pid_t pid,int *statloc,int options);

The process ID was returned successfully, and an error returned 0 or-1.

-->3, wait, and waitpid differences

Wait causes the caller to block until a child process terminates;

Waitpid can be set to non-blocking by setting an option, and waitpid is not waiting for the first end of the process but waits for the PID-specified process in the parameter.

option constants for Waitpid:
  Wnohang Waitpid will not block if the specified PID does not end
  wuntraced If a child process enters a paused execution, it returns immediately, but the end state is ignored.


The meaning of PID in Waitpid is changed according to its specific value :

Pid==-1 waits for any child process, at which time the Waitpid function is the same as the wait
PID >0 waiting for the process ID to be the same as the PID value of the child process
Pid==0 waits for any child process that is the same as the caller's process group ID
Pid<-1 any sub-process that waits for the process group ID to be equal to the PID absolute value

The Waitpid provides three features that wait does not have :
1 Waitpid allows us to wait for the specified process
2 Waitpid provides a non-blocking wait
3 Waitpid Support Work control

Use and difference of wait and waitpid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.