Beispiellösung

Content

Antworten zu den Fragen

Implementierung

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

volatile unsigned int have_token = 0;

void sighandler(int signo) {
   have_token = 1;
}

void run_member_of_ring(pid_t next) {
   for(int i = 0; i < 10; ++i) {
      if (!have_token) {
	 pause();
      }
      printf("[%5d] has token\n", (int) getpid()); fflush(stdout);
      if (kill(next, SIGUSR1) < 0) break;
      have_token = 0;
   }
}

pid_t create_tokenring(unsigned int members) {
   pid_t master = fork();
   if (master < 0) return master;
   if (master == 0) {
      struct sigaction sigact = {
	 .sa_handler = sighandler,
      };
      if (sigaction(SIGUSR1, &sigact, 0) < 0) {
	 exit(255);
      }
      master = getpid();
      pid_t next = master;
      for (unsigned int member = 1; member < members; ++member) {
	 pid_t child = fork();
	 if (child < 0) {
	    exit(255);
	 }
	 if (child == 0) {
	    run_member_of_ring(next);
	    exit(0);
	 }
	 next = child;
      }
      have_token = 1;
      run_member_of_ring(next);
      exit(0);
   }
   return master;
}

int main() {
   pid_t master = create_tokenring(3);
   int stat;
   waitpid(master, &stat, 0);
}
theon$ gcc -Wall -o tokenring tokenring.c
theon$ ./tokenring
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
[23964] has token
[23966] has token
[23965] has token
theon$ 

Frage zur Beispiellösung