From: kremlin Date: Mon, 3 Nov 2014 01:19:02 +0000 (-0500) Subject: created sep dirs for hw/labs, write lab 5 X-Git-Url: https://uglyman.kremlin.cc/gitweb/gitweb.cgi?a=commitdiff_plain;h=dfcba1d84b054fa52049feda0e5c7e47d040d544;p=assignments.git created sep dirs for hw/labs, write lab 5 --- diff --git a/lab/lab5/Makefile b/lab/lab5/Makefile new file mode 100644 index 0000000..fec97f3 --- /dev/null +++ b/lab/lab5/Makefile @@ -0,0 +1,12 @@ +.PHONY: all + +cc=/usr/bin/gcc +CARGS=-Wall -Werror -Wextra -pedantic -Wno-unused-parameter -Wno-unused -std=c99 +SRC=zombie.c -o zombie + +all: + $(CC) $(CARGS) $(SRC) + +debug: + $(CC) $(CARGS) $(SRC) -g -O0 + diff --git a/lab/lab5/zombie.c b/lab/lab5/zombie.c new file mode 100644 index 0000000..12b6d67 --- /dev/null +++ b/lab/lab5/zombie.c @@ -0,0 +1,39 @@ +#include +#include +#include + +#include +#include + +pid_t children[5]; + +void announce_death() { + + printf("i'm pid #%lu and i died.\n", (long) getpid()); +} + +int main(int argc, char *argv[]) { + + pid_t our_pid; + our_pid = getpid(); + + for(int i = 0; i < 5; i++) { + if((children[i] = fork()) == 0) + goto child; + else + sleep(1); + } + + for(int i = 0; i < 5; i++) { + + kill(children[i], SIGTERM); + wait(0); + } + + return 0; + + child: + printf("i'm pid #%lu!\n", (long)getpid()); + atexit(announce_death); + exit(0); +}