Operating System Lab Report
Operating System Lab Report
Operating System Lab Report
Experiment No: 1
Experiment Name: First-Come, First-Served (FCFS) Scheduling.
int main()
int pid[15];
int bt[15];
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&pid[i]);
for(int i=0;i<n;i++)
scanf("%d",&bt[i]);
}
2
int i, wt[n];
wt[0]=0;
float twt=0.0;
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);
printf("%d\t\t", bt[i]+wt[i]);
printf("\n");
twt += wt[i];
tat += (wt[i]+bt[i]);
float att,awt;
awt = twt/n;
att = tat/n;
Input:
Output:
4
Experiment No: 2
Experiment Name: Shortest Job First Scheduling.
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
Input:
Output:
6
Experiment No.: 3
Experiment Name: Priority Scheduling.
// b is array for burst time, p for priority and index for process id
int b[n],p[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int a=p[i],m=i;
//Finding out highest priority element and placing it at its desired position
for(int j=i;j<n;j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}
//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
7
swap(&index[i],&index[m]);
}
Input:
Output:
8
Experiment No.: 4
Experiment Name: Round Robin Scheduling.
int main()
{
//Input no of processed
int n;
printf("Enter Total Number of Processes:");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
int x = n;
}
else if(temp_burst_time[i] > 0)
{
temp_burst_time[i] = temp_burst_time[i] - time_slot;
total += time_slot;
}
if(temp_burst_time[i]==0 && counter==1)
{
x--; //decrement the process no.
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i],
total-arr_time[i], total-arr_time[i]-burst_time[i]);
wait_time = wait_time+total-arr_time[i]-burst_time[i];
ta_time += total -arr_time[i];
counter =0;
}
if(i==n-1)
{
i=0;
}
else if(arr_time[i+1]<=total)
{
i++;
}
else
{
i=0;
}
}
float average_wait_time = wait_time * 1.0 / n;
float average_turnaround_time = ta_time * 1.0 / n;
printf("\nAverage Waiting Time:%f", average_wait_time);
printf("\nAvg Turnaround Time:%f\n", average_turnaround_time);
return 0;
}
10
Input:
Output:
11
Experiment No.: 5
Experiment Name: Producer/Consumer Problem.
// Initialize a mutex to 1
int mutex = 1;
// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);
// Driver Code
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
// Switch Cases
switch (n) {
case 1:
case 2:
// Exit Condition
case 3:
exit(0);
break;
14
}
}
}
Experiment No.: 6
Experiment Name: Bounded Buffer Problem.
// Initialize a mutex to 1
int mutex = 1;
// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);
// Driver Code
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
// Switch Cases
switch (n) {
case 1:
case 2:
// Exit Condition
case 3:
exit(0);
break;
18
}
}
}
Experiment No.: 7
Experiment Name: Readers and Writer Problem.
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
int main()
{
int num_readers, num_writers;
20
int reader_ids[num_readers];
for(int i = 0; i < num_readers; i++) {
reader_ids[i] = i + 1;
pthread_create(&read[i], NULL, (void *)reader, (void *)&reader_ids[i]);
}
int writer_ids[num_writers];
for(int i = 0; i < num_writers; i++) {
writer_ids[i] = i + 1;
pthread_create(&write[i], NULL, (void *)writer, (void *)&writer_ids[i]);
}
return 0;
}
Input:
Output:
21
Experimpent No.: 8
Exaperiment Name: Dining philosopher problem.
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];
sleep(2);
// take up chopsticks
void take_fork(int phnum)
{
sem_wait(&mutex);
sem_post(&mutex);
sleep(1);
}
sem_wait(&mutex);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
while (1) {
int* i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&S[i], 0, 0);
pthread_join(thread_id[i], NULL);
}
24
Output:
25
Experiment No.: 9
Experiment Name: Sleeping Barber Problem.
sem_t customers;
sem_t barber;
pthread_mutex_t seats_mutex;
int free_seats = N;
pthread_mutex_lock(&seats_mutex);
if (free_seats > 0) {
// Sit down on a chair
free_seats--;
int main() {
pthread_t barber_tid, customer_tid;
pthread_join(barber_tid, NULL);
pthread_join(customer_tid, NULL);
return 0;
}
Output: