Os Lab Record
Os Lab Record
INSTITUTEOFTECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to JNTUK,
Kakinada) NH- 5, Chowdavaram, Guntur-522019
An ISO 9001:2015CertifiedInstitution, Accredited by NAAC&NBA
KHIT
PRACTICALRECORD
Name:……………………………………………………………………..
RollNo:……………..…... Year&Semester:…………..………..
Branch:…………………. Section:…………………………........
Lab:……………………………………………………………………..…
KALLAMHARANADHAREDDY
INSTITUTEOFTECHNOLOGY
(APPROVED BY AICTE NEW DELHI, AFFLIATED
TOJNTUK,KAKINADA)CHOWDAVARAM,GUNTUR-19
RollNo:
CERTIFICATE
ThisistoCertifythattheBonafideRecordoftheLaboratoryWorkdonebyMr/Ms……………
……………………………………………………………………………..
of……..B.Tech/M.Tech/Diploma……...Semesterin ………..Branchhascompleted…..…..
experimentsin……………………………………………………….………………………..
LaboratoryduringtheAcademicyear20 -20
.
EX. PAGE
NO DATE NAMEOF THEEXPERIMENT FROM TO MARKS SIGNATURE
.
Date: Roll No:
Exercise-1
Aim: Simulate the following CPU scheduling algorithms
a) FCFS b) SJF c) Round robin d) Priority CPU scheduling
a) 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 calculating waiting time of each process
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]);
//calculating and printing turnaround time of each process
printf("%d\t\t", bt[i]+wt[i]);
printf("\n");
//for calculating total waiting time
twt += wt[i];
//for calculating total turnaround time
tat += (wt[i]+bt[i]);
}
float att,awt;
//for calculating average waiting time
awt = twt/n;
//for calculating average turnaround time
att = tat/n;
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);
}
Output:
Enter the number of processes: 3
Enter process id of all the processes: 1 2 3
Enter burst time of all the processes: 5 11 11
Process ID Burst Time Waiting Time TurnAround Time
1 5 0 5
2 11 5 16
3 11 16 27
Avg. waiting time= 7.000000
Avg. turnaround time= 16.000000
b) SJF
#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);
//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:
Enter Number of Processes: 3
Enter Burst Time and Priority Value for Process 1: 10 2
Enter Burst Time and Priority Value for Process 2: 5 0
Enter Burst Time and Priority Value for Process 3: 8 1
Order of process Execution is
P1 is executed from 0 to 10
P3 is executed from 10 to 18
P2 is executed from 18 to 23
Exercise-2
AIM: Multiprogramming memory management implementation of fork(), wait(), exec(), and exit(), system calls.
a) FORK():
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void forkexample()
{
// child process because return value zero
if (fork() == 0)
printf("Hello from Child!\n");
// parent process because return value non-zero.
else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}
Output:
Hello from Parent!
Hello from Child!
b) EXEC: //EXEC.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
printf("I am EXEC.c called by execvp()");
printf("\n");
return 0;
}
/*Now,create an executable file of EXEC.c using command
gccEXEC.c -o EXEC
*/
//execDemo.c
Source code:-
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);
printf("Ending-----");
return 0;
}
Output:
I am EXEC.c called by execvp()
Ending-----
c)Wait:
Source code:
// C program to demonstrate working of wait()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;
if (fork()== 0)
exit(0); /* terminate child */
else
cpid = wait(NULL); /* reaping parent */
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);
return 0;
}
Output:
Parent pid = 90177
Child pid = 90178
Date:
Exercise-3
AIM: Simulate the following
a) Multiprogramming with a fixed number of tasks (MFT)
b) Multiprogramming with a variable number of tasks (MVT)
Output:
Enter Main Memory Size:
50
Enter number of processes
5
Enter the size of process 0
10
10 bytes of memory is allocated to process 0
Internal fragmentation is: 0
Enter the size of process 1
9
9 bytes of memory is allocated to process 1
Internal fragmentation is: 1
Enter the size of process 2
8
8 bytes of memory is allocated to process 2
Internal fragmentation is: 2
Enter the size of process 3
10
10 bytes of memory is allocated to process 3
Internal fragmentation is: 0
Enter the size of process 4
2
2 bytes of memory is allocated to process 4
Internal fragmentation is: 8
Total internal fragmentation is: 11
Exercise-4
AIM: Write a C program to simulate the following contiguous memory allocation Techniques
a) First Fit b) Best Fit c) Worst Fit
a) First Fit
Source code:
#include<stdio.h>
#include<string.h>
void main()
{
int bsize[10],psize[10],nb,np,flags[10],allocation[10],i,j;
clrscr();
memset(flags,0,10);
memset(allocation,-1,10);
printf("Enter total no. of blocks: \n");
scanf("%d", &nb);
printf("Enter size of each block: \n");
for(i=0;i<nb;i++)
scanf("%d",&bsize[i]);
printf("Enter total no. of processes: \n");
scanf("%d",&np);
printf("Enter size of each process: \n");
for(i=0;i<np;i++)
scanf("%d",&psize[i]);
for(i=0;i<np;i++)
for(j=0;j<nb;j++)
if(flags[j]==0&&bsize[j]>=psize[i])
{
allocation[j]=i;
flags[j]=1;
break;
}
//display allocation details
printf("Block No.\tBlock Size\t\tProcess No.\t\tProcess Size\n");
for(i=0;i<nb;i++)
{
printf("%d\t\t%d\t\t\t",i,bsize[i]);
if(flags[i]==1)
printf("%d\t\t\t\t%d\n",allocation[i],psize[allocation[i]]);
else
printf("Not allocated\n");
}
getch();
}
Output:
Enter total no. of blocks:
5
Enter size of each block:
10
15
10
15
10
Enter total no. of processes:
5
Enter size of each process:
10
9
11
12
5
Block No. Block Size Process No. Process Size
0 10 0 10
1 15 1 9
2 10 4 5
3 15 2 11
4 10 Not allocated
b) Best Fit
Sorce code:
#include<stdio.h>
#include<conio.h>
void main()
{
int bs[10],ps[10],i,j,nb,np,temp,lowest=10000;
static int bf[10],ff[10];
clrscr();
printf("Enter the number of blocks:\n");
scanf("%d",&nb);
printf("Enter the size of each blocks:\n");
for(i=0;i<nb;i++)
scanf("%d",&bs[i]);
printf("Enter the number of processes:");
scanf("%d",&np);"
printf("Enter the size of each process:\n");
for(i=0;i<np;i++)
scanf("%d",&ps[i]);
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(bf[j]!=1)
{
temp=bs[j]-ps[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
bf[ff[i]]=1;
lowest=10000;
}
printf("Process No\tProcess Size\tBlock No\tBlock Size\n");
for(i=0;i<np;i++)
{
printf("\n%d\t\t%d\t\t",i,ps[i]);
if(ff[i]!=0)
printf("%d\t\t%d\n",ff[i],bs[ff[i]]);
else
printf("Not Allocated\n");
}
getch();
}
b) Best Fit
Sorce code:
#include<stdio.h>
#include<conio.h>
void main()
{
int bs[10],ps[10],i,j,nb,np,temp,lowest=10000;
static int bf[10],ff[10];
clrscr();
printf("Enter the number of blocks:\n");
scanf("%d",&nb);
printf("Enter the size of each blocks:\n");
for(i=0;i<nb;i++)
scanf("%d",&bs[i]);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("Enter the size of each process:\n");
for(i=0;i<np;i++)
scanf("%d",&ps[i]);
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(bf[j]!=1)
{
temp=bs[j]-ps[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
bf[ff[i]]=1;
lowest=10000;
}
printf("Process No\tProcess Size\tBlock No\tBlock Size\n");
for(i=0;i<np;i++)
{
printf("\n%d\t\t%d\t\t",i,ps[i]);
if(ff[i]!=0)
printf("%d\t\t%d\n",ff[i],bs[ff[i]]);
else
printf("Not Allocated\n");
}
getch();
}
Output:
Enter the number of blocks:
5
Enter the size of each blocks:
10
10
15
10
15
Enter the number of processes:5
Enter the size of each process:
9
10
14
8
14
Process No Process Size Block No Block Size
0 9 0 10
1 10 1 10
2 14 2 15
3 8 3 10
4 14 4 15
C)Worst Fit:
Source code:
#include<stdio.h>
int main()
{
int n,n1,i;
printf("enter the number of processes:");
scanf("%d",&n);
int process[n];
printf("\n enter the size of processes:\n");
for(i=0;i<n;i++)
{
scanf("%d",&process[i]);
}
printf("enter the no of memoryblocks:");
scanf("%d",&n1);
int blocks[n1];
printf("\n enter the size of blocks:\n");
int total=0;
for(i=0;i<n1;i++)
{
scanf("%d",&blocks[i]);
total=total+blocks[i];
}
int process1[n1];
int job[n1];
int frag[n1];
int check[n1];
for(i=0;i<n1;i++)
{
check[i]=0;
}
int j,used=0;
i=0;
while(i<n)
{
int max=-1,j1=-1,k=-1,max1;
for(j=0;j<n1;j++)
{
max1=blocks[j];
if(max1>=max&&check[j]==0&&max1>=process[i])
{
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;
frag[j1]=blocks[j1]-process[i];
used=used+process[i];
check[j1]=1;
int l;
}
i++;
}
printf("blocksize\tprocess size\tprocessno\tfragmentation\n");
for(i=0;i<n1;i++)
{
printf("%d\t\t%d\t\t%d\t\t%d\n",blocks[i],process1[i],job[i],frag[i]);
}
printf("totalmemoryallocation:%d\n",total);
printf("memoryused:%d\n",used);
}
Output:
enter the number of processes: 5
enter the size of processes:
10
15
10
14
3
enter the no of memory blocks: 5
enter the size of blocks:
15
15
15
15
15
Exericise-6
AIM: Simulate Bankers Algorithm for Deadlock Prevention.
Source code:
#include< stdio.h>
#include< conio.h>
void main()
{
int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15];
int pno,rno,i,j,prc,count,t,total;
count=0;
clrscr();
printf("\n Enter number of process:");
scanf("%d",&pno);
printf("\n Enter number of resources:");
scanf("%d",&rno);
for(i=1;i< =pno;i++)
{
flag[i]=0;
}
printf("\n Enter total numbers of each resources:");
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
printf("\n Enter Max resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
}
printf("\n available resources:\n");
for(j=1;j<= rno;j++)
{
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf(" %d \t",work[j]);
}
do
{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}
printf("\n Allocated matrix Max need");
for(i=1;i<= pno;i++)
{
printf("\n");
for(j=1;j<= rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0;
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:");
for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
}
}
}while(count!=pno&&prc!=0);
if(count==pno)
printf("\nThe system is in a safe state!!");
else
Process 2 completed
Available matrix: 5 3 2
Allocated matrix Max need
010 |753 |743
000 |000 |000
302 |902 |600
211 |222 |011
002 |433 |431
Process 4 completed
Available matrix: 7 43
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 1 completed
Available matrix: 7 5 3
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 3 completed
Available matrix: 10 5 5
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 5 7
The system is in a safe state!!
Date:
Exercise-7
AIM: Simulate the following Page Replacement Algorithms
a) FIFO b) LRU c) LFU
a) FIFO
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("No of Page Faults: %d",count);
return 0;
}
Output:
ENTER THE NUMBER OF PAGES:
5
ENTER THE PAGE NUMBER :
1
2
5
1
3
ENTER THE NUMBER OF FRAMES: 2
Ref string page frames
1 1 1
2 1 2
5 5 2
1 5 1
3 3 1
No of Page Faults: 5
b) LRU
#include<stdio.h>
void main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nNo of Page Faults: %d",c);
}
Output:
Enter no of pages:5
Enter the reference string:
1
5
2
3
5
Enter no of frames: 2
1 5
2 5
2 3
5 3
No of Page Faults: 5
c) LFU
#include<stdio.h>
void print(int frameno,int frame[])
{
int j;
for(j=0;j<frameno;j++)
printf("%d\t",frame[j]);
printf("\n");
}
int main()
{
int i,j,k,n,page[50],frameno,frame[10],move=0,flag,count=0,count1[10]={0},
repindex,leastcount;
float rate;
printf("Enter the number of pages\n");
scanf("%d",&n);
printf("Enter the page reference numbers\n");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter the number of frames\n");
scanf("%d",&frameno);
for(i=0;i<frameno;i++)
frame[i]=-1;
printf("Page reference string\tFrames\n");
for(i=0;i<n;i++)
{
printf("%d\t\t\t",page[i]);
flag=0;
for(j=0;j<frameno;j++)
{
if(page[i]==frame[j])
{
flag=1;
count1[j]++;
printf("No replacement\n");
break;
}
}
if(flag==0&&count<frameno)
{
frame[move]=page[i];
count1[move]=1;
move=(move+1)%frameno;
count++;
print(frameno,frame);
}
else if(flag==0)
{
repindex=0;
leastcount=count1[0];
for(j=1;j<frameno;j++)
{
if(count1[j]<leastcount)
{
repindex=j;
leastcount=count1[j];
}
}
frame[repindex]=page[i];
count1[repindex]=1;
count++;
print(frameno,frame);
}
}
rate=(float)count/(float)n;
printf("Number of page faults is %d\n",count);
printf("Fault rate is %f\n",rate);
return 0;
}
Output:
Enter the number of pages:
5
Enter the page reference numbers:
1
5
3
2
5
Enter the number of frames:
2
Page reference string Frames
1 1 1
5 1 5
3 3 5
2 2 5
5 No replacement
Number of page faults is 4
Fault rate is 0.800000
Date:
Exercise-8
Aim: Write a C program to simulate the following file organization techniques
a) Single level directory b) Two level directory
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);
}
}
getch();
}
Output:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
Enter name of directory -- KHIT1
Directory created
Exercise- 9
Aim: Simulate the following file allocation strategies
a) Sequenced b) Indexed c) Linked
a) Sequenced
#include < stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}
Output:
Files Allocated are :
Enter starting block and length of files: 14 3
14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)0
b) Indexed
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
c) Linked
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
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",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Output:
Enter how many blocks already allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2
2-------->1
3 Block is already allocated
4-------->1
Do you want to enter more file(Yes - 1/No - 0)0
Date:
Exercise-10
Aim: Write a C program that illustrates two processes communicating using shared memory
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
struct country
{
char name[30];
char capital_city [30];
char currency[30];
int population;
};
int main(int argc,char*argv[])
{
int shm_id;
char*shm_addr;
int*countries_num;
struct country*countries;
struct shmid_dsshm_desc;
shm_id=shmget(100,2048,IPC_CREAT|IPC_EXCL\0600);
if(shm_id==-1)
{
perror(“main:shmget:”);
exit(1);
}
shm_addr=shmat(shm_id,NULL,0);
if(!shm_addr)
{
perror(“main:shmat:”);
exit(1);
}
countries_num=(int*)shm_addr;
(*countries_num=0);
countries=(struct country*)((void*)shm_addrsizeof(int));
strcpy(countries[0],name,”U.S.A”);
strcpy(countries[0],capital_city,”WASHINGTON”);
strcpy(countries[0],currency,”U.S.DOLLAR”);
countries[0].population=250000000;
(*countries_num);
strcpy(countries[1].name,”israel”);
strcpy(countries[1].capital_city,”jerushalem”);
strcpy(countries[1].currency,”NEW ISRAEL SHEKED”);
countries[1].population=6000000;
(*countries_num);
strcpy(countries[2].name,”France”);
strcpy(countries[2].capital_city,”paris”);
strcpy(countries[2].currency,”Frank”);
countries[2].population=60000000;
(*countries_num);
for(i=0;i<(*countries_num);i++)
{
printf(“country%d:\n”,i+1);
printf(“name:%d:\n”,i+1);
printf(“currency:%s:\n”,countries[i].currency);
printf(“population:%d:\n”,countries[i].population);
}
if(shmdt(shm_addr)==-1)
{
perror(“main:shmdt:”);
}
if(shmctl(shm_id,IPC_RMID,&SHM_DESC)==-1)
{
perror(“main:shmctl:”);
}
return 0;
}
Output:
Shared memory ID=65537 child pointer 3086680064
Child value =1
Shared memory ID=65537 child pointer 3086680064
Parent value=1
Parent value=42
Child value=42
Date:
Exercise-11
AIM: write a c program to simulate producer and consumer problem using semaphore
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Output:
1.Producer
2.Consumer
3.Exit
Enter your choice:1
Producer produces the item 1
Enter your choice:2
Consumer consumes item 1
Enter your choice:
Date:
Exercise-12
Aim: Write a c program to simulate disk scheduling algorithms
A)FCFC B) SCAN C) C-SCAN
A) FCFC
#include<stdio.h>
#include<math.h>
int size=8;
void FCFS(int arr[],int head)
{
int seek_count= 0,i;
int cur_track,distance;
for(i=0;i<size;i++)
{
cur_track=arr[i];
// calculate absolute distance
distance=fabs(head-cur_track);
// increase the total count
seek_count+=distance;
// accessed track is now new head
head=cur_track;
}
printf("Total number of seek operations: %d\n",seek_count);
// Seek sequence would be the same
// as request array sequence
printf("Seek Sequence is\n");
for(i=0;i<size;i++)
{
printf("%d\n",arr[i]);
}
}
//Driver code
int main()
{
// request array
int arr[8]={176,79,34,60,92,11,41,114};
int head=50;
FCFS(arr,head);
getch();
return 0;
}
Output:
Total number of seek operations: 510
Seek Sequence is
176
79
34
60
92
11
41
114
B) SCAN
#include <stdio.h>
int request[50];
int SIZE;
int pre;
int head;
int uptrack;
int downtrack;
struct max
{
int up;
int down;
} kate[50];
int dist(int a,int b)
{
if(a>b)
return a-b;
return b-a;
}
void sort(int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(request[j]>request[j+1])
{
int temp=request[j];
request[j]=request[j+1];
request[j+1]=temp;
}
}
}
j=0;
i=0;
while(request[i]!=head)
{
kate[j].down=request[i];
j++;
i++;
}
downtrack=j;
i++;
j=0;
while(i<n)
{
kate[j].up=request[i];
j++;
i++;
}
uptrack=j;
}
void scan(int n)
{
int i;
int seekcount=0;
printf("SEEK SEQUENCE = ");
sort(n);
if(pre<head)
{
for(i=0;i<uptrack;i++)
{
printf("%d",head);
seekcount=seekcount+dist(head,kate[i].up);
head=kate[i].up;
}
for(i=downtrack-1;i>0;i--)
{
printf("%d",head);
seekcount=seekcount + dist(head, kate[i].down);
head=kate[i].down;
}
}
else
{
for(i=downtrack-1;i>=0;i--)
{
printf("%d",head);
seekcount=seekcount+dist(head,kate[i].down);
head=kate[i].down;
}
for(i=0;i<uptrack-1;i++)
{
printf("%d",head);
seekcount=seekcount+dist(head,kate[i].up);
head=kate[i].up;
}
}
printf("%d\nTOTAL DISTANCE :%d",head,seekcount);
}
int main(){
int n, i;
printf("ENTER THE DISK SIZE :\n");
scanf("%d",&SIZE);
printf("ENTER THE NO OF REQUEST SEQUENCE :\n");
scanf("%d",&n);
printf("ENTER THE REQUEST SEQUENCE :\n");
for(i=0;i<n;i++)
scanf("%d",&request[i]);
printf("ENTER THE CURRENT HEAD :\n");
scanf("%d",&head);
request[n]=head;
request[n+1]=SIZE-1;
request[n+2]=0;
printf("ENTER THE PRE REQUEST :\n");
scanf("%d",&pre);
scanf(n+3);
getch();
return 0;
}
Output:
ENTER THE DISK SIZE:
4
C) C-SCAN
#include <stdio.h>
#include <stdlib.h>
int main(){
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d", &move);
for (i = 0; i < n; i++){
for (j = 0; j < n - i - 1; j++){
if (RQ[j] > RQ[j + 1]){
int temp;
temp = RQ[j];
RQ[j] = RQ[j + 1];
RQ[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++){
if (initial < RQ[i]){
index = i;
break;
}
}
if (move == 1){
for (i = index; i < n; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
TotalHeadMoment = TotalHeadMoment + abs(size - RQ[i - 1] - 1);
TotalHeadMoment = TotalHeadMoment + abs(size - 1 - 0);
initial = 0;
for (i = 0; i < index; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
else{
for (i = index - 1; i >= 0; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
TotalHeadMoment = TotalHeadMoment + abs(RQ[i + 1] - 0);
TotalHeadMoment = TotalHeadMoment + abs(size - 1 - 0);
initial = size - 1;
for (i = n - 1; i >= index; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
printf("Total head movement is %d", TotalHeadMoment);
return 0;
}
Output:
Enter the number of Requests
3
Enter the Requests sequence
2
1
0
Enter initial head position
1
Enter total disk size
3
Enter the head movement direction for high1 and for low0
1
Total head movement is 4.