330 likes | 955 Views
Zombie and orphan processes. Zombie process (from wikipedia). When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. Zombie process (from wikipedia). However, the process's entry in the process table remains.
E N D
Zombie process (from wikipedia) • When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes.
Zombie process (from wikipedia) • However, the process's entry in the process table remains. • The parent is sent a SIGCHLD signal indicating that a child has died; the handler for this signal will typically execute the wait system call, which reads the exit status and removes the zombie. • The zombie's process ID and entry in the process table can then be reused. • However, if a parent ignores the SIGCHLD, the zombie will be left in the process table.
Orphan process (from wikipedia) • A zombie process is not the same as an orphan process. • Orphan processes don't become zombie processes; instead, they are adopted by init (process ID 1), which waits on its children.
Example child process code printf( "child: %s running with %d arg(s). \n", argv[0], argc ); puts( "child: sleeping" ); sleep( 15 ); puts( "child: bye" ); return 0;
Parent that creates an orphan. puts( "parent: forking" ); pid_t ret = fork(); //now there’s two of us! switch (ret) { //which one? case -1: //here when fork failed puts( "parent: error: fork failed!" ); break; case 0: //here when executing in child puts( "child: here (before execl)!" ); if (execl( "./childTest.exe", "./childTest.exe", 0 )==-1) perror( "child: execl failed:" ); puts( "child: here (after execl)!" ); //should never get here break; default: //here when executing in parent printf( "parent: child has pid=%d \n", ret ); puts( "parent: bye" ); break; } return 0;
Parent that creates a zombie (but only for 2 minutes). puts( "parent: forking" ); pid_t ret = fork(); switch (ret) { case -1: //here when fork failed puts( "parent: error: fork failed!" ); break; case 0: //here when executing in child puts( "child: here (before execl)!" ); if (execl( "./childTest.exe", "./childTest.exe", 0 )==-1) perror( "child: execl failed:" ); puts( "child: here (after execl)!" ); //should never get here break; default: //here when executing in parent printf( "parent: child has pid=%d \n", ret ); puts( "parent: sleeping" ); sleep( 120 ); puts( "parent: bye" ); break; } return 0;
What does a zombie look like? • normal (no zombie) $ ps PID TTY TIME CMD 1074 pts/2 00:00:00 bash 1280 pts/2 00:00:00 parentTest.exe 1281 pts/2 00:00:00 childTest.exe 1283 pts/2 00:00:00 ps
What does a zombie look like? • abnormal (zombie) $ ps PID TTY TIME CMD 1074 pts/2 00:00:00 bash 1280 pts/2 00:00:00 parentTest.exe 1281 pts/2 00:00:00 childTest.exe <defunct> 1288 pts/2 00:00:00 ps
What does a zombie look like? $ ps -l Warning: /boot/System.map has an incorrect kernel version. F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 000 S 561 1074 1073 0 76 0 - 628 11a418 pts/2 00:00:00 bash 000 S 561 1301 1074 0 70 0 - 436 11f22b pts/2 00:00:00 parentTes 004 Z 561 1302 1301 0 70 0 - 0 119ffb pts/2 00:00:00 childTest 000 R 561 1320 1074 0 77 0 - 646 - pts/2 00:00:00 ps
Zombie process (from wikipedia) • When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. However, the process's entry in the process table remains. • The parent is sent a SIGCHLD signal indicating that a child has died; the handler for this signal will typically execute the wait system call, which reads the exit status and removes the zombie. • The zombie's process ID and entry in the process table can then be reused. However, if a parent ignores the SIGCHLD, the zombie will be left in the process table.
Avoiding zombies • To avoid creating zombie processes, the parent must: • Handle the SIGCHLD signal. • See man signal. • In this signal handling function, the parent must wait for the child process. • See man 2 wait (linux) or man –s 3C wait (solaris). • We will explore signals as an interprocess communications (IPC) mechanism shortly.