# OPERATING SYSTEMS

1: SIMULATE THE FOLLOWING CPU SCHECULING ALGORITHMS.
a) Round Robin
b) SJF
c) FCFS
d) Priority
a) Round Robin
#include<stdio.h>
void main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter process name & estimated time:");
scanf("%s %d",pn[i],&et[i]);
}
printf("The processes are:");
for(i=0;i<n;i++)
printf("process %d: %s\n",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n %s -> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else
if((et[i]<=ts)&&et[i]!=0)
{
1

x=x+et[i];
printf("\n %s -> %d",pn[i],et[i]);
et[i]=0;}
}
}
printf("\n Total Estimated Time:%d",x);
}

Output:
Enter the no of processes: 2
Enter the time quantum: 3
Enter the process name & estimated time: p1 12
Enter the process name & estimated time: p2 15
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3
Total Estimated Time: 27

2

b) SJF
#include <stdio.h>
#include <string.h>
struct process
{
int at,bt,st,wt,tt,ft,f;
char name;
}
main()
{
int n,n1,np,ftime,i,j,sum=0,sum1=0,temp2;
char temp1;
struct process p[100],temp;
float awt,atat;
clrscr();
printf("enter size");
scanf("%d",&n);
printf("enter process,at,bt");
for(i=0;i<n;i++)
{
scanf(" %c%d%d",&p[i].pr,&p[i].at,&p[i].bt);
p[i].f=0;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].at>p[j].at)
{
temp2=p[i].at;
p[i].at=p[j].at;
p[j].at=temp2;
temp2=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp2;
temp1=p[i].pr;
p[i].pr=p[j].pr;
p[j].pr=temp1;
}
}
}
p[0].wt=0;
p[0].ft=p[0].at+p[0].bt;
p[0].tt=p[0].ft;
p[0].f=1;
ftime=p[0].ft;
n1=1;
while(n1<n)
{
3

wt=ftime-p[np]. ftime=p[np].i<n.name.i++) { twt=twt+p[i].ft.wt. for(i=0.bt<p[np].awt).att).j++) { if(p[i].i++) { for(j=i.tt=p[np]. } for(i=0. n1++.i<n. p[np].at<=ftime)&&(p[i].p[i]. } awt= (float)twt/n.wt.wt>p[j]. } } } printf("PROCESS\tATIME \tBTIME \tWTIME \tTATIME \n "). p[j]=temp.f!=1)&&(p[i].f=1.ft-p[np].i++) { printf(" %c\t%d\t%d\t%d\t%d\n".f!=1)) np=i. att=(float)ttt/n.i<n. } for(i=0.at.at.p[i]. p[np]. tt= %f\n”.wt) { temp=p[i].ft=ftime+p[np]. ttt=ttt+p[i].p[i]. } 4 .i++) { if((p[i].bt.j<n.tt.for(i=1.p[i].tt).at. printf(“avg.bt. printf(“avg. } p[np]. p[np].at<=ftime)&&(p[i]. wt= %f \n”.p[i].i<n. if((p[i]. p[i]=p[j].bt)) np=i.

5 5 . arrival time.5 WTIME 0 3 8 11 TATIME 7 7 10 18 avg tt= 10.Output: Enter the no.of process: 4 Enter process. burst time: A 1 6 B 2 7 C 3 2 D 4 4 PROCESS ATIME BTIME A 1 6 D 4 4 C 3 2 B 2 7 avg wt= 5.

clrscr().i++) { p[i].c) FCFS #include <stdio. } p[0].tat.p[i].bt.tat=p[0].tat=p[i].wt.&n).t=0.h> #include <string. printf(" \n average waiting time=%f \n average turn around time= %f\n".i<n. int at. } 6 .p[i].wt.st=0.sum=0.p[0].atat). void main() { int i.&p[i]. scanf("%d". p[i].i++) { sum=sum+p[i].i<n.bt: \n"). sum1=sum1+p[i]. p[i].st. } for(i=0. for(i=0.&p[i].tat).wt=0.st=p[i-1].n.p[0].atat=0.i<n.wt.bt). for(i=0.bt. printf("enter nameof the process.i<n. float awt=0.tat+1.awt.tat.na. printf("enter the no.sum1=0.of processess \n"). }p[10]. } awt=(float)sum/n. atat=(float)sum1/n.st.tat. } printf("\nname \tst\twt\ttat\n").p[i].st-1+p[i]. getch().wt=p[i-1].h> struct process { char na.i++) { scanf(" %c%d".j. for(i=1.na.p[i].bt.i++) { printf(" %c\t%d\t%d\t%d\n".

tt=12.5 TATIME 3 8 15 24 7 .Output: Enter the size: 4 Enter the process.5 STIME 0 3 8 15 WTIME 0 3 8 15 avg.wt=6. burst time A 3 B 5 C 7 D 9 PROCESS A B C D avg.

j.&n).tat.i++) { p[i]. } } } p[0].st. clrscr().of processess \n").p[0]. }p[10]. printf("enter the no.na=p[j]. int at. p[i].wt=0.t=0.&p[i].i++) { p[i]. } for(i=0. p[i]. scanf("%d".j<n.n. for(i=0.h> #include <string.at.bt.p[0].wt.at.at.&p[i].na. p[j].bt=temp. temp=p[i].bt.sum=0. float awt=0. p[i].i++) { for(j=i+1.at) { temp=p[i].bt.at.st+p[i-1]. p[j].i<n.sum1=0.at=temp.st=0.temp.tat=p[0].bt: \n").j++) { if(p[i].et.i<n.bt).i++) { scanf(" %c%d%d".&p[i].na.st=p[i-1].h> struct process { char na. void main() { int i.bt. } 8 .bt=p[j]. } for(i=0.i<n.et=p[i]. p[j].atat=0.na.st+p[i].bt.na=temp1. temp1=p[i].i<n. for(i=1.FCFS with arrival time: #include <stdio.at=p[j].bt.at>p[j]. char temp1. printf("enter nameof the process.

i<n.p[i].tat.tat=p[0].for(i=1.i<n.i<n. } printf("\nname \tst\twt\ttat\n").wt=6.awt.i++) { printf(" %c\t%d\t%d\t%d\n". for(i=0.na. } p[0]. } awt=(float)sum/n.p[i]. sum1=sum1+p[i]. } for(i=0.wt. burst time: A 1 6 B 2 7 C 3 2 D 4 4 PROCESS ATIMEBTIME WTIME A 1 6 B 2 7 C 3 2 D 4 4 Avg.et.tat=p[i].00 TATIME 0 6 4 11 10 12 11 15 9 . atat=(float)sum1/n.st.wt=p[i-1].at.tat).atat).tt=11. getch().i<n.et-p[i].p[i].25 avg. } Output: Enter the size: 4 Enter process arrival time.et-p[i]. printf(" \n average waiting time=%f \n average turn around time= %f\n".i++) { p[i].at.i++) { p[i].wt.i++) { sum=sum+p[i]. for(i=1.p[i].

pn[j]).i. scanf("%d".i++) for(j=0.&n).t). char pn[10][10]. strcpy(pn[j]. at[j]=temp.j.pn[i]).&at[i].j<n.i++) { printf("Enter process name. wt[i]=st[i]-at[i].p[10].totta=0. } for(i=0.arrivaltime.ata. 10 .&et[i].h> #include<string. at[i]=at[j].n. temp=et[i].st[10].j++) { if(p[i]<p[j]) { temp=p[i].ta[10]. strcpy(t. p[j]=temp.i++) { if(i==0) { st[i]=at[i].i<n.temp.ft[10]. float awt. temp=at[i]. scanf("%s%d%d%d". int totwt=0.t[10].h> void main() { int et[20]. for(i=0.at[10]. et[j]=temp. } } for(i=0.i<n. et[i]=et[j]. printf("Enter the number of process:").d) Priority #include<stdio.execution time & priority:").pn[i].i<n. strcpy(pn[i]. p[i]=p[j].&p[i]).wt[10].

666667 waitingtime tatime 3 6 11 11 . } Output: Enter the number of process:3 Enter process name.ta[i]). ft[i]=st[i]+et[i]. printf("\nAverage turnaroundtime is:%f". } awt=(float)totwt/n.at[i]. ata=(float)totta/n.execution time & priority:3 5 6 3 Pname arrivaltime executiontime priority 1 2 3 1 0 2 4 5 2 1 3 5 6 3 5 Average waiting time is:2.i<n.arrivaltime.et[i].p[i].arrivaltime.i++) printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d".awt).execution time & priority:2 4 5 2 Enter process name.000000 Average turnaroundtime is:6. } totwt+=wt[i].pn[i]. ta[i]=ft[i]-at[i]. wt[i]=st[i]-at[i]. printf("\nAverage waiting time is:%f". printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime").arrivaltime. } else { st[i]=ft[i-1]. ta[i]=ft[i]-at[i]. totta+=ta[i]. for(i=0.execution time & priority:1 2 3 1 Enter process name.ft[i]=st[i]+et[i].wt[i].ata).

} 12 .h> void main() { int f[50].h> #include<stdlib. } if(j==(st+len)) printf("the file is allocated to disk").2: SIMULATE ALL FILE ALLOCATION STRATIGIES.j<(st+len). scanf("%d%d". a) Sequential b) Indexed c) Linked a) Sequential #include<stdio. printf("\n%d->%d". x: printf("enter starting block and length of file").i++) f[i]=0. } else { printf("block already alloted").j. printf("\n if u want to enter more files ?(y-1/n-0)"). break. for(j=st. else exit(1).st.j. if(c==1) goto x.j++) if(f[j]==0) { f[j]=1.&c).&len).i<50.c.len. for(i=0.i.f[j]). scanf("%d".&st.

Output: Enter starting block and length of file 3 7 3 ->1 4 ->1 5 ->1 6 ->1 7 ->1 8 ->1 9 ->1 The file is allocated to disk. If u want to enter more files? (y-1/n-0) 1 Enter starting block and length of file 4 8 Block already allocated If u want to enter more files? (y-1/n-0) 1 13 .

index[50].i++) f[i]=0. scanf("%d". for(i=0.j<n.count=0.&n).&c). count=0. scanf("%d". printf("enter no of files on index"). x: printf("enter index block"). if(c==1) goto x.k<n. goto y.b) Indexed #include<stdio.i<50. printf("\n enter another file indexed").i. } printf("\n index is already allocated"). } else { printf("\n file in the index already allocated").j.c.i. scanf("%d". for(k=0. printf("\nif you enter one more block(1/0)").j++) f[index[j]]=1. } y: printf("enter files\n"). } 14 .&i). printf("\n file indexed").f[index[k]]). if(count==n) { for(j=0.k.index[k]. for(i=0.&index[i]).k++) printf("\n %d -> %d : %d". printf("\n allocated"). if(f[i]!=1) { f[i]=1. if(f[index[i]]==0) count++.h> void main() { int f[50].i<n.n.i++) scanf("%d".

Output: Enter index block 5 Enter no. of files on index: 2 Enter files 10 20 File in the index is already allocated Enter another file index enter files 15 . of files on index: 3 Enter files 667 668 669 Allocated File indexed 3 ->667:1 3 ->668:1 3 ->669:1 Index is already allocated If you enter one more block (1/0) 1 Enter index block 4 Enter no.

j<(k+st).i.c. else exit(1).i<p. for(j=st.f[j]).i++) { scanf("%d".h> void main() { int f[50].c) Linked #include<stdio. f[a]=1.a.len. for(i=0. } 16 .&st. k=len.n. } } printf("\n if you want to enter one more file ?(yes-1/no-0)"). scanf("%d". k++. for(i=0.p.k.j++) { if(f[j]==0) { f[j]=1.i++) f[i]=0. scanf("%d%d".j.j. printf("enter block no's that are already allocated"). } else { printf("\n%d->file is already allocated".&c). printf("enter how many blocks that are already allocated").&a).&p).i<50. if(c==1) goto x. scanf("%d".h> #include<stdlib. printf("\n%d->%d".j).&len). } x: printf("enter starting index block and length").st.

Output: Enter how many blocks that are already allocated 4 Enter block no.’s that are already allocated 2 4 6 8 Enter starting index block and length 0 10 0 ->1 1 ->1 2 -> file is already allocated 3 ->1 4 -> file is already allocated 5 ->1 6 -> file is already allocated 7 ->1 8 -> file is already allocated 9 ->1 10 ->1 11 ->1 12 ->1 13 ->1 If you want to enter one more files? (yes-1/no-0) 0 17 .

printf("enter the no of processes:"). printf("the memory allocated for process%d is: %d ".p. count=count+m1. } printf("\nexternal fragmentation for this process is:%d".m1=0. scanf("%d".&m). scanf("%d". if(m1<=m) { if(count==m) printf("there is no further memory remaining:").&p).h> void main() { int m=0.m2). } } 18 .m2=0.i+1.i<p. for(i=0. scanf("%d". m2=m-m1.i+1).i+1).m). m=m2.&m1).count=0. printf("\nremaining memory is: %d".i.i++) { printf("\nenter memory req for process%d: ". printf("enter the memory capacity:"). } else { printf("memory is not allocated for process%d".3: PROGRAM TO SIMULATE MVT AND MFT. MVT: #include<stdio.m2).

Output: Enter the memory capacity: 50 Enter the no of processes: 3 Enter memory req for process 1: 12 The memory allocated for process 1: 50 Remaining memory is: 38 External fragmentation for process is : 38 Enter memory req for process 2: 27 The memory allocated for process 2: 38 Remaining memory is : 11 External fragmentation for this process is : 11 Enter memory req for process 3: 30 Memory is not allowed for process 3. 19 .

fra1.f2=0. 20 .p1.s. pos=m. } else { printf("\nProcess not allocated in partition%d". s=m/p.i<p1.i++) { if(pos<s) { printf("\nThere is no further memory for process%d". printf("Enter the no of partitions:").fra2.h> int main() { int m. pos=pos-s.i.i+1). fra1=s-m1[i]. int m1[4].i+1).&m).MFT: #include<stdio. printf("Each partn size is:%d". scanf("%d". for(i=0. } else { printf("\nEnter the memory req for process%d:".f. scanf("%d".f1=0.s).p.i+1).s1. printf("Enter the memory size:"). printf("\nEnter the no of processes:"). scanf("%d". printf("\nInternal fragmentation for process is:%d". scanf("%d".&p). s1=m1[i]. f1=f1+fra1. if(m1[i]<=s) { printf("\nProcess is allocated in partition%d".&m1[i]). while(s1>s) { s1=s1-s.fra1).&p1). m1[i]=0.pos.i+1). break.

i++) printf("\n%5d\t%5d". printf("\nThe tot no of fragmentation is:%d". } } } printf("\nProcess\tallocatedmemory").i+1. f2=f2+fra2. fra2=s-s1.pos=pos-s. f=f1+f2. printf("\nExternal Fragmentation for this process is:%d". return 0. } pos=pos-s. for(i=0. } Output: Enter the memory size: 100 Enter the no of partitions: 2 Each partn size: 50 Enter the no of processes: 2 Enter the memory req for process1: 20 Process is allocated in partition1 Internal fragmentation for process is: 30 Enter the memory req for process2: 30 Process is allocated in partition2 Internal fragmentation for process is : 20 Process 1 2 allocatedmemory 20 30 The tot no of fragmentation is : 50 4: SIMULATE ALL FILE ORAGANOZATION TECHNIQUES.f).m1[i]).i<p1.fra2). 21 .

a) Single Level Directory b) Two Level a) Single Level Directory #include<stdio.myfiles[i].myfiles[i].name.i<n.i.size). for(i=0. p=present(myfiles[i].myfiles[i].i++) { printf("%s %s %s %d\n". char owner[10].&myfiles[i].i<n. while(1) { scanf("%s".myfiles[i].myfiles[i]. struct filedet myfiles[10]. } printf("the file details are\n ").size).owner. }.size"). for(i=0. scanf("%d". scanf("%d".h> #include<stdlib.i).owner. } scanf("%s". int size.date).myfiles[i]. char date[15].name). printf("enter no of files").myfiles[i]. printf("enter other name").&n).h> #include<conio.name. scanf("%s". 22 . void main() { int p.date. int present(char[10].int). clrscr(). printf("name\towner\tdate\tsize"). if(p==1) { printf("file already exits\n").date. } if(p==0) break.i++) { printf("enter filename.h> struct filedet { char name[10].owner).n.

} } return res.x)==0) { res=1.of files: 2 Enter file name: A Enter owner name.j<k.int k) { int j=0.date.date.res=0.name.j++) { if(strcmp(myfiles[j].h> 23 . } Output: Enter no.size: 258 25-8-13 24 ONAME FILE DATE SIZE 514 A 16-8-13 20 258 B 25-8-13 24 b) Two Level #include<stdio.size: 514 16-8-13 20 Enter file name: B Enter owner name. } int present(char x[10].} getch(). for(j=0.

printf("enter no of subdirectories:").name).d[i].&m).j<d[i].n.name). for(i=0.i<m. scanf("%d".j++) printf("\n\t%s".j<d[i]. void main() { int m.h> struct dir { int n.dname). scanf("%s".(j+1)). } } printf("\n main directory is: %s".n).d[i]. scanf("%s".n.f[j]).d[i].i++) { printf("\n%d subdirectory is: %s". } } Output: Enter name of the main directory JNTUH 24 . char f[20][50].d[i].i. scanf("%s". for(j=0. for(i=0. scanf("%d".(i+1).j. char name[20].i++) { printf("\nenter name of subdirectory %d:". printf("\nenter no of files in subdirectory %d:".k.dname).(i+1)). printf("\nfiles in this subdirectory are:"). char dname[30].#include<string. }. printf("enter name of the main directory:"). struct dir d[5].(i+1)). for(j=0.j++) { printf("\nenter name of %d file:".f[j]).i<m.&d[i].

of files in subdirectory 1: 3 Enter name of 1 file: CSE Enter name of 2 file: ECE Enter name of 3 file: EEE Enter name of subdirectory 2: NON-CIRCUIT BRANCH Enter no. of files in subdirectory 2: 3 Enter name of 1 file: MECH Enter name of 2 file: CIVIL Enter name of 3 file: METALLURGY Main directory is: JNTUH 1 subdirectory is: CIRCUIT BRANCH Files in this subdirectory are: CSE ECE EEE 2 subdirectory is: NON-CIRCUIT BRANCH Files in this subdirectory are: MECH CIVIL METALLURGY 5: SIMULATE BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE.h> 25 .Enter no. #include <stdio. of subdirectories: 2 Enter name of subdirectory 1: CIRCUIT BRANCH Enter no.

j++) scanf("%d". for(i = 0. r. i< p. scanf("%d".alloc[i][j]. } printf("\n\nEnter the allocation for each process : "). j. for(j = 0. printf("\n\nEnter the Max Matrix for each process : "). int p. printf("\n\nEnter the no of resources : "). &r). count = 0. &p). j < r. j++) scanf("%d". for(j = 0. i < p. for(i = 0. i + 1). completed[10].#include <stdlib. &Max[i][j]).i + 1). printf("Enter the no of processes : "). safeSequence[10].h> int main() { int Max[10][10]. need[10][10]. &alloc[i][j]). i < p. j < r. process. i++) completed[i] = 0. i. for(i = 0. scanf("%d". i++) for(j = 0. i++) { printf("\nFor process %d : ". j < r. j++) need[i][j] = Max[i][j] . } printf("\n\nEnter the Available Resources : "). i < r. alloc[10][10]. do { 26 . for(i = 0. count. &avail[i]). avail[10]. i < p. i++) { printf("\nFor process %d : ". i++) scanf("%d". for(i = 0.

printf("\t\t"). for(i = 0. i++) { for( j = 0. Max[process][j] = 0. j++) { if(avail[j] < need[i][j]) { process = -1. for(i = 0.printf("\n Max matrix:\tAllocation matrix:\n"). j++) { avail[j] += alloc[process][j]. process + 1). i++) { if(completed[i] == 0)//if not completed { process = i . safeSequence[count] = process + 1. j++) printf("%d ". } } } if(process != -1) break. alloc[i][j]). break. for( j = 0. count++. j < r. Max[i][j]). i < p. } if(process != -1) { printf("\nProcess %d runs to completion!". } process = -1. completed[process] = 1. j < r. } 27 . j++) printf("%d ". alloc[process][j] = 0. for(j = 0. j < r. i < p. j < r. printf("\n"). for(j = 0.

i++) printf("%d ". if(count == p) { printf("\nThe system is in a safe state!!\n"). safeSequence[i]). printf("Safe Sequence : < "). printf(">\n"). i < p. } Output: Enter the no of processes : 3 Enter the no of resources : 3 Enter the Max Matrix for each process : For process 1 : 5 4 2 For process 2 : 6 2 1 For process 3 : 8 0 3 Enter the allocation for each process : For process 1 : 5 7 1 For process 2 : 2 0 7 For process 3 : 4 0 2 Enter the Available Resources : 4 5 1 Max matrix: 542 621 803 Allocation matrix: 571 207 402 28 .} } while(count != p && process != -1). } else printf("\nThe system is in an unsafe state!!"). for( i = 0.

0. 29 .Process 1 runs to completion! Max matrix: Allocation matrix: 000 000 621 207 803 402 Process 2 runs to completion! Max matrix: Allocation matrix: 000 000 000 000 803 402 Process 3 runs to completion! The system is in a safe state!! Safe Sequence : < 1 2 3 > 6: SIMULATE BANKERS ALGORITHM FOR DEAD LOCK PREVENTION.h> int curr[5][5].0. maxclaim[5][5]. int alloc[5] = {0. #include <stdio. avl[5].0}.0.

int maxres[5]. i. } printf("\nEnter the number of resources: ").i++) { 30 . scanf("%d". scanf("%d".j++) { scanf("%d". for(i=0. for(i=0. for(i=0.&maxclaim[i][j]).i++) { for(j=0. int count = 0. count++. j.j++) { scanf("%d".j<r.i++) { for(j=0.&maxres[i]).&curr[i][j]). safe=0.i).i<r. for(i=1. for(i=0. r.i<p. p.&r). } } printf("\nEnter allocated resource table:\n"). exec.i++) { running[i]=1. running[5].i<p. int main() { printf("\nEnter the number of processes: ").i<p. } } printf("\nThe resource of instances: ").j<r.i++) { printf("\nEnter the resource for instance :%d: ". } printf("\nEnter maximum resource table:\n").i<=r.&p). scanf("%d".

for(i=0.i++) { for(j=0.j<r.maxclaim[i][j]).i++) { for(j=0.i++) { avl[i]=maxres[i]-alloc[i].curr[i][j]).i++) { printf("\t%d". } for(i=0. } 31 . for(i=0.alloc[i]).i<p.j<r.j++) { alloc[j]+=curr[i][j].printf("\t%d".i<r.i++) { for(j=0.j<r. } for(i=0.j++) { printf("\t%d". } printf("\n").maxres[i]). for(i=0. } } printf("\nAllocated resources:").j++) { printf("\t%d". } printf("\nThe allocated resource table:\n"). } printf("\nThe maximum resource table:\n").i<r.i<p. } printf("\n").i<p.

for(j=0.j<r.i+1). for(i=0.i<p.j++) { if(maxclaim[i][j] .curr[i][j] > avl[j]){ exec=0. //Main procedure goes below to check for unsafe state. for(i=0.printf("\nAvailable resources:"). } }} if(!safe) { printf("\nThe processes are in unsafe state. break. running[i]=0. break.\n").i++) { if(running[i]) { exec=1.i<r. for(j=0. } printf("\n"). while(count!=0) { safe=0. } break. count--.j++) { avl[j]+=curr[i][j]. safe=1.j<r.avl[i]). } } if(exec) { printf("\nProcess%d is executing\n". } else { 32 .i++) { printf("\t%d".

printf("\nThe process is in safe state").i++) { printf("\t%d". } printf("\n").i<r. for(i=0.avl[i]). printf("\nSafe sequence is:"). a) FIFO b) LRU a) FIFO 33 . 7: SIMULATE ALL PAGE REPLACEMENT ALGORITHMS. } } } Output: Enter the number of processes: 3 Enter the number of resources: 3 Enter the resource for instance :1: 2 Enter the resource for instance :2: 3 Enter the resource for instance :3: 1 Enter maximum resource table: 5 6 7 2 1 0 3 6 1 Enter allocated resource table: 1 3 2 8 9 0 1 2 3 The resource of instances: 0 2 3 The allocated resource table: 1 3 2 8 9 0 1 2 3 The maximum resource table: 5 6 7 2 1 0 3 6 1 Allocated resources: Available resources:-10 10 -12 14 -2 5 The processes are in unsafe state.

frame[k]).j. j=(j+1)%no.i<=n. return 0.frame[10]. } printf("\n"). } Output: ENTER THE NUMBER OF PAGES: 20 ENTER THE PAGE NUMBER : 70120304230321201701 34 . count++.i<no.k<no.i++) { printf("%d\t\t". if (avail==0) { frame[j]=a[i].&a[i]).avail.n.i++) frame[i]= -1. avail=0. printf("\n ENTER THE PAGE NUMBER :\n").k++) printf("%d\t". j=0.#include<stdio. printf("\n ENTER THE NUMBER OF PAGES:\n").&no). printf("\n ENTER THE NUMBER OF FRAMES :"). for(k=0.count).a[50].i<=n.a[i]). for(i=1.&n). for(i=0. for(k=0. for(i=1.no. scanf("%d".h> int main() { int i.k.k++) if(frame[k]==a[i]) avail=1.count=0.k<no. printf("\tref string\t page frames\n"). scanf("%d".i++) scanf("%d". } printf("Page Fault Is %d".

h> main() { int q[20].p[50].j.n.t.k=0.d.c2[20].c1. printf("Enter no of pages:").r.b[20].i.c=0.f.ENTER THE NUMBER OF FRAMES :3 ref string page frames 7 7 -1 -1 0 7 0 -1 1 7 0 1 2 2 0 1 0 3 2 3 1 0 2 3 0 4 4 3 0 2 4 2 0 3 4 2 3 0 0 2 3 3 2 1 0 1 3 2 0 1 2 0 1 7 7 1 2 0 7 0 2 1 7 0 1 Page Fault Is 15 b)LRU #include<stdio. 35 .

j++) { if(p[i]!=q[j]) c1++.&f).i<n. q[k]=p[k].r++) b[r]=c2[r]. for(j=i-1.r<f. printf("Enter no of frames:"). for(i=0. } else { for(r=0.scanf("%d". printf("Enter the reference string:"). for(j=0.i<n. for(i=1. else break. } } for(r=0. printf("\n\t%d\n".&p[i]).r++) { c2[r]=0.i++) scanf("%d". } if(c1==f) { c++. c++. k++. printf("\n").j<n.q[k]).i++) { c1=0.r<f. scanf("%d".j--) { if(q[r]!=p[j]) c2[r]++.&n).j<k. if(k<f) { q[k]=p[i]. k++. 36 .j<f.j++) printf("\t%d".q[j]). for(j=0.

j<f.r++) { for(j=r.q[r]).for(r=0. } printf("\n"). printf("\t%d". } } } for(r=0.j++) { if(b[r]<b[j]) { t=b[r]. b[j]=t.c). } } } printf("\nThe no of page faults is %d".r<f.r++) { if(c2[r]==b[0]) q[r]=p[i].r<f. b[r]=b[j]. } Output: Enter no of pages:10 Enter the reference string:7 5 9 4 3 37 .

h> void main() { int np. printf("enter how many pages").7 9 6 2 1 Enter no of frames:3 7 7 7 4 4 4 9 9 9 1 5 5 5 3 3 3 6 6 6 9 9 9 7 7 7 2 2 The no of page faults is 10 8: SIMULATE PAGING TECHNIQUE OF MEMORY MANGEMENT.ps.i. 38 . int *sa. #include<stdio.

&np).&ps).i<np.sa[i]). scanf("%d". for(i=0.scanf("%d". sa=(int *) malloc (2 *np). printf("enter page size\n").i+1. printf("page %d\tadress%u\n". } } Output: Enter how many pages 5 Enter page size 4 Page 1 address 1900 Page 2 address 1908 Page 3 address 1916 Page 4 address 1924 Page 5 address 1932 LINUX PROGRAMS 1: WRITE A SHELL SCRIPT TO GENERATE A MULTIPLICATION TABLE.i++) { sa[i]=(int) malloc(ps). echo "Multiplication Table" 39 .

echo "Enter a number" read n m=0 echo for((i=1.k<=10.k++)) do m=`expr \$k \* \$i` echo \$i "*" \$k "=" \$m done echo " " done Output: Multiplication Table Enter a number 3 1*1=1 1*2=2 40 .i++)) do for((k=1.i<=n.

1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1 * 10 = 10 2*1=2 2*2=4 2*3=6 2*4=8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 3*1=3 3*2=6 3*3=9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30 2: WRITE A SHELL SCRIPT THAT COPIES MULTIPLE FILES TO A DICTIONARY. iter=1 echo Enter new dir: read nn mkdir \$nn echo Enter number of files: read na 41 .

sh 3: WRITE A SHELL SCRIPT WHICH COUNTS THE NUMBER OF LINES AND WORDS PRESENT IN A GIVEN FILE. echo" enter a file name" read fname terminal= `tty` exec<\$fname nol=0 now=0 while read line 42 .sh \$ls newd prime.sh fact.while [ \$iter –le \$na ] do echo Enter file name: read fn cp \$fn \$nn iter=`expr \$iter + 1` done Output: enter a new directory name newd enter number of files 2 enter filename prime.sh enter file name fact.

echo "List of Files which have Read.do no=`expr \$nol+1` set \$line now= `expr \$now+ \$#` done echo "no. of words:71 4: WRITE A SHELL SCRIPT WHICH DISPLAYS THE LIST OF ALL FILES IN THE GIVEN DIRECTORY. Write and Execute Permissions in Current Directory" for file in * do if [ -r \$file -a -w \$file -a -x \$file ] then echo \$file fi done 43 . of lines:23 no. of words:\$now" exec>\$terminal Output: enter a file name palindrome. of lines: \$nol" echo"no.sh no.

Write and Execute Permissions in Current Directory page priority 5: WRITE A SHELL SCRIPT (SMALL CALCULATOR) THAT ADDS.Output: List of Files which have Read..MULTIPLIES AND DIVIDES THE GIVEN TWO INTEGERS.\$b` 44 . 2)echo "subtraction of two numbers is" y=`expr \$a . echo "enter the numbers" read a read b echo "enter ur choice" read i case \$i in 1)echo "Addition of two numbers is" x=`expr \$a + \$b` echo \$x.SUBTRACTS.

. 45 ..h> #include<stdlib. 3)echo "multiplication of two numbers is" z=`expr \$a \* \$b` echo \$z. esac Output: enter the numbers 2 3 enter ur choice 3 multiplication of two numbers is 6 7: WRITE A C PROGRAM THAT COUNTS NUMBER OF BLANKS IN A TEXT FILE..h> int main() { char fn[30]. 4)echo "division of two numbers is" s=`expr \$a / \$b` echo \$s.h> #include<string. a) Using Standard I/O b) Using System Calls a) Using Standard I/O #include<stdio.echo \$y.

fp).1. } l=strlen(buf). char buf[65000].h> #include<string.h> #include<fcntl. exit(0). int count=0.h> int main() 46 . fp=fopen(fn. char c. if(fp==NULL) { printf("no such file exists\n").i<l.FILE *fp. } Output: enter the fle namemv.65000.c the number of blanks in the given file is 67 b) Using System Calls #include<stdio. for(i=0. int l=0.count). } printf("\nthe number of blanks in the given file is %d\n". gets(fn). printf("enter the fle name"). int i=0.h> #include<stdlib. } while(!feof(fp)) { fread(buf.i++) { if(buf[i]==' ') count++."r").

count).h> int main() { 47 .i++) if(buf[i]==' ') count++.h> #include<string.buf.{ int n=0. n=read(fd. gets(fn). a) cat b) ls c) mv a) cat #include<stdio. char fn[50]. int fd=0. char buf[65000]. int i=0.O_RDONLY). if(n<0) { printf("read error\n").i<n. exit(0).h> #include<fcntl.h> #include<stdlib.65000). printf("enter the file name"). printf("the number of blanks in the given file is %d\n". } for(i=0. int count=0. } Output: enter the file namehome the number of blanks in the given file is 17 8: IMPLEMENT IN C THE FOLLOWING UNIX COMMANDS USING SYSTEM CALLS. fd=open(fn.

buf.O_RDONLY).n)!=n) { printf("write error\n"). char buf[65000].O_RDONLY). exit(0).fd2.buf. int fd1. } } if(n<0) { printf("read error\n")./stask8a bash: . gets(f1). fd2=open(f2./stask8a: No such file or directory 48 .n))!=n) { printf("write error\n"). while((n=read(fd1. } while((n=read(fd2.n=0.buf.f2[50].char f1[50].c cse@cse-Not-Specified:~/11011M2109 gcc stask8a.65000))>0) { if((t=write(1. printf("enter the files to be concatenated"). gets(f2). exit(0). exit(0). int t. } } Output: cse@cse-Not-Specified:~/11011M2109\$ vi stask8a. exit(0).c cse@cse-Not-Specified:~/11011M2109\$ . fd1=open(f1.65000))>0) { if(write(1. } } if(n<0) { printf("read error\n").buf.

h> #include<sys/types.c msgqsend.c read error b) ls #include<stdio.h> #include<stdlib. printf("enter the name of the directory whose contents are to be displayed").h> int main() { DIR *dp. struct dirent *dirp. gets(dn). if(dp==NULL) 49 . dp=opendir(dn).h> #include<dirent.out enter the files to be concatenated msgqrec.cse@cse-Not-Specified:~/11011M2109\$ .h> #include<string. char dn[30]./a.

{
printf("no such directory exists\n");
exit(0);
}
{
printf("%s\n",dirp->d_name);
}
closedir(dp);
}

Output:
enter the name of the directory whose contents are to be displayed11011M2109
msgqrec.c
msgqrec
..
a.out
msgqsend.c~
.
producer_consumer2.c
producer_consumer2.c~

c) mv
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
int main()
{
char source[50],dest[50];
struct stat *fd;
int i=0;
fd=(struct stat*) malloc(sizeof(struct stat));
//rename("/home/kumar/delete.txt","/home/kumar/nsl/");
printf("enter the name of the source file");
gets(source);
printf("enter the name of the destination");
50

gets(dest);
stat(dest,fd);
/*if(S_ISDIR(fd->st_mode))
{
printf("destination is a directory hence copying to the directory\n");
i=rename(source,dest);
printf("return value of rename is %d\n",i);
if(!i)
printf("copied\n");
else
printf("error in copying\n");
}*/
//else
//{
//
printf("destination is a not a directory\n");
i=rename(source,dest);
//printf("return value of rename is %d\n",i);
if(!i)
printf("copied\n");
else
printf("error in copying\n");
//}
printf("end of operation\n");
}

Output:
enter the name of the source filefork.c
enter the name of the destinationhome
copied
end of operation

51

9: WRITE A PROGRAM THAT TAKES ONE OR MORE
FILE/DIRECTORY NAMES AS COMMAND LINE INPUT AND
REPORTS THE FOLLOWING IINFORMATION ON TE FILE :
1) File type
3) Time of last access
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
int main(int argc,char* argv[])
{
struct stat buff;
52

&buff). return 0.//opening a file with read permissions //to check if file exists if(fd == -1) { printf("File not found\n"). break. close(fd). break.O_RDONLY).argv[0]). break.(long)(0777&buff.c Type of file is Regular file No of links : 1 Time of last access : Thu Oct 31 03:09:00 2013 53 . switch(buff. case S_IFDIR:printf("Directory File\n").ctime(&buff. } printf("No of links : %d\n". printf("Time of last access : %s".st_nlink).st_mode)). case S_IFSOCK:printf("Socket file \n"). } Output: server:Lu Admin\$ .19 9. break. } fd = open(argv[1]. } fstat(fd.st_mode & S_IFMT) { case S_IFBLK:printf("Block file\n"). default: printf("Unknown file type\n").st_atime)).int fd.buff. exit(0). case S_IFREG:printf("Regular file \n"). //file descriptor if(argc != 2) { //this means no file is given at command line printf("Correct usage is %s <File name> ". printf("Type of file is "). printf("File permissions : %lo\n". exit(0).

char *argv[]) { int fd[2]. #include<stdio.h> #include<stdlib. dup2(fd[1]. close(fd[1]).k.pid. 54 .NULL).File permissions : 644 server:Lu Admin\$ 10: WRITE A C PROGRAM THAT ILLUSTRATES HOW TO EXECUTE TWO COMMAND CONCURRENTLY WITH A COMMAND PIPE. if(pid==0) { close(fd[0]). } pid=fork(). exit(1).argv[1]. execlp(argv[1]. if(k==-1) { perror("pipe").h> int main(int argc.1). k=pipe(fd).

getpid()). #include<stdio. close(fd[1]). } } Output: server:Lu Admin\$ gcc 10. ls_sort. } else { wait(2).perror("execl").h> int main() { int pid . printf("pid =%d \n ".argv[2].out ls sort a. printf("ppid=%d \n ". dup2(fd[0]. pid=fork().out ls-l_sort.0). if(pid==0) { printf"in child process \n ").c named. } else if(pid>0) { printf("in parent process \n ").getppid()).c server:Lu Admin\$ . perror("execl").c 11: WRITE A C PROGRAM THAT ILLUSTRATES THE CREATION OF CHILD PROCESS USING FORK SYSTEM CALL. printf("pid =%d \n ". printf("ppid =%d \n " getppid()). execlp(argv[2]. } else { 55 . close(fd[0]).NULL)./a.getpid()).

} return 0. } Output: In parent process Pid=2831 Ppid=2230 In child process Pid=2832 Ppid=1 12: WRITE A C PROGRAM THAT DISPLAYS THE REAL TIME OF A DAY EVERY 60 SECONDS. int times_up().h> #include <sys/signal. } 56 .) { times_up(1).printf("error in creation of child process"). sleep(60). void main() { for (.h> #include <sys/time.h> /* Declarations */ void main(). . #include <stdio.

h> #include<pthread. }buf. return (sig).} int times_up(sig) int sig.full. int in. char *ctime(). long time(struct tms *ptr). printf("It is now %s\n".h> #define N 5 struct buffer { int b[N].h> #include<semaphore. (USING SEMAPHORES) #include<stdio. while(1) 57 . ctime (&now)). time (&now). { long now.h> #include<stdlib. } Output: It is now Thu Oct 3 16:09:15 2013 14: WRITE A C PROGRAM THAT IMPLEMENTS A PRODUCERCONSUMER SYSTEM WITH TWO PROCESSES.out. sem_t empty. void *P() { int i=1.

buf.in]=i. pthread_join(t2.out]. printf("\nProduced :%d".NULL).out+1)%N. sleep(2). buf.full)).NULL).P.empty).b[buf.empty)).in=0.0. while(1) { sem_wait(&(buf.out=(buf.0. } } void *C() { int item.C.NULL./main.item).full). buf.NULL).N). sem_init(&(buf.{ sem_wait(&(buf.i++). sem_post(&(buf.out <INT> <INT> <INT> Segmentation fault cse@cse-Not-Specified:~/11011P0519\$ a. pthread_create(&t2.empty)). buf.t2.in+1)%N.b[buf. } } int main() { pthread_t t1.c -lpthread cse@cse-Not-Specified:~/11011P0519\$ . pthread_join(t1. sem_post(&(buf. printf("Consumed :%d".NULL).out 10 10 10 58 . } Output: cse@cse-Not-Specified:~/11011P0519\$ gcc producer_consumer2.full)). sem_init(&(buf. buf.0). item=buf. pthread_create(&t1.NULL.out USAGE:./a.in=(buf.out=0.

SEGSIZE. char *argv[ ]) { int shmid. if((shmid=shmget(key.cntr.h> #include<sys/shm.h> #include<sys/types.a.'s'). key_t key.SEGSIZE.h> #include<string. char buff[ ]="Hello world".h> #include<sys/ipc. char *segptr. key=ftok(". IPC_CREAT | IPC_EXCL | 0666))== -1) { if((shmid=shmget(key./a.h> #define SEGSIZE 100 int main(int argc.".out 10 10 10 producer produced 35005211 consumer consumed 35005211 producer produced 1726956429 consumer consumed 1726956429 producer produced 278722862 consumer consumed 278722862 producer produced 468703135 producer produced 1801979802 producer produced 635723058 producer produced 1125898167 consumer consumed 1125898167 Exit the program 15: WRITE A C PROGRAM THAT ILLUSTRATES INTER PROCESS COMMUNICATION USING SHARED MEMORY SYSTEM CALLS.0))== -1) 59 .h> #include<stdlib. #include <stdio.out: command not found cse@cse-Not-Specified:~/11011P0519\$ .

printf("DATA:-%s\n".\n").0)== -1) printf("Can’t Remove Shared memory Segment. } printf("Writing data to shared memory. printf("DONE\n").\n").out Creating a new shared memory seg -----.c ramyakala@linux-ng56:~/Desktop/oslab/fwdlinux141516> . } } else { printf("Creating a new shared memory seg \n"). printf("SHMID:%d". if(shmctl(shmid. exit(1).Shared Memory Segments -------key shmid owner perms bytes nattch status 0x00000000 1507328 ramyakala 600 393216 2 dest 0x00000000 262145 ramyakala 700 7680 2 dest 0x00000000 294914 ramyakala 700 33120 2 dest 0x00000000 327683 ramyakala 700 11508 2 dest 0x00000000 360452 ramyakala 700 11508 2 dest 60 .. } system("ipcs_m")..\n").{ perror("shmget"). strcpy(segptr. printf("Reading data from shared memory. printf("DONE\n")...buff)..\n"). else printf("Removed Successfully"). printf("Removing shared memory Segment. exit(1)..0.0))==(char*)-1) { perror("shmat").. if((segptr=shmat(shmid.. shmid)./a.IPC_RMID.segptr). } Output: ramyakala@linux-ng56:~/Desktop/oslab/fwdlinux141516> gcc shmem1.

.h> #define MAXSIZE 128 void die(char *s) { perror(s). a) Creating a Message Queue b) Writing to a Message Queue c) Reading from a Message Queue a) Receiver side: #include <sys/types. DATA:-Hello world DONE Removing shared memory Segment.h> #include <sys/ipc. } 61 .h> #include <stdlib..-----. Removed Successfully 16: WRITE A C PROGRAM THAT ILLUSTARES THE FOLLOWING. exit(1).Message Queues -------key msqid owner perms used-bytes messages SHMID:11501613Writing data to shared memory..Semaphore Arrays -------key semid owner perms nsems -----...h> #include <stdio. DONE Reading data from shared memory..h> #include <sys/msg.

/msgqrec 40 62 . &rcvbuffer. 0666)) < 0) die("msgget()"). 1. key_t key. rcvbuffer.typedef struct msgbuf { long mtype. exit(0). key = 1234. if ((msqid = msgget(key. main() { int msqid. }.c:18:1: warning: useless storage class specifier in empty declaration [enabled by default] cse@cse-Not-Specified:~/11011P0519\$ . printf("%s\n". if (msgrcv(msqid.mtext). 0) < 0) die("msgrcv").c -o msgqrec msgqrec. char mtext[MAXSIZE]. MAXSIZE. //Receive an answer of message type 1. } Output: cse@cse-Not-Specified:~/11011P0519\$ vi msgqrec. struct msgbuf rcvbuffer.c cse@cse-Not-Specified:~/11011P0519\$ cc msgqrec.

exit(1). main() { 63 .h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <sys/ipc. }.h> #define MAXSIZE 128 void die(char *s) { perror(s).b) Sender side: #include <sys/types. } struct msgbuf { long mtype. char mtext[MAXSIZE].h> #include <sys/msg.

scanf("%[^\n]". } Output: cse@cse-Not-Specified:~/11011P0519\$ vi msgqsend.int msqid. buflen). sbuf. die("msgsnd").c: In function ‘main’: msgqsend. key = 1234.mtype. } else printf("Message Sent\n"). int msgflg = IPC_CREAT | 0666. IPC_NOWAIT) < 0) { printf ("%d. //Message Type sbuf. &sbuf. buflen = strlen(sbuf.c -o msgqsend msgqsend. %d.mtext). size_t buflen.c cse@cse-Not-Specified:~/11011P0519\$ cc msgqsend. key_t key. sbuf. msgflg )) < 0) //Get the message queue ID for the given key die("msgget"). %d\n". %s.sbuf. exit(0). getchar(). msqid. if ((msqid = msgget(key. if (msgsnd(msqid. but argument 3 has type ‘long int’ [-Wformat] cse@cse-Not-Specified:~/11011P0519\$ ./msgqsend Enter a message to add to message queue : 40 Message Sent 64 . struct msgbuf sbuf.c:45:9: warning: format ‘%d’ expects argument of type ‘int’. buflen.mtext) + 1 .mtext. printf("Enter a message to add to message queue : ").mtype = 1.

---------------------------------------------------------------------------------------------------------------- 65 .