notify () and notifyAll () What is the difference? wait ()

First explain the two concepts.

Wait pool: A thread calls a hypothesis after a wait of an object () method, thread A releases the lock of the object, into the pool to wait for the object, the thread will not wait for the pool to compete lock of the object .
Lock pool: only get a lock object, synchronized thread to execute the code objects, lock objects that only one thread can get, other threads can only wait for the lock pool
distinction:

notify () method randomly wake up a thread waiting for a pooled object, enter the lock pool; all threads notifyAll () Wake object waiting in the pool, the pool into the lock.

Programming, sometimes need to wait for a process to another process, the most common is the parent process to wait for their child process, the parent process or sub-process recycling their resources, including zombie process. Here briefly system calls: wait ()

Introduction wait ()
function prototype is

include <sys / types.h> / * type provided pid_t definition * /

include <wait.h>

int wait (int * status)
function is a function of:
the parent process to wait once called himself immediately blocked by the wait automatically analyze a child process if the current process has quit, if it finds such a child process has been turned into zombies , wait collects this information to child processes, and put it after the complete destruction of return; If you do not find such a sub-process, wait would have been blocked here until one appears.
Note:

When a parent forgot () function waits for the child process has been terminated with the wait, the child will go into a state of non-parent process, the child process is a zombie process at this time.

wait () to the fork () supporting the emergence, if you call wait before using the fork () (), wait () return value -1, normally wait () returns a value of PID of the child process.

If the first terminate the parent process, the child process will continue normally, but it (PID 1) inherited by the init process, when the child dies, init process to capture the state.

Parameter status to save some of the state in which he quit the collection process, it is a pointer to an int pointer. But if we care about is how to die for the child process, just want to put this zombie destroyed, (in fact in most cases, we would think so), we can set this parameter to NULL, it like this:
PID = wait (NULL);
If successful, the process will return to wait collected subprocess ID, the calling process if the child process is not, the call fails, then wait returns -1 is set errno ECHILD.

Guess you like

Origin www.cnblogs.com/xjyxp/p/11441891.html