Simulation Based Assignment
Academic Task Number: CA1 Maximum Marks: 30
Course code: CSE316 Course title: Operating Systems
Date of allotment: 28/09/2023 Date of Submission: 28/10/2023
Allocation of Problems as follows:
Assigned
Serial
Roll No. Problem Class Link to Join and submit code online
No.
No.
1 2 14 72 37 49 60 1 https://onlinegdb.com/classroom/invite/PMpP0ZPuw
2 3 15 27 38 50 61 2 https://onlinegdb.com/classroom/invite/XRcImbYxh
3 4 16 28 39 51 62 3 https://onlinegdb.com/classroom/invite/HBb5DBTN7
4 5 17 29 40 52 63 4 https://onlinegdb.com/classroom/invite/ghUs0Nf4U
5 6 18 30 41 53 64 5 https://onlinegdb.com/classroom/invite/cWvEUGS7p
6 7 19 31 42 54 65 6 https://onlinegdb.com/classroom/invite/1u3zLFxrU
7 8 20 32 44 55 66 7 https://onlinegdb.com/classroom/invite/V72HYla92
8 9 21 33 45 56 67 8 https://onlinegdb.com/classroom/invite/dutYEPe-P
9 11 23 34 46 57 68 9 https://onlinegdb.com/classroom/invite/Dd2f_B4Nw
10 12 24 35 47 58 69 10 https://onlinegdb.com/classroom/invite/1jVh66lxP
11 13 25 36 48 59 70 11 https://onlinegdb.com/classroom/invite/jZRwjBL3-
12 1 71 26 73 12 https://onlinegdb.com/classroom/invite/xxMKKHvRn
Evaluation Rubrics
Criteria Marks Exemplary (100%) Satisfactory (80%) Needs Improvement
weightage (50%)
Functionality 10 The program works The program works mostly The program does not work
correctly and completely correctly and meets most correctly or does not meet
meets all the requirements of the requirements the requirements specified
specified in the assignment. specified in the in the assignment.
assignment.
Efficiency 5 The program is highly The program is somewhat The program is not efficient
efficient and has minimal efficient and has moderate and has high resource
resource consumption. resource consumption. consumption.
Readability 5 The code is very readable, The code is mostly The code is not very
with clear and concise readable, with some readable, with unclear
comments, variable and comments, variable and comments, variable and
function names, and function names, and function names, and poor
formatting. formatting that could be formatting.
improved.
Modularity 1 The program is well- The program is somewhat The program is not well-
organized and divided into organized and divided into organized and lacks
modules or functions that some modules or modules or functions.
perform specific tasks. functions.
Testing 2 The program is thoroughly The program is tested with The program is not
tested with different test some test cases, but may adequately tested and may
cases to ensure that it not cover all scenarios or have significant issues or
functions as intended in have adequate testing. bugs.
various scenarios.
Timeliness 2 The program is The program is The program is
completed/uploaded within completed/uploaded completed/uploaded
the specified timeframe or slightly after the deadline, significantly after the
deadline. but within a reasonable deadline or is not
timeframe. completed.
Understanding 5 The student is having proper The student fully The student is unable to
understanding of the understands the code that provide clarity on the logic
uploaded code. was uploaded. or syntax of the uploaded
code.
Problem 1: A host of a party has invited N > 2 guests to his house. Due to fear of Covid-19 exposure,
the host does not wish to open the door of his house multiple times to let guests in. Instead, he
wishes that all N guests, even though they may arrive at different times to his door, wait for each
other and enter the house all at once. The host and guests are represented by threads in a
multithreaded program. Given below is the pseudocode for the host thread, where the host waits for
all
guests to arrive, then calls openDoor(), and signals a condition variable once. You must write the
corresponding code for the guest threads. The guests must wait for all N of them to arrive and for
the host to open the door, and must call enterHouse() only after that. You must ensure that all N
waiting guests enter the house after the door is opened. You must use only locks and condition
variables for synchronization.
The following variables are used in this solution: lock m, condition variables cv host and cv guest,
and integer guest count (initialized to 0). You must not use any other variables in the guest for
synchronization.
//host
lock(m)
while(guest_count < N)
wait(cv_host, m)
openDoor()
signal(cv_guest)
unlock(m).
Problem 2: You are given a shared data structure that can be read by multiple threads simultaneously,
but can only be written to by one thread at a time. Readers can access the data structure concurrently,
but writers must have exclusive access. When a writer is writing to the data structure, no readers can
access it. Similarly, when a reader is reading from the data structure, no writers can write to it. Your
task is to design a program that coordinates the readers and writers, ensuring that no reader or writer
is left waiting indefinitely and that the data structure is not accessed concurrently by readers and
writers.
You should implement a solution using one of the following synchronization mechanisms: locks,
semaphores, or monitors. Your program should also include a mechanism to test the correctness of
your solution, such as printing debug information or using assertions to verify that the data structure
is being accessed correctly.
Problem 3: Consider the classic readers-writers synchronization problem described below. Several
processes/threads
wish to read and write data shared between them. Some processes only want to read the shared
data (“readers”), while others want to update the shared data as well (“writers”). Multiple readers
may concurrently access the data safely, without any correctness issues. However, a writer must
not access the data concurrently with anyone else, either a reader or a writer. While it is possible
for each reader and writer to acquire a regular mutex and operate in perfect mutual exclusion, such
a solution will be missing out on the benefits of allowing multiple readers to read at the same time
without waiting for other readers to finish. Therefore, we wish to have special kind of locks called
reader-writer locks that can be acquired by processes/threads in such situations. These locks have
separate lock/unlock functions, depending on whether the thread asking for a lock is a reader or
writer. If one reader asks for a lock while another reader already has it, the second reader will also
be granted a read lock (unlike in the case of a regular mutex), thus encouraging more concurrency
in the application.
Write down pseudocode to implement the functions readLock, readUnlock, writeLock, and
writeUnlock that are invoked by the readers and writers to realize reader-writer locks. You must use
condition variables and mutexes only in your solution.
Problem 4: You are given a group of philosophers seated at a round table, with a fork to the left and
right of each philosopher. To eat spaghetti, a philosopher must hold both forks. However, only one
philosopher can hold a fork at a time. If a philosopher tries to pick up a fork that is already being held
by another philosopher, they must wait until the fork is released. Your task is to design a program that
coordinates the philosophers, ensuring that no philosopher is left waiting indefinitely and that no two
philosophers are holding the same fork simultaneously.
You should implement a solution using one of the following synchronization mechanisms: locks,
semaphores, or monitors. Your program should also include a mechanism to test the correctness of
your solution, such as printing debug information or using assertions to verify that the forks are being
accessed correctly.
Problem 5: You are given a file that needs to be processed by a program using the open, read, write,
lseek, and close system calls. Your task is to write a program that performs the following steps:
1. Open the file named as input.txt using the open system call, specifying the file name and the
appropriate flags and permissions.
2. Use the lseek system call to move the file pointer to a specific position (20th character) in the
file.
3. Use the read system call to read 30 bytes from the file into a buffer in memory.
4. Use the write system call to write data from the buffer to another file named as output.txt as
well as on standard output.
5. Close the file using the close system call to release any resources associated with it.
Your program should include error handling for each system call to detect and handle any errors that
may occur. You may also need to include additional logic to handle corner cases, such as reaching the
end of the file or encountering unexpected data. Additionally, you may want to include options to
specify the input and output file names, the position to start reading from, and the number of bytes
to read at a time.
Problem 6: Consider a clinic with one doctor and a very large waiting room (of infinite capacity). Any
patient
entering the clinic will wait in the waiting room until the doctor is free to see her. Similarly, the
the doctor also waits for a patient to arrive to treat. All communication between the patients and
the doctor happens via a shared memory buffer. Any of the several patient processes, or the doctor
process can write to it. Once the patient “enters the doctors office”, she conveys her symptoms
to the doctor using a call to consultDoctor(), which updates the shared memory with the
patient’s symptoms. The doctor then calls treatPatient() to access the buffer and update it
with details of the treatment. Finally, the patient process must call noteTreatment() to see the
updated treatment details in the shared buffer, before leaving the doctor’s office. A template code
for the patient and doctor processes is shown below. Enhance this code to correctly synchronize
between the patient and the doctor processes. Your code should ensure that no race conditions
occur due to several patients overwriting the shared buffer concurrently. Similarly, you must
ensure that the doctor accesses the buffer only when there is valid new patient information in it,
and the patient sees the treatment only after the doctor has written it to the buffer. You must use
only semaphores to solve this problem. Clearly list the semaphore variables you use and their
initial values first. Please pick sensible names for your variables.
Problem 7: You are tasked with writing a program that dynamically allocates memory for a data
structure. Your program should perform the following steps:
1. Prompt the user to enter the number of elements to allocate memory for.
2. Use the malloc() function to allocate memory for the specified number of elements.
3. Prompt the user to enter values for the allocated memory.
4. Store the user's input in the allocated memory.
5. Print the values stored in the allocated memory.
6. Use the free() function to release the memory allocated in step 2.
Your program should include error handling for the memory allocation step to detect and handle any
errors that may occur. You may also want to include options to specify the size and type of the
elements to be allocated, as well as the input and output formats. Additionally, you may want to
consider using functions or structs to organize and manipulate the allocated memory.
Problem 8: You are given a list of page requests and need to implement the Least Recently Used
(LRU) page replacement algorithm to simulate memory allocation. Your task is to write a program
that performs the following steps:
1. Prompt the user to enter the size of the page table and the sequence of page requests.
2. Use an array or linked list to represent the page table.
3. Traverse the page requests, and for each request:
a. Check if the requested page is already in the page table.
b. If the page is not in the page table, find the least recently used page and replace it
with the requested page.
c. If the page is in the page table, update its position to indicate that it was recently
used.
4. Print out the sequence of page faults.
Your program should implement the LRU algorithm correctly, keeping track of the most recent use
of each page and selecting the least recently used page to replace. You may want to use a data
structure, such as a queue or stack, to keep track of the order in which pages were last used.
Additionally, you may want to include options to specify the number of page requests, the page size,
and the input and output formats.
Problem 9: You are given a task to implement a program that uses threads to perform two separate
operations concurrently, where one thread generates random numbers and puts them into a buffer,
and the other thread retrieves the numbers from the buffer and computes the average. Your task is
to write a program that performs the following steps:
1. Create two threads: one for producing numbers and the other for computing the average.
2. Initialize a buffer that can hold a specified number of integers.
3. The producer thread should generate a random integer and put it in the buffer. It should wait
if the buffer is full.
4. The consumer thread should retrieve an integer from the buffer and add it to a sum variable.
It should wait if the buffer is empty.
5. After retrieving all numbers from the buffer, the consumer thread should calculate the average
and print it out.
6. The program should run until all numbers are produced, consumed, and averaged.
Your program should use synchronization primitives, such as semaphores or mutexes, to ensure that
the threads are properly coordinated and do not access the buffer or the sum variable simultaneously.
Additionally, you may want to include options to specify the buffer size, the range of random numbers
to generate, and the number of integers to produce and consume.
Problem 10: You are given a list of disk requests in the order in which they were received and need
to implement the First-Come, First-Served (FCFS) disk scheduling algorithm to simulate disk
scheduling. Your task is to write a program that performs the following steps:
1. Prompt the user to enter the starting disk head position and the list of disk requests.
2. Use an array or linked list to represent the list of disk requests.
3. Traverse the list of disk requests in the order in which they were received, and for each
request:
a. Calculate the distance between the current head position and the requested position.
b. Add the distance to a running total of seek time.
c. Update the head position to the requested position.
4. Print out the total seek time.
Your program should implement the FCFS algorithm correctly, servicing requests in the order in which
they were received. Additionally, you may want to include options to specify the number of disk
requests, the range of disk positions, and the input and output formats.
Problem 11: You are tasked with writing a program that reads a text file, replaces a given word with
a new word, and writes the modified text back to the same file using the open, read, write, lseek, and
close system calls. Your program should perform the following steps:
1. Prompt the user to enter the name of the text file to be modified, the word to be replaced,
and the new word.
2. Use the open() function to open the file for reading and writing.
3. Use the lseek() function to move the file pointer to the beginning of the file.
4. Use the read() function to read the contents of the file into a buffer.
5. Use the strstr() function to search for the specified word in the buffer.
6. If the word is found, use the memcpy() function to replace it with the new word.
7. Use the lseek() function to move the file pointer to the beginning of the file.
8. Use the write() function to write the modified contents of the buffer back to the file.
9. Use the close() function to close the file.
Your program should include error handling for the file opening and reading steps to detect and
handle any errors that may occur. Additionally, you may want to include options to specify the
location of the word to be replaced, the input and output formats, and whether to replace all
occurrences of the word or just the first occurrence.
Problem 12: Consider the following classical synchronization problem called the barbershop problem.
A barbershop consists of a room with N chairs. If a customer enters the barbershop and all chairs are
occupied, then the customer leaves the shop. If the barber is busy, but chairs are available, then the
customer sits in one of the free chairs and awaits his turn. The barber moves onto the next waiting
seated customer after he finishes one hair cut. If there are no customers to be served, the barber
goes to sleep. If the barber is asleep when a customer arrives, the customer wakes up the barber to
give him a hair cut. A waiting customer vacates his chair after his hair cut completes. Your goal
is to write the pseudocode for the customer and barber threads below with suitable synchronization.
You must use only semaphores to solve this problem. Use the standard notation of invoking
up/down functions on a semaphore variable.
The following variables (3 semaphores and a count) are provided to you for your solution. You
must use these variables and declare any additional variables if required.
semaphore mutex = 1, customers = 0, barber = 0;
int waiting_count = 0;
Some functions to invoke in your customer and barber threads are:
• A customer who finds the waiting room full should call the function leave() to exit the
shop permanently. This function does not return.
• A customer should invoke the function getHairCut() in order to get his hair cut. This
function returns when the hair cut completes.
• The barber thread should call cutHair() to give a hair cut. When the barber invokes this
function, there should be exactly one customer invoking getHairCut() concurrently
NOTE: No Submission will be accepted after deadline.