Permalink
Browse files

exectest/forktest, modified

- use poll() to wait instead of while(1), use usleep()
  • Loading branch information...
1 parent ba0c157 commit 38ff8a71865b8fade003c7735acd9ba1aae71046 @dr-itz committed Oct 1, 2012
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,2 @@
+forktest
+demoexec
@@ -0,0 +1,8 @@
+CC=gcc
+
+test: forktest demoexec
+
+forktest: forktest.c
+ $(CC) -o forktest forktest.c
+demoexec: demoexec.c
+ $(CC) -o demoexec demoexec.c
@@ -0,0 +1,25 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdio.h>
+
+int main ()
+{
+ pid_t pid;
+ int status;
+
+ pid = fork();
+ if (pid == 0) {
+ printf("Child %d, executing \"/bin/date\"\n", getpid());
+ execl("/bin/date", "date", NULL);
+
+ printf("This should not happen!\n");
+ }
+
+ printf("Parent waiting for child's dead\n");
+ waitpid(pid, &status, 0);
+ printf("Parent, PID: %d: Child with PID %d just died.\n", getpid(), pid);
+
+ exit(0);
+}
@@ -0,0 +1,33 @@
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <poll.h>
+
+int main()
+{
+ pid_t k, child;
+ int p, status, q;
+
+ child = fork();
+ if (child != 0) {
+ for (p = 0; p < 20; p++) {
+ printf("\t\t\tParent, iteration %d, child PID=%d\n", p, child);
+ usleep(500000);
+ }
+ waitpid(child, &status, 0);
+ printf("\t\t\tChild died, PID was, %d\n", child);
+
+ /* wait for interrupt ^C */
+ poll(NULL, 0, -1);
+ } else {
+ pid_t pid = getpid();
+ printf("Child PID= %d \n", pid);
+ for (p = 0; p < 10; p++) {
+ printf ("Child PID %d, iteration %d\n", pid, p);
+ usleep(500000);
+ }
+ exit(1);
+ }
+}

0 comments on commit 38ff8a7

Please sign in to comment.