0% found this document useful (0 votes)
159 views

Concurrency in Operating System

The dining philosophers problem is a classic synchronization problem involving the allocation of limited resources amongst a group of processes in a deadlock-free and starvation-free manner. The problem describes five philosophers who sit around a table sharing five forks, where each philosopher needs two forks to eat and must wait for both forks to become available. The document presents code for simulating the dining philosophers problem using semaphores to synchronize access to shared forks between philosopher processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Concurrency in Operating System

The dining philosophers problem is a classic synchronization problem involving the allocation of limited resources amongst a group of processes in a deadlock-free and starvation-free manner. The problem describes five philosophers who sit around a table sharing five forks, where each philosopher needs two forks to eat and must wait for both forks to become available. The document presents code for simulating the dining philosophers problem using semaphores to synchronize access to shared forks between philosopher processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Concurrency in Operating System

Muhammad Mazarino Zhafir


1106018751, [email protected]
Department of Electrical Engineering, Faculty of Engineering
Universitas Indonesia
Depok 16424

Abstract This paper will explain abut concurrency in


operating system, including mutual-exclusion, semaphore,
producer consumer, and dining philosopher. There are
explanation about definition, function, and then
simulation.
Keywords Mutual Exclusion, semaphore, producer
consumer, dining philosopher

I.

INTRODUCTION

Concurrency is a fundamental thing in operating system.


Concurrency is a condition when more than one process
running at the same time. This processes can running
independently or together with another processes. So, there
will be a communication, interaction, and synchronization
among the processes.

II.
A.

BASIC THEORY

Mutual Exclusion
Sometimes, many processes needs a resources at the
same time. This resources called Critical Resource. A part of
program that using Critical Resources is entering Critical
Section/Region. Only one program allowed to enter the
Critical Region. We cant make any prediction about the
output, because it depends on the running processes that
competing each other. This condition called Race Condition.
We must prevent the Race Condition, so that we can know the
prediction of the output and the output is not depends on the
running processes. Mutual Exclusion check and ensure that in
a time, only one process is accessing resources. So, there
wont be more than one process at the same time.
The criteria of Mutual Exclusion is
a. Mutual-exclusion must be guaranteed.
b. Only one process is allowed to enter the critical
section at the same time.
c. Process that currently in noncritical section, may not
blocked the other processes to enter the critical
section.
d. The waiting time to enter the critical region may not
be long, to prevent deadlock and starvation.

e.

f.

After a process done, there is no process in critical


section. So the next process must enter critical
section immidiately, without delay time.
There is no assumption about the relative speed
or amount of the processes.

B.

Semaphore
Semaphore is a data structure that used to process
synchronization, and solve the problem when more than one
process is running at the same time. And then the order of
process also must be set.
There are two kinds of semaphore:
a. Binary semaphore. Only have 1 or 0 value. Well
known as primitive semaphore.
b. Counting semaphore. Have 0, 1, and the other
integer value. Many operating system does not
implement this semaphore directly, but by using
the basic binary semaphore.
C.

Producer-consumer
Producer and consumer is two kinds of processes, that
willing to communicate. Producers have information to send
to the consumers, and the information placed in a buffer
which have limited size. And then consumers obtain it. The
buffer is a shared resource, therefore, use of the buffer must
be synchronized.
One process be a producer, produces information and
puts it in the buffer. Meanwhile the other process be a
consumer, consumes information from the buffer. To
synchronize these processes, when the buffer is full, the
producer will be blocked. Otherwise, when the buffer is
empty, consumer will be blocked.
D.

Dining-philosophers
To help understand the dining philosophers problem,
here is a story. Five philosophers sit around a circular table.
Each philosopher spends his life alternatively thinking and
eating. In the centre of the table is a large plate of spaghetti. A
philosopher needs two forks to eat a helping of spaghetti. So,

five forks only can afford for two philosopers, while three
other philosophers must wait.
Output:

The dining philosophers problem is a classic


synchronization problem involving the allocation of limited
resources amongst a group of processes in a deadlock-free and
starvation-free manner.
III.
A. Producer-Consumer
Code:

SIMULATION
B. Dining-philosopher
Code:
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N
sem_t mutex;
sem_t S[N];
void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);
int state[N];
int phil_num[N]={0,1,2,3,4};
int main()
{

int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phi
l_num[i]);
printf("Philosopher %d is thinking\n",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}

void put_fork(int ph_num)


{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d
down\n",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinking\n",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}

Output:

void *philospher(void *num)


{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
void take_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungry\n",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}
void test(int ph_num)
{
if (state[ph_num] == HUNGRY && state[LEFT]
!= EATING && state[RIGHT] != EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and
%d\n",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is Eating\n",ph_num+1);
sem_post(&S[ph_num]);
}
}

IV.

CONCLUSION

We can ensure that only one process running in a same


time with mutual-exclusion. Semaphore is a signal that used to
synchronize the process. The most problem occured in
concurrency programming is producer-consumer and dining
philosopher.
There is still a problem in solving dining philosopher
problem. It still allow the deadlock when all philosophers take
the forks on the left at the same time. Then, there is no forks
are available

V.

REFERENCES

[1] Stallings, William. (2004) Operating System, Sixth


Edition. Pearson Prentice Hall.
[2] Sari, Riri Fitri Dr. Ir. MM. (2005) Sistem Operasi
Modern. Penerbit Andi.
[3] Silberschatz, Abraham; Peterson, James L.
(1988). Operating Systems Concepts. AddisonWesley.
[4] Tanenbaum, Andrew S. (2008) Modern Operating
Systems. Pearson Prentice Hall

You might also like