Concurrency in Operating System
Concurrency in Operating System
I.
INTRODUCTION
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.
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:
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);
}
Output:
IV.
CONCLUSION
V.
REFERENCES