Commit 61825fd6 authored by Sander van Veen's avatar Sander van Veen

Continued assignment 4 of Operating Systems.

parent 834a9bcd
PROG = ass4 PROG = ass4
CC = gcc CC = gcc
CFLAGS = -std=c99 -pedantic -Wall -Wextra # -O3 -D_POSIX_SOURCE CFLAGS = -std=c99 -pedantic -Wall -Wextra # -D_POSIX_SOURCE
LFLAGS = -lpthread LFLAGS = -lpthread
OFILES = main.o OFILES = main.o
RM = rm -f RM = rm -f
ifdef DEBUG
CFLAGS += -ggdb
else
CFLAGS += -O3
endif
$(PROG): $(OFILES) $(PROG): $(OFILES)
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $(OFILES) $(CC) $(CFLAGS) $(LFLAGS) -o $@ $(OFILES)
......
...@@ -2,33 +2,46 @@ ...@@ -2,33 +2,46 @@
#include "stdlib.h" #include "stdlib.h"
#include "pthread.h" #include "pthread.h"
/* temporary include: */
#include <unistd.h> #include <unistd.h>
int forks_len; int forks_len;
pthread_mutex_t *forks; pthread_mutex_t *forks;
pthread_cond_t wait_threshold; pthread_cond_t wait_threshold;
typedef struct diner_stats {
int meals;
} diner_stats;
int count = 0; int count = 0;
#define COUNT_LIMIT 12 #define COUNT_LIMIT 12
void *philo_start(void *t_id) { void *philo_start() {
printf("P #%d: hello everybody!\n", (int) t_id); int t_id = pthread_self();
printf("P #%d: hello everybody!\n", t_id);
for(int i = 0; i < 5; i++) { for(int i = 0; i < 5; i++) {
// Let each philosopher take a random fork. // Let each philosopher take a random fork.
int f = rand() % forks_len; //int f = rand() % forks_len;
pthread_mutex_lock(&forks[f]); //pthread_mutex_lock(&forks[f]);
pthread_mutex_lock(&forks[t_id]);
// Check the value of count and signal waiting thread when condition is pthread_mutex_lock(&forks[(t_id+1) % forks_len]);
// reached. Note that this occurs while mutex is locked.
pthread_cond_signal(&wait_threshold); //while( pthread_mutex_trylock(&forks[(t_id+1) % forks_len]) ) {
// pthread_mutex_unlock(&forks[t_id]);
// // Check the value of count and signal waiting thread when condition is
// // reached. Note that this occurs while mutex is locked.
// //pthread_cond_signal(&wait_threshold);
// sleep(1);
// pthread_mutex_lock(&forks[t_id]);
//}
// Do something useless // Do something useless
printf("P #%d: I'm eating.\n", (int) t_id); printf("P #%d: I'm eating.\n", t_id);
sleep(1);
pthread_mutex_unlock(&forks[f]); pthread_mutex_unlock(&forks[t_id]);
pthread_mutex_unlock(&forks[(t_id+1) % forks_len]);
} }
pthread_exit(NULL); pthread_exit(NULL);
...@@ -38,10 +51,11 @@ void host_start(int philos) { ...@@ -38,10 +51,11 @@ void host_start(int philos) {
pthread_attr_t attr; pthread_attr_t attr;
pthread_t *threads = malloc(philos * sizeof(pthread_t)); pthread_t *threads = malloc(philos * sizeof(pthread_t));
forks = malloc(philos * sizeof(pthread_mutex_t)); forks = malloc(philos * sizeof(pthread_mutex_t));
diner_stats *stats = malloc(philos * sizeof(diner_stats));
forks_len = philos; forks_len = philos;
int rc;
void *status; void *status;
int rc;
// POSIX standard specifies that threads are joinable by default, but // POSIX standard specifies that threads are joinable by default, but
// unfortunately, not all pthread implementations set threads as joinable by // unfortunately, not all pthread implementations set threads as joinable by
...@@ -52,14 +66,18 @@ void host_start(int philos) { ...@@ -52,14 +66,18 @@ void host_start(int philos) {
pthread_cond_init(&wait_threshold, NULL); pthread_cond_init(&wait_threshold, NULL);
// Create the cutlery. // Create the cutlery.
for(int i = 0; i < philos; i++) for(int i = 0; i < philos; i++) {
pthread_mutex_init(&forks[i], NULL); pthread_mutex_init(&forks[i], NULL);
stats[i].meals = 0 ;
}
// Invite the philosophers. // Invite the philosophers.
for(int i = 0; i < philos; i++) { for(int i = 0; i < philos; i++) {
printf("Host: inviting philo #%d\n", i); printf("Host: inviting philo #%d\n", i);
rc = pthread_create(threads+i, &attr, philo_start, (void*)&stats[i]);
if( (rc = pthread_create(&threads[i], &attr, philo_start, (void *)i)) ) { if( rc ) {
fprintf(stderr, "pthread_create() returned: %d\n", rc); fprintf(stderr, "pthread_create() returned: %d\n", rc);
exit(-1); exit(-1);
} }
......
TT\documentclass[twocolumn]{article} \documentclass[twocolumn]{article}
\usepackage{url} \usepackage{url}
\title{Web-based digital examination for specific learning areas} \title{Web-based digital examination for specific learning areas}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment