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

Continued assignment 4 of Operating Systems.

parent 834a9bcd
PROG = ass4
CC = gcc
CFLAGS = -std=c99 -pedantic -Wall -Wextra # -O3 -D_POSIX_SOURCE
CFLAGS = -std=c99 -pedantic -Wall -Wextra # -D_POSIX_SOURCE
LFLAGS = -lpthread
OFILES = main.o
RM = rm -f
ifdef DEBUG
CFLAGS += -ggdb
else
CFLAGS += -O3
endif
$(PROG): $(OFILES)
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $(OFILES)
......
......@@ -2,33 +2,46 @@
#include "stdlib.h"
#include "pthread.h"
/* temporary include: */
#include <unistd.h>
int forks_len;
pthread_mutex_t *forks;
pthread_cond_t wait_threshold;
typedef struct diner_stats {
int meals;
} diner_stats;
int count = 0;
#define COUNT_LIMIT 12
void *philo_start(void *t_id) {
printf("P #%d: hello everybody!\n", (int) t_id);
void *philo_start() {
int t_id = pthread_self();
printf("P #%d: hello everybody!\n", t_id);
for(int i = 0; i < 5; i++) {
// Let each philosopher take a random fork.
int f = rand() % forks_len;
pthread_mutex_lock(&forks[f]);
// 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);
//int f = rand() % forks_len;
//pthread_mutex_lock(&forks[f]);
pthread_mutex_lock(&forks[t_id]);
pthread_mutex_lock(&forks[(t_id+1) % forks_len]);
//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
printf("P #%d: I'm eating.\n", (int) t_id);
sleep(1);
printf("P #%d: I'm eating.\n", t_id);
pthread_mutex_unlock(&forks[f]);
pthread_mutex_unlock(&forks[t_id]);
pthread_mutex_unlock(&forks[(t_id+1) % forks_len]);
}
pthread_exit(NULL);
......@@ -38,10 +51,11 @@ void host_start(int philos) {
pthread_attr_t attr;
pthread_t *threads = malloc(philos * sizeof(pthread_t));
forks = malloc(philos * sizeof(pthread_mutex_t));
diner_stats *stats = malloc(philos * sizeof(diner_stats));
forks_len = philos;
int rc;
void *status;
int rc;
// POSIX standard specifies that threads are joinable by default, but
// unfortunately, not all pthread implementations set threads as joinable by
......@@ -52,14 +66,18 @@ void host_start(int philos) {
pthread_cond_init(&wait_threshold, NULL);
// Create the cutlery.
for(int i = 0; i < philos; i++)
for(int i = 0; i < philos; i++) {
pthread_mutex_init(&forks[i], NULL);
stats[i].meals = 0 ;
}
// Invite the philosophers.
for(int i = 0; i < philos; 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);
exit(-1);
}
......
TT\documentclass[twocolumn]{article}
\documentclass[twocolumn]{article}
\usepackage{url}
\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