OS Record Final Print
OS Record Final Print
OS Record Final Print
AIM:
To implement the Basic unix commands.
COMMANDS:
1
5. THE UNIX CALENDER:
Cal:
[exam@fosslab ~]$ cal 2 2015
February 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
7. THE id COMMAND:
[exam@fosslab ~]$ id
uid=662(exam) gid=662(exam) groups=662(exam)
1.CREATE A DIRECTORY:
[exam@fosslab ~]$ mkdir student
[exam@fosslab ~]$ cd student
[exam@fosslab student]$
2
3.REMOVING A DIRECTORY:
[exam@fosslab student]$ rmdir student
[exam@fosslab ~]$
6. THE PATH
3
7.CHANGE THE PASSWORD:
[exam@fosslab ~]$ passwd
(current) UNIX PASSWORD: ********
New Password: ********
Re-enter Password: ********
$
3. REMOVING A FILE:
Rm:
[exam@fosslab student ~]$ rm test1
[exam@fosslab student ~]$ cat test1
Cat :test1: No such file or directory
4.MOVING A FILE:
[exam@fosslab student~]$ cat >test1
the basic unix commands are cat,pwd,clear,cp,mv,rm,mv,test..^C
mv:
[exam@fosslab student ~]$ mv test test1
[exam@fosslab student ~]$ cat test 1
the basic unix commands are cat, pwd, mkdir, rmdir, cd, path,clear,cp,rm,
mv,ls,wc.
4
program
public_html
share
stud25
student
test
test1
[exam@fosslab ~]$
5
student
test
test1
6
5. THE more COMMAND:
[exam@fosslab ~]$ more test1
a.out
data
mylist
program
public_html
stud25
student
test
test1
6. PASTING FILES:
[exam@fosslab ~]$ paste std
Aswini
Bharathi
Charu
Deepa
7. COMPARING FILES:
cmp
[exam@fosslab ~]$ cmp test1 std
test1 std differ: byte 1, line 1
7
11. SENDING MAIL TO USERS:
mail:
[exam@fosslab ~]$ mail user2
Subject: about operating systems
THE OPERATING SYSTEMS BOOk is a "practice of some materials to gain knowledge"
EOT
RESULT:
Thus the basic UNIX commands were executed successfully.
8
EX NO: 1B
SYSTEM CALLS OF UNIX OPERATING SYSTEM
DATE:
I. CLOSE()
AIM:
To write the program to implement the system calls close().
ALGORITHM:
Step 1: Start
Step 2: In the main function pass the arguments.
Step 3: Create structure as stat buff and the variables as integer.
Step 4: Use the for loop initialization.
Step 5: Stop.
9
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
int main()
if(fd1<0)
perror("c1");
exit(1);
if(close(fd1)<0)
perror("c1");
exit(1);
10
OUTPUT:
RESULT:
Thus the program was executed and verified successfully.
11
II. GETPID()
AIM:
ALGORITHIM:
Step 1: Start
Step 2: Get the process id integer value by using the system call getpid()
Step 3: It returns the process id of the calling process.
Step 4: After getting the pid value it prints the process id number an exists.
Step 5: Then compile the program either with the gcc or cc command.
Step 6: Run the program.
Step 7: Stop
12
PROGRAM:
Example.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
printf("PID of example.c=%d\n",getpid());
execv("./hello",args);
printf("BACK TO EXAMPLE.C");
Hello.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
printf("PID of hello.c=%d\n",getpid());
return 0;
13
OUTPUT:
RESULT:
Thus the program for getpid() system call has been executed and verified
successfully.
14
III. FORK()
AIM:
To write the program to create a Child Process using system call fork().
ALGORITHM:
15
PROGRAM:
#include <stdio.h>
#include<unistd.h>
int main()
int id;
printf("hello world!\n");
id=fork();
if (id>0)
else if(id==0)
else
return 0;
16
OUTPUT:
RESULT:
Thus the program for fork() system call has been executed and verified successfully.
17
IV. OPEN()
AIM:
To write the program to implement the system call open( ).
ALGORITHM:
18
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
int main()
int fd=open("foo.txt",O_RDONLY|O_CREAT);
printf("fd=%d\n",fd);
if(fd ==-1)
printf("Eror no.=%d\n",errno);
printf("Program");
return 0;
OUTPUT:
RESULT:
Thus the program for open() system call has been executed and verified successfully.
19
V. READ()
AIM:
To write the program to implement the system call read( ).
ALGORITHM:
20
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
fd = open("foo.txt", O_RDONLY);
if(fd < 0)
perror("r1");
exit(1);
sz = read(fd, c, 10);
printf("called read(% d, c, 10). returned that" " % d bytes were read.\n" , fd, sz);
c[sz] = '\0';
21
OUTPUT:
RESULT:
Thus the program for read() system call has been executed and verified successfully.
22
VI. WRITE ()
AIM:
To write the program to implement the system call write( ).
ALGORITHM:
23
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
main()
int sz;
if(fd < 0)
perror("r1");
exit(1);
close(fd);
24
OUTPUT:
RESULT:
Thus the program for write() system call has been executed and verified successfully.
25
EX NO: 2
SIMULATION OF UNIX COMMANDS (GREP, CP, LS)
DATE:
AIM:
To write a c program to simulate grep unix command.
ALGORITHM:
Step 3: Read the file name from the user and open the file in the read only mode.
Step 5: Read a line of string from the file and search the pattern in that line.
26
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<string.h>
#include <fcntl.h>
}
}
}
27
test.txt
he is using ubuntu...
OUTPUT:
RESULT:
Thus the program for simulating grep unix command has been executed and verified
successfully.
28
B. SIMULATION OF CP UNIX COMMAND
AIM:
To write a c program to simulate cp unix command.
ALGORITHM:
29
PROGRAM:
#include<stdio.h>
void get_matrix(int m[20][20],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&m[i][j]);
}
}
void print_matrix(int m[20][20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n\t");
for(j=0;j<n;j++)
{
printf("%d\t",m[i][j]);
}
}
printf("\n");
}
int main()
{
int m[20][20],n,i,trace=0;
printf("\n enter the no.of.R&c for the square matrix (n*n):");
scanf("%d",&n);
printf("\n enter the elements for matrix (%d*%d)\n",n,n);
get_matrix(m,n);
printf("\n matrix read:");
print_matrix(m,n);
for(i=0;i<n;i++)
{
trace = trace +m[i][i];
}
printf("\n sum of diagonal elements(trace) of the matrix :%d\n",trace);
return 0;
}
30
OUTPUT:
RESULT:
Thus the program for simulating cp unix command has been executed and verified
successfully.
31
C. SIMULATION OF LS UNIX COMMAND
AIM:
To write a c program to simulate ls unix command.
ALGORITHM:
Step 4: Open the directory using opendir() system call and report error if the directory is not
available.
Step 6: Display the directory entry ie., name of the file or sub directory.
Step 7: Repeat the step 6 and 7 until all the entries were read.
32
PROGRAM:
#include<stdio.h>
#include<dirent.h>
int main(void)
{
struct dirent *de;
DIR *dr = opendir(".");
if(dr==NULL)
{
printf("could not open current directory");
return 0;
}
while ((de=readdir(dr))!=NULL)
printf("%s\t",de->d_name);
closedir(dr);
return 0;
}
OUTPUT:
RESULT:
Thus the program for simulating ls unix command has been executed and verified
successfully.
33
EX NO: 3
SHELL PROGRAMMING
DATE:
SHELL SCRIPT:
echo "enter radius"
read r
val=`expr 3 \* $r \* $r`
echo "$val"
OUTPUT:
RESULT:
Thus the shell program to find the area of circle is written and executed successfully
34
EX NO: 3B
SHELL PROGRAMMING – BIGGEST OF TWO NUMBERS
DATE:
AIM:
To write a shell program to find the biggest of two numbers
SHELL SCRIPT:
echo "enter two numbers"
read a b
if [ $a -gt $b ]
then
echo "a is big"
else
echo "b is big"
fi
OUTPUT:
RESULT:
Thus the shell program to find the biggest of two numbers is written and executed
successfully
35
EX NO: 3C
SHELL PROGRAMMING – BIGGEST OF THREE NUMBERS
DATE:
AIM:
To write a shell program to find the biggest of three numbers
SHELL SCRIPT:
echo "enter three numbers"
read a b c
if [ $a -gt $b ]&&a [ $b -gt $c ]
then
echo "a is big"
elif [ $b -gt $c ]
then
echo "b is big"
else
echo "c is big"
fi
OUTPUT:
RESULT:
Thus the shell program to find the biggest of three numbers is written and executed
successfully.
36
EX NO: 3D
SHELL PROGRAMMING – NATURAL NUMBERS UPTO ‘N’
DATE:
AIM:
To write a shell program to find n natural numbers
SHELL SCRIPT:
echo "upper limit"
read n
$i=0
while [ $i -lt $n ]
do
echo $i
i=`expr $i+1`
done
OUTPUT:
RESULT:
Thus shell program to find n natural numbers is written and executed successful
37
EX NO: 3E
SHELL PROGRAMMING – NATURAL NUMBERS USING FOR
LOOP
DATE:
AIM:
To write a shell program to find n natural numbers using for loop.
SHELL SCRIPT:
for i in 1 2 3 4 5
do
echo $i
done
OUTPUT:
RESULT:
Thus the shell program to find n natural numbers using for loop is written and
executed successfully
38
EX NO: 3F
SHELL PROGRAMMING – ARITHMETIC OPERATIONS
DATE:
AIM:
To write a shell program to perform arithmetic operations on two numbers.
SHELL SCRIPT:
echo "enter two numbers"
read a b
echo "1.Add 2.Sub 3.Mul 4.Div 5.Exit"
read op
case $op in
1) c=`expr $a + $b` ;;
2) c= `expr $a - $b` ;;
3) c= `expr $a \ * $b` ;;
4) c= `expr $a / $b` ;;
5) exit
esac
echo $c
OUTPUT:
RESULT:
Thus the shell program to perform arithmetic operations on two numbers is written
and executed successfully.
39
EX NO: 3G
SHELL PROGRAMMING – GROSS SALARY OF AN
EMPLOYEE
DATE:
AIM:
To write a shell program to calculate gross salary of an employee
SHELL SCRIPT:
echo "Basic Salary"
read basic
da=`expr $basic \* 10 / 100`
hra=`expr $basic \* 20 / 100`
gross=`expr $basic + $da + $hra`
echo "$gross"
OUTPUT:
RESULT:
Thus the shell program to calculate gross salary of an employee is written and
executed successfully.
40
EX NO: 3H
SHELL PROGRAMMING – FACTORIAL OF A NUMBER
DATE:
AIM:
To write a shell program to find factorial of a number.
SHELL SCRIPT:
echo "enter number"
read n
i=1
f=1
while [ $i -lt $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done
echo "Factorial is...$f"
OUTPUT:
RESULT:
Thus the shell program to find factorial of a number is written and executed
successfully.
41
EX NO: 4
CPU SCHEDULING ALGORITHMS
DATE:
ALGORITHM:
42
PROGRAM:
#include<stdio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
printf("enter the of processer:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter burst time");
scanf("\n %d",&t[i]);
}
printf("\n FIRST COME FRIST SERVE SCHEDULING");
printf("\n processid\twaittingtime\tturnaroundtime\n");
printf("1\t\t0\t\t%d\n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n%d\t\t%d\t\t%d",i+1,wt,tat);
printf("\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n average waiting time%4.2f",awt);
printf("\n average turnaround time %4.2f",atat);
}
43
OUTPUT:
RESULT:
Thus the FCFS process scheduling program was executed and verified successfully.
44
EX NO: 4B
SHORTEST JOB FIRST – SCHEDULING ALGORITHM
DATE:
AIM:
To write a program to implement cpu scheduling algorithm for shortest job first
scheduling.
ALGORITHM:
45
PROGRAM:
#include<stdio.h>
void main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
printf("enter the number of processer");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter burst time %d",i);
scanf("\n%d",&t[i]);
}
for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
{
for(k=0;k<i+1;k++)
{
if(t[i]>t[k])
{
int temp;
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}}
printf("\n\n SHOREST JOB SSHEDULING\n\n");
printf("\nprocerrid\tburst tim\twaitingtime\tturnaround time\n\n");
wt[0]=0;
for(i=0;i<n;i++)
{
sum=0;
for(k=0;k<i;k++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
46
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d\t\t%5d\t\t%5d\t\t%5d\n",p[i],t[i],wt[i],tt[i]);
}
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n AVERAGE WAITING TIME %4.2f",awt);
printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);
}
}
47
OUTPUT:
RESULT:
48
EX NO: 4C
PRIORITY – SCHEDULING ALGORITHM
DATE:
AIM:
To write a ‘C’ program to perform priority scheduling.
ALGORITHM:
49
PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt,atat;
printf("\n-----------PRIORITY SCHEDULING-----------\n");
printf("Enter the number of process: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pid[i]=i;
printf("Enter the Burst time of Pid %d: ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d: ",i);
scanf("%d",&pr[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
t=pr[i];
pr[i]=pr[j];
pr[j]=t;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=pid[i];
pid[i]=pid[j];
pid[j]=t;
}
}
tat[0]=bt[0];
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
}
printf("\n ------------------------------------ \n");
printf("Pid \t Priority \t Burst time \t WaitingTime \tTurnAroundTime \n");
printf("\n ------------------------------------ \n");
for(i=0;i<n;i++)
{
50
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d",pid[i],pr[i],bt[i],bt[i],wt[i],ta
t[i]);
}
for(i=0;i<n;i++)
{
ttat=ttat+tat[i];
twt=twt+wt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n\n Avg.Waiting Time: %f\n Avg.Turn Around Time: %f\n",awt,atat);
}
OUTPUT:
RESULT:
Thus the priority scheduling program was executed and verified successfully
51
EX NO: 4D
ROUND ROBIN – SCHEDULING ALGORITHM
DATE:
AIM:
ALGORITHM:
52
PROGRAM:
#include<stdio.h>
void main()
{
int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
printf("\n ROUND ROBIN SCHEDULING \n");
printf("Enter the number of processors \n");
scanf("%d",&n);
n1=n;
printf("\n Enter the Timeslice \n");
scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("\n Enter the process ID %d",i);
scanf("%d",&pid[i]);
printf(" Enter the Burst Time for the process");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]=0;
n--;
flag[i]=0;
}
}
twt=0;
ttat=0;
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}}
awt=(float)twt/n1;
atat=(float)ttat/n1;
53
printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");
printf("\n Process \t Process ID \t BurstTime \t Waiting Time \t Turnaround Time \n");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n",i,pid[i],bt[i],wt[i],tat[i]);
}
printf("\n The average Waiting Time=%4.2f",awt);
printf("\n The average Turn around Time=%4.2f",atat);
}
OUTPUT:
RESULT:
Thus the Round Robin scheduling program was executed and verified
successfully.
54
EX NO: 5
SEMAPHORES
DATE:
AIM:
ALGORITHM:
55
PROGRAM
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n 1.producer\n2.consumer\n3.exit\n");
while(1) {
printf(" \nenter ur choice");
scanf("%d",&n);
switch(n)
{
case 1:if((mutex==1)&&(empty!=0))
producer();
else
printf("buffer is full\n");
break;
case 2:if((mutex==1)&&(full!=0))
consumer();
else
printf("buffer is empty");
break;
case 3:exit(0);
break;
}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
56
x++;
printf("\n producer produces the items %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}
OUTPUT:
RESULT:
Thus the program for Producer & consumer Problem using Semaphore was executed
successfully
57
EX NO: 6A
SHARED MEMORY
DATE:
AIM
To demonstrate communication between process using shared memory.
ALGORITHM:
Server
Client
58
PROGRAM:
//SERVER//
/*Sharedmemoryserver-shms.c*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/un.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#defineshmsize27
main()
{
charc;
intshmid;
key_tkey= 2013;
char*shm,*s;
if((shmid=shmget(key,shmsize,IPC_CREAT|0666))<0)
{
perror("shmget");
exit(1);
}
printf("Sharedmemoryid:%d\n",shmid);
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
memset(shm,0,shmsize);
s=shm;
printf("Writing(a-z)ontosharedmemory\n");
for(c='a';c<='z';c++)
*s++=c;
*s='\0';
while(*shm!='*');
printf("Clientfinishedreading\n");
if(shmdt(shm)!=0)
fprintf(stderr,"Couldnotclosememorysegment.\n");
shmctl(shmid,IPC_RMID,0);
}
//CLIENT//
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
59
#include<sys/ipc.h>
#include<sys/shm.h>
#defineshmsize27
main()
{
int shmid;
key_tkey=2013;
char*shm,*s;
if((shmid=shmget(key,shmsize,0666))<0)
{
printf("Servernotstarted\n");
exit(1);
}
else
printf("Accessingsharedmemoryid:%d\n",shmid);
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
printf("Sharedmemorycontents:\n");
for(s=shm;*s!='\0';s++)
putchar(*s);
putchar('\n');
*shm='*';
}
60
OUTPUT:
//SERVER
//CLIENT
RESULT:
Thuscontentswrittenontosharedmemorybytheserverprocessisreadbytheclient
process.
61
EX NO: 6B
IPC – CHAT MESSAGING
DATE:
AIM
To exchange message between server and client using message queue.
ALGORITHM
Server
1. Declare a structure mesgq with type and text fields.
2. Initialize key to 2013 (some random value).
3. Create a message queue using msgget with key & IPC_CREAT as parameter.
a. If message queue cannot be created then stop.
4. Initialize the message type member of mesgq to 1.
5. Do the following until user types Ctrl+D
a. Get message from the user and store it in text member.
b. Delete the new line character in text member.
c. Place message on the queue using msgsend for the client to read.
d. Retrieve the response message from the client using msgrcv function
e. Display the text contents.
6. Remove message queue from the system using msgctl with IPC_RMID as
parameter.
7. Stop
Client
1. Declare a structure mesgq with type and text fields.
2. Initialize key to 2013(same value as in server).
3. Open the message queue using msgget with key as parameter.
a. If message queue cannot be opened then stop.
4. Do while the message queue exists
a. Retrieve the response message from the server using msgrcv function
b. Display the text contents.
c. Get message from the user and store it in text member.
d. Delete the new line character in text member.
e. Place message on the queue using msgsend for the server to read.
5. Print "Server Disconnected".
6. Stop.
62
PROGRAM
Server
/*Serverchatprocess-srvmsg.c*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
structmesgq
{
longtype;
chartext[200];
}mq;
main()
{
int msqid,len;
key_tkey=2013;
if((msqid=msgget(key,0644|IPC_CREAT))==-1)
{
perror("msgget");
exit(1);
}
while(fgets(mq.text,sizeof(mq.text),stdin)!=NULL)
{
len=strlen(mq.text);
if(mq.text[len-1]=='\n') mq.text[len-1]='\0';
msgsnd(msqid,&mq,len+1,0);
msgrcv(msqid,&mq,sizeof(mq.text),0,0);
printf("FromClient:\"%s\"\n",mq.text);
}
msgctl(msqid,IPC_RMID,NULL);
}
Client
/*Clientchatprocess-climsg.c*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
63
structmesgq
{
longtype;
chartext[200];
}mq;
main()
{
int msqid,len;
key_tkey=2013;
if((msqid=msgget(key,0644))==-1)
{
printf("Servernotactive\n");
exit(1);
}
printf("Clientready:\n");
while(msgrcv(msqid,&mq,sizeof(mq.text),0,0)!=-1)
{
printf("FromServer:\"%s\"\n",mq.text);
fgets(mq.text,sizeof(mq.text),stdin);
len=strlen(mq.text);
if(mq.text[len-1]=='\n') mq.text[len-1]='\0';
msgsnd(msqid,&mq,len+1,0);
}
printf("ServerDisconnected\n");
}
64
OUTPUT:
//CHAT SERVER
//CHAT CLIENT
RESULT
Thus chat session between client and server was done using message queue.
65
EX NO: 7A
DEADLOCK AVOIDANCE – BANKER’S ALGORITHM
DATE:
AIM:
To implement deadlock avoidance by using Banker’s Algorithm.
Banker’s Algorithm:
When a new process enters a system, it must declare the maximum number of
instances of each resource type it needed. This number may exceed the total number of
resources in the system. When the user request a set of resources, the system must determine
whether the allocation of each resources will leave the system in safe state. If it will the
resources are allocation; otherwise the process must wait until some other process release the
resources.
Data structures
n-Number of process, m-number of resource types.
Available: Available[j]=k, k – instance of resource type Rj is available.
Max: If max[i, j]=k, Pi may request at most k instances resource Rj.
Allocation: If Allocation [i, j]=k, Pi allocated to k instances of resource Rj
Need: If Need[I, j]=k, Pi may need k more instances of resource type Rj,
Need[I, j]=Max[I, j]-Allocation[I, j];
Safety Algorithm
1. Work and Finish be the vector of length m and n respectively, Work=Available and
Finish[i] =False.
2. Find an i such that both
Finish[i] =False
Need<=Work
If no such I exists go to 4.
3. work=work+Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state.
ALGORITHM:
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. Or not we allow the request.
10. Stop the program.
67
PROGRAM:
/* BANKER’S ALGORITHM */
#include<stdio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{ int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
clrscr();
printf("\n ENTER THE NO. OF PROCESSES:");
scanf("%d",&n);
printf("\n ENTER THE NO. OF RESOURCES:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d \n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCE %d:",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
68
for(i=0;i<n;i++)
{
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].a1[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",av[j]);
}
}
printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j] >av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
69
printf("\n P %d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
else
printf("\n DEADLOCK OCCURED");
70
OUTPUT:
//RUN: NO deadlock
71
//RUN2: Deadlock occurs
RESULT
Thus the banker’s algorithm is implemented successfully for Deadlock avoidance &
Dead Lock Prevention.
72
EX NO: 7B
DEADLOCK DETECTION – BANKER’S ALGORITHM
DATE:
AIM
To implement deadlock detection by using Banker’s Algorithm.
ALGORITHM:
1. Mark each process that has a row in the Allocation matrix of all zeros.
2. Initialize a temporary vectorW to equal the Available vector.
3. Find an indexi such that processi is currently unmarked and thei th row ofQ
is less than or equal to W . That is,Q ik … Wk, for 1 … k … m . If no such row is
found, terminate the algorithm.
4. If such a row is found, mark processi and add the corresponding row of the
allocation matrix to W . That is, setWk = Wk + Aik, for 1 … k … m . Return
to step 3.
73
PROGRAM:
#include<stdio.h>
static int mark[20];
int i,j,np,nr;
int main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];
printf("\nEnter the no of process: ");
scanf("%d",&np);
printf("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the request matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
74
else
break;
}
if(count==nr)
mark[i]=1;
}
// initialize W with avail
for(j=0;j<nr;j++)
w[j]=avail[j];
for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
}
}
}
75
printf("\n No Deadlock possible");
}
OUTPUT:
Deadlock detected
--------------------------------
RESULT:
Thus the banker’s algorithm is implemented successfully for Deadlock detection.
76
EX NO: 8A
IMPLEMENTATION OF THREADING AND
SYNCHRONIZATION APPLICATIONS
DATE:
AIM:
To implement threading and synchronization techniques using c language.
ALGORITHM:
77
PROGRAM:
#include <stdio.h>
#include <pthread.h>
/*creating thread*/
ret=pthread_create(&id,NULL,&threadFunction,NULL);
if(ret==0){
printf("Thread created successfully.\n");
}
else{
printf("Thread not created.\n");
return 0; /*return from main*/
}
while(1)
{
printf("I am main function.\n");
}
return 0;
}
78
OUTPUT:
Thread created successfully.
I am threadFunction.
I am threadFunction.
I am threadFunction.
I am threadFunction.
…
…
I am threadFunction.
I am main function.
I am main function.
I am main function.
I am main function.
…
…
I am main function.
I am threadFunction.
… and so on.
RESULT:
Thus to implement threading and synchronization techniques using c language has
been executed and verified successfully.
79
EX NO: 8B
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
DATE:
ALGORITHM:
1. Declare structures hole and process to hold information about set of holes and processes
respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. If hole size>process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
b. Otherwise check the next from the set of holes
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availabilit y.
9.Stop the program.
80
PROGRAM
#include<stdio.h>
struct process
{
int size; int flag; int holeid;
}p[10];
struct hole
{
int size;
int actual;
}h[10];
main()
{
int i,np,nh,j;
printf("EnterthenumberofHoles:");
scanf("%d",&nh);
for(i=0;i<nh;i++){
printf("EntersizeforholeH%d:",i);
scanf("%d",&h[i].size);
h[i].actual=h[i].size;
}
printf("\nEnternumberofprocess:");
scanf("%d",&np);
for(i=0;i<np;i++){
printf("enterthesizeofprocessP%d:",i);
scanf("%d",&p[i].size);
p[i].flag=0;
}
for(i=0;i<np;i++){
for(j=0;j<nh;j++){
if(p[i].flag!=1){
if(p[i].size<=h[j].size){
p[i].flag=1; p[i].holeid=j; h[j].size-=p[i].size;
}}}}
printf("\n\tFirstfit\n");
printf("\nProcess\tPSize\tHole");
for(i=0;i<np;i++){
if(p[i].flag!=1)
printf("\nP%d\t%d\tNotallocated",i,p[i].size);
else
printf("\nP%d\t%d\tH%d",i,p[i].size,p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0;i<nh;i++)
printf("\nH%d\t%d\t%d",i,h[i].actual,h[i].size);
printf("\n");
}
81
OUTPUT:
RESULT:
Thus processes were allocated memory using first fit method.
82
II. WORST FIT ALLOCATION
AIM:
To allocate memory requirements for processes using worst fit allocation.
ALGORITHM:
83
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
84
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
RESULT
Thus processes were allocated memory using worst fit method.
85
III. BEST FIT ALLOCATION
AIM:
To allocate memory requirements for processes using best fit allocation.
ALGORITHM:
1. Declare structures hole and process to hold information about set of holes and processes
respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. Sort the holes according to their sizes in ascending order
b. If hole size>process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
c. Otherwise check the next from the set of sorted hole.
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availabilit y.
9. Stop
86
PROGRAM:
#include<stdio.h>
struct process
{
int size;
int flag;
int holeid;
}p[10];
struct hole
{
int hid;
int size;
int actual;
}h[10];
main()
{
int i,np,nh,j;
void bsort(structhole[],int);
printf("Enter the number of Holes:");
scanf("%d",&nh);
for(i=0;i<nh;i++)
{
printf("Enter size for holeH%d:",i);
scanf("%d",&h[i].size);
h[i].actual=h[i].size;
h[i].hid=i;
}
printf("\nEnter number of process:");
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of processP%d:",i);
scanf("%d",&p[i].size);
p[i].flag=0;
}
for(i=0;i<np;i++)
{
bsort(h,nh);
for(j=0;j<nh;j++)
{
if(p[i].flag!=1)
{
if(p[i].size<=h[j].size)
{
p[i].flag=1;
p[i].holeid=h[j].hid;
h[j].size-=p[i].size;
}
}
}
87
}
printf("\n\tBestfit\n");
printf("\nProcess\tPSize\tHole");
for(i=0;i<np;i++)
{
if(p[i].flag!=1)
printf("\nP%d\t%d\tNotallocated",i,p[i].size);
else
printf("\nP%d\t%d\tH%d",i,p[i].size,p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0;i<nh;i++)
printf("\nH%d\t%d\t%d",h[i].hid,h[i].actual, h[i].size);
printf("\n");
}
Void bsort(structholebh[],intn)
{
struct holetemp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bh[i].size>bh[j].size)
{
temp=bh[i];
bh[i]=bh[j];
bh[j]=temp;
}
}
}
}
88
OUTPUT:
RESULT:
Thus processes were allocated memory using best fit method.
89
EX NO: 9
PAGING TECHNIQUE OF MEMORY MANAGEMENT
DATE:
AIM:
To implement the Memory management policy- Paging.
ALGORITHM:
90
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;
91
}
}
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();
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();
}
92
OUTPUT:
RESULT:
Thus the Memory management policy- Paging isimplemented successfully.
93
EX NO: 10
PAGE REPLACEMENT ALGORITHMS
DATE:
ALGORITHM:
94
PROGRAM:
#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
printf("\n \t\t\t FIFO 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);
}
95
OUTPUT:
RESULT:
Thus the FIFO page replacement algorithm is implemented successfully.
96
EX NO: 10B
LRU PAGE REPLACEMENT ALGORITHM
DATE:
AIM:
To implement page replacement algorithm LRU (Least Recently Used LRU (Lease
Recently Used). Here we select the page that has not been used for the longest period of time.
ALGORITHM:
1: Create a queue to hold all pages in memory
2: When the page is required replace the page at the head of the queue
3: Now the new page is inserted at the tail of the queue
4: Create a stack
5: When the page fault occurs replace page present at the bottom of the stack
97
PROGRAM:
#include<stdio.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);
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;
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;
}
99
OUTPUT:
RESULT:
Thus the LRU page replacement algorithm is implemented successfully.
100
EX NO: 10C
OPTIMAL (LFU) PAGE REPLACEMENT ALGORITHM
DATE:
AIM:
To implement page replacement algorithms Optimal (The page which is not used for
longest time)
ALGORITHM:
Optimal algorithm:
Here we select the page that will not be used for the longest period of time.
OPTIMAL:
1: Create an array
2: When the page fault occurs replace page that will not be used for the
longest period of time.
101
PROGRAM:
/*OPTIMAL (LFU) page replacement algorithm*/
#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;
102
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);
}
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;
}
103
OUTPUT:
RESULT:
Thus the LFU page replacement algorithm is implemented successfully.
104
EX NO: 11
VARIOUS FILE ORGANIZATION TECHNIQUES
DATE:
105
PROGRAM:
#include<stdio.h>
main()
{
int master,s[20];
char f[20][20][20];
char d[20][20];
int i,j;
printf("enter number of directorios:");
scanf("%d",&master);
printf("enter names of directories:");
for(i=0;i<master;i++)
scanf("%s",&d[i]);
printf("enter size of directories:");
for(i=0;i<master;i++)
scanf("%d",&s[i]);
printf("enter the file names :");
for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" directory\tsize\tfilenames\n");
printf("*************************************************\n");
for(i=0;i<master;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
printf("\n");
}
printf("\t\n");
}
106
OUTPUT:
RESULT:
Thus the Single level directory structure is written and executed successfully.
107
EX NO: 11B
IMPLEMENTATION OF TWO-LEVEL DIRECTORY
DATE:
AIM:
To write a C program to implement Two-level directory structure in C.
ALGORITHM:
1. Start
2. Declare the number, names and size of the directories and subdirectories and file
names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop.
108
PROGRAM:
#include<stdio.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
{
int i,j,k,n;
clrscr();
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:");
scanf("%s",&dir[i].fname[j][k]);
}}}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++){
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");}
printf("\n"); }
}
109
OUTPUT:
RESULT:
Thus a C program to implement Two-level directory structure is written and executed
successfully.
110
EX NO: 12
FILE ALLOCATION STRATEGIES
DATE:
ALGORITHM:
1: Start the program.
2: Get the number of memory partition and their sizes.
3: Get the number of processes and values of block size for each process.
4: First fit algorithm searches all entire memory block until a hole which is
big enough is encountered. It allocates that memory block for the requesting
process.
5: Best-fit algorithm searches the memory blocks for the smallest hole which can
be allocated to requesting process and allocates if.
6: Worst fit algorithm searches the memory blocks for the largest hole and
allocates it to the process.
7: Analyses all the three memory management techniques and display the best
algorithm which utilizes the memory resources effectively and efficiently.
8: Stop the program.
111
PROGRAM:
#include<stdio.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]);
}
112
OUTPUT:
RESULT:
Thus the SEQUENTIAL file allocation method is implemented successfully.
113
EX NO: 12B
FILE ALLOCATION STRATEGIES – INDEXED FILE
ALLOCATION
DATE:
AIM:
To Write a C Program to implement Indexed File Allocation method.
ALGORITHM:
1: Start.
2: Let n be the size of the buffer
3: check if there are any producer
4. If yes check whether the buffer is full
5. If no the producer item is stored in the buffer
6: If the buffer is full the producer has to wait
7: Check there is any consumer.If yes check whether the buffer is empty
8: If no the consumer consumes them from the buffer
9: If the buffer is empty, the consumer has to wait.
10: Repeat checking for the producer and consumer till required
11: Terminate the process.
114
PROGRAM:
#include<stdio.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]);
}
115
OUTPUT:
RESULT:
Thus the indexed file allocation method is implemented successfully
116
EX NO: 12C
FILE ALLOCATION STRATEGIES – LINKED FILE
ALLOCATION
DATE:
AIM:
To Write a C Program to implement Linked File Allocation method.
ALGORITHM:
1: Create a queue to hold all pages in memory
2: When the page is required replace the page at the head of the queue
3: Now the new page is inserted at the tail of the queue
4: Create a stack
5: When the page fault occurs replace page present at the bottom of the stack
6: Stop the allocation.
117
PROGRAM:
#include<stdio.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");
}
}
118
OUTPUT:
RESULT:
Thus the linked file allocation method is implemented successfully.
119