Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Operating Systems/CPU Scheduling Algorithms/FirstComeFirstServe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
int main()
{
char pn[10][10];
int arr[10], bur[10], star[10], finish[10], tat[10], wt[10], i, n;
int totwt=0, tottat=0;
printf("Enter the number of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time: ");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++) {
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
printf("\nPName\tArrtime\t\tBurtime\t\t Start\t\t TAT\t\tFinish");
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t\t%6d\t\t%6d\t\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f", (float)totwt/3);
printf("\nAverage Turn Around Time:%f\n", (float)tottat/3);
}
61 changes: 61 additions & 0 deletions Operating Systems/CPU Scheduling Algorithms/Priority.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESSES 10
typedef struct
{
int pid; // process ID
int burst_time; // burst time (in milliseconds)
int priority; // priority (higher number means higher priority)
int waiting_time; // waiting time (in milliseconds)
int turnaround_time; // turnaround time (in milliseconds)
} Process;
int main()
{
Process processes [MAX_PROCESSES];
int num_processes, i, j;
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
for (i = 0; i < num_processes; i++) {
printf("\nEnter the burst time for process %d (in milliseconds): ", i+1);
scanf("%d", &processes[i].burst_time);
printf("Enter the priority for process %d (higher number means higher priority): ", i+1);
scanf("%d", &processes[i].priority);
processes[i].pid = i+1;
}
for (i = 0; i < num_processes - 1; i++)
{
for (j = i+1; j < num_processes; j++)
{
if (processes[i].priority < processes[j].priority)
{
Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
int total_waiting_time = 0, total_turnaround_time = 0;
for (i = 0; i < num_processes; i++)
{
if (i == 0)
{
processes[i].waiting_time = 0;
}
else
{
processes[i].waiting_time = processes[i-1].turnaround_time;
}
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
printf("\nPID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < num_processes; i++)
{
printf("%d\t%d ms\t\t%d\t\t%d ms\t\t%d ms\n", processes[i].pid, processes[i].burst_time, processes[i].priority, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("\nAverage waiting time: %.2f ms", (float)total_waiting_time/num_processes);
printf("\nAverage turnaround time: %.2f ms\n",
(float)total_turnaround_time/num_processes);
return 0;
}
57 changes: 57 additions & 0 deletions Operating Systems/CPU Scheduling Algorithms/RoundRobin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<stdio.h>
struct process {
int burst, wait, comp, f;
} p[20]={0,0};
int main( ) {
int n, i, j, totalwait=0, totalturn=0, quantum,flag=1, time=0;
printf("\nEnter the no. of processes: ");
scanf("%d",&n);
printf("\nEnter the Quantum time (in ms) : ");
scanf("%d",&quantum);
for(i=0;i<n;i++) {
printf("Enter the Burst Time (in ms) For Process #%2d : ",i+1);
scanf("%d",&p[i].burst);
p[i].f=1;
}
printf("\nOrder of Execution \n");
printf("\nProcess Starting Ending Remaining");
printf("\n\t\tTime\t Time \t\t Time");
while(flag==1)
{
flag=0;
for(i=0;i<n;i++)
{
if(p[i].f==1)
{
flag=1;
j=quantum;
if((p[i].burst-p[i].comp)>quantum)
{
p[i].comp+=quantum;
}
else
{
p[i].wait=time-p[i].comp;
j=p[i].burst-p[i].comp;
p[i].comp=p[i].burst;
p[i].f=0;
}
printf("\nProcess # %-3d %-10d %-10d %-10d", i+1, time,
time+j, p[i].burst-p[i].comp);
time+=j;
}
}
}
printf("\n\n--------------------");
printf("\nProcess \t Waiting Time TurnAround Time ");
for(i=0;i<n;i++)
{
printf("\nProcess # %-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n-------------------");
printf("\nWaiting Time: %fms",totalwait/(float)n);
printf("\nTurnAround Time : %fms\n\n",totalturn/(float)n);
return 0;
}
44 changes: 44 additions & 0 deletions Operating Systems/CPU Scheduling Algorithms/ShortestJobFirst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<stdio.h>
#include<string.h>
int main()
{
int i=0, pno[10], bt[10], n, wt[10], temp=0, j, tt[10];
float sum,at;
printf("\nEnter the no. of process: ");
scanf("\n %d",&n);
printf("\nEnter the burst time of each process: \n");
for(i=0;i<n;i++)
{
printf("P%d ",i);
scanf("%d",&bt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pno[i];
pno[i]=pno[j];
pno[j]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
sum=sum+wt[i];
}
printf("\nProcess No \t Burst Time\t Waiting time \t Turn around time\n");
for(i=0;i<n;i++)
{
tt[i]=bt[i]+wt[i];
at+=tt[i];
printf("\n P%d\t\t %d\t\t %d\t\t %d",i,bt[i],wt[i],tt[i]);
}
printf("\n\nAverage waiting time: %f\nAverage turn around time: %f\n", sum/n, at/n);
}
52 changes: 52 additions & 0 deletions Operating Systems/Memory Allocation Methods/BestFit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include<stdio.h>
void main() {
int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20];
printf("\nMEMORY MANAGEMENT SCHEME - BEST FIT \n");
printf("Enter No. of Blocks: ");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("Enter the %dst block size: ",i);
scanf("%d",&a[i]);
}
printf("Enter No. of Process: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst process: ",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
if(p[i]>p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\nProcess\tBlock Size\n");
for(i=0;i<n;i++)
printf("%d\t%d\n",p[i],a[i]);
printf("\n\n");
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
if(p[j]<=a[i]) {
printf("The process %d [size %d] allocated to block %d\n", j, p[j], a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
if(p[j]!=10000)
printf("The process %d is not allocated\n",p[j]);
}
38 changes: 38 additions & 0 deletions Operating Systems/Memory Allocation Methods/FirstFit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<stdio.h>
void main()
{
int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20];
printf("\nMEMORY MANAGEMENT SCHEME - FIRST FIT \n");
printf("Enter No. of Blocks: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst block size: ",i);
scanf("%d",&a[i]);
}
printf("Enter No. of Process: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst process: ",i);
scanf("%d",&p[i]);
}
printf("\nProcess\tBlock Size\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\n",p[i],a[i]);
printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The process %d [size %d] allocated to block %d\n",j,p[j],a[i]);
p[j]=10000; break;
}
}
}
for(j=0;j<m;j++)
if(p[j]!=10000)
printf("The process %d is not allocated\n",j);
}
54 changes: 54 additions & 0 deletions Operating Systems/Memory Allocation Methods/WorstFit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<stdio.h>
void main()
{
int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20];
printf("\nMEMORY MANAGEMENT SCHEME - WORST FIT \n");
printf("Enter No. of Blocks: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst block size: ",i);
scanf("%d",&a[i]);
}
printf("Enter No. of Process: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst process: ",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\nProcess\tBlock Size\n");
for(i=0;i<n;i++)
printf("%d\t%d\n",p[i],a[i]);
printf("\n\n");
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
if(p[j]<=a[i]) {
printf("The process %d [size %d] allocated to block %d\n",j,p[j],a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
if(p[j]!=10000)
printf("The process %d is not allocated\n",p[j]);
}
34 changes: 34 additions & 0 deletions Operating Systems/Page Replacement Algorithms/FirstInFirstOut.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
void main() {
int a[5], b[20], n, p=0, q=0, m=0, h, k, i, q1=1;
char f='F';
printf("Enter the Number of Pages: ");
scanf("%d",&n);
printf("Enter %d Page Numbers: ",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++) {
if(p==0) {
if(q>=3)
q=0;
a[q]=b[i]; q++;
if(q1<3) {
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3)) {
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<q1;k++) {
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults: %d\n",m);
}
Loading