Lab 4: Experiments with OS Simulator and Synchronisation
This file is
- Right-click this link for the jupyter notebook
lab04-tasks.ipynb and download it into yourDesktop/AOS2026 directory. - Open a terminal on your system.
- Change to the AOS2026 directory on your Desktop.
- Copy the OS Simulator that you implemented as your assignment into the lab04 directory.
- You may have to compile your simulator C source files again to create a working executable.
- Activate the AOS2026 or the aos2026 virtual environment by
typing
source AOS2026 (or aos2026)/bin/activate If successful, you will see the virtual environment name prefixed to your terminal prompt. - Open your downloaded notebook in the usual manner by running
jupyter notebook command in your terminal. - Run your simulator as indicated below in the different notebook cells.
Task 1: Study of Round Robin Scheduling Algorithm
In this task, we will explore the functioning of the Round Robin algorithm. Run your simulator with about 8 - 12 processes of different sizes and types. Specify Round Robin scheduler. Make a note of the following:
- The sequence in which the processes complete.
- The Response, Waiting and Running times.
- Is there any relationship between the sequence of processes and their sizes and types? Explain in two-three sentences.
- How much variation is there in the Response times? Are they nearly the same for all the processes or do they vary?
- Is there a link between the Running times and the sizes; and between Waiting times and process types?
In [ ]:
%%bash
# Run your simulator executable with the necessary arguments
Answers and Explanations
(Double-click here to open the cell for writing your answers)Task 2: Comparison of Scheduling Algorithms
Repeat the above experiment but after changing the scheduling algorithm to any other: FCFS, Priority, SJF, ...
In [ ]:
%%bash
(Run your simulator here so that the results are seen below)
Answers and Explanations
(Double-click here to open a cell in which you can write down your observations)Task 3: Bounded Buffer Reader-Writer Problem with Semaphores
This is yet another variant of the Reader-Writer problem where the buffer is circular, i.e. the number of items to write is more than the size of the buffer and get written from the start again once the end of the buffer is reached. Of course, no writer can write into a location that has not yet been seen by a reader.Here are the parameters for the problem for this lab.
- The size of the buffer is 5000.
- The number of writers cannot be more than 50.
- In this task, there is only one reader.
- The writer and reader threads start concurrently.
- The number of items written is given as a command-line argument and must be more than 8000.
- Each writer writes the value i into the buffer location i. Remember that the writers may write into the same location again but only after the reader verified the value in that location.
Fill in the code in the cell below and run it in our usual way in the cells further below.
In [ ]:
%%file semaphores_02.c
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#define MAXNITEMS 5000
#define MINITEMS 8000
#define MAXNTHREADS 50
#define MIN(A, B) ((A) > (B) ? (B) : (A))
#define MAX(A, B) ((A) > (B) ? (A) : (B))
int nitems;
struct {
sem_t /* Insert all the semaphores as needed */
int buff[MAXNITEMS];
int nput;
int nputval;
} shared;
void *reader(void *), *writer(void *);
int main(int argc, char **argv)
{
int i, sum=0, nthreads, count[MAXNTHREADS];
pthread_t tid_writer[MAXNTHREADS], tid_reader;
nitems = MAX(atoi(argv[1]), MINITEMS); /* First argument: # items */
nthreads = MIN(atoi(argv[2]), MAXNTHREADS); /* Second argument: # writers */
/* Create necessary writers and the single reader threads */
for (i=0; i<nthreads; i++) {
count[i] = 0;
}
/* Wait for all the threads to complete; print items counts as well
as total number of items written */
for (i=0; i<nthreads; i++) {
pthread_join(tid_writer[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
sum += count[i];
}
pthread_join(tid_reader, NULL);
printf("Total:\t%d\n", sum);
exit(0);
}
/*** The writer function; insert required synchronisation primitives ***/
void *writer(void *arg)
{
for ( ; ; ) {
if (shared.nput >= nitems) {
return(NULL);
}
shared.buff[shared.nput % MAXNITEMS] = shared.nputval;
shared.nput++;
shared.nputval++;
*((int *) arg) += 1;
}
}
/*** The reader function; insert synchronisation primitives as needed ***/
void *reader(void *arg)
{
int i;
for (i=0; i<nitems; i++) {
if (shared.buff[i % MAXNITEMS] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}
return(NULL);
}
In [ ]:
%%bash
gcc -o sem02 semaphores_02.c -lpthread
In [ ]:
%%bash
./sem02 32453 6
Task 4: Bounded Buffer Problem with Multiple Writers and Multiple Readers
This is a small variation of the problem in the previous task. Now, instead of a single reader, we have multiple readers too. The number of readers is 4; or 5 less than the number of writers whichever is larger. Everything else remains the same. Copy and modify the above source code in the cell below to solve this version of the problem.In [ ]:
%%file semaphores_03.c
In [ ]:
%%bash
gcc -o sem03 semaphores_03.c -lpthread
In [ ]:
%%bash
./sem03 13879 6