21 Şubat 2016 Pazar

waitpid

Giriş
Parent uygulama başlattığı child uygulamanın bitmesini bekler. Eğer beklemezse ve child sonlanırsa zombie haline gelir.

waitpid metodu
Eğer birden fazla child uygulama başlatılmışsa, herhangi birinin sonlanmasını beklemek için waitpid() kullanılır. Metodun imzası şöyle.
pid_t waitpid(pid_t pid, int *status, int options);
parametreler
İlk parametreye -1 geçilersek, herhangi bir child uygulama için bekleriz. İkinci parametreye NULL geçerek child uygulamanın nasıl çıktığını bilmek istemediğimizi belirtiriz. Üçüncü parametreye WNOHANG geçerek bloke olmak istemediğimizi belirtiriz. Örnek:
/* SIGCHLD handler. */
static void sigchld_hdl (int sig)
{
    pid_t child_pid;

    /* Wait for all dead processes.
     * We use a non-blocking call to be sure this signal handler will not
     * block if a child was cleaned up in another part of the program. */
    while ((child_pid = waitpid(-1, NULL, WNOHANG)) > 0) {
          // child_pid contains the terminated child process' pid
    }
}
Eğer 3. parametre için WUNTRACED kullansaydık şu anlama gelirdi. Child uygulama SIGSTOP ile durdurulsa bile waitpid haberdar olur.

 If the WUNTRACED option is set, children of the current process 
that are stopped due to a SIGTTIN, SIGTTOU, SIGTSTP, or SIGSTOP signal 
also have their status reported.





Hiç yorum yok:

Yorum Gönder