Os Lab Manual Bcs303 PDF
Os Lab Manual Bcs303 PDF
OPERATING SYSTEMS
BCS303
III Semester CS
Prepared By
Krishna V
Asst. Professor
Dept. of Data Science
Vision and Mission of the Institute
Vision
To become a leading institute for quality technical education and research with ethical values.
Mission
M1: To continually improve quality education system that produces thinking engineers having
good technical capabilities with human values.
M2: To nurture a good eco-system that encourages faculty and students to engage in meaningful
research and development.
M3: To strengthen industry institute interface for promoting team work, internship and
entrepreneurship.
M4: To enhance educational opportunities to the rural and weaker sections of the society to equip
with practical skills to face the challenges of life.
Mission
M1: To nurture a positive environment with state of art facilities conducive for deep learning and
meaningful research and development.
M2: To enhance interaction with industry for promoting collaborative research in emerging
technologies.
M3: To strengthen the learning experiences enabling the students to become ethical professionals
with good interpersonal skills, capable of working effectively in multi-disciplinary teams.
PROGRAM EDUCATIONAL OBJECTIVES (PEOS)
PEO 1: Successful and ethical professionals in IT and ITES industries contributing to societal
progress.
PEO 2: Engaged in life-long learning, adapting to changing technological scenarios.
PEO 3: Communicate and work effectively in diverse teams and exhibit leadership qualities.
1 Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process, terminate process)
2 Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority.
3 Develop a C program to simulate producer-consumer problem using semaphores.
Program 1
// Develop a c program to implement the Process system calls (fork (), exec (), wait (), create
process, terminate process)
process.c
#include<stdio.h> // printf()
#include<stdlib.h> // exit()
#include<sys/types.h> // pid_t
#include<sys/wait.h> // wait()
#include<unistd.h> // fork(), execl()
int main()
{
pid_t pid;
pid = fork();
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid());
printf("Executing add program in CHILD process\n");
execl("./add.exe","add.exe",(char *)0);
}
else if(pid > 0)
{
printf("It is the parent process and pid is %d\n",getpid());
//int status;
wait(NULL); //wait(&status);
printf("Child is reaped(cleared)\n");
}
else
{
printf("Error in forking..\n");
exit(EXIT_FAILURE);
}
return 0;
}
COMPILATION STEPS:
Step 1: Create add.c program for adding two numbers
gedit add.c
#include<stdio.h>
void main( )
{
int a,b,res;
cc process.c
./a.out
Program-2
Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time
a) FCFS
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20): ");
scanf("%d",&n);
printf("\nEnter Process Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P[%d]: ",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
Output:
b) SJF
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=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;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{ tat[i]=bt[i]+wt[i];
total+=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)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
return 0;
}
Output:
c) Round Robin
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
Output:
d) Priority
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
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=total/n;
total=0;
tat[i]=bt[i]+wt[i];
total+=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=total/n;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}
Output:
Program-3
Develop a C program to simulate producer-consumer problem using semaphores.
prodcons.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5
item = rand() % 50+1; // Produce a random item within 1-50 , rand() % 100+11-100
sem_wait(&empty);
sem_wait(&mutex);
in = (in + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&full);
pthread_exit(NULL);
}
sem_post(&mutex);
sem_post(&empty);
pthread_exit(NULL);
}
int main() {
// Initialize semaphores
sem_init(&mutex, 0, 1);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
int a[5]={1,2,3,4,5};
int i;
pthread_t p[5],c[5];
pthread_join(c[i], NULL);
// Destroy semaphores
sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);
return 0;
}
Compilation steps:
cc prodcons.c –pthread
./a.out
OUTPUT:
Program-4
Develop a C program which demonstrates inter-process communication between a reader process and a
writer process. Use mkfifo, open, read, write and close APIs in your program.
writer.c
/*Writer Process*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include<string.h>
int main()
{
int fd;
char buf[1024],msg[100];
/* create the FIFO (named pipe) */
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf(“Enter the message to be write infile\n”);
gets(msg);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd, msg, sizeof(msg));
/* write the message to the FIFO */
close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}
reader.c
/* Reader Process*/
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
Program 5
/* 5. Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance. */
banker.c
#include<stdio.h>
#include<stdlib.h>
int p,r,i,j,max[10][10],alloc[10][10],avail[10],need[10][10];
int request[10][10],finish[10],work[10];
int safety_algo()
{
int k,count=0,flag;
for(i=0;i<p;i++)
finish[i]=0;
for(j=0;j<r;j++)
work[j]=avail[j];
while(count<p)
{
k=0;
for(i=0;i<p;i++)
{
flag=0;
for(j=0;j<r;j++)
if(finish[i]!=0||need[i][j]>work[j])
{
flag=1;
break;
}
if(flag!=1)
{
finish[i]=1;
for(j=0;j<r;j++)
work[j]=work[j]+alloc[i][j];
printf("p%d\t",i);
count++;
k++;
}
}
if(k==0)
{
return 0;
}
}
return 1;
}
void resource_request(int i)
{
int j;
for(j=0;j<r;j++)
{
if(request[i][j]>need[i][j])
{
printf("Error::request more than demand\n");
return;
}
}
for(j=0;j<r;j++)
{
if(request[i][j]>avail[j])
{
printf("request more than available,process has to wait\n");
return;
}
}
for(j=0;j<r;j++)
{
alloc[i][j]=alloc[i][j]+request[i][j];
avail[j]=avail[j]-request[i][j];
need[i][j]=need[i][j]-request[i][j];
}
if(safety_algo()==1)
printf("\n SAFE::process %d can be allocated\n",i);
else
printf("UNSAFE::process shas towait\n");
}
int main()
{
printf("enter the number of processes:");
scanf("%d",&p);
printf("\n enter the number of resources:");
scanf("%d",&r);
printf("\n enter the max matrix\n");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("\n enter the alloc matrix\n");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("\n enter the available resources\n");
for(j=0;j<r;j++)
scanf("%d",&avail[j]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
printf("\tNEED\t\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
printf("%d\t",need[i][j]);
printf("\n");
}
if(safety_algo()==1)
printf("\n current state of system::SAFE\n");
else
printf("\n current state of system::UNSAFE\n");
printf("enter the new request(processid)\n");
scanf("%d",&i);
printf("\n enter the request\n");
for(j=0;j<r;j++)
scanf("%d",&request[i][j]);
resource_request(i);
return 0;
}
Execution:
[root@localhost pgms]# cc 8.c
RUN1:
[root@localhost pgms]# ./a.out
enter the number of processes:5
enter the number of resources:3
enter the max matrix
753
322
902
222
433
enter the alloc matrix
010
200
302
211
002
enter the available resources
332
NEED
743
122
600
011
431
P1 P3 P4 P0 P2
current state of system::SAFE
enter the new request(processid)
1
enter the request
102
SAFE::process 1 can be allocated
RUN2:
[root@localhost pgms]# ./a.out
enter the number of processes:5
enter the number of resources:3
enter the max matrix
753
322
902
222
433
enter the alloc matrix
010
200
302
211
002
enter the available resources
332
NEED
743
122
600
011
431
P1 P3 P4 P0 P2
current state of system::SAFE
enter the new request(processid)
2
enter the request
102
Error::request more than demand
#include<stdio.h>
int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
return 0;
}
Output:
b) Best Fit
#include<stdio.h>
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}
c) First Fit
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1 || b[j]!=0 ) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
b[j]=temp;
goto x;
}
}
}
x: frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t",i,f[i],ff[i],frag[i]);
}
Output:
Program 7
/* 7.Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU */
#include<stdio.h>
int n,f;
int in[100];
int p[50];
int hit=0;
int i,j,k;
int pgfaultcnt=0;
void getData()
{
printf("\n Enter length of page reference sequence:");
scanf("%d",&n);
printf("\n Enter the page reference sequence:\n");
for(i=0;i<n;i++)
scanf("%d",&in[i]);
printf("\nEnter no.of frames:");
scanf("%d",&f);
}
void initialize()
{
pgfaultcnt=0;
for(i=0;i<f;i++)
p[i]=9999;
}
{
hit=1;
break;
}
}
return hit;
}
void dispPages()
{
for(k=0;k<f;k++)
{
if(p[k]!=9999)
printf("%d ",p[k]);
}
}
void dispPgFaultCnt()
{
printf("\nTotal no. of pagefaults:%d",pgfaultcnt);
}
initialize();
int least[50];
for(i=0;i<n;i++) {
printf("
\nPage%d:",in[i]);
if(isHit(in[i])==0) {
for(j=0;j<f;j++) {
int pg=p[j];
int found=0;
for(k=i-1;k>=0;k--)
{
if(pg==in[k]) {
least[j]=k;
found=1;
break; }
else
found=0;
}
if(!found)
least[j]=-9999;
}
int min=9999;
int repindex;
for(j=0;j<f;j++)
{
if(least[j]<min)
{
min=least[j];
repindex=j;
}
}
p[repindex]=in[i];
pgfaultcnt++;
dispPages();
}
else
printf("No. pagefault!");
}
dispPgFaultCnt();
}
int main()
{
int choice;
while(1)
{
printf("\nPage Replacement Algorithms\n1.Enter data\n2.FIFO\n3.LRU\n4.Exit\nEnter your
choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
getData();
break;
case 2:
fifo();
break;
case 3:
lru();
break;
default:
return 0;
break;
}
}
}
OUTPUT
RUN 1
[root@localhost pgms]# cc 9.c
[root@localhost pgms]# ./a.out
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:1
Enter length of page reference sequence:5
Enter the page reference sequence:
12345
Page1:1
Page2:1 2
Page3:3 2
Page4:3 4
Page5:5 4
Total no. of pagefaults:5
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:4
RUN 2
3.LRU
4.Exit
Enter your choice:1
Enter length of page reference sequence:16
Enter the page reference sequence:
0123012301234567
Enter no.of frames:3
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:1
Enter length of page reference sequence:16
Enter the page reference sequence:
0123012301234567
Enter no.of frames:3
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:2
Page0:0
Page1:0 1
Page2:0 1 2
Page3:3 1 2
Page0:3 0 2
Page1:3 0 1
Page2:2 0 1
Page3:2 3 1
Page0:2 3 0
Page1:1 3 0
Page2:1 2 0
Page3:1 2 3
Page4:4 2 3
Page5:4 5 3
Page6:4 5 6
Page7:7 5 6
Total no. of pagefaults:16
[root@localhost pgms]#
Program-8
Simulate following File Organization Techniques
a) Single level directory
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}
Output:
b) Two-Level Directory
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\tEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}
Output:
Program-9
Develop a C program to simulate the Linked file allocation strategies.
linkedalloc.c
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",f[j]);
}
else
{
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("\n Do you want to enter more file(Yes - 1/No - 0):");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
OUTPUT:
cc linkedalloc.c
./a.out
RUN 1:
Enter how many blocks already allocated: 3
2---->4------>6
RUN 2:
Program 10
scan.c
#include<stdio.h>
int main()
{
int i,j,sum=0,n;
int d[20];
int disk; //loc of head
int temp,max;
int dloc; //loc of disk in array
for(i=0;i<n;i++)
{
if(disk==d[i])
{
dloc=i; break;
}
}
for(i=dloc;i>=0;i--)
{
printf("%d -->",d[i]);
}
printf("0 -->");
for(i=dloc+1;i<n;i++)
{
printf("%d-->",d[i]);
}
sum=disk+d[n-1];
printf("\nmovement of total cylinders %d",sum);
return 0;
}
Output:
cc scan.c
./a.out
RUN 1
Enter no of location 8
Enter position of head 53
Enter elements of disk queue
98
183
37
122
14
124
65
67
53->37->14->0->65->67->98->122->124->183->
Movement of total cylinders 236.
RUN 2:
Enter no of location 8
Enter position of head 50
Enter elements of disk queue
90
120
35
122
38
128
65
68
50 -->38 -->35 -->0 -->65-->68-->90-->120-->122-->128-->
movement of total cylinders 178