0% found this document useful (0 votes)
229 views

Os Lab Manual

This document contains a C program code for implementing the Shortest Job First (SJF) CPU scheduling algorithm. The program takes input of the number of processes, their names, burst times and arrival times. It then calculates the waiting times and turnaround times of each process using SJF. Finally, it prints the average waiting time, average turnaround time and a Gantt chart showing the schedule.

Uploaded by

ajith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views

Os Lab Manual

This document contains a C program code for implementing the Shortest Job First (SJF) CPU scheduling algorithm. The program takes input of the number of processes, their names, burst times and arrival times. It then calculates the waiting times and turnaround times of each process using SJF. Finally, it prints the average waiting time, average turnaround time and a Gantt chart showing the schedule.

Uploaded by

ajith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

UNIVERSITY COLLEGE OF ENGINEERING PATTUKKOTTAI

(Constituent college of Anna University: Chennai-25)


RAJAMADAM - 614701

RECORD NOTE BOOK

UNIVERSITY COLLEGE OF ENGINEERING


UNIVERSITY COLLEGE OF ENGINEERING
PATTUKKOTTAI
RAJAMADAM - 614701

ANNA UNIVERSITY:: CHENNAI 600025

RECORD NOTE BOOK

NAME :

REGISTER NUMBER :

SUBJECT CODE & SUBJECT NAME :

SEMESTER :

BRANCH :

ACADEMIC YEAR :
BONAFIDE CERTIFICATE

REGISTER NO:

Certified to be the bonafide record of work done by

Mr./Ms.__________________________ of Semester

B.E/B.Tech________________________________ degree course

for_______________________________________________________

Laboratory in University College of Engineering, Pattukkottai,

Rajamadam during the academic year________________.

Staff Incharge Head of the Department

Submitted for the university practical Examination held on

_______________at University college of Engineering, Pattukkottai,

Rajamadam.

INTERNAL EXAMINER EXTERNAL EXAMINER


LIST OF EXPERIMENTS

EX.NO DATE Experiment Page Signature


PROGRAM:
#include<stdio.h>
main(int arc,char*ar[])
{
int pid;
char s[100];
pid=fork();
if(pid<0)
printf("error");
else if(pid>0)
{
wait(NULL);
printf("\n Parent Process:\n");
printf("\n\tParent Process id:%d\t\n",getpid());
execlp("cat","cat",ar[1],(char*)0);
error("can’t execute cat %s,",ar[1]);
}
else
{
printf("\nChild process:");
printf("\n\tChildprocess parent id:\t %d",getppid());
sprintf(s,"\n\tChild process id :\t%d",getpid());
write(1,s,strlen(s));
printf(" ");
printf(" ");
printf(" ");
execvp(ar[2],&ar[2]);
error("can’t execute %s",ar[2]);
}
}
OUTPUT:
[root@localhost ~]# ./a.out tst date
Child process:
Child process id :
3137 Sat Apr 10 02:45:32 IST 2010
Parent Process:
Parent Process id:3136
sd
dsaASD[root@localhost ~]# cat tst
sd
dsaASD
PROGRAM:
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
main(int c,char* arg[])
{
DIR *d;
struct dirent *r;
int i=0;
d=opendir(arg[1]);
printf("\n\t NAME OF ITEM \n");
while((r=readdir(d)) != NULL)
{
printf("\t %s \n",r->d_name);
i=i+1;
}
printf("\n TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS
%d \n",i);
}

OUTPUT:
[root@localhost ~]# cc dr.c
[root@localhost ~]# ./a.out lab_print
NAME OF ITEM
pri_output.doc
sjf_output.doc
fcfs_output.doc
rr_output.doc
ipc_pipe_output.doc
pro_con_prob_output.doc
TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS 8
PROGRAM:
#include<stdio.h>
#include<string.h>
main(int ag,char* arg[])
{
char buf[200],line[200];
int i,j,n,fd1,count=0,opt;
if(ag==4)
{
fd1=open(arg[3],0);
if(strcmp(arg[1],"-c")==0)
opt=2;
if(strcmp(arg[1],"-i")==0)
opt=3;
}
else if(ag==3)
{
fd1=open(arg[2],0);
opt=1;
}
if(fd1==-1)
printf("error in opening");
j=0;
switch(opt)
{
case 1:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strstr(line,arg[1])!=0)
write(1,line,j+1);
}
}
}
break;
case 2:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strstr(line,arg[2])!=0)
count=count+1;
j=-1;
}
}
}
printf("%d \n",count);
break;
case 3:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strcasestr(line,arg[2])!=0)
write(1,line,j+1);
j=-1;
}
}
}
break;
}
close(fd1);
}

OUTPUT:
[root@localhost ~]# cat tst sd dsa AS D
[root@localhost ~]# ./a.out -i a tst aA
[root@localhost ~]# ./a.out -c a tst 1[root@localhost ~]# ./a.out -c A tst1
[root@localhost ~]# ./a.out -c sd tst 1[root@localhost ~]# ./a.out -c s tst 2
Program:
echo “enter the first string”
read str1
echo “enter the second string”
read str2
echo “the concatenated string is” $str1 $str2
Sample I/P:
Enter first string: Hello
Enter first string: World
Sample O/P:s
The concatenated string is Hello World
Program:
echo “enter the first string”
read str1
echo “enter the second string”
read str2
if [ $str1 = $str2 ]
then
echo “strings are equal”
else
echo “strings are unequal”
fi
Sample I/P:1
Enter first string: hai
Enter second string: hai
Sample O/P:1
The two strings are equal
Sample I/P:2
Enter first string: hai
Enter second string: cse
Sample O/P:2
The two strings are not equal
Program:
echo "enter A"
read a
echo "enter B"
read b
echo "enter C"
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is greater"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "B is greater"
else
echo "C is greater"
fi
Sample I/P:
Enter A:23
Enter B:45
Enter C:67
Sample O/P:
C is greater
Program :
echo enter the number
read n
a=-1
b=1
i=0
while [ $i –le $n ]
do
t=`expr $a + $b`
echo $t
a=$b
b=$t
i=`expr $i + 1
done
Sample I/P :
Enter the no: 5
Sample O/P:
0
1
1
2
3
5
Program:
echo 1.Addition
echo 2.Subraction
echo 3.Multiplication
echo 4.Division
echo enter your choice
read a
echo enter the value of b
read b
echo enter the value of c
read c
echo b is $b c is $c
case $a in
1)d=`expr $b + $c`
echo the sum is $d
;;
2)d=`expr $b - $c`
echo the difference is $d
;;
3)d=`expr $b \* $c`
echo the product is $d
;;
4)d=`expr $b / $c`
echo the quotient is $d
;;
esac
Sample I/P :
1.Addition
2.Subraction
3.Multiplication
4.Division
Enter your choice:1
Enter the value of b:3
Enter the value of c:4
b is 3 c is 4
the sum is 7
Sample O/P:
b is 3 c is 4
the sum is 7
PROGRAM:

#include<stdio.h>
#include<string.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],w;
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void fcfs();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n---------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n---------------------------------------------------------\n");
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t\t",Wt[i]);
printf("%d",Wt[n]+Bu[n]);
printf("\n---------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
Wt[1]=0;
for(i=2;i<=n;i++)
{
Wt[i]=Bu[i-1]+Wt[i-1];
}
for(i=1;i<=n;i++)
{
Twt=Twt+(Wt[i]-A[i]);
Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
}
Att=(float)Ttt/n;
Awt=(float)Twt/n;
printf("\n\n Average Turn around time=%3.2f ms ",Att);
printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
}
void fcfs()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(A[i]>A[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
A[i]=A[j];
Bu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
void main()
{
int ch;
Getdata();
fcfs();
}
OUTPUT:

[2cse@localhost ~]$ cc fcfs.c


[2cse@localhost ~]$ ./a.out
Enter the number of processes: 3
Enter the process name: p1
Enter The BurstTime for Process p1 = 4
Enter the Arrival Time for Process p1 = 0
Enter the process name: p2
Enter The BurstTime for Process p2 = 6
Enter the Arrival Time for Process p2 = 1
Enter the process name: p3
Enter The BurstTime for Process p3 = 8
Enter the Arrival Time for Process p3 = 2
Average Turn around time=9.67 ms
AverageWaiting Time=3.67 ms

GANTT CHART
------------------------------------------
| p1 | p2 | p3 |
------------------------------------------
0 4 10 18
------------------------------------------
PROGRAM:
#include<stdio.h>
#include<string.h>
int Twt,Ttt,A[20],Wt[20],n,Bu[20],B[10];
float Att,Awt;
char pname[20][20];
void Getdata();
void Gantt_chart();
void Sjf();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\nGANTT CHART");
printf("\n----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t\t",Wt[i]);
printf("%d",Wt[n]+B[n]);
printf("\n------------------------------------------------------------\n");
printf("\n");
}
void Sjf()
{
int w,t,i,Tt=0,temp,j;
char S[10],c[20][20];
int temp1;
printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM
\n\n");
Twt=Ttt=0;
w=0;
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
S[i]='T';
Tt=Tt+B[i];
}
for(i=1;i<=n;i++)
{
for(j=3;j<=n;j++)
{
if(B[j-1]>B[j])
{
temp=B[j-1];
temp1=A[j-1];
B[j-1]=B[j];
A[j-1]=A[j];
B[j]=temp;
A[j]=temp1;
strcpy(c[j-1],pname[j-1]);
strcpy(pname[j-1],pname[j]);
strcpy(pname[j],c[j-1]);
}
}
}
Wt[1]=0;
w=w+B[1];
t=w;
S[1]='F';
while(w<Tt)
{
i=2;
while(i<=n)
{
if(S[i]=='T'&&A[i]<=t)
{
Wt[i]=w;
S[i]='F';
w=w+B[i];
t=w;
i=2;
}
else
i++;
}
}
for(i=1;i<=n;i++)
{
Twt=Twt+(Wt[i]-A[i]);
Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
}
Att=(float)Ttt/n;
Awt=(float)Twt/n;
printf("\n\n Average Turn around time=%3.2f ms ",Att);
printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
Gantt_chart();
}
void main()
{
Getdata();
Sjf();
}
OUTPUT:

[2cse@localhost ~]$ cc sjf.c


[2cse@localhost ~]$ ./a.out

Enter the number of processes: 3

Enter the process name: p1


Enter The BurstTime for Process p1 = 5
Enter the Arrival Time for Process p1 = 0

Enter the process name: p2


Enter The BurstTime for Process p2 = 8
Enter the Arrival Time for Process p2 = 1

Enter the process name: p3


Enter The BurstTime for Process p3 = 3
Enter the Arrival Time for Process p3 = 2

SHORTEST JOB FIRST SCHEDULING ALGORITHM

Average Turn around time=8.67 ms


AverageWaiting Time=3.33 ms

GANTT CHART
-------------------------------------------
| p1 | p3 | p2 |
--------------------------------------------
0 5 8 16
---------------------------------------------
PROGRAM:
#include<stdio.h>
main()
{
int prs[10],prty[10];
int n,i,j,twait=0,tta=0,tot=0,totl=0,temp,temp1;
int wait[10],bst[10],p[10];
float wavg,tavg;
printf("\n\t\tPRIORITY SCHEDULING");
printf("\n enter the no of process:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter the process name: P");
scanf("%d",&prs[i]);

printf("\n enter the burstTime");


scanf("%d",&bst[i]);

printf("\n enter the priority");


scanf("%d",&prty[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(prty[i]>=prty[j])
{
temp=bst[i];
bst[i]=bst[j];
bst[j]=temp;
temp1=prs[i];
prs[i]=prs[j];
prs[j]=temp1;
}
}
}
printf ("\n\n\n sorted process with priority");
printf("\n-------------------------------------");
printf("\n Processname \t burst Time \n");
printf("\n-------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("\tp%d \t %d \n\n",prs[i],bst[i]);
}
printf("\n\n");
printf("\n-------------------------------------------------------------");
printf("\n Processor\tBursttime\tTurnaroundtime\tWaitingtime\n");
printf("\n-------------------------------------------------------------");
for(i=1;i<=n;i++)
{
tta=tta+bst[i];
wait[i]=tta-bst[i];
printf("\n\tp%d\t%d\t\t%d\t\t%d",prs[i],bst[i],tta,wait[i]);
twait=twait+wait[i];
totl=totl+tta;
}
wavg=twait/n;
tavg=totl/n;
printf("\n\n\n\t\t***GRANTT CHART***\n");
printf("\n__________________________________________________\n");
for(i=1;i<=n;i++)
printf("|\tp%d\t",prs[i]);
printf("|\t\n");
printf("\n__________________________________________________\n");
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t\t",wait[i]);
printf("%d",wait[n]+bst[n]);
printf("\n");
printf("\n\n Total burst time is :%d",tta);
printf("\n\n Total turnaround time :%d",totl);
printf("\n\n The average turnaround time:%f",tavg);
printf("\n\n Total waiting time :%d",twait);
printf("\n\n The avg waiting time :%f",wavg);
}
OUTPUT:
[2cse@localhost ~]$ cc pri.c
[2cse@localhost ~]$ ./a.out

PRIORITY SCHEDULING
enter the no of process:5

enter the process name: P1


enter the burstTime5
enter the priority2

enter the process name: P2


enter the burstTime6
enter the priority4

enter the process name: P3


enter the burstTime2
enter the priority3

enter the process name: P4


enter the burstTime8
enter the priority1

enter the process name: P5


enter the burstTime7
enter the priority5
sorted process with priority
-------------------------------------
Processname burst Time
------------------------------------
p4 8
p1 5
p3 2
p2 6
p5 7
-------------------------------------------------------------
Processor Bursttime Turnaroundtime Waitingtime
-------------------------------------------------------------
p4 8 8 0
p1 5 13 8
p3 2 15 13
p2 6 21 15
p5 7 28 21

***GRANTT CHART***
___________________________________________
| p4 | p1 | p3 | p2 | p5|
___________________________________________
0 8 13 15 21 28

Total burst time is :28


Total turnaround time :85
The average turnaround time:17.000000

Total waiting time :57


The avg waiting time :11.000000
PROGRAM:
#include<stdio.h>
struct process
{
int at,bt,wt,tat,st,ft,flag,id,tbt;
}p[10],temp;
int n,t,save_et[10],save_id[10],turn,btsum;
float awt,atat;
void read();
void print();
void rndrbn();
void fifoq();
main()
{
int ch;
read();
fifoq();
rndrbn();
print();
}
void read()
{
int i;
printf("Enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arriving time ,burst time of process p%d : ",(i+1));
scanf("%d%d",&p[i].at,&p[i].bt);
p[i].id=i+1;
p[i].wt=p[i].flag=0;
p[i].tbt=p[i].bt;
btsum+=p[i].bt;
}
printf("Enter time quantum : ");
scanf("%d",&t);
}
void fifoq()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].at>p[j+1].at)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void rndrbn()
{
int cnt=n;
int i=0;
int et=0;
int sum=0;
float twt=0;
float ttat=0;
while(cnt!=0)
{
if((p[i].bt)>t)
{
et=t;
p[i].bt-=t;
}
else
{
et=p[i].bt;
p[i].bt=0;
}
p[i].st=sum;
if((p[i].flag)==0)
{
p[i].wt=p[i].st-p[i].at;
p[i].flag++;
}
else
p[i].wt=p[i].wt+(p[i].st-p[i].ft);
sum=sum+et;
p[i].ft=sum;
save_et[turn]=et;
save_id[turn++]=p[i].id;
if((p[i].bt)==0)
{
cnt--;
}
do
{
i=(i+1)%n;
}while((p[i].bt)==0 && cnt!=0);
}
for(i=0;i<n;i++)
{
p[i].tat=p[i].wt+p[i].tbt;
twt+=p[i].wt;
ttat+=p[i].tat;
}
awt=twt/n;
atat=ttat/n;
}
void print()
{
int i,sum=0;
for(i=0;i<=btsum;i++)
printf("---");
printf("\n");
printf("| ");
for(i=0;i<turn;i++)
printf(" %p*d |",-(save_et[i]-1),save_id[i]);
printf(" ");
printf("\n");
for(i=0;i<=btsum;i++)
printf("---");
printf("\n");
printf(" ");
for(i=0;i<turn;i++)
{
printf("%p*d ",-(save_et[i]),sum);
sum+=save_et[i];
}
printf("%d\n",sum);
printf("\nPid\tWT\t TT");
for(i=0;i<n;i++)
{
printf("\n%d\t %d \t%d\n",p[i].id,p[i].wt,p[i].tat);
}
printf("AWT=%f\t\t ATT=%.2f\n",awt,atat);
btsum=0;
}
OUTPUT:
[2cse@localhost ~]$ ./a.out
Enter no of processes:3
Enter arriving time ,burst time of process p1 : 0 5
Enter arriving time ,burst time of process p2 : 2 6
Enter arriving time ,burst time of process p3 : 3 8
Enter time quantum : 2
-----------------------------------------------------------------
| 1 | 2 | 3 | 1 | 2 | 3 | 1 | 2 | 3 | 3 |
-----------------------------------------------------------------
0 2 4 6 8 10 12 13 15 17 19
Pid WT TT
1 8 13
2 7 13
3 8 16
AWT=7.666667 ATT=14.0
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
OUTPUT:
Enter no.of files: 2
Enter no. of blocks occupied by file1 4
Enter the starting block of file1 2
Enter no. of blocks occupied by file2 10
Enter the starting block of file2 5
Filename Start block length
1 2 4
2 5 10
Enter file name: rajesh
File name is:12803 length is:0 blocks occupied
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{ printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
OUTPUT:
Enter no. of files:2
Enter starting block and size of file1: 2 5
Enter blocks occupied by file1:10
enter blocks of file1:3
2 5 4 6 7 2 6 4 7
Enter starting block and size of file2: 3 4
Enter blocks occupied by file2:5
enter blocks of file2: 2 3 4 5 6
File index length
1 2 10
2 3 5
Enter file name: venkat
file name is:12803
Index is:0 Block occupied are:
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
OUTPUT:
Enter no. of files:2
Enter file name:venkat
Enter starting block:20
Enter no.of blocks:6
Enter block numbers: 4
12
15
45
32
25
Enter file name:rajesh
Enter starting block:12
Enter no.of blocks:5
Enter block numbers:6
5
4
3
2
File start size block
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2
PROGRAM:
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#define NBUFF 10
int nitems;
struct {
int buff[NBUFF];
sem_t mutex, nempty, nstored;
} shared;
void *produce (void *);
void *consume (void *);
int main(int argc, char **argv)
{
pthread_t tid_produce, tid_consume;
if(argc !=2)
{
printf("Useage: filename <nitems> ");
return 0;
}
printf ("\n\n Producer - Consumer Problem using Semaphore\n");
printf (" -------------------------------------------\n\n");
nitems=atoi(argv[1]);
sem_init(&shared.mutex,0,1);
sem_init(&shared.nempty,0,NBUFF);
sem_init(&shared.nstored,0,0);
pthread_setconcurrency(2);
pthread_create(&tid_produce,NULL,produce,NULL);
pthread_create(&tid_consume,NULL,consume,NULL);
pthread_join(tid_produce,NULL);
pthread_join(tid_consume,NULL);
sem_destroy(&shared.mutex);
sem_destroy(&shared.nempty);
sem_destroy(&shared.nstored);
}
void *produce (void *arg)
{
int i;
for(i=0;i<nitems;i++)
{
sem_wait(&shared.nempty);
sem_wait(&shared.mutex);
shared.buff[i % NBUFF]=i;
printf ("\tProducer.....");
printf ("buff[%d] = %d\n\n",i,shared.buff[i % NBUFF]);
sem_post(&shared.mutex);
sem_post(&shared.nstored);
sleep(3);
}
return NULL;
}
void *consume (void *arg)
{
int i;
for(i=0;i<nitems;i++)
{
sem_wait(&shared.nstored);
sem_wait(&shared.mutex);
printf("\tConsumer.....");
printf("buff[%d] = %d\n\n\n",i,shared.buff[i%NBUFF]);
sem_post(&shared.mutex);
sem_post(&shared.nempty);
sleep(3);
}
return NULL;
}

OUTPUT:
[cse2@localhost ~]$ cc -pthread prod_cons.c
[cse2@localhost ~]$ ./a.out 4
Producer - Consumer Problem using Semaphore
-------------------------------------------
Producer.....buff[0] = 0
Consumer.....buff[0] = 0

Producer.....buff[1] = 1
Consumer.....buff[1] = 1

Producer.....buff[2] = 2
Consumer.....buff[2] = 2

Producer.....buff[3] = 3
Consumer.....buff[3] = 3
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.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\\bgi");
cleardevice( ) ; setbkcolor(BLACK) ;

puts( "Enter no of files u have?");


scanf("%d",&count);
for(i=0;i<count;i++)
{
cleardevice( );
setbkcolor(BLACK);
printf(" Enter file%dname",i+ 1);
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 Direct ory");


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:
Program:
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element*link[5];
};
typedef struct tree_element node;
void main( )
{
int gd= DETECT,gm;
node*root;
root=NULL;
clrscr( );
create(&root,0,"null",0,639,320);
clrscr( ) ;
initgraph(&gd,&gm,"c:\\tc\\bgi");display(root);
getch( );
closegraph( );
}
create(node**root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(* root)=(node*)malloc(sizeof(node));
printf("enter name of dir/ file(under%s):",dname);fflush(stdin);
gets((*root)->name);if(lev==0||lev==1)(*root)->ftype=1;else(*root)->ftype=2;(* root)-
>level=lev;(* root) ->y=50+lev*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(lev==0||lev==1)
{
if((*root)->level==0)
printf(" How many users");
else
printf(" how many files");
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]),lev+1,(* root )->name,lx+gap*i,lx+gap*i+gap,lx+gap *i+gap/2);
}
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
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}

OUTPUT:
PROGRAM:
#include<stdio.h>
#include<graphics.h>
Struct tree_element
{
char name[20];
int
x,y,ftype,lx,rx,nc,level;
struct
tree_element*link[5]; };
typedef struct tree_element
node; void main( )
{
int
gd=DETECT,gm;node*ro
ot; root=NULL;
clrscr( );
create(&root,0,"root",0,639,320)
; clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI"
);
display(root);getch();closegraph(
);
}
create(node**root,int lev, char * dname, int lx , int rx, int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("Enter name of dir/file(under%s):",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir / 2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;(*root)->lx=lx;(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
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);
elsefillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}

OUTPUT:
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
typedef struct
{
char from[20];
char to[20];
}link;
link L[10]; int nofl;
node * root;
void main()
{
int gd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("how many links");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");
fflush(stdin);
gets(L[i].to);
}
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;
}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname); fflush(stdin);
gets((*root)->name);
printf("enter 1 for dir/ 2 for
file:");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("no of sub directories /files (for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create( & ( (*root)->link[i] ) , lev+1 ,
(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
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
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root-
>name); for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}}}
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int r[1][10],av[1][10];
intall[10][10],max[10][10],ne[10][10],w[10],safe[10];
int i=0,j=0,k=0,l=0,np=0,nr=0,count=0,cnt=0;
clrscr();
printf("enter the number of processes in a
system"); scanf("%d",&np);
printf("enter the number of resources in a system");
scanf("%d",&nr);
for(i=1;i<=nr;i++)
{
printf("\n enter the number of instances of resource R%d " ,i);
scanf("%d",&r[0][i]);
av[0][i]=r[0][i];
}
for(i=1;i<=np;i++)
for(j=1;j<=nr;j++)
all[i][j]=ne[i][j]=max[i][j]=w[i]=0;
printf(" \nEnter the allocation matrix");
for(i=1;i<=np;i++)
{
printf("\n"
);
for(j=1;j<
=nr;j++)
{
scanf("%d",&all[
i][j]);
av[0][j]=av[0][j]-
all[i][j];
}
}
printf("\nEnter the maximum
matrix"); for(i=1;i<=np;i++)
{ printf("\n");
for(j=1;j<=nr;j++)
{
scanf("%d",&max[i][j]);
}
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nr;j++)
{
ne[i][j]=max[i][j]-all[i][j];
}
}
for(i=1;i<=np;i++)
{
printf("pocess P%d",i);
for(j=1;j<=nr;j++)
{
printf("\n allocated %d\t",all[i][j]);
printf("maximum %d\t",max[i][j]);
printf("need %d\t",ne[i][j]);
}
printf("\n_________________________\n");
}
printf("\nAvailability");
for(i=1;i<=nr;i++)
printf("R%d %d\t",i,av[0][i]);

printf("\n____________");
printf("\n safe sequence");

for(count=1;count<=np;count++)
{

for(i=1;i<=np;i++)
{
cnt=0;
for(j=1;j<=nr;j++)
{
if(ne[i][j]<=av[0][j] && w[i]==0)
cnt++;
}
if(cnt==nr)
{
k++;
safe[k]=i;
for(l=1;l<=nr;l++)
av[0][l]=av[0][l]+all[i][l];
printf("\n P%d ",safe[k]);
printf("\t Availability");
for(l=1;l<=nr;l++)
printf("R%d %d\t",l,av[0][l]);
w[i]=1;
}
}
}
getch();
}
OUTPUT:
PROGRAM:
#include <stdio.h>
main()
{
int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
printf("Enter total no of processes");
scanf("%d",&tp);
printf("Enter total no of resources");
scanf("%d",&tr);
printf("Enter claim (Max. Need) matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
}
OUTPUT:

Enter total no. of processes : 4


Enter total no. of resources : 5
Enter claim (Max. Need) matrix :
01001
00101
00001
10101
Enter allocation matrix :
10110
11000
00010
00000
Enter resource vector (Total resources) :
21121
Enter availability vector (available resources) :
00001
deadlock causing processes are : 2 3
PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames....");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults...%d",pf);
getch();
}

OUTPUT:
FIFO PAGE REPLACEMENT ALGORITHM
Enter no.of frames....4
Enter number of reference string..
6
Enter the reference string..
564123
The given reference string:
...................................... 5 6 4 1 2 3
Reference np5-> 5 -1 -1 -1
Reference np6-> 5 6 -1 -1
Reference np4-> 5 6 4 -1
Reference np1-> 5 6 4 1
Reference np2-> 2 6 4 1
Reference np3-> 2 3 4 1

No.of pages faults...6


PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();

void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);

printf(" Enter no.of reference string..");


scanf("%d",&nor);

printf("\n Enter reference string..");


for(i=0;i<nor;i++)
scanf("%d",&ref[i]);

printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");


printf("\n\t The given reference string:");
printf("\n………………………………..");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}

if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}

OUTPUT:
LRU PAGE REPLACEMENT ALGORITHM
Enter no.of Frames....3
Enter no.of reference string..6
Enter reference string..6 5 4 2 3 1
LRU PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1

Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6
PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n.................................");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n................................");
printf("\nThe given string");
printf("\n....................\n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int i,j,temp,notfound;
for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
}

OUTPUT:
OPTIMAL PAGE REPLACEMENT ALGORITHM
Enter no.of Frames....3
Enter no.of reference string..6
Enter reference string..6 5 4 2 3 1
OPTIMAL PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child,shmid,i;
char * shmptr;
child=fork();
if(!child)
{
shmid=shmget(2041,32,IPC_CREAT|0666);
shmptr=shmat(shmid,0,0);
printf(“\n Parent writing\n”);
for(i=0;i<10;i++)
{
shmptr[i]=’a’+i;
putchar(shmptr[i]);
}
printf(“\n\n %s”, shmptr);
wait(NULL);
}
else
{
shmid=shmget(2041,32,0666);
shmptr=shmat(shmid,0,0);
printf(“\n Child is reading\n”);
for(i=0;i<10;i++)
putchar(shmptr[i]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
return 0;
}

OUTPUT:
[cse2@localhost ~]$ cc share.c
[cse2@localhost ~]$ ./a.out
Parent writing
abcdefghij
Child is reading
abcdefghij
PROGRAM:
#include<unistd.h>
main()
{
int fd[3],pid;
char *msg=("OS LAB\n");
char buff[25];
pipe(fd);
pid=fork();
if(pid==0)
{
puts("child process\n");
close(fd[0]);
write(fd[1],msg,30);
}
else
{
sleep(1);
puts("parent process\n");
close(fd[1]);
read(fd[0],buff,30);
printf("%s",buff);
}
}

OUTPUT:

[cse2@localhost ~]$ cc pipe.c


[cse2@localhost ~]$ ./a.out
child process

parent process

OS LAB
[cse2@localhost ~]$
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 25
void printlayout(int[],int );
int firstfit(int[],int, int);
int bestfit(int[],int, int);
void main()
{
int i,a[25],n,req,choice,pos,ch;
clrscr();
printf("How MAny Segments");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Segent Size");
scanf("%d",&a[i]);
}
loop:
printf("How Much Is Your Memory Requirment");
scanf("%d",&req);
printf("\n1.Bestfit\n2.Firstfit\n3.Exit\n");
printf("Enter Your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
pos=bestfit(a,n,req);
break;
case 2:
pos=firstfit(a,n,req);
break;
}
printf("\tBestfit and Firstfit Algorithm\n");
printf("\t______________________________\n\n");
printlayout(a,n);
printf("Your Memory Requirment is :%d\n\n",req);
printf("Alloted Memory Region is:%d\n\n",a[pos]);
a[pos]=0;
printf("Do You Want To Continue 1/0");
scanf("%d",&ch);
if(ch==1)
goto loop;
getch();
}
void printlayout(int a[],int n)
{
int i,j;
printf("\t\tMemory Free List");
printf("\n\t\t_______________\n\n");
printf("\t\t|~~~|\n");
for(i=0;i<n;i++)
{
if(a[i]!=0)
{
for(j=1;j<=(a[i]/100);j++)
printf("\t\t| |\n");
printf("\t\t| %d|\n",a[i]);
printf("\t\t|---|\n");
}
}
printf("\n\n");
}
int firstfit(int a[],int n,int r)
{
int i;
for(i=0;i<n;i++)
if(a[i]>=r)
break;
return i;
}
int bestfit(int a[],int n,int r)
{
int b[25],i,j,temp,val;
for(i=0;i<n;i++)
b[i]=a[i];
for(i=0;i<n-1;i++)
for(j=i;j<n-1;j++)
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
for(i=0;i<n;i++)
if(b[i]>=r)
break;
val=b[i];
for(i=0;i<n;i++)
if(a[i]==val)
break;
return i;
}
OUTPUT:
[cse2@localhost ~]$ cc memsch1.c
[cse2@localhost ~]$ ./a.out
How Many Segments4

Segent Size500
Segent Size200
Segent Size300
Segent Size400

How Much Is Your Memory Requirment200


1.Bestfit
2.Firstfit
3.Exit

Enter Your Choice 1


Bestfit and Firstfit Algorithm
___________________________________
Memory Free List
___________________________________

|~~~|
| |
| |
|500|
| --- |
| |
|200|
| --- |
| |
| |
|300|
| --- |
| |
| |
|400|
| --- |

Your Memory Requirment is :200


Alloted Memory Region is:200
Do You Want To Continue 1/0
1
How Much Is Your Memory Requirment250

1.Bestfit
2.Firstfit
3.Exit
Enter Your Choice
1
Bestfit and Firstfit Algorithm
___________________________________
Memory Free List
___________________________________
|~~~|
| |
| |
| |
|500|
| --- |
| |
|300|
| --- |
| |
| |
|400|
| --- |

Your Memory Requirment is :250


Alloted Memory Region is:300
Do You Want To Continue 1/0
0
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
void *func(int n);
pthread_t philosopher[5];
pthread_mutex_t chopstick[5];
int main()
{
int i,k;
void *msg;
for(i=1;i<=5;i++)
{
k=pthread_mutex_init(&chopstick[i],NULL);
if(k==-1)
{
printf(“\n Mutex initialization failed”);
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_create(&philosopher[i],NULL,(void *)func,(int *)i);
if(k!=0)
{
printf(“\n Thread creation error \n”);
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_join(philosopher[i],&msg);
if(k!=0)
{
printf(“\n Thread join failed \n”);
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_mutex_destroy(&chopstick[i]);

if(k!=0)
{
printf(“\n Mutex Destroyed \n”);
exit(1);
}
}
return 0;
}
void *func(int n)
{
printf(“\nPhilosopher %d is thinking “,n);
pthread_mutex_lock(&chopstick[n]);
pthread_mutex_lock(&chopstick[(n+1)%5]);
printf(“\nPhilosopher %d is eating “,n);
sleep(3);
pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n+1)%5]);
printf(“\nPhilosopher %d Finished eating “,n);
}

OUTPUT:
[root@localhost ~]$ gcc -o c dining.c -pthread
[root@localhost ~]$ ./c
Philosopher 1 is thinking
Philosopher 1 is eating
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 3 is eating
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 1 Finished eating
Philosopher 3 Finished eating
Philosopher 4 is eating
Philosopher 5 is eating
Philosopher 2 is eating
Philosopher 4 Finished eating
Philosopher 5 Finished eating
Philosopher 2 Finished eating
PROGRAM:
#include <stdio.h>
#include <conio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmsize,lmsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: "); scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter the partition size: ");
scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages",page);
}
void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
getch();
printf("\n\nPAGE TABLE\n\n"); printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n"); printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
getch();
clrscr();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: "); scanf("%d",&baddr);
printf("\nEnter theLogical Address: "); scanf("%d",&laddr);
paddr = laddr / psize; disp = laddr % psize; if(ptable[paddr].pbit==1)
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
clrscr();
info();
assign();
cphyaddr();
getch();
}
OUTPUT:

MEMORY MANAGEMENT USING PAGING


Enter the Size of Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 no.of frames
The Logical memory is divided into 4 no.of pages
Enter the Frame number where page 0 must be placed: 5
Enter the Frame number where page 1 must be placed: 6
Enter the Frame number where page 2 must be placed: 7
Enter the Frame number where page 3 must be placed: 2
PAGE TABLE
PageAddress FrameNo. PresenceBit
0 5 1
1 6 1
2 7 1
3 2 1
FRAME TABLE
FrameAddress PageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
The Physical Address where the instruction present: 1013

You might also like