Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/mman.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <assert.h>
- #include <semaphore.h>
- #include <time.h>
- #define N 5
- #define LEFT(x) (((x) - 1 + N)%N)
- #define RIGHT(x) (((x) + 1)%N)
- #define THINKING 0
- #define HUNGRY 1
- #define EATING 2
- /* Die Struktur der im SHM abgelegten Daten */
- struct shared_state {
- int state[N];
- sem_t mutex;
- sem_t s[N];
- };
- static struct shared_state *shared;
- int error;
- void msleep(unsigned int ms)
- {
- struct timespec ts;
- assert(ms > 0);
- assert(ms <= 10000);
- ts.tv_sec = ms / 1000;
- ts.tv_nsec = (ms * 1000000) % 1000000000;
- nanosleep(&ts, NULL);
- }
- void think(){
- msleep(100);
- }
- void eat(){
- msleep(100);
- }
- void test(int i){
- if(shared->state[i] == HUNGRY && shared->state[LEFT(i)]!=EATING && shared->state[RIGHT(i)]!=EATING){
- shared->state[i] = EATING;
- error = sem_post(&shared->s[i]);
- if(error != 0){
- perror("sem_post\n");
- exit(EXIT_FAILURE);
- }
- }
- }
- void take_forks(int i){
- error = sem_wait(&shared->mutex);
- if(error != 0){
- perror("sem_wait\n");
- exit(EXIT_FAILURE);
- }
- shared->state[i] = HUNGRY;
- test(i);
- error = sem_post(&shared->mutex);
- if(error != 0){
- perror("sem_post\n");
- exit(EXIT_FAILURE);
- }
- error = sem_wait(&shared->s[i]);
- if(error != 0){
- perror("sem_wait\n");
- exit(EXIT_FAILURE);
- }
- }
- void put_forks(int i){
- error = sem_wait(&shared->mutex);
- if(error != 0){
- perror("sem_wait\n");
- exit(EXIT_FAILURE);
- }
- shared->state[i] = THINKING;
- test(LEFT(i));
- test(RIGHT(i));
- error = sem_post(&shared->mutex);
- if(error != 0){
- perror("sem_post\n");
- exit(EXIT_FAILURE);
- }
- }
- void philosopher(int i){
- for (size_t j = 0; j < 10; j++) {
- printf("Philosoph %d denken\n",i);
- think();
- printf("Philosoph %d take forks\n",i);
- take_forks(i);
- printf("Philosoph %d essen\n",i);
- eat();
- printf("Philosoph %d put forks\n",i);
- put_forks(i);
- }
- }
- int main(int argc, char *argv[])
- {
- printf("Dinierende Philosophen mit Prozessen ...\n\n");
- pid_t pids[N];
- shared = mmap(NULL,sizeof(*shared),PROT_READ | PROT_WRITE,MAP_ANONYMOUS | MAP_SHARED,-1,0);
- if(shared == MAP_FAILED){
- perror("mmap\n");
- exit(EXIT_FAILURE);
- }
- printf("mmap\n");
- error = sem_init(&shared->mutex,0,1);
- if(error != 0){
- perror("sem_init\n");
- exit(EXIT_FAILURE);
- }
- for (size_t i = 0; i < N; i++) {
- error = sem_init(&shared->s[i],0,1);
- if(error != 0){
- perror("sem_init\n");
- exit(EXIT_FAILURE);
- }
- }
- for (size_t i = 0; i < N; i++) {
- printf("fork\n");
- pids[i] = fork();
- if(pids[i] < 0){
- perror("fork\n");
- exit(EXIT_FAILURE);
- } else if(pids[i] == 0){
- philosopher(i);
- exit(EXIT_SUCCESS);
- }
- }
- int status;
- pid_t terminated_process;
- int n = N;
- while(n > 0){
- terminated_process = wait(&status);
- printf("%d\n", terminated_process);
- --n;
- }
- printf("fertig\n");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement