Professional Documents
Culture Documents
Round robin
#include<stdio.h>
main() {
int pt[10][10],a[10][10],at[10],pname[10][10],i,j,n,k=0,q,sum=0;
float avg; printf("\n\n Enter the number of processes : "); scanf("%d",&n); for(i=0;i<10;i++) {
for(j=0;j<10;j++)
{ pt[i][j]=0; a[i][j]=0; }
for(i=0;i<n;i++)
{ j=0; printf("\n\n Enter the process time for process %d : ",i+1); scanf("%d",&pt[i][j]);
for(i=0;i<n;i++)
{ a[2*j][i]=k; if((pt[i][j]<=q)&&(pt[i][j]!=0))
else if(pt[i][j]!=0)
{ pt[i][j+1]=pt[i][j]-q;
else
{ a[2*j][i]=0; a[2*j+1][i]=0;
for(i=0;i<n;i++)
sum+=a[0][i]; for(i=0;i<n;i++) {
for(j=1;j<10;j++)
if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0))
sum+=((a[j+1][i]-a[j][i]));
avg=(float)sum/n;
} avg=(float)sum/n;
OUTPUT:
Enter the process time for process 1 : 8 Enter the process time for process 2 : 3 Enter the process
time for process 3 : 6 Enter the process time for process 4 : 1
0 P1 2 2 P2 4 4 P3 6 6 P4 7 7 P1 9 9 P2 10 10 P3 12 12 P1 14 14 P3 16 16 P1 18
Average waiting time = 8.250000 msec
RESULT:
Thus the program for implementing RR scheduling algorithm was written and successfully
executed.
SJF
2. Write C programs to simulate the following CPU Scheduling algorithms:
FCFS
Priority
#include<stdio.h>
main() {
float avgwt,avgtt;
for(i=0;i<n;i++)
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pt[i]>pt[j])
wt[0]=0;
for(i=0;i<n;i++)
for(i=0;i<n;i++)
printf("|\t%s\t",pname[i]);
printf("\n-----------------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d\n",ss);
printf("\n-----------------------------------------------------------------\n");
avgwt=(float)sum/n;
avgtt=(float)sbt/n;
OUTPUT:
[root@localhost ~]# ./a.out Enter the number of processes : 4 Enter the NAME and BURSTTIME
NAME : p1
BURSTTIME : 8
NAME : p2
BURSTTIME : 3
NAME : p3
BURSTTIME : 6
NAME : p4
BURSTTIME : 1
Enter the priorities of the processes Priority of process1 : 1 Priority of process2 : 5 Priority of process3 : 2
Priority of process4 : 4
GANTT CHART
------------------------------------------------------
| p1 | p3 | p4 | p2
------------------------------------------------------
0 8 14 15 28
Total WAITING TIME of the process = 37 Total TURNAROUND TIME of the process = 55 Average WAITING
TIME of the process = 9.250000 Average TURNAROUND TIME of the process = 13.750000
3. Write C programs to simulate the following File organizing techniques:
a. Single Level Directory
ALGORITHM
Step1: Start
Step3: initgraph(fgd,fgm,”c:\turboc)
Step4: setbkcolor(GREEN)
Cleardevice()
Setbkcolor(GREEN)
Setfillstyle(1,MAGENTA)
Step7: settextxy(2,0,4),settextjustify(1,1)
Step9: setcolor(BLUE)
Do
Step11: line(320,150,cir_x,250)
Step12: Fillellipse(Cir_x,250,30,30)
Outtextxy(Cir_x,250,fname[j])
Step13: end
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\tc");
cleardevice();
setbkcolor(GREEN);
scanf("%d",&count);
for(i=0;i<count;i++)
cleardevice();
setbkcolor(GREEN);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"ROOT DIRECTORY");
setcolor(BLUE);
for(j=0;j<=i;j++)
cir_x+=mid;
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[j]);
getch();
OUTPUT:
ALGORITHM
Step1: Start
Step3: initgraph(fgd,fgm,”c:\turboc)
Step4: setbkcolor(GREEN)
Cleardevice()
Setbkcolor(GREEN)
Setfillstyle(1,MAGENTA)
Step7: settextxy(2,0,4),settextjustify(1,1)
Step9: setcolor(BLUE)
Do
Step11: line(320,150,cir_x,250)
Step12: Fillellipse(Cir_x,250,30,30)
Outtextxy(Cir_x,250,fname[j])
Step13: end
PROGRAM
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
struct tree_element
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element*link[5];
};
void main()
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"NULL",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc");
diplay(root);
getch();
closegraph();
int i,gap;
if(*root==NULL)
(*root)=(node*)malloc(sizeof(node));
fflush(stdin);
gets((*root)->name);
if(len==0||len==1)
(*root)->ftype=1;
else
(*root)->ftype=2;
(*root)->level=len;
(*root)->y=50+len*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
if(len==0||len==1)
if((*root)->level==0)
printf("(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
else
(*root)->nc=0;
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&(*root)->link[i],len+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap);
else
(*root)->nc=0;
display(node **root)
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x;root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillstyle(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
}}
getch();
OUTPUT:
Contiguous
b) Linked
c) Indexed
5. Write a C Program to copy contents of one file to another using system calls.
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
int main(int args,char *ar[])
{
char *source=ar[1];
char *dest="def.txt";
char *buf=(char *)malloc(sizeof(char)*120);
int fd1,fd2;
fd1=open(source,O_CREAT,0744);
fd2=open(dest,O_CREAT,0744);
while(read(fd1,buf,120)!=-1)
{
printf("%s",buf);
//printf("Processing\n");
write(fd2,buf,120);
}
printf("Process Done");
close(fd1);
close(fd2);
}
6. Write a C Program to simulate Bankers Algorithm for DeadLock Avoidance
#include<stdio.h>
Void main()
int n,i,j,c,p[20],av[20],max[20][20],all[20][20],all1[20][20],
int need[20][20];
scanf(“%d”,&n);
for(i=0;i<n;i++);
scanf(“%d”,&p[i]);
for(i=0;i<3;i++);
scanf(“%d”,&av[i]);
for(i=0;<n;i++);
for(j=0;j<3;j++)
scanf(“%d”,&max[i][j]);
}
printf(“\n”);
for(i=0;<n;i++);
for(j=0;j<3;j++)
scanf(“%d”,&alli][j]);
printf(“\n”);
for(i=0;<n;i++);
for(j=0;j<3;j++)
all1[i][j]=all[i][j];
for(i=0;<n;i++);
for(j=0;j<3;j++)
need[i][j]=max[i][j]-all[i][j];
}
for(i=0;<3;i++);
c=0;
for(j=0;j<n;j++)
c=c+all[j][i];
av[i]=av[i]-c;
printf(“\n A \t B \t C \n”);
for(i=0;<n;i++);
for(j=0;j<3;j++)
printf(“%d \t”need[i][j]);
printf(“\n”);
Printf(“\n AVAILABLE”);
for(i=0;<3;i++);
printf(“%d\t”av[j]);
for(i=0;<n;i++);
{
for(j=0;j<3;j++)
if(need[i][j]<=av[j])
all[i][j]=all1[i][j]+need[i][j];
av[j]=all1[i][j]+av[j];
else
exit(0);
printf(“\n***SUCCESS***);
printf(“\n***ALLOCATED MATRIX***);
printf(“\n A \t B \t C \n);
for(i=0;<n;i++);
for(j=0;j<3;j++)
Printf(“%d \t”,all[i][j])
printf(“\n”);
}
for(i=0;i<3;i++);
Printf(“\n %d”,av[j]);
Output:
Safe Sequence :
p2 p4 p5 p1 p3
Need matrix :
743
122
600
011
431
7. Write a C Program to simulate Banker’s Algorithm For Dead Lock Prevention
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int pno,rno,i,j,k,op,p,re,n,max[10][10],curr[10][10];
int req[10][10],av[10],r[10],a;
clrscr();
scanf("%d",&pno);
printf("Enter no of resourses");
for(i=0;i<rno;i++)
scanf("%d",&n[i]);
av[i]=r[i];
for(i=0;i<pno;i++)
for(j=0;j<pno;j++)
scanf("%d",&max[i][j]);
for(i=0;i<pno;i++)
for(j=0;j<rno;j++)
scanf("%d",&curr[i][j]);
for(i=0;i<pno;i++)
for(j=0;j<rno;j++)
req[i][j]=max[i][j]-curr[i][j];
printf("%d",req[i][j]);
printf("\n");
value=safe(pno,rno);
if(value==0)
else
getch();
int rest[10],currav[10],temp[10],pos=0;
int i,j,p,count=0,possible=1;
for(i=0;i<n;i++)
rest[i]=1;
for(i=0;i<k;i++)
currav[i]=av[i];
while(possible)
j=0;
for(i=0;i<n;i++) {
if(rest[i]!=0)
for(p=0;p<k;p++)
if(req[i][p]<=currav[p])
currav[p]=currav[p]+max[i][p];
else
break;
if(p==k)
rest[i]=0;
j++;
if(rest[i]==0)
count++;
temp[pos++]=i;
}}
}
if(count==n)
possible=0;
if(j==0)
possible=0;
for(i=0;i<n;i++)
if(rest[i]!=0)
return(0);
for(i=0;i<n;i++)
return(1);
OUTPUT:
Enter no of resourses2
21
21
00
00
P0 P1
FIFO
ALGORITHM
Step1: Start
Step2: Global Declaration fifo(),t[5],pgf,n,a[20],I,j,frm
Step3: pgf fifo()
Step4: Read pos,flag,i 0
Step5: while i<n do
Step6: for pos=0 to pos<frm & i<n do
Step7: for j=0 to j<pos or j<frm do
Step8: if a[i]=t[j] then
Flag 1, break
Step9: if flag=1 then continue
Step10: t [pos] a[i]
Step11: for j 0 to j<frm do
Print t[j]
Step12: pgf pgf+1,pos pos+1
Step13: return pgf to Step3
Step14: end
PROGRAM
#include<stdio.h>
#include<conio.h>
int t[3],pgf=0,n,a[20],i,j,frm;
void main()
clrscr();
printf("Nter the no.of elements in reference string");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&frm);
pgf=fifo();
getch();
int fifo()
int pos,flag;
i=0;
while(i<n)
if(a[i]==t[j])
flag=1;
break;
if(flag==1)
continue;
t[pos]=a[i];
printf("\n");
for(j=0;j<frm;j++)
printf("%d \t",t[j]);
printf("\n");
pgf++;
pos++;
return(pgf);
OUTPUT:
1 0 0
1 2 0
1 2 4
No of page fault is 3
Optimal Page Replacement
ALGORITHM
Step1: Start
If pos>=1 then
For m 1 to m<=pos do
If a[i]=f[m] then
Pres 1
Break;
F[++pos]=a[i],pfault pfault+1
Print f[k]
Step7: pres 0
If a[i]=f[m]
Pres ,break
Step9: if pres=0 do
If f[j]=a[k] then
If max[j]=999 then
Max[j] k
Break
Step10: l max[1],pos 1
If l<max[p] then
Pos p
L max[p]
Max[k] 999
Step14: pfault=pfault+1
Step15: pres 0
Step17: end
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int a[20],f[20],pos,max[20],p,pfault=0,l,i,k,m,j,pres=0,n,size;
clrscr();
scanf("d",&n);
for(i=1;i<=n;i++)
{
printf("Nter %d Element",i);
scanf("%d",&a[i]);
max[i]=999;
scanf("%d",&size);
printf("\n****Optimal Policy****\n");
for(i=1,pos=0;pos<size,i<=n;i++)
if(pos>=1)
for(m=1;m<=pos;m++)
if(a[i]==f[m])
pres=1;
break;
if(pres==0)
f[++pos]=a[i];
++pfault;
for(k=1;k<=pos;k++)
printf("%d",f[k]);
for(j=1;j<=size-pos;j++)
printf("\t");
printf("\n");
pres=0;
for(i=1;i<=n;i++)
for(m=1;m<=size;m++)
if(a[i]==f[m])
pres=1;
break;
if(pres==0)
for(j=1;j<=size;j++)
for(k=i+1;k<=n;k++)
if(f[j]==a[k])
{
if(max[j]==999)
max[j]=k;
break;
l=max[i];
pos=1;
for(p=2;p<=size;p++)
if(l<max[p])
pos=p;
l=max[p];
f[pos]=a[i];
for(k=1;k<=size;k++)
printf("%d",f[k]);
max[k]=999;
++pfault;
printf("\n");
}
pres=0;
getch();
OUTPUT:
Nter 1 Element2
Nter 2 Element3
Nter 3 Element4
Nter 4 Element6
Nter 5 Element6
Nter 6 Element5
Nter 7 Element4
Nter 8 Element3
Nter 9 Element3
Nter 10 Element7
****Optimal Policy****
2
23
234
634
534
734
ALGORITHM
Step1: start
Min[i]=-1
For m 0 to m<pos do
If a[i]=frm[m] then
Pres 1,break
Frm[pos++] a[i],pg++
For m 0 to m<nfm do
Pres 1 break
If frm[j]=a[k] then
Min[j]=k
Pos=0
L=min[0]
If(min<p3<1) then
L=min[p],pos=p
Min[k] -1
Step17: pres 0
Step19: end
PROGRAM
#include<stdio.h>
#include<conio.h>
int t[3],pgf=0,n,a[20],i,j,frm[10],nfm,k;
int min[10];
void main()
scanf("%d",&n);
for
scanf("%d",&a[i]);
printf("Nternthe no of frames");
scanf("%d",&nfm);
for(i=0;i<nfm;i++)
min[i]=-1;
pgf(lru();
getch();
int lru()
int po=-1,pres=0;m,l,p;
for(i=0,pos=0;pos<nfm ,i<n;i++)
if(pos>=1)
for(m=0;m<pos;m++)
if(a[i]==frm[m])
pres=1;
break;
if(pres==0)
frm[pos++]=a[i];
pgf+
+;
for"(k=0;k<nfm;k++)
printf("%d",frm[k]);
printf("\n");
pres=0;
for(i=0;i<n;i++)
for(m=0;m<nfm;m++)
if(a[i]==frm[m])
pres=1;
break;
if(pres==0)
for(j=0;j<nfm;j++)
for(k=0;k<1;k++)
if(frm[j]==a[k])
min[j]=k;
pos=0;
l=min[0];
for(p=1;p<nfm;p++)
if(mi9n[p]<l)
l=min[p];
pos=p;
frm[pos]=a[i];
for(k=0;k<nfm;k++)
printf("%d",frm[k]);
min[k]=-1;
pgf++;
printf?("\n");
pres=0;
return pgf;
}
OUTPUT:
1 0 0 0
1 2 0 0
1 2 3 0
1 2 3 4
1 2 5 4
1 2 5 6
4 2 5 6
No of page faults is : 7
9. Write C Program to implement the following techniques of memory management:
Paging
10. Write a C Program to implement the ls | sort command.( Use unnamed pipe)
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main() {
int ret_val;
int pfd[2];
char buff[32];
if (fork() == 0) {
/* child program */
if (ret_val != strlen(string1)) {
else {
/* parent program */
if (ret_val != strlen(string1)) {
exit(0);
}
11. Write a C Program to solve the Dining-philosopher problem using semaphores
12. Write a C Program to implement ipc between two unrelated processes using named pipe.
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
int main()
int pfds[2];
char buf[30];
if(pipe(pfds==-1)
Perror(“pipe”);
exit(1);
Write(pfds[1],”test”,5);
Read(pfds[0],buf,5);
Printf(“read\”%s\”’\n”,buf);
Output:
[student @gcet~]$ vi pipes1.c
[student @gcet~]$ cc pipes1.c
[student @gcet~]$ ./a.out
read”test”