Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

exectest/forktest, modified

- use poll() to wait instead of while(1), use usleep()
  • Loading branch information...
commit 38ff8a71865b8fade003c7735acd9ba1aae71046 1 parent ba0c157
Daniel Ritz authored
0  W40/Makefile → W40-callByValue/Makefile
File renamed without changes
0  W40/callbyvalue.c → W40-callByValue/callbyvalue.c
File renamed without changes
2  W40-parallel-processes/.gitignore
... ... @@ -0,0 +1,2 @@
  1 +forktest
  2 +demoexec
8 W40-parallel-processes/Makefile
... ... @@ -0,0 +1,8 @@
  1 +CC=gcc
  2 +
  3 +test: forktest demoexec
  4 +
  5 +forktest: forktest.c
  6 + $(CC) -o forktest forktest.c
  7 +demoexec: demoexec.c
  8 + $(CC) -o demoexec demoexec.c
25 W40-parallel-processes/demoexec.c
... ... @@ -0,0 +1,25 @@
  1 +#include <stdlib.h>
  2 +#include <unistd.h>
  3 +#include <sys/types.h>
  4 +#include <sys/wait.h>
  5 +#include <stdio.h>
  6 +
  7 +int main ()
  8 +{
  9 + pid_t pid;
  10 + int status;
  11 +
  12 + pid = fork();
  13 + if (pid == 0) {
  14 + printf("Child %d, executing \"/bin/date\"\n", getpid());
  15 + execl("/bin/date", "date", NULL);
  16 +
  17 + printf("This should not happen!\n");
  18 + }
  19 +
  20 + printf("Parent waiting for child's dead\n");
  21 + waitpid(pid, &status, 0);
  22 + printf("Parent, PID: %d: Child with PID %d just died.\n", getpid(), pid);
  23 +
  24 + exit(0);
  25 +}
33 W40-parallel-processes/forktest.c
... ... @@ -0,0 +1,33 @@
  1 +#include <sys/types.h>
  2 +#include <sys/wait.h>
  3 +#include <unistd.h>
  4 +#include <stdlib.h>
  5 +#include <stdio.h>
  6 +#include <poll.h>
  7 +
  8 +int main()
  9 +{
  10 + pid_t k, child;
  11 + int p, status, q;
  12 +
  13 + child = fork();
  14 + if (child != 0) {
  15 + for (p = 0; p < 20; p++) {
  16 + printf("\t\t\tParent, iteration %d, child PID=%d\n", p, child);
  17 + usleep(500000);
  18 + }
  19 + waitpid(child, &status, 0);
  20 + printf("\t\t\tChild died, PID was, %d\n", child);
  21 +
  22 + /* wait for interrupt ^C */
  23 + poll(NULL, 0, -1);
  24 + } else {
  25 + pid_t pid = getpid();
  26 + printf("Child PID= %d \n", pid);
  27 + for (p = 0; p < 10; p++) {
  28 + printf ("Child PID %d, iteration %d\n", pid, p);
  29 + usleep(500000);
  30 + }
  31 + exit(1);
  32 + }
  33 +}

0 comments on commit 38ff8a7

Please sign in to comment.
Something went wrong with that request. Please try again.