Network Analysis Os
Network Analysis Os
#include <stdio.h>
typedef struct
int processId;
int arrivalTime;
int burstTime;
} Process;
}
// Function to calculate the average waiting time and average turnaround time
calculateWaitingTime(processes, n, waitingTime);
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
}
int main()
scanf("%d", &n);
Process processes[n];
// Input the arrival time and burst time for each process
processes[i].processId = i + 1;
scanf("%d", &processes[i].arrivalTime);
scanf("%d", &processes[i].burstTime);
calculateAverageTimes(processes, n);
return 0;
SJF
#include <stdio.h>
// Structure to represent a process
typedef struct
int processId;
int arrivalTime;
int burstTime;
} Process;
*a = *b;
*b = temp;
}
// Function to calculate the waiting time for each process
// Function to calculate the average waiting time and average turnaround time
sortProcesses(processes, n);
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
int main()
scanf("%d", &n);
// Create an array of processes
Process processes[n];
// Input the arrival time and burst time for each process
processes[i].processId = i + 1;
scanf("%d", &processes[i].arrivalTime);
scanf("%d", &processes[i].burstTime);
calculateAverageTimes(processes, n);
return 0;
Round Robin
#include <stdio.h>
struct Process {
int id;
int burst;
int arrival;
int wait;
int complete;
int turnAround;
int remaining;
};
p[i].remaining = p[i].burst;
change = 0;
finishing++;
currentTime += p[i].remaining;
p[i].complete = currentTime;
totWaitTime += p[i].wait;
totTurnAroundTime += p[i].turnAround;
p[i].remaining = 0;
} else {
currentTime += slice;
p[i].remaining -= slice;
change++;
if (change == 0)
currentTime++;
}
printf("Avg Waiting Time: %.2f\n", totWaitTime / n);
#include <stdio.h>
struct Process {
int id;
int burst;
int arrival;
int wait;
int complete;
int turnAround;
int remaining;
};
p[i].remaining = p[i].burst;
change = 0;
finishing++;
currentTime += p[i].remaining;
p[i].complete = currentTime;
totTurnAroundTime += p[i].turnAround;
p[i].remaining = 0;
} else {
currentTime += slice;
p[i].remaining -= slice;
change++;
if (change == 0)
currentTime++;
int main() {
int n, slice;
scanf("%d", &n);
scanf("%d", &slice);
scanf("%d", &p[i].id);
scanf("%d", &p[i].arrival);
scanf("%d", &p[i].burst);
RR(p, n, slice);
return 0;
Priority Schedular
#include <stdio.h>
#include <stdlib.h>
struct Process {
int id;
int arrTime;
int burstTime;
int priority;
};
waitingTimes[0] = 0;
int totalWaitingTime = 0;
int totalTurnAroundTime = 0;
totalWaitingTime += waitingTimes[i];
totalTurnAroundTime += turnAroundTimes[i];
free(waitingTimes);
free(turnAroundTimes);
free(completionTimes);
}
int main() {
int n;
scanf("%d", &n);
scanf("%d", &processes[i].id);
scanf("%d", &processes[i].arrTime);
scanf("%d", &processes[i].burstTime);
scanf("%d", &processes[i].priority);
averageTime(processes, n);
free(processes);
return 0;
Bankers algorithm:
#include <stdio.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
int need[MAX_PROCESSES][MAX_RESOURCES];
// Check if the requested resources are within the available resources and the process's need
return 0;
return 1;
int work[MAX_RESOURCES];
int safeSequence[MAX_PROCESSES];
int count = 0;
work[i] = available[i];
int found = 0;
work[j] += allocated[i][j];
finish[i] = 1;
safeSequence[count] = i;
count++;
found = 1;
if (found == 0)
{
if (count == n)
printf("\n");
printf("\nAllocation Matrix:\n");
printf("\n");
printf("\nMax Matrix:\n");
{
printf("%d ", max[i][j]);
printf("\n");
printf("\nAvailable Resources:\n");
printf("\n");
else
int main()
int available[MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int allocated[MAX_PROCESSES][MAX_RESOURCES];
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &available[i]);
scanf("%d", &max[i][j]);
scanf("%d", &allocated[i][j]);
}
// Perform resource allocation using the Banker's Algorithm
return 0;
#include <stdbool.h>
#define MAX_PROCESSES 10
bool isCycleUtil(int process, bool visited[], bool recursionStack[], int graph[][MAX_PROCESSES], int n)
if (visited[process] == false)
// Mark the current process as visited and add it to the recursion stack
visited[process] = true;
recursionStack[process] = true;
if (graph[process][i] == 1)
// If the adjacent process is not visited and there is a cycle in its subtree, return true
return true;
}
// If the adjacent process is visited and present in the recursion stack, return true
else if (recursionStack[i])
return true;
return false;
return true;
return false;
}
int main()
scanf("%d", &n);
int graph[MAX_PROCESSES][MAX_PROCESSES];
scanf("%d", &graph[i][j]);
if (isCycle(graph, n))
else
return 0;
}
Peterson Sol:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 2
int i,j;
int turn = 0;
flag[threadId] = true;
turn = 1 - threadId;
// Critical section
flag[threadId] = false;
pthread_exit(NULL);
int main()
pthread_t threads[NUM_THREADS];
int threadIds[NUM_THREADS];
threadIds[i] = i;
pthread_join(threads[i], NULL);
return 0;
Bounded Buffer:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 16
sem_t fill;
sem_t empty;
int buffer[BUFFER_SIZE];
int counter;
//pthread_mutex_t mutex;
sem_t mutex;
void init()
sem_init(&mutex,0,1);
//pthread_mutex_init(&mutex,NULL);
sem_init(&fill,1,0);
sem_init(&empty,1,BUFFER_SIZE);
counter=0;
buffer[counter++]=item;
int consume()
{
counter--;
return buffer[counter];
int item;
item=rand()%5;
sem_wait(&empty);
/*printf("sem_wait = %d\n",wait );
int val;
printf("emptyb=%d\n", sem_getvalue(&empty,&val));
printf("%d\n",val );
printf("emptya=%d\n", sem_getvalue(&empty,&val));
printf("%d\n",val );*/
//pthread_mutex_lock(&mutex);
sem_wait(&mutex);
produce(item);
//pthread_mutex_unlock(&mutex);
sem_post(&mutex);
sem_post(&fill);
int item;
sem_wait(&fill);
//pthread_mutex_lock(&mutex);
sem_wait(&mutex);
item =consume();
sem_post(&mutex);
//pthread_mutex_unlock(&mutex);
sem_post(&fill);
int main()
int i,number_of_producers,number_of_consumers;
init();
scanf("%d",&number_of_producers);
scanf("%d",&number_of_consumers);
for(i=0;i<number_of_producers;i++)
pthread_create(&producerThread[i],NULL,producer,NULL);
for(i=0;i<number_of_consumers;i++)
pthread_create(&consumerThread[i],NULL,consumer,NULL);
for(i=0;i<number_of_producers;i++)
pthread_join(producerThread[i],NULL);
}
for(i=0;i<number_of_consumers;i++)
pthread_join(consumerThread[i],NULL);
return 0;
#include <stdio.h>
void main()
scanf("%d", &nb);
scanf("%d", &np);
scanf("%d", &b[i]);
scanf("%d", &p[i]);
}
if (barray[j] != 1)
if (temp >= 0)
parray[i] = j;
lowest = temp;
fragment[i] = lowest;
barray[parray[i]] = 1;
lowest = 10000;
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
fragment[i]);
int main()
{
int n, n1, i;
scanf("%d", &n);
int process[n];
scanf("%d", &process[i]);
scanf("%d", &n1);
int blocks[n1];
int total = 0;
scanf("%d", &blocks[i]);
int process1[n1];
int job[n1];
int frag[n1];
int check[n1];
check[i] = 0;
int j, used = 0;
i = 0;
while (i < n)
{
int max = -1, j1 = -1, k = -1, max1;
max1 = blocks[j];
max = max1;
j1 = j;
else
if (check[j] == 0)
process1[j] = 0;
job[j] = 0;
frag[j] = blocks[j];
if (k != j1)
process1[j1] = process[i];
job[j1] = i + 1;
check[j1] = 1;
int l;
i++;
printf("blocksize\tprocess size\tprocessno\tfragmentation\n");
for (i = 0; i < n1; i++)
printf("totalmemoryallocation:%d\n", total);
printf("memoryused:%d\n", used);
#include<stdio.h>
void main()
flags[i] = 0;
allocation[i] = -1;
scanf("%d", &bno);
scanf("%d", &bsize[i]);
scanf("%d", &pno);
scanf("%d", &psize[i]);
allocation[j] = i;
flags[j] = 1;
break;
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
int i,j;
int main()
scanf("%d", &numFrames);
printf("Enter the number of pages: ");
scanf("%d", &numPages);
scanf("%d", &pages[i]);
frames[i] = -1;
int pageFound = 0;
if (frames[j] == page) {
pageFound = 1;
break;
if (pageFound == 0) {
frames[frameIndex] = page;
faults++;
}
printf("\n");
printf("%d\t", frames[j]);
return 0;
Lru
#include<stdio.h>
minimum = time[i];
pos = i;
return pos;
int main()
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
scanf("%d", &pages[i]);
frames[i] = -1;
flag1 = flag2 = 0;
if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
if(flag1 == 0){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
if(flag2 == 0){
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
printf("\n");
printf("%d\t", frames[j]);
return 0;
int main()
scanf("%d", &no_of_frames);
scanf("%d", &no_of_pages);
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
flag1 = flag2 = 0;
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
if(flag1 == 0){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
if(flag2 == 0){
flag3 =0;
temp[j] = -1;
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
if(flag3 ==0){
max = temp[0];
pos = 0;
max = temp[j];
pos = j;
frames[pos] = pages[i];
faults++;
printf("\n");
printf("%d\t", frames[j]);
return 0;
}
Thread:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int g = 0;
static int s = 0;
++s; ++g;
printf("Thread ID: %d, Static: %d, Global: %d\n", *myid, ++s, ++g);
int main()
int i;
pthread_t tid;
pthread_exit(NULL);
return 0;