created sep dirs for hw/labs, write lab 5
authorkremlin <kremlin@slarmf.my.domain>
Mon, 3 Nov 2014 01:19:02 +0000 (20:19 -0500)
committerkremlin <kremlin@slarmf.my.domain>
Mon, 3 Nov 2014 01:19:02 +0000 (20:19 -0500)
lab/lab5/Makefile [new file with mode: 0644]
lab/lab5/zombie.c [new file with mode: 0644]

diff --git a/lab/lab5/Makefile b/lab/lab5/Makefile
new file mode 100644 (file)
index 0000000..fec97f3
--- /dev/null
@@ -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 (file)
index 0000000..12b6d67
--- /dev/null
@@ -0,0 +1,39 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <signal.h>
+#include <sys/wait.h>
+
+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);
+}