Professional Documents
Culture Documents
Name : DHANUSH S
Year : II
Semester : IV
Academic year : 2020-2021 EVEN
1
211719104033
BONAFIDE CERTIFICATE
NAME ……………DHANUSH S……………
ACADEMICYEAR…2020-2021…….YEAR…2nd…..BRANCH…CSE …A…
Certified that this is the Bonafide record of work done by the above student in the
Signature Signature
Faculty-in-charge HOD
2
211719104033
3
211719104033
Ex DATE INDEX
No Page No
1 19-02-2021 UNIX commands 5
IMPLEMENTATION OF SYSTEM CALLS
2a 19-02-2021 fork() process 16
2b 19-02-2021 Retrieving process ID using getpid() 17
2c 19-02-2021 Usage of getpid() and getppid() system calls 18
2d 19-02-2021 Execution of exit() system call 19
2e 19-02-2021 Execution of wait() system call 20
2f 19-02-2021 Execution of close() system call 21
2g 19-02-2021 Execution of stat() system call 22
2h 19-02-2021 Execution of opendir() and readdir() system calls 23
UNIX COMMAND IN C
3a 26-02-2021 Execution of cp system calls 25
3b 26-02-2021 Execution of ls and grep() system calls 27
SHELL PROGRAMMING
4a 26-02-2021 Sum of two numbers 28
4b 26-02-2021 Greatest of three numbers 29
4c 26-02-2021 Arithmetic operations using case statement 30
4d 26-02-2021 Sum of n numbers 32
4e 05-03-2021 Factorial of a number 33
4f 05-03-2021 Fibonacci series 34
CPU SCHEDULING 35
5a 05-03-2021 Implementation of First Come First Served scheduling 38
algorithm without arrival time.
5b 05-03-2021 Implementation of First Come First Served scheduling 40
algorithm with arrival time.
5c 06-03-2021 Implementation of Shortest Job First scheduling algorithm 43
5d 06-03-2021 Implementation of Priority scheduling algorithm 45
5e 06-03-2021 Implementation of Round Robin scheduling algorithm 48
6 12-03-2021 SEMAPHORES 51
IMPLEMENTATION OF SHARED MEMORY AND IPC
7a 12-03-2021 Implementation of Interprocess communication using 54
Shared memory
4
211719104033
5
211719104033
AIM:
To execute various UNIX commands in detail.
GENERAL COMMANDS
[201401065@putty~]$ who am i
Displays our own login terminal and 201401065pts/2 2016-01-29 11.15 (172-15-12-
$who am i
other details 115.lightspeed.sntcca.sbcglobal.net)
[201401065@putty~]$ date
Displays the date which is stored in Fri jan29 11.15 (IST) 2016
$date
particular format
[201401065@putty~]$ cal
January 2016
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
$cal<month> Displays calendar of specified month 10 11 12 13 14 15 16
< year> and year 17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
[201401065@putty~]$ clear
[201401065@putty~]$
Clears the screen and displays the new
$clear
screen
1
211719104033
[201401065@putty~]$ uname
$uname Displays OS details Linux
[201401065@putty~]$ uname -a
Linux.putty.com 2.6.32-358.e16.i686#1 smpfri
$uname –a Displays all machine details 29 21:50:49 utc 21016 i686 i386 Gnu/Linux
[201401065@putty~]$ uname -s
Linux
$uname –s
Displays the OS name
[201401065@putty~]$ uname -v
#1 SMP Fri Jan 29 21:50:49 Utc 2016
2
211719104033
[201401065@putty~]$ uname -r
$uname –r 2.6.32-358.el6.i686
Displays the release of OS
[201401065@putty~]$ uname –n
$uname –n Putty.com
Displays the name of network mode
[201401065@putty~]$ uname -m
$uname – i686
m Displays the type of OS
[201401065@putty~]$ bc
bc 1.06.95
$bc
Copyright 1991-1994, 1997, 1998, 2000, 2004,
2006 Free Software Foundation, Inc.
2+3
5
[201401065@putty~]$ pwd
$pwd /home/201401065/pgm1
Displays the currently working directory
3
211719104033
SYNOPSIS
LISTING COMMANDS
4
211719104033
Su two.c
$ls –i Displays the node number of each file [201401065@putty~]$ ls –i
26543903 a.out 26545681 ffs.c 26545241 max.c
26544524 rad1.c
$ls –l Lists the permission given to each file [201401065@putty~] ls –l
Total 248
-rwxrwxr-x it28 it28 6356 Oct 24 21:34 a.out
-rw-rw-r-- 1 it28 it28 0 Oct 10 12.00 f1
-rw-rw-r-- it28 it28 193 Oct 6 10.50 fact
[201401065@putty~]$ greppgm1
$grep<pattern> Displays the line in which the given
<filename> pattern is seen Operating system is defined as interface
between user and hardware.
$cat –n filename Displays the file with line numbers. [201401065@put cat -n os
ty~]$
1. os is an
interface
$more<filename> Displays the file page by page [201401065@put
ty~] more os
os is an interface
5
211719104033
echo $c
done
2 4 20 os
$wc –l Displays the number of [201401065@put wc -l os
ty~]$
lines 2 os
FILTER COMANDS
6
211719104033
$head -5<file name> Displays the top 5 lines of the line [root@localhost~]$ head -5 alpha
a
b
c
d
e
DIRECTORY COMMANDS
7
211719104033
RESULT:
FORK PROCESS
AIM:
To write a UNIX C program to create a child process from parent process using fork() system
call.
ALGORITHM:
Step 1: Start.
Step 2: Invoke a fork() system call to create a child process that is a duplicate of parent process.
Step 3: Display a message.
Step 4: Stop.
PROGRAM 1:
// 3A)FORK PROCESS
#include<stdio.h>
#include <stdlib.h>
#include<unistd.h>
void main()
{
fork();
fork();
fork();
printf("Hello World\n");
}
OUTPUT:
8
211719104033
RESULT:
Thus a UNIX C program to create a child process from parent process using fork()
system call is executed successfully.
9
211719104033
AIM:
To write a UNIX C program to retrieve the process id using getpid() system call.
ALGORITHM:
Step 1: Start.
Step 2: Invoke fork() system call to create a child process.
Step 3: Use getpid() sysyem call to retrieve the process id.
Step 4: Display the process id.
Step 5: Stop.
PROGRAM:
OUTPUT:
RESULT:
Thus a UNIX C program to retrieve the process id using getpid() system call is
executed successfully.
10
211719104033
AIM:
To write a UNIX C program to create a child process and to retrieve the process id.
ALGORITHM:
Step 1: Start.
Step 2: Invoke a fork() and getpid() system call to create a child process that is a duplicate of
parent process and to retrieve the process id of the child created. Step 3: Display a message.
Step 4: Stop.
PROGRAM:
OUTPUT:
RESULT:
Thus a UNIX C program to create a child using fork() and retrieving its process id using
getpid() system call is executed successfully.
11
211719104033
12
211719104033
AIM:
To write a C program to implement exit() system call.
ALGORITHM:
Step 1:Start.
Step 2:Declare the variables.
Step 3:Check that the child process is zero after creation.
Step 4:Print parent process id.
Step 5:Sleep the parent process.
Step 6:The parent terminates, and then child will be orphan.
Step 7:Stop.
PROGRAM:
OUTPUT:
RESULT:
13
211719104033
14
211719104033
AIM:
To perform simulation of wait system call using c.
ALGORITHM:
Step 1: Start.
Step 2: Create process using fork and assign it to a variable
Step 3: Check for the condition pid is equal to 0
Step 4: If it is true print the value of i and teriminate the child process
Step 5: If it is not a parent process has to wait until the child teriminate
Step 6: Stop the execution
PROGRAM:
#include<stdio.h>
#include<unistd.h>
inti=10;
void main()
{
intpid=fork();
if(pid==0)
{
printf("initial value of i %d \n ",i);
i+=10;
printf("value of i %d\n",i);
printf("child terminated \n");
}
else
{
wait(0);
printf("value of i in parent process %d",i);
}
}
OUTPUT:
RESULT:
Thus the UNIX C program to simulate wait system call was executed successfully.
15
211719104033
AIM:
To create the file,read data from the file,update the file and close the file.
ALGORITHM:
Step 1:Start.
Step 2:Get the data from the user.
Step 3:Open a file.
Step 4:Read from the file.
Step 5:Close the file.
Step 6:Stop.
PROGRAM:
#include<stdio.h>
int main()
{
char str[100];
FILE *fp;
fp=fopen("rev.dat","r");
while(!feof(fp))
{
fscanf(fp,"%s",str);
printf(" %s ",str);
}
fclose(fp);
}
OUTPUT:
RESULT:
Thus the UNIX C program to simulate wait system call was executed successfully.
16
211719104033
AIM:
To write a C program to display the attribute of a file using the stat( ) system call of UNIX
operating system.
ALGORITHM:
Step 1:Start.
Step 2: Using stat( ) system call display the attributes of the given file.
Step 3: If stat ( ) call fails, then call perror to print a diagnostic message to the standard error.
Step 4: If call succeeds, then print the various attributes like file size,user id, group id,fileinode
number etc.
Step 5: Stop.
PROGRAM:
#include<errno.h>
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
int main()
{
struct stat statv;
if(stat(“/home/it/tab/OS/Notes.txt”,&statv)==-1)
perror(“stat”);
else
{
printf(“\n The file user id is: %d”,statv.st_uid); printf(“\n The file group id is:
%d”,statv.st_gid);
printf(„\n The file size is :%d”,statv.st_size);
printf(“\n The file inode number is :%d”,statv.st_ino);
}
return 0;
}
OUTPUT:
RESULT:
Thus a C program is written to display the attribute of a file using the stat( ) system call of UNIX
operating system.
17
211719104033
AIM:
To write a UNIX C program to simulate opendir and readdir system calls.
ALGORITHM:
Step 1: Start.
Step 2: Invoke opendir() system call to open a directory by passing command line argument
as parameter.
Step 3: Invoke readdir() system call to read the opened directory.
Step 4:Until the end of directory is encountered, read all the files in directory and display its directory
name,inode number and length of the record.
Step 5:Stop.
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<dirent.h>
main(intargc,char *argv[])
{
DIR *dirname;
structdirent *dir;
dirname=opendir(argv[1]);
dir=readdir(dirname);
while(dir!=NULL)
{
printf("Entry found :%s\n",dir->d_name);
printf("Inode number of entry:%d\n",dir->d_ino);
printf("Length of this record:%d\n",dir->d_reclen);
getchar();
}
}
18
211719104033
OUTPUT:
RESULT:
Thus a UNIX C program to simulate opendir and readdir system calls is
executed successfully.
19
211719104033
AIM:
To write a C program to simulate the cp command.
ALGORITHM:
Step 1:Start.
Step 2: Enter the two file names old_link and new_link as command line arguments.
Step 3: Check whether the two path names are different,if so exit.
Step 4: Else using link( ) set up new_link as a new reference to old_link.
Step 5: If link( ) fails, it returns -1 as an error status else it will call unlink( ) to
remove the old_link and its return value is that of the unlink( ) call. Step 6: Stop.
PROGRAM:
#define _GNU_SOURCE
#include<stdio.h>
void main(intargc,char **argv)
{
FILE *fp1, *fp2;
char ch;
intpos;
20
211719104033
fcloseall();
}
OUTPUT:
RESULT:
Thus the C program to simulate cp command was executed successfully.
21
211719104033
AIM:
To write a UNIX C program to simulate grep and ls system calls.
ALGORITHM:
Step 1: Start.
Step 2: Enter the options for listing and pattern searching.
Step 3:If the option is for ls command, using system() function display all the files in the directory.
Step 4: If the option is for pattern searching, using system() function search a pattern in a file.
Step 5: Stop.
PROGRAM:
#include<stdio.h>
void main()
{
char c;
int n;
printf("1.List of files in the directory\n2.List the lines\nEnter the option:");
scanf("%d",&n);
switch(n){
case 1:
system("ls");
break;
case 2:
system("grepprintf ex1a.c");
break;
}
}
OUTPUT:
RESULT:
Thus a UNIX C program to simulate grep and ls system calls is executed successfully.
22
211719104033
SHELL PROGRAMMING
AIM:
To write a UNIX shell program to find sum of two numbers.
ALGORITHM:
Step 1: Start.
Step 2: Read two values
Step 3: Add the values
Step 4: Print the sum
Step 5: Stop.
PROGRAM:
echo "Enter two numbers"
read n1 n2
s=`expr $n1 + $n2`
echo "The sum of two numbers are $s"
OUTPUT:
RESULT:
Thus, the program to find the sum of two numbers using shell program is
executed successfully.
23
211719104033
AIM:
To write a UNIX shell program to find the greatest among three numbers.
ALGORITHM:
Step 1: Start.
Step 2: Read three values.
Step 3: Compare the three numbers.
Step 4: Display the greater number.
Step 5: Stop.
PROGRAM:
OUTPUT:
RESULT:
Thus a shell program to find the greatest among three numbers is executed successfully
24
211719104033
AIM:
To write a UNIX shell program to add, subtract, multiply and divide two numbers using
case statement.
ALGORITHM:
Step 1: Start.
Step 2: Read two values.
Step 3: Enter your choice.
Step 4: Execute the corresponding case function.
Step 5: Display the result
Step 6: Stop.
PROGRAM:
echo "Enter two values"
read a b
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multuplication"
echo "4.Division"
echo "Enter your choice"
read ch
case "$ch" in
1) x=`expr $a + $b`
echo "The sum is $x";;
2) x=`expr $a - $b`
echo "The diff is $x";;
3) x=`expr $a \* $b`
echo "The product is $x";;
4) x=`expr $a / $b`
echo "The quotient is $x";;
esac
25
211719104033
OUTPUT:
RESULT:
26
211719104033
Thus a shell program to add, subtract, multiply and divide two numbers using
case statement is executed successful
27
211719104033
AIM:
To write a UNIX shell program to find the sum of n numbers.
ALGORITHM:
Step 1: Start.
Step 2: Read the value of n.
Step 3: Read the values till n is not equal to zero.
Step 4: Add the numbers.
Step 5: Display the result
Step 6: Stop.
PROGRAM:
OUTPUT:
RESULT:
Thus a shell program to find the sum of n numbers is executed successfully
28
211719104033
AIM:
To write a UNIX shell program to find the factorial of a given number.
ALGORITHM:
Step 1: Start.
Step 2: Read the number.
Step 3: Multiply i from the given number.
Step 4: Decrement i.
Step 5: Repeat this until I is greater than 0
Step 5: Display the factorial
Step 6: Stop.
PROGRAM:
echo "Enter the number"
read x
i=`expr $x - 1`
while [ $i -gt 0 ]
do
x=`expr $x \* $i`
i=`expr $i - 1`
done
echo "The factorial is $x"
OUTPUT:
RESULT:
Thus a shell program to find the factorial of a given number is executed successfully
29
211719104033
ALGORITHM:
Step 1: Start.
Step 2: Read the number.
Step 3: Print the first two numbers.
Step 4: Add the two numbers and print the result.
Step 5: Add the result with previous number.
Step 5: Print sequentially till the end number.
Step 6: Stop.
PROGRAM:
OUTPUT:
RESULT:
Thus a shell program to find the Fibonacci series for the given number is
executed successfully.
30
211719104033
AIM:
To write a UNIX C program to implement first come first serve without arrival time CPU
scheduling algorithm.
PROBLEM DESCRIPTION:
CPU scheduler will decide which process should be given the CPU for its execution. For
this it use different algorithm to choose among the process. one among that algorithm is
FCFS algorithm.
In this algorithm the process which arrives first is given the cpu after finishing its request only
it will allow CPU to execute other process.
ALGORITHM:
Step 1:Start.
Step 2: Create the number of process.
Step 3: Get the ID and Service time for each process.
Step 4: Initially, Waiting time of first process is zero and Total time for the
first process is the starting time of that process.
Step 5: Calculate the Total time and processing time for the remaining processes.
Step 6: Waiting time of one process is the Total time of the previous process.
Step 7: Total time of process is calculated by adding Waiting time and Service time.
Step 8: Total waiting time is calculated by adding the waiting time for lack process.
Step 9: Total turnaround time is calculated by adding all total time of each process.
Step 10: Calculate Average waiting time by dividing the total waiting time by total number
of processes.
Step 11: Calculate Average turnaround time by dividing the total time by the total
number of processes.
Step 12: Display the result.
Step 13:Stop.
PROGRAM:
//FCFS
#include <stdio.h>
#include <stdlib.h>
void main()
{
intn,b[10],t=0,w=0,i,r=0,a=0;
printf("Enter number of processes:");
scanf("%d",&n);
float avg,avg1;
printf("Enter the burst time:\n");
for(i=1;i<=n;i++)
31
211719104033
scanf("%d",&b[i]);
printf("Gantt Chart: ");
for(i=1;i<=n;i++)
printf("P%d ",i);
printf("\nProcess\tburst time\twaiting time\tturn_around time\n");
for(i=1;i<=n;i++)
{
t=t+w;
r=r+b[i];
printf("P%d\t%d\t%d\t%d\n",i,b[i],w,r);
w=w+b[i];
a=a+r;
}
avg=(float)t/n;
avg1=(float)a/n;
printf("\n Average waiting time: %f",avg);
printf("\n Average turnaround time: %f \n",avg1);
}
OUTPUT:
RESULT/INFERENCE:
From the above output we infer that the process that arrives first is
executed first irrespective of its burst time.Thus,the processes with lesser burst times need to wait
since the process that arrives first gets executed first.Therefore,in scenarios where the processes have
large burst time,there’ll be large average waiting time and average turnaround time.Thus,the
program
to simulate FCFS CPU Scheduling algorithm is executed successfully
32
211719104033
AIM:
To write a C program to implement the array representation of the CPU scheduling algorithm
first come first serve using arrival time.
PROBLEM DESCRIPTION:
CPU scheduler will decide which process should be given the CPU for its execution. For this
it uses different algorithm to choose among the process. One among that algorithm is FCFS
algorithm.
In this algorithm the process which arrive first is given the cpu after finishing its
request only it will allow cpu to execute other process
ALGORITHM:
Step 1:Start.
Step 2: Create the number of process.
Step 3: Get the ID and Service time for each process.
Step 4: Initially, Waiting time of first process is zero and Total time for the first process
is the starting time of that process.
Step 5: Calculate the Total time and Processing time for the remaining processes.
Step 6: Waiting time of one process is the Total time of the previous process.
Step 7: Total time of process is calculated by adding Waiting time and Service
time.
Step 8: Total waiting time is calculated by adding the waiting time for lack process.
Step 9: Total turn around time is calculated by adding all total time of each process.
Step 10: Calculate Average waiting time by dividing the total waiting time by
total number of process.
Step 11: Calculate Average turn around time by dividing the total time by the
number of process.
Step 12: Display the result.
Step 13:Stop.
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char pn[10][10],t[10];
intarr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
inttotwt=0,tottat=0;
printf("Number of processes:");
33
211719104033
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:\t");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
}
for(i=0; i<n; i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("\nProcessArrtimeBurstimeWaitTime Start TAT Finish");
for(i=0; i<n; i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%6d\t%6d",pn[i],arr[i],bur[i],wt[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f",(float)totwt/n);
printf("\nAverage Turn Around Time:%f",(float)tottat/n);
return 0;
}
34
211719104033
OUTPUT:
RESULT/INFERENCE:
From the above output we infer that the process that arrives first is executed
first irrespective of its burst time.Also,consider the other parameter,arrivaltime.Thus,in cases where
multiple processes have the same arrival time,the process that arrived first amongst the processes with
the same burst time will be executed first.Therefore,processes with lesser burst times need to wait since
the process that arrives first gets executed first.Therefore,in scenarios where the processes have large
35
211719104033
burst
time,there’ll
be large
EX NO.: 5c SHORTEST JOB FIRST SCHEDULING ALGORITHM average
DATE: 06-03-2021 waiting time
and average
turnaround time.Thus,the program to simulate FCFS CPU Scheduling algorithm is executed
successfully.
AIM:
To write a UNIX C program to implement shortest job first scheduling algorithm.
PROBLEM DESCRIPTION:
CPU scheduler will decide which process should be given the CPU for its execution. For this it use
different algorithm to choose among the process. one among that algorithm is SJF algorithm.In this
algorithm the process which has less service time given the CPU after finishing its request only it will
allow CPU to execute next other process.
ALGORITHM:
Step 1: Start.
Step 2: Get the input process and their burst time.
Step 3: Sort the processes based on burst time.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time.
Step 6: Print the details about all the processes.
Step 7: Stop.
PROGRAM:
//SHORTEST JOB FIRST
#include<stdio.h>
int main()
{
intn,w[100],tot[100],i,j,awt,atot;
float avwt,avtot;
struct
{
intp,bt; }sjf[10],temp;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
36
211719104033
OUTPUT:
37
211719104033
RESULT/INFERENCE:
From the above output we infer that the only parameter taken into
consideration is the burst time.Irrespective of when the process arrived,the process can get executed if it
has relatively smaller burst time.Therefore,in SJF the average turnaround time & waiting time can be
reduced.Thus,the
program to simulate
EX NO.:5d PRIORITY SCHEDULING ALGORITHM SJF CPU Scheduling
DATE: 06-03-2021 algorithm is executed
successfully.
AIM:
To write a UNIX C program to implement priority scheduling algorithm.
PROBLEM DESCRIPTION:
A priority is assigned to each process and CPU is allocated to the process with highest priority.
Priorities may be based on internally measurable quantities like time limits, memory requirements,
number of open files etc. and some external quantities like importance of the process, type and
amount spent for each process etc. It depicts the order in which the processes are executed, calculates
the turnaround time and average waiting time of the processes.
ALGORITHM:
Step 1:Start.
Step 2: Get the number of process
Step 3: Get the id and service time for each process.
Step 4: Initially the waiting time of first short process as 0 and total time of first short is process
the service time of that process.
Step 5: Calculate the total time and waiting time of remaining process.
Step 6: Waiting time of one process is the total time of the previous process.
Step 7: Total time of process is calculated by adding the waiting time and service time of
each process.
Step 8: Total waiting time calculated by adding the waiting time of each process.
Step 9: Total turn around time calculated by adding all total time of each process.
Step 10: Calculate average waiting time by dividing the total waiting time by total number of process.
Step 11: Calculate average turn around time by dividing the total waiting time by total number
of process.
Step 12: Display the result.
Step 13:Stop.
38
211719104033
PROGRAM:
//PRIORITY SCHEDULING
#include<stdio.h>
int main()
{
intn,temp=0,w[20],b[20],p[20],t2[20],j,t1,d[20],i,te=0,b1[20],t3=0;
float t,r;
w[1]=0;
printf("Enter no of processes:");
scanf("%d",&n);
printf("Enter the burst times:\n");
for(i=1;i<=n;i++)
{
printf("P%d:",i);
scanf("%d",&b[i]);
d[i]=i;
}
printf("Enter the priorities:\n");
for(i=1;i<=n;i++)
{
for(i=1;i<=n;i++)
{
printf("P%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(p[i]<p[j])
{
temp=p[i];
t1=d[i];
te=b[i];
p[i]=p[j];
d[i]=d[j];
b[i]=b[j];
p[j]=temp;
d[j]=t1;
b[j]=te;
}
printf("Gantt chart:");
for(i=1;i<=n;i++)
printf(" P %d\t",d[i]);
printf("\n Process\t priority \t burst time \t waiting time \t turnaround time");
for(i=1;i<=n;i++)
39
211719104033
{
t=d[i];
w[i+1]=w[i]+b[i];
t2[i]=b[i]+w[i];
t3+=t2[i];
printf("\nP %d\t\t %d\t\t%d\t\t%d\t\t%d",d[i],p[i],b[i],w[i],t2[i]);
}
}
temp=0;
for(i=1;i<=r;i++)
temp+=w[i];
t=(float)temp/n;
r=(float)t3/n;
printf("\n Average waiting time:%.2f",t);
printf("\n Averageturnaround time:%.2f",r);
OUTPUT:
40
211719104033
RESULT/INFERENCE:
From the above output we infer that the only parameter considered is the
priority given to the processes.Therefore,the process having the highest priority will get executed
first irrespective of when it arrived.In priority scheduling,particular cases might result in low average
turnaround time & average time while in some cases the average turnaround time & average time
might be
high.Thus,the
program to
EX NO.: 5e ROUND ROBIN SCHEDULING ALGORITHM simulate Priority
DATE: 06-03-2021 CPU Scheduling
algorithm is
executed successfully.
AIM:
To write a UNIX C program to implement round robin scheduling algorithm.
PROBLEM DESCRIPTION:
CPU scheduler will decide which process should be given the CPU for its execution .For
this it use different algorithm to choose among the process .one among that algorithm is
Round robin algorithm.
In this algorithm we are assigning some time slice .The process is allocated according to the
time slice ,if the process service time is less than the time slice then process itself will release
the CPU voluntarily .The scheduler will then proceed to the next process in the ready queue
.If the CPU burst of the currently running process is longer than time quantum ,the timer will
go off and will cause an interrupt to the operating system .A context switch will be executed
and the process will be put at the tail of the ready queue.
ALGORITHM:
Step 1: Start.
Step 2: Get the input process and their burst time.
Step 3: Sort the processes based on priority.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time.
Step 6: Print the details about all the processes.
41
211719104033
Step 7: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
inti, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;
42
211719104033
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_tat);
printf("\n Average Waiting Time: \t%f", avg_wt);
}
OUTPUT:
43
211719104033
RESULT/INFERENCE:
From the above output we infer that the process that arrives first is
executed first but only for designated time quantum.Thus,the processes will be executed in the
order of their arrival but the execution takes place only for the specific time slice.This scheduling
ensures all processes get a chance to be executed atleast partially and processes that have arrived
later but have relatively shorter burst times will have to wait lesser for execution.Thus,in this
scheduling the average waiting time can be reduced.Thus,the program to simulate Roundrobin CPU
Scheduling algorithm is executed successfully.
44
211719104033
AIM:
To write a UNIX C program to implement producer consumer problem using semaphores.
ALGORITHM:
Step 1: Start.
Step 2: Initialize mutex to 1, full to 0 and empty to n.
Step 3: If mutex=1 and empty is greater than zero, invoke producer function. Otherwise
display the message buffer is full.
Step 4: If mutex=1 and full is greater than zero, invoke consumer function. Otherwise display
the message buffer is empty.
Step 5: In producer function, produce an item wait for empty buffer and mutex. If any buffer
is empty add the produced item to the buffer and signal mutex and full buffer.
Step 6: In consumer function, wait for full buffer and mutex. If any buffer is full, remove the
item from the buffer to the consumer. Signal mutex and empty buffer. Step 7: Stop.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
intmutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces item %d", x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item %d",x);
x--;
++mutex;
}
45
211719104033
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
#pragma omp critical
for (i = 1; i> 0; i++) {
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)
&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!");
}
break;
case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!");
}
break;
case 3:
exit(0);
break;
}
}
}
46
211719104033
OUTPUT:
RESULT:
47
211719104033
AIM:
To write a UNIX C program to implement interprocess communication using shared memory.
ALGORITHM:
Step 1: Start.
Step 2: Create the shared memory for parent process using shmget() system call.
Step 3: Attach the shared memory to the child process.
Step 4: Create child process using fork ().
Step 5: Parent process writes the content in the shared memory.
Step 6: The child process reads the content from the shared memory.
Step 7: Detach and release the shared memory.
Step 8: Stop.
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<fcntl.h>
main()
{
char *shmptr;
intshmid,child,i;
shmid=shmget(2041,30,IPC_CREAT|0666);
shmptr=shmat(shmid,0,0);
child=fork();
if(!child)
{
printf("PARENT WRITING\n");
for(i=0;i<20;i++)
{
shmptr[i]='a'+i;
putchar(shmptr[i]);}
wait(0);
}
else
{
printf("\nCHILD READING\n");
for(i=0;i<20;i++)
putchar(shmptr[i]);
48
211719104033
shmdt(NULL);
shmctl(shmid,IPC_RMID,0);
}
}
OUTPUT:
RESULT:
Thus a UNIX C program to implement interprocess communication using shared
memory is executed successfully.
49
211719104033
AIM:
To write a UNIX C program to implement interprocess communication using pipes.
ALGORITHM:
Step 1: Start.
Step 2: Create a pipe structure using pipe() system call.Pipe() system call returns 2 file descriptors
fd[0] and fd[1].fd[0] is opened for reading and fd[1] is opened for writing. Step 3:Create a child
process using fork() system call.
Step 4:Close the read end of the parent process using close().
Step 5:Write the data in the pipe using write().
Step 6:Close the write end of the child process using close().
Step 7:Read the data in the pipe using read()..
Step 8:Stop.
PROGRAM:
#include<stdio.h>
int main()
{
intfd[2],child;
char a[20];
printf("Enter the string to enter into the pipe:");
scanf("%s",a);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],a,5);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf("The string retrieved from the pipe is %s\n",a);
}
return 0;
}
OUTPUT:
RESULT:
50
211719104033
51
211719104033
AIM:
To implement interprocess communication using message Queues.
ALGORITHM:
Step 1:Start.
Step 2:Create a message queue using msgget system call
Step 3:If the message queue,is not created goto
Step1 Step 4:Copy the string “Hai’ to mtext
Step 5:Create a child process
Step 6:If the child process is successfully created then send the message otherwise go to step 8
Step 7:If the parent process got the focus receive the message sent by the child
Step 8:Stop.
PROGRAM:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
structmsg_buffer {
long msg_type;
char msg[100];
} message;
main()
{
key_tmy_key;
intmsg_id;
my_key = ftok("progfile", 65);
msg_id = msgget(my_key, 0666 | IPC_CREAT);
message.msg_type = 1;
printf("Write Message : ");
fgets(message.msg, 100, stdin);
msgsnd(msg_id, &message, sizeof(message), 0);
printf("Sent message is : %s \n", message.msg);
}
OUTPUT:
52
211719104033
RESULT:
Thus the program for inter process communication using message Queues was
implemented
AIM:
To implement deadlock avoidance using Banker’s Algorithm in C.
ALGORITHM:
1. request ≤ max, else set error condition as process has crossed maximum made by it.
2. request ≤ available, else process waits until resources are available.
Some of the resources that are tracked in real systems are memory, semaphores and interface access.
The Banker's ALGORITHM derives its name from the fact that this ALGORITHM could be used in
a banking system to ensure that the bank does not run out of resources, because the bank would never
allocate its money in such a way that it can no longer satisfy the needs of all its customers.
By using the Banker's ALGORITHM, the bank ensures that when customers request money the bank
never leaves a safe state.
If the customer's request does not cause the bank to leave a safe state, the cash will be allocated,
otherwise the customer must wait until some other customer deposits enough.
Let n be the number of processes in the system and m be the number of resource types.
Then we need the following data structures:
1. Available: A vector of length m indicates the number of available resources of each type.
IfAvailable[j] = k, there are k instances of resource type Rj available.
53
211719104033
2. Max: Ann×mmatrix defines the maximum demand of each process. If Max[i,j] = k, thenPi
may request at most k instances of resource type Rj.
3. Allocation: Ann×mmatrix defines the number of resources of each type currentlyallocated
to each process. If Allocation[i,j] = k, then process P i is currently allocated k instances of
resource type Rj.
4. Need: Ann×mmatrix indicates the remaining resource need of each process. If Need[i,j] =k,
then Pi may need k more instances of resource type Rj to complete the task.Note: Need[i,j] =
Max[i,j] - Allocation[i,j]
54
211719104033
PROGRAM:
55
211719104033
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(need[i][j]<=avail[j])
k++;
}
if(k==resource && completed[i]==0 )
{
printf("P[%d] ",i);
completed[i]=1;
for(j=0;j<resource;j++)
avail[j]=avail[j]+allot[i][j];
count1++;
}
k=0;
}
if(count1==count2)
{
printf("\nStop ..After this.....Deadlock\n");
break;
}
}
}
OUTPUT:
RESULT/INFERENCE:
Thus,we infer that the available resources,allocated resources and required
resources are taken int consideration by Banker’s algorithm in order to prevent a
deadlock.Thus,aUNIX C Program to implement deadlock avoidance using Banker’s Algorithm was
executed
successfully.
56
211719104033
AIM:
To write a UNIX C Program to implement deadlock prevention using Banker’s algorithm.
ALGORITHM:
Step 1: Start.
Step 2: Obtain the required data through char and in data types.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop.
PROGRAM:
#include <stdio.h>
#include <conio.h>
void 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]);
57
211719104033
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);
}
}
58
211719104033
OUTPUT:
RESULT:
Thus the UNIX C Program to implement deadlock detection using Banker’s
Algorithm was executed successfully.
59
211719104033
AIM:
ALGORITHM:
Step 1:Start
Step 2: Two threads (jobs) are created in the start function of these threads
Step 3: Counter is maintained by which user gets the logs about job number which is started and stopped.
Step 4:Stop.
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
void* doSomeThing(void
*arg) {unsigned long i = 0;
counter += 1;
int main(void)
int i = 0,ERR;
while(i < 2)
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;}
60
211719104033
OUTPUT:
[201401065@putty~]$ cc opt.c
[201401065@putty~]$ ./a.out
Job 1 started
Job 2 started
Job 2 finished
Job 2 finished
61
211719104033
RESULT:
Thus the C program to implement for threading and synchronization applications was executed successfully
and verified.
AIM:
To write a C program to implement Memory Management concept using the technique First Fit
Algoithm.
ALGORITHM:
Step 1:Start.
Step 2:Get the number of free space available.
Step 3:Get the starting address of each free space and how much space available.
Step 4:While getting the address and space we have to check whether there is duplication, if there
is duplication then give the error message for duplication & get the address and space once again.
Step 5:Get the space for the process.
Step 6:Search the first memory location from the available list its size should be greater than or equal
to the process size an or equal to available space in FIFO order. Step 7:If there is no sufficient space
output the error message.
Step 8:Display the process and its corresponding allocated memory space.
Step 9:Stop.
PROGRAM:
#include<stdio.h>
structff{
intid,as;
}mm[10];
void main()
{
int pd,i,temp,temp1,cc,j,n,a,flag=0,flag1=0;
printf("Enter the no of space to be used: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the address: ");
scanf("%d",&mm[i].id);
printf("Please enter the space for.%d: ",mm[i].id);
scanf("%d",&mm[i].as);
for(j=0;j<i;j++)
62
211719104033
OUTPUT:
63
211719104033
RESULT:
Thus a UNIX C program to implement memory management scheme using first fit
is executed successfully.
ALGORITHM:
Step 1:Start.
Step 2:Get the number of free space available.
Step 3:Get the starting address of each free space and how much space available.
Step 4:While getting the address and space we have to check whether there is duplication, if there is
duplication then give the error message for duplication & get the address and space once again.
Step 5:Get the space for the process.
Step 6:Sort the available list in ascending order based on the space.
Step 7:Search the first memory location from the available list its size should be greater than or
equal to the process size an or equal to available space in FIFO order. Step 8:If there is no sufficient
space output the error message.
Step 9:Display the process and its corresponding allocated memory space.
Step 10:Stop.
PROGRAM:
#include<stdio.h>
structff{
64
211719104033
intid,as;
}mm[10];
main()
{
int pd,i,temp,temp1,cc,j,n,a,flag=0,flag1=0;
printf("Enter the no space to be used:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the address:");
scanf("%d",&mm[i].id);
printf("Please enter the space for.%d:",mm[i].id);
scanf("%d",&mm[i].as);
for(j=0;j<i;j++)
if (mm[i].id<=mm[j].id+mm[j].as && mm[i].id>mm[j].id ||
(mm[i].id<mm[j].id&&mm[i].id+mm[i].as>mm[j].id))
{
flag=1;
printf("Duplicate address...");
break;
i--;
break;
}
}
printf("\nEnter the space required:");
scanf("%d",&a);
printf("Best Fit Algorithm :\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(mm[i].as>=mm[j].as)
temp1=mm[i].id;
mm[i].as=mm[j].as;
mm[i].id=mm[j].id;
if(mm[i].as>=a)
{
mm[i].id+=a;
mm[i].as-=a;
break;
}
else
printf("\nThe space not available in:P%d\n",i+1);
}
printf("\nAfter allocation space available:\n");
printf("\n**********************************");
65
211719104033
printf("\nProcessid \tAvailableSpace:\n");
printf("******************************\n");
for(i=0;i<n;i++)
printf("\n%d\t\t%d",mm[i].id,mm[i].as);
}
}
OUTPUT:
66
211719104033
RESULT:
Thus a UNIX C program to implement memory management scheme using Best fit is
executed successfully.
67
211719104033
AIM:
To write a C program to implement Memory Management concept using the technique Worst
Fit Algoithm.
ALGORITHM:
Step 1:Start.
Step 2:Read the number of partitions and total memory for each partition, number of process and
memory.
Step 3:Set allocated bit as zero and set free bit as the size for each partition.
Step 4:Search the largest hole to allocate the process in that hole.
Step 5:Set the free space as difference between allocated memory for process.
Step 6:Repeat the above three steps.
Step 7:Display the partition name, allocated memory, free memory and to which the process the
partition allotted memory.
Step 8:Display free space.
Step 9:Stop
PROGRAM:
//EXP-10.3
#include<stdio.h>
void main()
{
inti,j,temp,f[10],fp[10];
intno,p[15],part[15],pno,pr[15],prmem[15];
printf("*******************************************");
printf("\nIMPLEMENTATION OF WORST-FIT ALGORITHM");
printf("\n*******************************************");
printf("\nEnter the number of partitions:");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
p[i]=i;
printf("Enter the memory for partition %d:\t",i);
scanf("%d",&part[i]);
}
for(i=1;i<=no;i++)
{
for(j=1;j<=i;j++)
{
if(part[j]<part[i])
68
211719104033
{
temp=part[i];
part[i]=part[j];
part[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\nFree memory");
for(i=1;i<=no;i++)
printf("\n partition %d: \t %d",p[i],part[i]);
printf("\n Enter the number of process");
scanf("%d",&pno);
for(i=1;i<=pno;i++)
{
pr[i]=i;
printf("Enter the size for process %d:\t",i);
scanf("%d",&prmem[i]);
}
printf("\n----------------------------------------");
printf("\nPROCESS|\t PARTITION| \t FREE_MEMORY|\n");
printf("\n----------------------------------------\n");
j=1;
for(i=1;i<=no;i++)
{
f[i]=0;
fp[j]=0;
}
while(j<=pno)
{
for(i=1;i<=no;i++)
{
if((part[i]>=prmem[j]) && (f[i]==0))
{
part[i]=part[i]-prmem[j];
fp[j]=1;
printf("%d\t\t%d\t\t%d \n",pr[j],p[i],part[i]);
goto l1;
}
}
l1: j++;
}
for(i=1;i<=no;i++)
{
69
211719104033
if(f[i]==0)
printf("\t\t%d\t\t%d\n",p[i],part[i]);
}
printf("The following process is not allocatted:");
for(i=1;i<=pno;i++)
{
if(fp[i]==0)
printf(" %d ",pr[i]);
}
OUTPUT:
RESULT:
Thus a UNIX C program to implement memory management scheme using Best fit is
executed successfully
70
211719104033
AIM:
To write a UNIX C program to implement paging memory management scheme.
ALGORITHM:
Step 1:Start.
Step 2:Initialise all the temporary variables.
Step 3:Declare the variables used.
Step 4:Display a menu for the following operations
1.Load
2.Execute
3.Display
Step 5:If choice is 1, load the process number and process size. Initialize the resident to 0.
Step 6:If choice is 2, execute, enter the process to be executed then if process is present in secondary
memory, transfer it to the main memory and change its resident bit to 1.
Step 7:If the choice is display, then display the process number, secondary memory is starting
address, secondary memory ending address, resident bit, primary memory starting address, primary
memory ending address.
Step 8:If the choice is to exit, exit from the switch case.
Step 9:The program will be executed until the exit choice.
Step 10:Stop the program.
PROGRAM:
//11.1
#include<conio.h>
#include<stdio.h>
struct page
{
intnopages,sms,mms,sme,mme,pno,psize,residentbit;
} table[30]; void main()
{
intch, resibit, i=0, ss=4000, se=7000, ms=6000, me=9000, pn,j; int flag;
printf("PAGING OPERATIONS");
printf("\n---DETAILS OF SECONDARY MEMORY---");
printf("\nstarting address of secondary memory=%d", ss);
printf("\nending address of secondary memory=%d", se);
printf("\n\n---DETAILS OF MAIN MEMORY---");
printf("\nstarting address of main memory=%d", ms);
printf("\nending address of main memory=%d", me);
printf("\nPAGE SIZE=50");
71
211719104033
do
{
printf("\nMENU");
printf("\n1.LOAD");
printf("\n2.DISPLAY");
printf("\n3.EXECUTE");
printf("\n4.EXIT");
printf("\nENTER THE CHOICE:");
scanf("%d", &ch);
switch(ch)
{
case 1:
{
printf ("Enter the process number:");
scanf("%d", &table[i].pno);
printf("\nEnter the process size:");
scanf("%d", &table[i].psize);
table[i].nopages=table[i].psize/50;
if(table[i].psize%50!=0)
table[i].nopages++;
table[i].residentbit=0;
table[i].sms=ss;
table[i].sme=table[i].sms+table[i].nopages*50;
ss=table[i].sme;
table[i].mms=-1;
table[i].mme=-1;
i++;
break;
}
case 2:
{
printf("\n------------------------------------------------ ");
printf("\n PNO PSIZE NOPAGES SMS MMS SME MME RSBIT");
printf("\n------------------------------------------------- ");
for (j=0;j<i;j++)
{
printf("\n%d", table[j].pno);
printf("\t %d", table[j].psize);
printf("\t %d", table[j].nopages);
printf("\t %d", table[j].sms);
printf("\t %d", table[j].mms);
printf("\t %d", table[j].sme);
printf("\t %d", table[j].mme);
printf("\t %d", table[j].residentbit);
72
211719104033
}
printf("\n ----------------------------------");
break;
}
case 3:
{
flag=0;
printf("\nENTER THE PROCESS NUMBER TO BE EXECUTED:");
scanf("%d", &pn);
for(j=0;j<i;j++)
{
if(pn==table[j].pno)
{
flag=1;
if(table[j].residentbit==0);
{
table[j].mms=ms;
table[j].mme=table[j].mms+table[j].nopages*50;
ms=table[j].mme;
table[j].residentbit=1;
}
}
}
if(flag==0)
printf("\nPROCESS NOT PRESENT IN THE MAIN MEMORY FOR EXECUTION");
else
printf("\nPROCESS PRESENT IN THE MAIN MEMORY FOR EXECUTION");
break;
}
case 4:
{
exit(0);
break;
}
}
}
while(ch!=4);
}
73
211719104033
OUTPUT:
RESULT:
74
211719104033
75
211719104033
AIM:
To write a UNIX C program to implement segmentation memory management scheme.
ALGORITHM:
PROGRAM:
//11.2
#include<stdio.h>
void main()
{
intn,sn[50],li[50],base[50],la,pa,i,b,s;
printf("\nEnter the number of segments:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the limit (%d):",i);
scanf("%d",&li[i]);
}
printf("Enter the base for segment(0):");
scanf("%d",&base[0]);
for(i=1;i<n;i++)
{
printf("Enter the base for segment(%d):",i);
scanf(" %d",&base[i]);
if(base[i]<li[i-1]+base[i-1])
{
printf("Enter the valid base (%d)",i);
scanf("%d",&base[i]);
}
}
printf("Enter the segment no and offset:");
scanf("%d %d",&s,&la);
if(la>li[s]);
{
76
211719104033
OUTPUT:
RESULT:
Thus a UNIX C program to implement segmentation memory management scheme
is executed successfull
77
211719104033
AIM:
To write a UNIX C program to implement FIFO page replacement algorithm.
PROBLEM DESCRIPTION:
The FIFO Page Replacement algorithm associates with each page the time when that page was
brought into memory . When a page must be replaced , the oldest page is chosen . There is not strictly
necessary to record the time when a page is brought in. By creating a FIFO queue to hold all pages in
memory and by replacing the page at the head of the queue. When a page is brought into memory ,
insert it at the tail of the queue.
ALGORITHM:
Step 1:Start.
Step 2:Declare the size with respect to page length
Step 3:Check the need of replacement from the page to memory
Step 4:Check the need of replacement from old page to new page in
memory Step 5:Format queue to hold all pages
Step 6:Insert the page require memory into the queue
Step 7:Check for bad replacement and page fault
Step 8:Get the number of processes to be inserted
Step 9:Display the values
Step 10:Stop.
PROGRAM:
#include<stdio.h>
int main()
{
inti,j,n,a[50],frame[10],no,k,avail,count=0;
printf("ENTER THE NUMBER OF PAGES: ");
scanf("%d",&n);
printf("ENTER THE PAGE NUMBER: ");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("ENTER THE NUMBER OF FRAMES: ");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("ref\tstring\tpage\tframes\n");
for(i=1;i<=n;i++)
{
printf("%d\t",a[i]);
78
211719104033
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
OUTPUT:
RESULT:
79
211719104033
80
211719104033
AIM:
To write UNIX C program a program to implement LRU page replacement algorithm.
PROBLEM DESCRIPTION:
The Least Recently Used replacement policy chooses to replace the page which has not been
referenced for the longest time. This policy assumes the recent past will approximate the immediate
future. The operating system keeps track of when each page was referenced by recording the time of
reference or by maintaining a stack of references.
ALGORITHM:
Step 1:Start.
Step 2:Declare the size
Step 3:Get the number of pages to be inserted
Step 4:Get the value
Step 5:Declare counter and stack
Step 6:Select the least recently used page by counter value
Step 7:Stack them according the selection.
Step 8:Display the values
Step 9:Stop.
PROGRAM:
#include<stdio.h>
void main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
81
211719104033
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("%d\t",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("%d\t",q[r]);
}
82
211719104033
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
RESULT:
83
211719104033
84
211719104033
AIM:
To write a program in C to implement Optimal page replacement algorithm.
ALGORITHM:
Step1: Start.
Step2: Declare the required variables and initialize it.
Step3; Get the frame size and reference string from the user
Step4: Keep track of entered data elements
Step5: Accommodate a new element look for the element that is not likely to be used in future
replace.
Step6: Count the number of page fault and display the value
Step7: Stop.
PROGRAM:
#include<stdio.h>
int frames[10] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
intnf,pf=0,pageno[50],np=0,location[10];
int search(inti);
void replace(inti);
void print(inti);
int main()
{
inti=0;
printf("Optimal Page Replacement Algorithm :\n");
printf("-----------------------------------\n\n");
printf("Enter the number of frames : ");
scanf("%d",&nf);
printf("Enter the page numbers one by one.");
printf("(Enter -1 when finished)\n");
do
{
scanf("%d",&pageno[i]);
np++;
i++;
}while(pageno[i-1] != -1);
i=0;
np--;
printf("\nInput Frame Status\n");
printf("------------------------\n");
85
211719104033
while(pageno[i] != -1)
{
if(!search(i))
{
replace(i);
pf++;
print(i);
}
else
print(i);
i++;
}
printf("\n\nPage faults : %d",pf);
}
int search(inti)
{
int j;
for(j=0;j<nf;j++)
{
if(frames[j] == pageno[i])
return 1;
}
return 0;
}
void replace(inti)
{
intj,k,max;
for(j=0;j<nf;j++)
{
if(frames[j] == -1)
{
frames[j] = pageno[i];
return 0;
}
}
for(j=0;j<nf;j++)
{
for(k=i+1;k<np;k++)
{
location[j] = np+1;
if(pageno[k] == frames[j])
{
location[j] = k;
break;
}
}
86
211719104033
}
max = 0;
for(j=0;j<nf;j++)
{
if(location[j] > location[max])
max = j;
}
frames[max] = pageno[i];
}
void print(inti)
{
int j;
printf("\n %d - ",pageno[i]);
for(j=0;j<nf;j++)
printf(" %2d",frames[j]);
}
OUTPUT:
RESULT:
Thus the C programs to implement Optimal page replacement algorithm was executed
successfully.
87
211719104033
AIM:
To implement Sequential file allocation Technique using C.
ALGORITHM:
Step 1: Start.
Step 2: Read the number of files.
Step 3:Input the filename.
Step 4:Open the file in read only mode and find start, end and length of the file.
Step 5: Display the name, start, end and length of the file.
Step 6: Stop.
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int main()
{
char buff[10][10],name[10][10];
intfp,n[20],start=0,num,i,temp;
printf("Enter the number of files:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\nEnter the name of the file %d:",i+1);
scanf("%s",&name[i]);
fp=open(name[i],O_RDONLY);
read(fp,buff[i],100);
n[i]=strlen(buff[i]);
printf("%d",n[i]);
}
printf("\nFilename\tStart\tEnd\tLength\n");
for(i=0;i<num;i++)
{
if(i==0)
temp=0;
else
temp=start+1;
start=start+n[i]-1;
if(i!=0)
88
211719104033
start++;
printf("\n%s\t\t%d\t%d\t%d\n",name[i],temp,start,n[i]);
}
}
OUTPUT:
RESULT:
Thus the C program to implement sequential file allocation was executed successfully.
89
211719104033
AIM:
To write a C program to implement File Allocation concept using the technique Linked List
Technique.
ALGORITHM:
Step 1: Start.
Step 2: Read the number of files
Step 3: For each file, read the file name, starting block, number of blocks and block numbers of
the file.
Step 4: Start from the starting block and link each block of the file to the next block in a linked list
fashion.
Step 5: Display the file name, starting block, size of the file , and the blocks occupied by the file.
Step 6: Stop.
PROGRAM:
#include<stdio.h>
void main()
{
char a[10];
inti,sb,eb,fb[10];
printf("Enter the file name:");
scanf("%s",a);
printf("Enter the starting block:");
scanf("%d",&sb);
printf("Enter the ending block:");
scanf("%d",&eb);
for(i=0;i<5;i++)
{
printf("Enter the free block %d: ",i+1);
scanf("%d",&fb[i]);
}
printf("\nfile name \tstarting block\tending block\n");
printf("%s\t\t%d\t\t%d",a,sb,eb);
printf("\n%sfileutilisation linked type of following blocks",a);
printf("\n%d->",sb);
for(i=0;i<5;i++)
printf("%d->",fb[i]);
}
90
211719104033
OUTPUT:
RESULT:
91
211719104033
Thus the C program to implement File Allocation concept using the linked
allocation technique was executed successfully and verified.
92
211719104033
AIM:
To write a C program to implement File Allocation concept using the technique
indexed allocation Technique.
ALGORITHM:
Step 1: Start.
Step 2: Obtain the required data through char and int datatypes.
Step 3: Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop.
PROGRAM:
#include<stdio.h>
void main()
{
char a[10];
inti,ib,cib[10];
printf("\nEnter the file name:");
scanf("%s",a);
printf("\nIndex block:");
scanf("%d",&ib);
for(i=1;i<=5;i++)
{
printf("enter the child of index block %d:",i);
scanf("%d",&cib[i]);
}
printf("\n the list of files\tindex block\n");
printf("%s\t\t%d",a,ib);
printf("\n The above file utilization index block of child blocks following\t");
printf("\n");
for(i=1;i<=5;i++)
printf("%d\t\t",cib[i]);
printf("\n");
}
93
211719104033
OUTPUT:
RESULT:
Thus the C program to implement File Allocation concept using the indexed allocation
technique was executed successfully and verified.
94
211719104033
AIM:
To write a C program to implement File Organization concept using the technique
Single level directory.
ALGORITHM:
Step 1: Start.
Step 2: Obtain the required data through char and intdatatypes
Step 3: Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
cleardevice();
setbkcolor(GREEN);
puts("enter no of files do u have?");
scanf("%d",&count);
for(i=0;i<count;i++)
{cleardevice();
setbkcolor(GREEN);
printf("enter file %d name",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 directory");
setcolor(BLUE);
for(j=0;j<=i;j++,cir_x+=mid)
95
211719104033
{line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[j]);}
getch();
}
}
OUTPUT:
RESULT:
Thus the C program to implement File Organization concept using the technique Single
level directory was executed successfully and verified
96
211719104033
AIM:
To write a C program to implement File Organization concept using the technique two
level directories.
ALGORITHM:
Step 1: Start.
Step 2: Obtain the required data through char and in datatypes.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop.
PROGRAM:
#include<stdio.h>
#include<graphics.h>
structtree_element
{
char name[20];
intx,y,ftype,lx,rx,nc,level;
structtree_element *link[5];
};
typedefstructtree_element node;
void main()
{
intgd=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,intlev,char *dname,intlx,intrx,int x)
{
inti,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
97
211719104033
98
211719104033
{
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:
RESULT:
Thus the C program to implement File Organization concept using the technique two
level directory was executed successfully and verified.
99
211719104033
AIM:
To write a C program to implement File Organization concept using the technique
hierarchical level directory.
ALGORITHM:
Step 1: Start.
Step 2: Obtain the required data through char and int datatypes
Step 3: Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop.
PROGRAM:
#include<stdio.h>
#include<graphics.h>
structtree_element
{
char name[20];
intx,y,ftype,lx,rx,nc,level;
structtree_element *link[5];
};
typedefstructtree_element node;
void main()
{
intgd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
}
create(node **root,intlev,char *dname,intlx,intrx,int x)
{
inti,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
100
211719104033
101
211719104033
}
}
OUTPUT:
RESULT:
Thus the C program to implement File Organization concept using the technique
hierarchical level directory was executed successfully and verified.
102
211719104033
AIM:
To write a C program to implement File Organization concept using the technique using. DAG
ALGORITHM:
Step 1: Start.
Step 2: Obtain the required data through char and int datatypes.
Step 3: Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
structtree_element
{
char name[20];
intx,y,ftype,lx,rx,nc,level;
structtree_element *link[5];
};
typedefstructtree_element node;
typedefstruct
{
char from[20];
char to[20];
}link;
link L[10];
intnofl;
node * root;
void main()
{
intgd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
103
211719104033
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
inti;
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)
{
inti;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;
}
else
104
211719104033
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}}}
create(node **root,intlev,char *dname,intlx,intrx,int x)
{
inti,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)
{
inti;
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);
105
211719104033
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{display(root->link[i]);
}
}
}
OUTPUT:
106
211719104033
RESULT:
Thus the C program to implement File Organization concept using the technique
using DAG was executed successfully and verified.
107