Os Record

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

WEEK - 1 & 2

CPU Scheduling Algorithms:


1. FCFS
#include <stdio.h>
int main()
{
int pid[15];
int bt[15];
int n;
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter process id of all the processes: ");
for(int i=0;i<n;i++){
scanf("%d",&pid[i]);
}
printf("Enter burst time of all the processes: ");
for(int i=0;i<n;i++){
scanf("%d",&bt[i]);
}
int i, wt[n];
wt[0]=0;
for(i=1; i<n; i++) {
wt[i]= bt[i-1]+ wt[i-1];
}
printf("Process ID Burst Time Waiting Time TurnAround Time\n");
float twt=0.0;
float tat= 0.0;
for(i=0; i<n; i++){
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;
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);
}
Output:
2. SJF( NON-PREEMPTIVE)
#include<stdio.h>
int main(){
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++){
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++) {
pos=i;
for(j=i+1;j<n;j++){
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++) {
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
totalT+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
Output:
3. SRTF (PREEMPTIVE VERSION OF SJF)
#include <stdio.h>
int main() {
int a[10],b[10],x[10],i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;
printf("enter the number of Processes:\n");
scanf("%d",&n);
printf("enter arrival time\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter burst time\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
x[i]=b[i];
b[9]=9999;
for(time=0;count!=n;time++){
smallest=9;
for(i=0;i<n;i++) {
if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )
smallest=i;
}
b[smallest]--;
if(b[smallest]==0){
count++;
end=time+1;
avg=avg+end-a[smallest]-x[smallest];
tt= tt+end-a[smallest];
}
}
printf("\n\nAverage waiting time = %lf\n",avg/n);
printf("Average Turnaround time = %lf",tt/n);
return 0;}
OUTPUT:
4. PRIORITY SCHEDULING(PREEMPTIVE):
#include <stdio.h>
//Function to swap two variables
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);

// 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]);
swap(&index[i],&index[m]);
}
// T stores the starting time of process
int t=0;
//Printing scheduled process
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0;i<n;i++)
{
printf("P%d %d %d %d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
OUTPUT:
5. ROUND ROBIN:

#include<stdio.h>

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;

//Input details of processes


for(int i = 0; i < n; i++)
{
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}

//Input time slot


int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);

//Total indicates total time


//counter indicates which process is executed
int total = 0, counter = 0,i;

printf("Process ID Burst Time Turnaround Time Waiting Time\n");


for(total=0, i = 0; x!=0; )
{
// define the conditions
if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
{
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter=1;
}
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", average_turnaround_time);

return 0;

}
OUTPUT:
WEEK-3
1. Firstfit:
#include<stdio.h>
void main(){
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for(i = 0; i < 10; i++){
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("\nEnter no. of processes: ");
scanf("%d", &pno);
printf("\nEnter size of each process: ");
for(i = 0; i < pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i < pno; i++) //allocation as per first fit
for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i]){
allocation[j] = i;
flags[j] = 1;
break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i < bno; i++){
printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
Output:
2. BestFit:
#include<stdio.h>
void main(){
int a[20],p[20],i,j,n,m;
printf("Enter no of Blocks.\n");
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.\n");
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(p[j]<=a[i]) {
printf("The Process %d allocated to %d\n",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);
}
}
}
Output:
3. WorstFit:
#include<stdio.h>
void main(){
int a[20],p[20],i,j,n,m;
printf("Enter no of Blocks.\n");
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.\n");
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(p[j]<=a[i]){
printf("The Process %d allocated to %d\n",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);
}
}
}
Output:
WEEK- 4 & 5
Simulate the file allocation techniques:
1. Sequential file allocation

#include<stdio.h>
#include<stdlib.h>
void recursion (int file[]){
int flag=0,block,length,j,k,ch,i;
printf("Enter the starting block and the length:");
scanf("%d%d",&block,&length);
for(i=block;i<block+length;i++){
if(file[i]==0)
flag++;
}
if(length==flag){
for(i=block;i<block+length;i++){
if(file[i]==0){
file[i]=1;
printf("%d\t%d\n",i,file[i]);
}
}
if(i!=(block+length-1))
printf("The file is allocated to the disk\n");
}
else
printf("The file is not allocated to the disk\n");
printf("Do you want to enter files\n");
printf("Press 1 for YES, 0 for NO:");
scanf("%d",&ch);
if(ch==1)
recursion(file);
else
exit(0);
return;
}
int main(){
int file[30],i;
for(i=0;i<30;i++)
file[i]=0 ;
printf("Files allocated are\n");
recursion(file);
return 0;
}
Output:
2. Indexed file allocation

#include <stdio.h>
#include <stdlib.h>
int files[50], indexBlock[50], index, n;
void prompt2(){
int choice, ct = 0;
for(int i = 0; i < n ; i++){
scanf("%d", &indexBlock[i]);
if (files[indexBlock[i]] == 0)
ct++;
}
if (ct == n){
for(int i = 0; i < n; i++){
files[indexBlock[i]] = 1;
}
printf("Allocated all the files into disk and indexed into index file. \n");
for(int i = 0; i < n; i++){
printf("%d -----> %d : %d\n", index, indexBlock[i], files[indexBlock[i]]);
}
}
else{
printf("One or more files mentioned in the index are already allocated.\n");
}
printf("Do you want to enter more files? \n");
printf("Enter 1 for Yes, Enter 0 for NO: ");
scanf("%d", &choice);
if (choice == 1)
prompt1();
}
void prompt1(){
printf("Enter the index block: ");
scanf("%d", &index);
if (files[index] != 1){
printf("Enter the number of blocks needed for the index %d on disk: ", index);
scanf("%d", &n);
printf("Enter the files to be indexed onto index %d on the disk: \n", index);
prompt2();
}
else{
printf("Given index %d is already allocated.\n", index);
int fl = 0;
printf("Do you want to enter files into disk? (1 for YES, 0 for NO) \n");
scanf("%d", &fl);
if (fl == 1)
prompt1();
}
}
void main() {
for(int i = 0; i < 50 ; i++)
files[i] = 0;
prompt1();
}
Output:
3. Linked file allocation
#include <stdio.h>
#define MAX_BLOCKS 100
typedef struct Node {
int block;
struct Node* next;
} Node;
void allocateBlocks(Node* start, int fileBlocks[], int numFileBlocks) {
Node* current = start;
for (int i = 0; i < numFileBlocks; i++) {
current->next = (Node*)malloc(sizeof(Node));
current = current->next;
current->block = fileBlocks[i];
current->next = NULL;
}
}
void deallocateBlocks(Node* start) {
Node* current = start;
Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
}
void displayBlocks(Node* start) {
Node* current = start->next;
printf("Allocated Blocks: ");
while (current != NULL) {
printf(" %d ", current->block);
current = current->next;
}
printf("\n");
}
int main() {
Node* start = (Node*)malloc(sizeof(Node));

start->next = NULL;
int fileBlocks[MAX_BLOCKS];
int numFileBlocks, choice;
printf("Enter the total number of blocks required by the file: ");
scanf("%d", &numFileBlocks);
printf("\nMenu:\n");
printf("1. Allocate Blocks\n");
printf("2. Deallocate Blocks\n");
printf("3. Display Allocated Blocks\n");
printf("4. Exit\n");
while (1) {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the block numbers for the file (separated by spaces): ");
for (int i = 0; i < numFileBlocks; i++)
scanf("%d", &fileBlocks[i]);
allocateBlocks(start, fileBlocks, numFileBlocks);
printf("File blocks allocated successfully.\n");
break;
case 2:
deallocateBlocks(start);
printf("All blocks deallocated successfully.\n");
break;
case 3:
displayBlocks(start);
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
}
Output:
WEEK-6
1.Deadlock Detection:
#include<stdio.h>
static int mark[20];
int i,j,np,nr;
int main(){
int alloc[10][10],request[10][10],avail[10],r[10],w[10];
printf("\nEnter the no of process: ");
scanf("%d",&np);
printf("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++){
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the request matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
printf("\nEnter the allocation matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
for(j=0;j<nr;j++){
avail[j]=r[j];
for(i=0;i<np;i++){
avail[j]-=alloc[i][j];
}
}
for(i=0;i<np;i++){
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
for(j=0;j<nr;j++)
w[j]=avail[j];
for(i=0;i<np;i++){
int canbeprocessed=0;
if(mark[i]!=1){
for(j=0;j<nr;j++) {
if(request[i][j]<=w[j])
canbeprocessed=1;
else{
canbeprocessed=0;
break;
}
}
if(canbeprocessed){
mark[i]=1;
for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
}
}
}
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\n Deadlock detected");
else
printf("\n No Deadlock possible");
}
OUTPUT:
2.Deadlock Avoidance:
#include<stdio.h>
int main() {
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5],
terminate = 0;
printf("Enter the number of process and resources");
scanf("%d %d", & p, & c);
// p is process and c is diffrent resources
printf("enter allocation of resource of all process %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & alc[i][j]);
}
}
printf("enter the max resource process required %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & max[i][j]);
}
}
printf("enter the available resource");
for (i = 0; i < c; i++)
scanf("%d", & available[i]);

printf("\n need resources matrix are\n");


for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
need[i][j] = max[i][j] - alc[i][j];
printf("%d\t", need[i][j]);
}
printf("\n");
}
/* once process execute variable done will stop them for again execution */
for (i = 0; i < p; i++) {
done[i] = 0;
}
while (count < p) {
for (i = 0; i < p; i++) {
if (done[i] == 0) {
for (j = 0; j < c; j++) {
if (need[i][j] > available[j])
break;
}
//when need matrix is not greater then available matrix then if j==c will true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in available resources
*/
for (j = 0; j < c; j++) {
available[j] += alc[i][j];
}
count++;
terminate = 0;
} else {
terminate++;
}
}
}
if (terminate == (p - 1)) {
printf("safe sequence does not exist");
break;
}

}
if (terminate != (p - 1)) {
printf("\n available resource after completion\n");
for (i = 0; i < c; i++) {
printf("%d\t", available[i]);
}
printf("\n safe sequence are\n");
for (i = 0; i < p; i++) {
printf("p%d\t", safe[i]);
}
}

return 0;
}
Output:
WEEK-7 & 8
PAGE REPLACEMENT POLICIES:

1.FIFO:
#include <stdio.h>
int main(){
int referenceString[10], pageFaults = 0, m, n, s, pages, frames;
printf("\nEnter the number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter reference string values:\n");
for( m = 0; m < pages; m++){
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &referenceString[m]);
}
printf("\n What are the total number of frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++){
temp[m] = -1;
}
for(m = 0; m < pages; m++){
s = 0;
for(n = 0; n < frames; n++){
if(referenceString[m] == temp[n]{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
temp[m] = referenceString[m];
else if(s == 0)
temp[(pageFaults - 1) % frames] = referenceString[m];
printf("\n");
for(n = 0; n < frames; n++) {
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
Output:
2.OPTIMAL:
#include<stdio.h>
int main(){
int no_of_frames, no_of_pages, frames [10], pages [30], temp [10], flag1, flag2, flag3, i, j,
K, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
Output:
3.LRU:
#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1,
flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
f(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
Output:

You might also like