Operating System Project
Operating System Project
(Autonomous)
Operating System Assignment (COM-302)
Analysis Report
Submitted By:-
Semester - 3rd
Section - A3
1
CONTENTS
1. Tasks 3
2. Summary 3-4
3. Introduction 5-6
4. Implementation 6 - 11
Details
5. Code Snippet 11 - 15
6. Code Output 16 - 17
7. Algorithm 18 - 22
Explanation
8. Conclusion 22 - 26
9. Reference & 26 - 27
Repository Link
2
TASKS :
SUMMARY:
3
informed decision-making in CPU scheduling strategy selection. The
subsequent sections of the report delve into the specifics of the
implementation, test cases, metrics, results, and a thorough discussion of the
observed outcomes.
4
INTRODUCTION :
5
one set of threads, the producers, generate data, while another set, the
consumers, process that data. The critical aspect lies in orchestrating this
interaction to avoid potential race conditions, ensuring data integrity, and
maintaining the overall system's stability.
This program delves into the intricacies of synchronization and race
condition prevention through the strategic use of semaphores or mutex locks.
By carefully orchestrating access to shared resources, such as buffers or data
structures, the program aims to provide a robust solution that guarantees
orderly execution, minimizing the risk of conflicts and data corruption.
As we delve into the implementation details, this report will elucidate the
mechanisms employed to synchronize the activities of producers and
consumers, shedding light on how semaphores or mutex locks are
instrumental in facilitating a coordinated and secure execution flow. The
ensuing sections will provide a comprehensive overview of the program, its
synchronization strategies, and an in-depth analysis of the methodologies
employed to steer clear of race conditions, thereby ensuring a resilient and
thread-safe solution to the classic Producer-Consumer problem.
Implementation Details :
● TASK 1 :
Design Overview :
Data Structures :
➢ Process Representation :
6
A data structure is employed to represent processes,
encompassing attributes such as process ID, burst time, priority,
etc.
➢ Scheduling Queue :
Algorithm Implementation :
➢ First-Come-First-Served (FCFS) :
➢ Priority Scheduling :
Metrics Calculation :
➢ Turnaround Time :
➢ Waiting Time :
7
➢ Response Time :
Denotes the time taken from submitting a process until the first
response is received.
➢ Test Cases :
Experimental Setup :
Code Modularity :
8
● TASK 2 :
1. Language Choice :
The solution is implemented in C, a language well-suited for systems
programming and thread management. The POSIX threads (pthreads)
library is used for multi-threading support.
2. Synchronization Mechanisms :
○ Semaphores (sem_t) :
9
○ Threads:
● Two threads are created - one for the producer and one
for the consumer.
● The pthread_create function is used to spawn threads,
and pthread_join is used to wait for their completion.
5. Main Function :
10
6. Conclusion :
Code Snippet :
➢ TASK 1 :
11
12
13
➢ TASK 2:
14
15
CODE OUTPUT’S :
➢ TASK 1 :
16
➢ TASK 2 :
17
Algorithm Explanation :
● Task 1 :
1. First-Come-First-Served (FCFS):
● Algorithm:
● Implementation Steps:
● Algorithm:
● Implementation Steps:
18
3. Round Robin (RR):
● Algorithm:
● Implementation Steps:
4. Priority Scheduling:
● Algorithm:
● Implementation Steps :
● Task 2 :
Algorithm Explanation:
19
A shared buffer is used to store the data produced by
producers and consumed by consumers. In this example,
buffer is an array with a fixed size (BUFFER_SIZE).
Two indices, in and out, keep track of the next available
slot in the buffer for the producer to write (in) and the
next item to be consumed by the consumer (out).
Semaphores Initialization:
Producer Thread:
In the producer function, a loop is run to simulate the production of data. For
each iteration, a random item is produced.
● sem_wait(&empty): Waits if the buffer is full, i.e., no empty
slots for the producer.
● sem_wait(&mutex): Locks the buffer to ensure exclusive
access.
● The produced item is added to the buffer, and the in index is
updated.
● sem_post(&mutex): Unlocks the buffer.
● sem_post(&full): Signals that there is a new item in the buffer
available for consumption.
Consumer Thread:
20
● The item is consumed from the buffer, and the out index is
updated.
● sem_post(&mutex): Unlocks the buffer.
● sem_post(&empty): Signals that there is an empty slot in the
buffer available for production.
Main Function:
● Mutex (sem_mutex):
21
This combination of mutex locks and semaphores ensures that the producer
and consumer threads cooperate properly, preventing data corruption or race
conditions during concurrent access to the shared buffer. The semaphores
regulate the flow of producers and consumers, ensuring that the buffer is
accessed in a controlled and synchronized manner.
CONCLUSION :
● TASK 1 :
Key Findings:
● FCFS:
● SJF:
Provides fair execution for all processes with a fixed time quantum.
May have higher waiting times for processes with longer burst times.
● Priority Scheduling:
22
Allows for prioritizing processes based on predefined priorities.
Prone to "priority inversion" issues and may lead to starvation of
lower-priority processes.
Recommendations:
Future Considerations:
23
Scheduling or Multilevel Feedback Queue Scheduling for more
sophisticated analysis.
In summary, the choice of a CPU scheduling algorithm depends on the
specific characteristics and requirements of the system. The analysis and
comparison of these algorithms provide a foundation for making informed
decisions based on the nature of the workload and desired system behavior.
Future considerations may involve exploring more advanced scheduling
strategies for enhanced performance in diverse scenarios.
● TASK 2 :
● Mutex Locks:
● Semaphores:
24
● Buffer Management:
Ensuring Synchronization:
Semaphores are used to manage the state of the buffer. The full
semaphore ensures that the producer waits when the buffer is
full, and the empty semaphore ensures that the consumer waits
when the buffer is empty.
25
sections, preventing race conditions that could arise from
concurrent modification of shared data.
● Semaphore Signaling:
Future Considerations:
○ https://chat.openai.com/c/d204f853-6b36-42f9-a7f8-8adbcde384b1
○ https://www.geeksforgeeks.org/program-for-shortest-job-first-or-sjf-cpu
-scheduling-set-1-non-preemptive/
○ https://www.geeksforgeeks.org/producer-consumer-problem-using-sema
phores-set-1/
○ https://medium.com/@sohamshah456/producer-consumer-programmin
g-with-c-d0d47b8f103f
26
TASK 1 :
● https://github.com/sushean/OS-Assignment/blob/main/1.cs
TASK 2 :
● https://github.com/sushean/OS-Assignment/blob/main/Program.cs
27