Os Report
Os Report
Operating System(20CS22003)
By
P. Raziya(22R11A05H1)
Sk. Sania(22R11A05H6)
G. Sreeja(23R15A0517)
Under the guidance of Dr. K. Krishna Jyothi
Accredited by NBA
(UGC Autonomous)
1
TABLE OF CONTENTS
1 3
Abstract
2 4
Acknowledgement
3 5
Purpose of study
4 Introduction 6
5 Problem Statement 7
6 System Configuration 13
7 System Architecture 14
8 Proposed Solution 16
9 Implementation 17
10 Screen Short 21
11 Application 23
12 Implementation 25
13 Screen Short 28
14 Conclusion 35
15 Bibliography 36
ABSTRACT
2
The dining philosopher’s problem is a classical synchronization challenge in computer
science, illustrating the complexities of allocating shared resources without causing deadlock
or resource starvation. This report presents a novel approach to simulating the dining
mapping the philosophers to election candidates and the forks to critical voting resources, we
concurrent access effectively. The simulation aims to ensure fair resource allocation, prevent
deadlocks, and maintain system liveness, reflecting the intricacies of real-world electoral
and signal (V)—can orchestrate complex interactions among multiple candidates, ensuring
orderly voting and resource distribution. The results provide insights into the effectiveness of
agent environments.
3
ACKNOWLEDGEMENT
We would like to acknowledge and give my warmest thanks to our faculty “Dr K Krishna
Jyothi” mam who made this work possible. Their guidance and advice carried us through all
the stages of writing our project. We would also like to thank our classmates for letting our
defence be an enjoyable moment, and for your brilliant comments and suggestions, thanks to
you. We would also like to give special thanks to our families as a whole for their continuous
support and understanding when undertaking my research and writing my project and
providing the required equipment. The project would not have been successful without their
4
PURPOSE OF STUDY
critical voting resources, this study aims to demonstrate how semaphores can effectively
manage concurrent access to shared resources. The simulation seeks to prevent common
issues such as deadlock and resource starvation, ensuring that all candidates can access
voting resources fairly and efficiently. This approach provides a practical illustration of
competitive environment. Additionally, this study aims to bridge theoretical concepts with
systems. By modelling an election process, the study highlights the relevance and
intended to inform the design and implementation of robust systems, offering a foundational
framework for future research. Ultimately, the study emphasizes the practical implications of
5
INTRODUCTION
systems. Traditionally, this problem involves a set of philosophers who alternately think and
eat, requiring access to shared forks. The core challenge is to design a protocol that allows
the philosophers to share resources without causing deadlock or starvation. This problem
serves as a powerful metaphor for various real-world scenarios where multiple entities vie
In this report, we extend the dining philosophers problem to simulate an election process,
using semaphores as the synchronization mechanism. Here, the philosophers are analogous
to election candidates, and the forks represent critical voting resources. This simulation aims
such as wait (P) and signal (V), we strive to ensure fair resource allocation, prevent
deadlocks, and maintain system liveness. This study not only provides a practical
demonstration of semaphores but also highlights their relevance and applicability in real-
6
PROBLEM STATEMENT
The dining philosopher’s problem is a classic example in computer science that deals with
how multiple people (philosophers) can share a limited number of resources (forks) without
running into issues like deadlock or unfair resource distribution. This problem helps us
understand how to manage resource sharing in systems where many processes run at the
same time. Can see the problem from figure 2 ,3,4,5. How it runs as infinity loops.
In this study, from fig1: we adapt the dining philosopher’s problem to simulate an election
process. Here, the philosophers represent election candidates, and the forks are the critical
voting resources they need. The main challenge is to create a system using semaphores (a
tool for managing resource access) that ensures each candidate gets a fair chance to access
the voting resources without causing deadlock or any candidate being unfairly blocked from
accessing resources. This adapted problem aims to find practical solutions for managing
7
8
Fig1: the seating arrangement of philosophers
Problem Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
while (1) {
printf ("Philosopher %d is thinking\n", id);
sleep (1);
9
printf ("Philosopher %d picked up fork %d (right)\n", id, (id + 1) %
NUM_PHILOSOPHERS);
// Eating
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int ids[NUM_PHILOSOPHERS];
10
// Initialize semaphores
// Wait for philosopher threads to finish (they never will in this example)
for ( i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_join(philosophers[i], NULL);
}
// Destroy semaphores
for ( i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_destroy(&forks[i]);
}
sem_destroy(&room);
return 0;
}
11
SCREENSHORT
Fig2: this is the out of the problem which runs in infinity loop
12
Fig3: this is the continues repetition after the single turn.
13
Fig5: the output does not end
SYSTEM CONFIGURATION
RAM 4 GB
14
Software System Configuration
Component Specification
Scripts C-Script
Server-Side Script C
15
SYSTEM ARTCHITECTURE
ASUMING A SITUATION:
Imagine a small town with five candidates running for office. Each candidate needs to gather
signatures from five different voting districts to qualify for the election. There are only five
signature collectors (one for each district), and each candidate can only work with one
collector at a time.
In the traditional dining philosopher’s problem, the candidates (philosophers) would need to
pick up two forks to eat, but in our adapted problem, they need to work with one signature
collector at a time. If two candidates try to get a signature from the same collector at the
same time, neither can proceed, leading to a deadlock. We can see the flow of System by
Fig6.
To solve this, we use semaphores. Each signature collector is protected by a semaphore that
ensures only one candidate can access the collector at a time. When a candidate wants to get
a signature, they perform a "wait" operation on the semaphore. If the semaphore is available,
the candidate proceeds. If not, the candidate waits until the collector is free. After getting the
signature, the candidate performs a "signal" operation to release the semaphore, making the
This way, we ensure that all candidates get their turn to gather signatures without any two
candidates blocking each other, preventing deadlock and ensuring fair access to the voting
resources.
16
FLOW OF THE SYSTEM
Fig6: this is the flow of our proposed solution and can see the processes how each
fellow philosopher is Limited by 3.
17
PROPOSED SOLUTION
The code begins by defining constants for the number of philosophers (N) and their states
(THINKING, HUNGRY, EATING). It also defines arrays to store the state of each
philosopher, their IDs (ph. il), and the number of times each philosopher has eaten
(eat_count).By fig7,8,9 we can assume the structure and output of the philosopher’s.
Semaphores mutex and S[N] are declared. mutex is used to ensure mutual exclusion when
test () function: This function checks if a philosopher can start eating. If a philosopher is
hungry and its neighbors are not eating, it changes its state to eating and signals that it can
take_fork() function: This function is called when a philosopher wants to eat. It sets the
philosopher's state to hungry and then tries to eat by calling test(). After that, it waits until it
put_fork () function: This function is called when a philosopher finish eating. It sets the
philosopher's state to thinking, increments it eat count, and then calls test () for its
neighbours.
running in a loop until the philosopher has eaten EAT_LIMIT times. In each iteration, it
alternates between thinking, taking forks, eating, and putting forks down.
18
main () function: It initializes semaphores, creates threads for each philosopher, and then
IMPLEMENTATION
Solution Code:
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
#define EAT_LIMIT 3 // Each philosopher eats 3 times
int state[N];
int phil[N] = {0, 1, 2, 3, 4};
int eat_count[N] = {0}; // Counter for the number of times each philosopher has eaten
sem_t mutex;
sem_t S[N];
19
if (state[phnum] == HUNGRY && state[LEFT] != EATING && state[RIGHT] !=
EATING)
}
state[phnum] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and %d\n", phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is Eating\n", phnum + 1);
sem_post(&S[phnum]);
}
}
void take_fork(int phnum)
{
sem_wait(&mutex);
state[phnum] = HUNGRY;
printf("Philosopher %d is Hungry\n", phnum + 1);
test(phnum);
sem_post(&mutex);
sem_wait(&S[phnum]);
sleep(1);
}
20
printf("Philosopher %d putting fork %d and %d down\n", phnum + 1, LEFT + 1, phnum +
1);
printf("Philosopher %d is thinking\n", phnum + 1);
eat_count[phnum]++;
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
22
SCREENSHORT
Fig7: The proposed solution in which each philosopher eats 3 times consider as per day.
24
APPLICATIONS:
The Dining Philosophers problem illustrates resource sharing and deadlock prevention in
Operating Systems: Manages resource allocation and concurrency control using semaphores
and mutexes to prevent deadlocks and ensure fair access to resources like files and memory.
reservations efficiently to avoid network contention and ensure smooth data transmission.
Synchronizes threads to avoid race conditions and manage shared resources effectively,
Real-Time Systems: Schedules tasks and handles priority inversion to meet deadlines and
Election Algorithms: In distributed systems, election algorithms ensure that a single process
(like a leader or coordinator) is selected from a group of processes to avoid conflicts and
manage resources efficiently. Techniques from the Dining Philosophers problem help in
designing these algorithms to avoid deadlocks and ensure fair election processes.
25
These applications use semaphore-based synchronization techniques to achieve efficient and
fair resource allocation, preventing deadlocks and ensuring reliable operation. From fig10,11
A semaphore typically consists of a non-negative integer counter and two atomic operations:
wait (also known as P or down) and signal (also known as V or up). The wait operation
decrements the semaphore's counter and blocks the calling thread if the counter becomes
negative, while the signal operation increments the counter and unblocks a waiting thread if
necessary.
In conclusion, from figure 12,13,14 we can see the end of voting and consider the all
semaphores done and results are announced. The Dining Philosophers problem effectively
demonstrates the complexities of managing concurrent resource allocation and the potential
semaphores to control access to shared resources (forks), we can ensure mutual exclusion
concurrently without conflict. This approach not only illustrates key concepts in concurrent
programming but also provides a practical framework for addressing similar synchronization
26
IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM_CANDIDATES 5
sem_t booths[NUM_CANDIDATES];
sem_t room; // Semaphore to limit the number of candidates accessing booths at the same
time
void* candidate(void* num) {
int id = *(int*)num;
while (1) {
printf("Candidate %d is preparing to vote\n", id);
sleep(1);
sem_wait(&room); // Try to enter the room
// Pick up left booth
sem_wait(&booths[id]);
printf("Candidate %d picked up booth %d (left)\n", id, id);
// Pick up right booth
sem_wait(&booths[(id + 1) % NUM_CANDIDATES]);
printf("Candidate %d picked up booth %d (right)\n", id, (id + 1) %
NUM_CANDIDATES);
// Voting
printf("Candidate %d is voting\n", id);
sleep(1);
// Put down right booth
sem_post(&booths[(id + 1) % NUM_CANDIDATES]);
27
printf("Candidate %d put down booth %d (right)\n", id, (id + 1) % NUM_CANDIDATES);
int main() {
pthread_t candidates[NUM_CANDIDATES];
int ids[NUM_CANDIDATES];
int i;
// Initialize semaphores
sem_init(&room, 0, NUM_CANDIDATES - 1); // Allow up to NUM_CANDIDATES-1
candidates to enter room
for (i = 0; i < NUM_CANDIDATES; i++) {
sem_init(&booths[i], 0, 1);
}
28
// Create candidate threads
for (i = 0; i < NUM_CANDIDATES; i++) {
ids[i] = i;
pthread_create(&candidates[i], NULL, candidate, &ids[i]);
}
// Wait for candidate threads to finish (they never will in this example)
for (i = 0; i < NUM_CANDIDATES; i++) {
pthread_join(candidates[i], NULL);
}
// Destroy semaphores
for (i = 0; i < NUM_CANDIDATES; i++) {
sem_destroy(&booths[i]);
}
sem_destroy(&room);
return 0;
}
29
SCREENSHORT
Fig10: In this 1st 3 areas the voters started their voting & leaving the booth.
Fig11: In this next 4 booth voters are voting in the booth came (ri8) and left
30
Fig12: In this the all the candidates of all the areas came and voted in there
Respective booth and voting are completed.
31
// Continues………………
#define NUM_POLITICIANS 5
#define NUM_AREAS 10
int area = rand() % NUM_AREAS; // Randomly choose an area for the politician to vote
sem_wait(&booths[area]);
32
// Lock the votes array and update the vote count
pthread_mutex_lock(&vote_lock);
votes[id]++; // Increment the vote count for this politician
pthread_mutex_unlock(&vote_lock);
int main() {
pthread_t politicians[NUM_POLITICIANS];
int ids[NUM_POLITICIANS];
int i;
// Initialize semaphores and mutex
33
for (i = 0; i < NUM_AREAS; i++) {
sem_init(&booths[i], 0, 1);
pthread_mutex_init(&vote_lock, NULL);
34
if (votes[i] > max_votes) {
max_votes = votes[i];
winner = i;
}
}
35
SCREENSHORT
Fig14: Here we are declaring the results based on voters and candidates voting.
36
CONCLUSION
Hence, we concluded that the use of semaphores and mutexes for managing resource
allocation and synchronization in concurrent systems, applied to voting processes. In the first
example, candidates simulate the Dining Philosophers problem, using semaphores to ensure
that only a limited number can enter the voting room simultaneously and manage booth
access to avoid deadlock. The second example involves politicians voting in multiple areas,
with semaphores controlling booth entry and a mutex ensuring accurate vote tallying without
management, preventing contention, and ensuring fair access. The practical implementation
includes random vote assignments and tabular election results, demonstrating the techniques'
37
BIBILOGRAHY
www.google.com
www.wikipedia.com
www.gcet.library.com
38