Skip to content

Commit

Permalink
0.0.2 sleep forever
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslaff committed Nov 13, 2024
1 parent cdd0564 commit 7a5c61e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dymanic ELF binary is 18Kb, static (gcc -Wall -g --static -o zombie zombie.c)
You can find statically compiled binary in Releases page.

## Usage:
Accepts one argument (int), sleep time. Default value is 30.
Accepts one argument (int), sleep time. Default value is 0 (sleep forever).

~~~
$ ./zombie 50
Expand Down
15 changes: 11 additions & 4 deletions zombie.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
#include <unistd.h>
#include <sys/types.h>

char *version = "0.0.1";
char *version = "0.0.2";

int main(int argc, char **argv) {
pid_t pid = fork(); // Create a new child process

int sleeptime = 30;
int sleeptime = -1;

if(argc > 1){
sleeptime = atoi(argv[1]);
}

if(sleeptime == 0){
sleeptime = 30;
sleeptime = -1;
}

if (pid > 0) {
// Parent process: Just sleep, don't call wait().
printf("Parent process (PID: %d) is running, child (PID: %d) will become a zombie.\n", getpid(), pid);
printf("Sleep %d seconds...\n", sleeptime);
if(sleeptime>0){
printf("Sleep %d seconds...\n", sleeptime);
}else{
printf("Sleep forever...\n");
while(1){
sleep(86400);
}
}
sleep(sleeptime); // Keep the parent alive to allow time for observing the zombie process
} else if (pid == 0) {
// Child process: Exit immediately, becomes a zombie
Expand Down

0 comments on commit 7a5c61e

Please sign in to comment.