- Shivam L1-2
- Vipul Jain L1-18
- Mudit Kumar Tyagi L1-22
- Vaibhav Upreti L1-30
A zombie or a "defunct process" in Linux(or UNIX based kernel) is a process that has been completed, but its entry still remains in the process table due to lack of correspondence between the parent and child processes. Usually, a parent process keeps a check on the status of its child processes through the wait() function. When the child process has finished, the wait function signals the parent to completely exit the process from the memory. However, if the parent fails to call the wait function for any of its children, the child process remains alive in the system as a dead or zombie process. These zombie processes might accumulate, in large numbers, on your system and affect its performance.
A chart to illustrate Zombie process working
graph LR
A[Parent Process] -- wait --> B[Parent Process]
A -- fork--> C[Child Process]
C -- exec--> F[Child Process]
F -- exit-->E[Zombie Process]
E --> B
Save this file as zombie.c in your folder
```c
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid;
child_pid = fork ();
if (child_pid > 0) {
sleep (60);
}
else {
exit (0);
}
return 0;
}
The following process will run in your terminal for 60s even after closing this. You can check this by running the following commands-
Run the code in your terminal by entering these two commands or use coderunner VSC extension
gcc zombie.c
Now run the zombie program:
./a.out
The ps command will now also show this defunct process, open a new terminal and use the below command to check the defunct process:
ps -ef | grep a.out
501 9366 6755 0 5:40pm ttys000 0:00.00 ./a.out
501 9367 9366 0 5:40pm ttys000 0:00.00 (a.out)
501 9397 8546 0 5:40pm ttys002 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox a.out
Create a project to find sum of digits of a number until a single digit is occurred (without using recursion or iterative (loop) statements).
goto statements are used in such cases
A chart to illustrate the algorithm-
Save this file as zombie.c in your folder
```c
#include<stdio.h>
int main(){
int sum=0;
int num;
printf("-------------------------------------------------------------- \n");
printf("Enter number: ");
scanf("%d", &num);
jumpto:
sum+=num%10;
num/=10;
if (num==0){
printf("Sum of the digits of the number= %d", sum);
return 0;
}
else
goto jumpto;
}