08 Dininig Philosophers

You might also like

You are on page 1of 8

Department of Computer Science & Engineering

Hanyang University

Operating Systems (Spring 2013)


Week 8: Dining Philosophers

(
)

(MUTual EXclusion)
(Semaphore)

entry section :

exit section :

critical section :

remainder section :

*
do {
wait(mutex); // entry section
// critical section
signal(mutex); // exit section
// remainder section
}

- 1 -

Department of Computer Science & Engineering


Hanyang University

Mutex & Condition Variable

: http://linux.die.net/

mutex
- pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);

mutex lock
- pthread_mutex_lock(pthread_mutex_t *mutex);

mutex unlock
- pthread_mutex_unlock(pthread_mutex_t *mutex);

mutex
- pthread_mutex_destroy(pthread_mutex_t *mutex);

n Condition Variables :

condition variable
- pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);

condition variable
- pthread_cond_signal(pthread_cond_t *cond);
- pthread_cond_broadcast(pthread_cond_t *cond);

- 2 -

Department of Computer Science & Engineering


Hanyang University

condition variable
- pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
- pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex);

condition variable
- pthread_cond_destroy(pthread_cond_t *cond);

Example
#include <pthread.h>
#include <string.h>
#include <unistd.h>
pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t thread_cond = PTHREAD_COND_INITIALIZER;
struct com_data {
int a;
int b;
};
struct com_data mydata;
void *do_write(void *data)
{
mydata.a = 0;
mydata.b = 0;
while(1) {
pthread_mutex_lock(&mutex_lock);
mydata.a = random() % 6000;
mydata.b = random() % 6000;
pthread_cond_signal(&thread_cond);
pthread_mutex_unlock(&mutex_lock);
sleep(2);
}
}
void *do_read(void *data)
{
while(1) {
pthread_mutex_lock(&mutex_lock);
pthread_cond_wait(&thread_cond, &mutex_lock);
printf("%4d + %4d = %4d\n", mydata.a, mydata.b,
mydata.b);
pthread_mutex_unlock(&mutex_lock);
}
}
int main()
{
pthread_t p_thread[2];
int thr_id;

- 3 -

mydata.a

Department of Computer Science & Engineering


Hanyang University

int status;
int a = 1;
int b = 2;
thr_id = pthread_create(&p_thread[0], NULL, do_write, (void *)&a);
thr_id = pthread_create(&p_thread[1], NULL, do_read, (void *)&b);
pthread_join(p_thread[0], (void **) status);
pthread_join(p_thread[1], (void **) status);
return 0;
}

(Semaphore)

Example
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define WORK_SIZE 1024
sem_t bin_sem;
char work_area[WORK_SIZE];

- 4 -

Department of Computer Science & Engineering


Hanyang University

void *thread_function(void *arg)


{
sem_wait(&bin_sem);
while(strncmp("end", work_area, 3) != 0) {
printf("You input %d characters\n", strlen(work_area) -1);
sem_wait(&bin_sem);
}
pthread_exit(NULL);
}
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = sem_init(&bin_sem, 0, 0);
if (res != 0) {
perror("Semaphore initialization failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Input some text. Enter 'end' to finish\n");
while(strncmp("end", work_area, 3) != 0) {
fgets(work_area, WORK_SIZE, stdin);
sem_post(&bin_sem);
}
printf("\nWaiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined\n");
sem_destroy(&bin_sem);
exit(EXIT_SUCCESS);
}

- 5 -

Department of Computer Science & Engineering


Hanyang University


(Mutual Exclusion) -
.
(Progress) - ,
.
(Bounded Waiting) - (Starvation) ,
.

Starvation

- 6 -

Department of Computer Science & Engineering


Hanyang University

Deadlock .

n 5 .
1.
.
2. Deadlock

.
3. 5 .
4.
.

- 7 -

Department of Computer Science & Engineering


Hanyang University

: 5 1 3

: 5 2 11 59 (30% )

1.
-
-
2. (hwp, doc)
-
-
-
-

1. _.zip
ex) 2013000001_.zip
2. [] (oslecture@gmail.com)
ex) [8] 2013000001
3. ver.2
( )
ex) [8] 2013000001 ver.2

(60)
1. deadlock (-30)
2. starvation (+10)

- 8 -

You might also like