CS8461 - Oslabmanual - Process
CS8461 - Oslabmanual - Process
INDEX
Name of Lab In-charge:
Ex. Date of
Date Name of the Experiment Staff Sign
No. Submission
1
2A
2B
4A
4B
4C
4D
5A
5B
5C
5D
10
11
12
13A
13B
13C
14A
14B
14C
15A
15B
15C
16
LIST OF EXPERIMENTS
EXP. NO. NAME OF THE EXPERIMENTS
1 BASICS OF UNIX COMMANDS
2A SYSTEM CALLS IMPLEMENTATION: FORK, EXEC, GETPID, EXIT, WAIT, CLOSE
2B SYSTEM CALLS IMPLEMENTATION: STAT, OPENDIR, READDIR
3 SIMULATE UNIX COMMANDS LIKE CP, LS, GREP
4A SUM OF FIRST ‘N’ NUMBERS
4B ODD OR EVEN NUMBER
4C FACTORIAL OF A NUMBER
4D STRING PALINDROME
5A FCFS SCHEDULING
5B SJF SCHEDULING
5C PRIORITY SCHEDULING
5D ROUND ROBIN SCHEDULING
6 PRODUCER CONSUMER PROBLEM USING SEMAPHORES
7 IMPLEMENT SHARED MEMORY AND IPC
8 IMPLEMENT BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE
9 IMPLEMENT AN ALGORITHM FOR DEAD LOCK DETECTION
10 IMPLEMENT THREADING & SYNCHRONIZATION APPLICATIONS
11 IMPLEMENT MEMORY ALLOCATION METHODS FOR FIXED PARTITION
12 IMPLEMENT PAGING TECHNIQUE OF MEMORY MANAGEMENT
13A FIFO PAGE REPLACEMENT ALGORITHM
13B LRU PAGE REPLACEMENT ALGORITHM
13C OPTIMAL(LFU) PAGE REPLACEMENT ALGORITHM
14A SINGLE LEVEL DIRECTORY
14B TWO LEVEL DIRECTORY
14C HIERARCHICAL DIRECTORY
15A SEQUENTIAL FILE ALLOCATION
15B INDEXED FILE ALLOCATION
15C LINKED FILE ALLOCATION
16 ADDITIONAL EXPERIMENTS: READER WRITER PROBLEM
(A) PROGRAM OUTCOMES(POs)
PSO 1: Core Oriented - Proficient in the field of metalogic, hardware, software computing, to meet the
desired needs and to solve various problems.
PSO 2: Continuous improvement To sustain in transitional environment of computer science &
engineering to develop algorithms & projects using open source tools and efficient data structures
PSO 3: Intra – disciplinary - Apply Knowledge in different domains like data management science and
cognitive technologies.
PSO 4: Focusing growth - Exalt in innovative world by applying state –of-art methodologies with focus
on optimization and quality related activities.
COURSE OUTCOMES:
On completion of this lab course, the students will be able to
C217. Know and simulate UNIX commands for various operations, design and implement shell
1 programs, algorithms for CPU scheduling, file allocation and deadlock detection and avoidance
C217. Able to design and implement programs for inter process communication, shared memory, memory
2 allocation, paging techniques, threading and process synchronization
PO10
PO11
PO12
PSO
PSO
PSO
PSO
PO1
PO2
PO3
PO4
PO5
PO6
PO7
PO8
PO9
COs
4
C217.1 3 2 3 2 1 - - - 1 2 3 3 3 3 3 3
C217.2 3 2 3 2 2 - - - 2 3 3 3 3 3 3 3
Knowledge Course
S.No Experiment
level Outcomes
Cycle – 1
1.
R Basics of various UNIX commands. C217.1
2. Write programs using the following system calls of UNIX operating
U & Ap system fork, exec, getpid, exit, wait, close, stat, opendir, readdir
C217.1
3. Write C programs to simulate UNIX commands like cp, ls, grep,
U & Ap etc.
C217.1
4.
R & Ap Shell Programming. C217.1
5. Implement the following CPU scheduling algorithms
U & Ap C217.1
a)Round Robin b) SJF c) FCFS d) Priority
6.
U & Ap Implement Producer consumer problem with Semaphores C217.2
7. Implement Shared memory and IPC
U & Ap C217.2
Cycle – 2
8.
U & Ap Implement Bankers Algorithm for Dead Lock Avoidance. C217.1
9.
U & Ap Implement an Algorithm for Dead Lock Detection C217.1
10.
U & Ap Implement Threading & Synchronization Applications C217.2
11. Implementation of the following Memory Allocation Meth-
U & Ap C217.2
ods for fixed partition a) First Fit b) Worst Fit c) Best Fit
12.
U & Ap Implement Paging Technique of memory management. C217.2
13. Implement all page replacement algorithms
U & Ap C217.2
a) FIFO b) LRU c) LFU
14.
U & Ap Implement all File Organization Techniques C217.2
15. Implement all file allocation strategies
U & Ap C217.2
a) Sequential b) Indexed c)Linked
16.
U Additional Experiments: Reader Writer Problem C217.2
1. 2. 3.
1 2.
AIM:
To study and execute basics of UNIX commands.
DIRECTORY COMMANDS
1.Create a directory
roo@sys5:~/Desktop$ mkdir cpl
roo@sys5:~/Desktop$
4. Remove a directory
roo@sys5:~/Desktop$ rmdir NewFolder
roo@sys5:~/Desktop$
1. Create a file:
roo@sys5:~/Desktop$ cat>a.txt
Hi all
roo@sys5:~/Desktop$
roo@sys5:~/Desktop$ ls -l
total 12
-rw-rw-r-- 1 user1 user1 21 Oct 13 2016 a.txt
-rw-rw-r-- 1 user1 user1 4 Oct 13 2016 b.txt
roo@sys5:~/Desktop$
6. Copy a file:
roo@sys5:~/Desktop$ cp a.txt c.txt
[roo@sys5:~/Desktop$
8.Remove a file:
roo@sys5:~/Desktop$ rm d.txt
roo@sys5:~/Desktop$ ls
a.txt b.txt c.txt
roo@sys5:~/Desktop$
3. Who command:
Show who is logged on
roo@sys5:~/Desktop$ who
user1 pts/1 2016-10-13 14:02 (197.168.1.250)
roo@sys5:~/Desktop$
4.Who am I command:
Print the user name associated with the current effective user ID.
roo@sys5:~/Desktop$ whoami
sys5
roo@sys5:~/Desktop$
5. Head Command:
Displays the initial part of the text file. By default, it displays the first 10 lines of a file.
roo@sys5:~/Desktop$ head -n 1 a.txt
my name is user1
roo@sys5:~/Desktop$
6. Tail command:
Displays the last part of the text file. By default, IT displays the last 10 lines of a file.
roo@sys5:~/Desktop$ tail -n 1 a.txt
i am doing computer programming lab.
roo@sys5:~/Desktop$
7. Help command:
To display the help information about a particular command.
roo@sys5:~/Desktop$ man chmod
CHMOD(1) User Commands CHMOD(1)
NAME
chmod - change file access permissions
SYNOPSIS
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
DESCRIPTION
This manual page documents the GNU version of chmod. chmod changes the permissions of
each given file according to mode, which can be either a symbolic representation of changes to
make, or an octal number repre- senting the bit pattern for the new permissions.
8. echo command:
Display a line of text.
roo@sys5:~/Desktop$ echo "i am a good boy"
i am a good boy
roo@sys5:~/Desktop$
9. bc command
Basic calculator
roo@sys5:~/Desktop$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
45+34
79
23-13
10
Pipes: A mechanism in which the output of one command is redirected as input to another command.
St. Joseph’s Institute of Technology 8
C8461-Operating systems Laboratory Department of CSE 2018-2019
Grep filter:
It is used to search for a particular pattern from a file or from std. input.
roo@sys5:~/Desktop$ grep name a.txt
my name is user1
roo@sys5:~/Desktop$ grep am a.txt
my name is user1
i am doing computer programming lab.
roo@sys5:~/Desktop$
roo@sys5:~/Desktop$ grep -c am a.txt
2
roo@sys5:~/Desktop$
egrep command:
It is an extension for grep command Multiple Patterns can be searched in a file by issuing a
single command. These multiple patterns should be separated by a pipe ( | ) symbol.
roo@sys5:~/Desktop$ egrep "computer|is" a.txt
my name is user1
i am doing computer programming lab.roo@sys5:~/Desktop$
Viva Questions:
1. What is unix?
2. List some names of operating systems
AIM:
To write a program to create the hierarchy of Process using system calls of UNIX operating
system fork, exec, getpid, exit, wait, close.
ALGORITHM:
1. Create a process using fork() system call.
2. Create a hierarchy of the process using child address space.
3. Get the each process ID and Parent process ID using getpid() and getppid().
4. Terminate the process after the child process terminate using wait() system call
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int p1,p2,p3,p4,p5,p7,s1,s2,s3;
p1=fork();
if(p1== -1)
exit(1);
if(p1==0)
{
p2=fork();
if(p2== -1)
exit(1);
if(p2==0)
{
printf("I'M P4.\n");
printf("PROCESS ID OF P4 IS:%d\n",getpid());
printf("PARENT PROCESS ID OF P4 IS:%d\n",getppid());
}
else
{
p3=fork();
if(p3==-1)
exit(1);
if(p3==0)
{
printf("I'M P5.\n");
printf("PROCESS ID OF P5 IS:%d\n",getpid());
printf("PARENT PROCESS ID OF P5 IS:%d\n",getppid());
}
else
St. Joseph’s Institute of Technology 10
C8461-Operating systems Laboratory Department of CSE 2018-2019
{
wait(&s2);
printf("I'M P2.\n");
printf("PROCESS ID OF P2 IS:%d\n",getpid());
printf("PARENT PROCESS ID OF P2 IS:%d\n",getppid());
}
}
}
else
{
p4=fork();
if(p4==-1)
exit(1);
if(p4==0)
{
p5=fork();
if(p5==-1)
exit(1);
if(p5==0)
{
printf("I'M P6.\n");
printf("PROCESS ID OF P6 IS:%d\n",getpid());
printf("PARENT PROCESS ID OF P6 IS:%d\n",getppid());
}
else
{
p7=fork();
if(p7==-1)
exit(1);
if(p7==0)
{
printf("I'M P7.\n");
printf("PROCESS ID OF P7 IS:%d\n",getpid());
printf("PARENT PROCESS ID OF P7 IS:%d\n",getppid());
}
else
{
wait(&s3);
printf("I'M P3.\n");
printf("PROCESS ID OF P3 IS:%d\n",getpid());
printf("PARENT PROCESS ID OF P3 IS:%d\n",getppid());
}
}
}
else
{
wait(&s1);
printf("PROCESS ID OF P1 IS:%d\n",getpid());
}
}
}
OUTPUT:
[m1@cselab14 18-19]$ cc 2a.c
[m1@cselab14 18-19]$ ./a.out
I'M P4.
PROCESS ID OF P4 IS:7239
I'M P6.
PARENT PROCESS ID OF P4 IS:7237
PROCESS ID OF P6 IS:7240
PARENT PROCESS ID OF P6 IS:7238
I'M P5.
I'M P7.
PROCESS ID OF P5 IS:7241
PROCESS ID OF P7 IS:7242
PARENT PROCESS ID OF P5 IS:7237
I'M P2.
PARENT PROCESS ID OF P7 IS:7238
I'M P3.
PROCESS ID OF P2 IS:7237
PARENT PROCESS ID OF P2 IS:7236
PROCESS ID OF P3 IS:7238
PARENT PROCESS ID OF P3 IS:7236
PROCESS ID OF P1 IS:7236
RESULT: Thus the program to create the hierarchy of Process using system calls of UNIX
operating system fork, exec, getpid, exit, wait, close was written, executed and the output was
verified.
AIM:
To write a program using system calls of UNIX operating system stat, opendir, readdir.
ALGORITHM:
1. Read the arguments from command line
2. Check the command line arguments are valid or not
3. If valid, open the directory using opendir system call
4. Display the files of the opened directory using readdir system call
5. If the files are a directory open this directory and read their files and display.
PROGRAM:
#include <sys/types.h>
#include <dirent.h>
#include<stdio.h>
#include<sys/stat.h>
#include <unistd.h>
#include<pwd.h>
#include<grp.h>
#include<stdio.h>
#include<ctype.h>
#include <linux/unistd.h>
DIR *p;
struct dirent *p1;
struct stat buf;
p=opendir(".");
if(argv==1)
{
p1=readdir(p);
while(p1!=NULL)
{
if(strcmp(p1->d_name,".")==0 || strcmp(p1->d_name,"..")==0)
{
p1=readdir(p);
continue;
}
printf("%s\t",p1->d_name);
p1=readdir(p);
}
printf("\n");
}
else if(argv==2 && strcmp(cmdline[1],"-R")==0)
{
dir=opendir(".");
dirp=readdir(dir);
while(dirp!=NULL)
{
strcpy(dname,dirp->d_name);
printf("\t");
printf("%s\n",dirp->d_name);
dirp=readdir(dir);
}
}
}
OUTPUT:
[m1@cselab14 18-19]$ cc 2b.c
[m1@cselab14 18-19]$ ./a.out
cs6341_oslabmanual.docx CS8461_oslabmanual - Process.docx CS8461_oslabmanual.docx
CS8461_oslabmanual.pdf desktop.ini OS Ex1 & 2.docx os copy.c ls.c
2b.c grep.c Output 2 3~ 2a.c Output 2 3 a.out
RESULT: Thus the program using system calls of UNIX operating system stat, opendir, readdir was
written, executed and the output was verified.
Viva questions:
1. Describe the term directory in UNIX?
2. Describe fork() system call?
St. Joseph’s Institute of Technology 13
C8461-Operating systems Laboratory Department of CSE 2018-2019
AIM:
To Write C programs to simulate UNIX commands like cp, ls, grep, etc.
ALGORITHM:
1. Start the program.
2. Get the command as the command line arguments and check for the syntax of the command.
3. Check for the existence of the directory/file mentioned in the command.
4. Open the directory/file if exists.
5. Perform the necessary operation based on the command.
6. Close the directory/file.
PROGRAM:
//To cp command
#include<sys/stat.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<fcntl.h>
main(int argc,char **argv)
{
int f1,f2;
ssize_t s1,s2;
size_t count=sizeof(char);
char c;
f1=open(argv[1],O_RDONLY);
f2=open(argv[2],O_WRONLY|O_CREAT);
if(f1==-1)
printf("%s File does not exist\n",argv[1]);
else
{
while(s1!=0)
{
s2=write(f2,&c,count);
s1=read(f1,&c,count);
}
close(f1);
close(f2);
}
}
OUTPUT:
PROGRAM:
//To simulate ls command
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<unistd.h>
#include <linux/unistd.h>
OUTPUT:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
int m=strlen(pat);
if(m>n) return 0;
for(i=0;i<n-m;i++)
{
j=0;
while(j<m && buf[i+j]==pat[j])
j++;
if(j==m) flag=1;
}
if(flag==1)
return 1;
else
return 0;
}
OUTPUT:
RESULT: Thus the C programs to simulate UNIX commands like cp, ls, grep was written,
executed and the output was verified.
Viva questions:
St. Joseph’s Institute of Technology 16
C8461-Operating systems Laboratory Department of CSE 2018-2019
AIM:
To write a shell program to find the sum of n numbers.
ALGORITHM:
1. Get the number up to which the sum has to be calculated.
2. Initialize sum=0 and starting number i =1
3. While i less than or equal to n, do the following :
sum=sum+i
PROGRAM:
echo "SUM OF FIRST N NATURAL NUMBERS"
echo "ENTER THE LIMT N:"
read n
i=1
sum=0
while((i<=n))
do
((sum=sum+i))
((i=i+1))
done
echo "Sum: $sum"
OUTPUT:
roo@sys5:~/Desktop$ sh pgm2a.sh
SUM OF FIRST N NATURAL NUMBERS
ENTER THE LIMIT 5
SUM: 15
RESULT: Thus the shell program for calculating sum of N numbers was written, executed
and the output was verified.
Exp No: 4B ODD OR EVEN NUMBER
Date:
AIM:
To write a shell program to check whether the given number is odd or even.
ALGORITHM:
Step1: Get the number.
Step2: Divide the number by 2.
Step3: If the remainder is equal to 0, then the number is even.
Step4: Else, the number is odd.
Step5: Stop.
PROGRAM:
echo "ODD OR EVEN"
echo "ENTER A NUMBER:"
read n
if((n%2==0))
then
echo "EVEN"
else
echo "ODD"
fi
OUTPUT:
roo@sys5:~/Desktop$ sh pgm2a.sh
ODD OR EVEN-ENTER A NUMBER:
2
EVEN
roo@sys5:~/Desktop$ sh pgm2a.sh
ODD OR EVEN
ENTER A NUMBER:15 ODD
RESULT: Thus the shell program to check whether the given number is odd or even was
written, executed and the output was verified.
Ex.No.4C FACTORIAL OF A NUMBER
Date
AIM:
To write a shell program to find the factorial of a number.
ALGORITHM:
1. Get the number for which factorial has to be calculated as n.
2. Initialize fact =1
3. While n less than 0, do the following :
fact=fact*n
n=n-1
PROGRAM:
echo "Enter the number"
read n
((fact=1))
while (($n > 0))
do
((fact=fact*n))
((n=n-1))
done
echo "The factorial of the number is:$fact"
OUTPUT:
Enter the number
6
The factorial of the number is:720
RESULT: Thus the shell program for finding the factorial of a number was written, executed
and the output was verified.
Ex.No.4D STRING PALINDROME
Date:
AIM:
To write a shell program to check whether the given string is a palindrome or not.
ALGORITHM:
1. Input string
2. Calculate length of string .
3. Run a loop i=0 to len/2 of string
4. Compare ith character with len th character
5. If character doesn’t matches within loop then exit saying that it’s not a palindrome
6. If loop is successfully executed print that string is palindrome.
PROGRAM:
echo "enter a string"
read str
rev=`expr $str|rev`
if [ $rev = $str ]
then
echo "the given string is palindrome"
else
echo "the given string is not palindrome"
fi
OUTPUT:
enter a string
Madam
the given string is palindrome
RESULT: Thus the shell program for checking whether the given string is palindrome or not
was written, executed and the output was verified.
Viva Questions:
1. What is meant by unix shell?
2. What is a shell script?
3. What are the operations performed via shell scripts?
4. What are the advantages and disadvantages of shell scripts?
5. Write syntax for “for loop” in shell script
ALGORITHM:
Step 1: Include the header files for simulating FCFS scheme.
Step 2: Declare the variables to calculate the essential aspects of FCFS.
Step 3: Initially get the process time, arrival time and burst time.
Step 4: Swap all the processes in such a way that they are arranged in FCFS order.
Step 5: Prepare the gantt chart for each process so that their waiting time, turn around time is
calculated.
Step 6: Calculate the average waiting time and average turn around time and display the result.
PROGRAM:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,n,br[10],wt[10],tr[10],ttr=0,twt=0;
float awat,atur;
printf("Enter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for process %d: ",(i+1));
scanf("%d",&br[i]);
}
printf("\nPROCESS\tBURST TIME\tWAITING TIME\tTURN AROUND TIME\n");
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+br[i];
tr[i]=wt[i]+br[i];
printf("\n%d\t\t%d\t\t%d\t\t%d\n",i+1,br[i],wt[i],tr[i]);
}
for(i=0;i<n;i++)
{
ttr=ttr+tr[i];
twt=twt+wt[i];
}
printf("\nGAANT CHART\n\n");
for(i=0;i<n;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<n;i++)
St. Joseph’s Institute of Technology 21
C8461-Operating systems Laboratory Department of CSE 2018-2019
{
printf(" P%d |",i+1);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("%d\t",wt[i]);
}
awat=(float)twt/n;
atur=(float)ttr/n;
printf("\n\nTotal waiting time :%d",twt);
printf("\nTotal turn around time :%d",ttr);
printf("\n\nAverage waiting time :%.2f",awat);
printf("\nAverage turn around time :%.2f\n",atur);
}
OUTPUT
[admin@csk Desktop]$ cc 3a.c
[admin@csk Desktop]$ ./a.out
Enter no. of process :3
Enter the burst time for process 1: 4
Enter the burst time for process 2: 2
Enter the burst time for process 3: 1
PROCESS BURST TIME WAITING TIME TURN AROUND TIME
1 4 0 4
2 2 4 6
3 1 6 7
GAANT CHART
------------------------
P1 | P2 | P3 |
------------------------
0 4 6 7
Total waiting time :10
Total turn around time :17
Average waiting time :3.33
Average turn around time :5.67
RESULT: Thus the FCFS scheduling algorithm was written, executed and the output was verified.
ALGORITHM:
Step 1: Include the header files for simulating SJF scheme.
Step 2: Declare the variables to calculate the essential aspects of SJF.
Step 3: Initially get the process time and burst time.
Step 4: Swap all the processes in such a way that they are arranged in shortest job first (SJF) order.
Step 5: Prepare the gantt chart for each process so that their waiting time, turn around time is
calculated.
Step 6: Calculate the average waiting time and average turn around time and display the result.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
main()
{
int i,j,k,n,p[10],br[10],wt[10],tr[10],ttr=0,twt=0,t;
float awat,atur;
printf("\nEnter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter the burst time for process %d: ",(i+1));
scanf("%d",&br[i]);
p[i]=i+1;}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(br[i]>br[j]){
t=br[i];
br[i]=br[j];
br[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;}}}
printf("\nPROCESS\tBURST TIME \t WAITING TIME \t TURN AROUND TIME\n");
wt[0]=0;
for(i=0;i<n;i++){
wt[i+1]=wt[i]+br[i];
tr[i]=wt[i]+br[i];}
for(i=0;i<n;i++){
ttr=ttr+tr[i];
twt=twt+wt[i];}
for(i=0;i<n;i++){
printf("\n%d\t\t%d\t\t%d\t\t%d\n",p[i],br[i],wt[i],tr[i]);}
printf("\nGAANT CHART\n\n");
for(i=0;i<n;i++){
printf("--------");}
printf("\n");
for(i=0;i<n;i++)
St. Joseph’s Institute of Technology 23
C8461-Operating systems Laboratory Department of CSE 2018-2019
{
printf(" P%d |",p[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("%d\t",wt[i]);
}
awat=(float)twt/n;
atur=(float)ttr/n;
printf("\nTotal waiting time :%d\n",twt);
printf("\nTotal turnaround time :%d\n",ttr);
printf("\nAverage waiting time :%.2f\n",awat);
printf("\nAverage turn around time :%.2f\n",atur);
}
OUTPUT
[admin@csk Desktop]$ cc 3b.c
[admin@csk Desktop]$ ./a.out
Enter no. of process :3
Enter the burst time for process 1: 6
Enter the burst time for process 2: 3
Enter the burst time for process 3: 2
PROCESS BURST TIME WAITING TIME TURN AROUND TIME
3 2 0 2
2 3 2 5
1 6 5 11
GAANT CHART
------------------------
P3 | P2 | P1 |
------------------------
0 2 5 11
Total waiting time :7
Total turnaround time :18
Average waiting time :2.33
Average turn around time :6.00
[admin@csk Desktop]$
RESULT: Thus the C program for SJF scheduling was successfully executed and the output
was verified.
ALGORITHM:
Step 1: Include the header files for simulating priority scheme.
Step 2: Declare the variables to calculate the essential aspects of priority scheme.
Step 3: Initially get the process time, arrival time and burst time along with their priorities.
Step 4: Execute the process by referring to their priority values.
Step 5: Prepare the gantt chart for each process so that their waiting time, turn around time is
calculated.
Step 6: Calculate the average waiting time and average turn around time and display the result.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
main(){
int i,j,k,n,p[10],pri[10],bur[10],wat[10], tur[10],ttur=0,twat=0,t;
float awat,atur;
printf("\nEnter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter the burst time for process %d: ",(i+1));
scanf("%d",&bur[i]);
p[i]=i+1;}
for(i=0;i<n;i++){
printf("\nEnter the priority for process %d: ",(i+1));
scanf("%d",&pri[i]);}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(pri[i]>pri[j]){
t=bur[i];
bur[i]=bur[j];
bur[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
t=pri[i];
pri[i]=pri[j];
pri[j]=t;}}}
printf("\nPROCESS\tBURST TIME \t PRIORITY \t WAITING TIME \tTURNAROUND TIME\n");
wat[0]=0;
for(i=0;i<n;i++){
wat[i+1]=wat[i]+bur[i];
tur[i]=wat[i]+bur[i];}
for(i=0;i<n;i++){
ttur=ttur+tur[i];
twat=twat+wat[i];}
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\n",p[i],bur[i],pri[i],wat[i],tur[i]);
printf("\nGAANT CHART\n\n");
St. Joseph’s Institute of Technology 25
C8461-Operating systems Laboratory Department of CSE 2018-2019
for(i=0;i<n;i++)
printf("--------");
printf("\n");
for(i=0;i<n;i++)
printf(" P%d |",p[i]);
printf("\n");
for(i=0;i<n;i++)
printf("--------");
printf("\n");
for(i=0;i<=n;i++)
printf("%d\t",wat[i]);
awat=(float)twat/n;
atur=(float)ttur/n;
printf("\nTotal waiting time :%d\n",twat);
printf("\nTotal turnaround time :%d\n",ttur);
printf("\nAverage waiting time :%.2f\n",awat);
printf("\nAverage turn around time :%.2f\n",atur);
}
OUTPUT
[admin@csk Desktop]$ cc 3c.c
[admin@csk Desktop]$ ./a.out
Enter no. of process :3
Enter the burst time for process 1: 3
Enter the burst time for process 2: 1
Enter the burst time for process 3: 4
Enter the priority for process 1: 3
Enter the priority for process 2: 2
Enter the priority for process 3: 1
PROCESS BURST TIME PRIORITY WAITING TIME TURNAROUND TIME
3 4 1 0 4
2 1 2 4 5
1 3 3 5 8
GAANT CHART
------------------------
P3 | P2 | P1 |
------------------------
0 4 5 8
Total waiting time :9
Total turnaround time :17
Average waiting time :3.00
Average turn around time :5.67
RESULT:
Thus the C program for Priority scheduling was executed successfully.
Ex No: 5D IMPLEMENTATION OF ROUND ROBIN SCHEDULING
Date:
AIM:
To write a ‘C’ program to implement ROUND ROBIN Scheduling.
ALGORITHM:
Step 1: Include the header files for simulating ROUND ROBIN scheduling scheme.
Step 2: Declare the variables to calculate the essential aspects of ROUND ROBIN.
Step 3: Initially get the time slice along with process time and burst time.
Step 4: Start from the 1st process and execute it for the given time slice and move to 2 nd process and
so on till all process gets executed for each of the time slice allotted.
Step 5: Prepare the gantt chart for each process getting executed within the time slice in a round robin
fashion.
Step 6: Calculate the waiting time and turn around time along with their averages display the result.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
main()
{
int i,x=-1,k[10],m=0,n,t,s=0;
int a[50],temp,b[50],p[10],bur[10],bur1[10];
int wat[10],tur[10],ttur=0,twat=0,tres=0,j=0;
float awat,atur,ares;
printf("Enter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the burst time for process %d: ",(i+1));
scanf("%d",&bur[i]);
bur1[i]=bur[i];}
printf("\nEnter the slicing time :");
scanf("%d",&t);
for(i=0;i<n;i++){
b[i]=bur[i]/t;
if((bur[i]%t)!=0)
b[i]=b[i]+1;
m=b[i]+m;}
printf("\nGAANT CHART\n\n");
for(i=0;i<m;i++)
printf("--------");
printf("\n");
a[0]=0;
while(j<m){
if(x==n-1)
x=0;
else
x++;
if(bur[x]>=t){
bur[x]=bur[x]-t;
a[j+1]=a[j]+t;
if(b[x]==1){
St. Joseph’s Institute of Technology 27
C8461-Operating systems Laboratory Department of CSE 2018-2019
p[s]=x;
k[s]=a[j+1];
s++;}
j++;
b[x]=b[x]-1;
printf(" P%d |",x+1);}
else if(bur[x]!=0){
a[j+1]=a[j]+bur[x];
bur[x]=0;
if(b[x]==1){
p[s]=x;
k[s]=a[j+1];
s++;}
j++;
b[x]=b[x]-1;
printf(" P%d |",x+1);}}
printf("\n");
for(i=0;i<m;i++)
printf("--------");
printf("\n");
for(j=0;j<=m;j++)
printf("%d\t",a[j]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(p[i]>p[j]){
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=k[i];
k[i]=k[j];
k[j]=temp;}}}
for(i=0;i<n;i++)
{
wat[i]=k[i]-bur1[i];
tur[i]=k[i];
}
printf("\nPROCESS\tBURST TIME \t WAITING TIME \t TURN AROUND TIME\t");
printf("RESPONSE TIME\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\n",p[i]+1,bur1[i],wat[i],
tur[i],a[i]);
}
for(i=0;i<n;i++)
{
ttur=ttur+tur[i];
twat=twat+wat[i];
tres=tres+a[i];
}
awat=(float)twat/n;
atur=(float)ttur/n;
ares=(float)tres/n;
printf("\nTotal waiting time:%d",twat);
printf("\nTotal turnaround time:%d",ttur);
printf("\nTotal response time:%d",tres);
printf("\nAverage waiting time :%.2f",awat);
printf("\nAverage turn around time :%.2f",atur);
printf("\nAverage response time:%.2f",ares);
St. Joseph’s Institute of Technology 28
C8461-Operating systems Laboratory Department of CSE 2018-2019
OUTPUT
[admin@csk Desktop]$ cc 3d.c
[admin@csk Desktop]$ ./a.out
Enter no. of process :3
Enter the burst time for process 1: 4
Enter the burst time for process 2: 3
Enter the burst time for process 3: 2
Enter the slicing time :2
GAANT CHART
----------------------------------------
P1 | P2 | P3 | P1 | P2 |
----------------------------------------
0 2 4 6 8 9
PROCESS BURST TIME WAITING TIME TURN AROUND TIME RESPONSE
TIME
1 4 4 8 0
2 3 6 9 2
3 2 4 6 4
Total waiting time:14
Total turnaround time:23
Total response time:6
Average waiting time :4.67
Average turn around time :7.67
RESULT:
Thus the C program for round robin scheduling was executed and output was verified
successfully.
Viva Questions:
St. Joseph’s Institute of Technology 29
C8461-Operating systems Laboratory Department of CSE 2018-2019
1. Define process.
2. What are the other scheduling algorithms?
3. Advantage of Round Robin scheduling?
4. What is meant by response time and slice time?
5. What is meant by burst time, waiting time and turn around time?
Ex No: 6 PRODUCER CONSUMER PROBLEM USING SEMAPHORES
Date:
AIM:
To write a program to implement producer consumer problem using semaphore
ALGORITHM:
Step 1: Declare the buffer size variable.
Step 2: Initialize empty, full and the value of mutex.
Step 3: For case 1 , produce the item till the buffer is full after performing the wait operation
of variables empty and mutex.
Step 4: Then perform signal of full.
Step 5: For case 2, consume the item till the buffer is empty. Perform the wait and signal operation..
Step 6: print the contents of the buffer.
PROGRAM:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define SIZE 3
struct process{
char a[10];
}buffer[SIZE];
int mutex=1,full=0,empty=SIZE,f=0;
int main()
{
int ch,i;
printf("\tProducer Consumer Problem:\n");
while(1){
printf("\n The choices are\n");
printf("1.Producer Routine\n2.Consumer Routine\n3.Buffer Contents\n4.Exit\n");
printf("Enter your Choice:");
scanf("%d",&ch);
switch(ch){
case 1:
empty=wait(empty);
mutex=wait(mutex);
if(f==0){
printf("\nEnter the item to be added:\n");
scanf("%s",&buffer[full]);
full=signal(full);printf("\nItem Produced Successfully:\n");}
else{
printf("\nBuffer is full\n");
f=0;}
mutex=signal(mutex);
break;
case 2:
full=wait(full);
mutex=wait(mutex);
if(f==0){
printf("\nOne Item is Consumed:\n");
St. Joseph’s Institute of Technology 30
C8461-Operating systems Laboratory Department of CSE 2018-2019
OUTPUT
[admin@csk Desktop]$ cc 5.c
[admin@csk Desktop]$ ./a.out
Producer Consumer Problem:
The choices are
1.Producer Routine
2.Consumer Routine
3.Buffer Contents
4.Exit
Enter your Choice:1
Enter the item to be added:
item1
Item Produced Successfully:
The choices are
1.Producer Routine
2.Consumer Routine
3.Buffer Contents
4.Exit
Enter your Choice:1
Enter the item to be added:
item2
Item Produced Successfully:
1.Producer Routine
2.Consumer Routine
3.Buffer Contents
4.Exit
Enter your Choice:1
Enter the item to be added:
item3
Item Produced Successfully:
RESULT: Thus the C program for producer consumer problem using semaphores was
executed successfully and the output was verified.
Viva Questions:
1. What is semaphore?
2. Define synchronization. What is the purpose of synchronization?
3. What are the types of semaphores?
4. What are the operations on the semaphore?
5. Define busy waiting, spin lock.
AIM:
To write a program to implement inter process communication and shared memory to print
alphabets from a to z.
ALGORITHM:
Step 1: Create and shared memory using shmget.
Step 2: Attach pointer to a memory using shmat.
Step 3: Create child process to write data to memory.
Step 4: Print the shared memory data using server process.
Step 5: Stop the execution.
PROGRAM
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define SHMSZ 27
main()
{
char c;
int shmid,childpid;
key_t key;
char *shm, *s;
key = 5679;
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);}
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);}
s = shm;
// error occured
if((childpid = fork()) < 0) {
perror("Fork Failed");
exit(1);}
// child process
else if(childpid == 0) {
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s='\0';}
else{
for (s=shm;*s!='\0';s++)
putchar(*s);
putchar('\n');}
exit(0);
}
OUTPUT
[admin@csk Desktop]$ cc 10a.c
[admin@csk Desktop]$ ./a.out
abcdefghijklmnopqrstuvwxyz
[admin@csk Desktop]$ ipcs
RESULT: Thus the C program to implement inter process communication and shared memory to
print alphabets from a to z was executed successfully and the output was verified.
Viva Questions:
1. What is IPC?
2. What the methods to implement IPC?
3. What is shared memory?
4. What is message passing?
5. What is RPC?
DATA STRUCTURES
n-Number of process, m-number of resource types.
St. Joseph’s Institute of Technology 35
C8461-Operating systems Laboratory Department of CSE 2018-2019
PROGRAM:
#include<stdio.h>
void main()
{
int i, work[5],avl[5],alloc[10][10],l;
int need[10][10],n,m,I,j,avail[10],max[10][10],k,count,fcount=0,pr[10];
char finish[10]={'f','f','f','f','f','f','f','f','f','f'};
printf("enter the no of process");
scanf("%d",&n);
printf("enter the no of resources");
scanf("%d",&m);
printf("enter the total no of resources");
for(i=1;i<=m;i++)
scanf("%d",&avail[i]);
printf("enter the max resources req by each pr alloc matrix");
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&max[i][j]);
printf("process allocation matrix");
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&alloc[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
need[i][j]=max[i][j]-alloc[i][j];
for(i=1;i<=n;i++)
{
k=0;
for(j=1;j<=m;j++)
{
k=k+alloc[i][j];
St. Joseph’s Institute of Technology 36
C8461-Operating systems Laboratory Department of CSE 2018-2019
}
avl[i]=avl[i]-k;
work[i]=avl[i];
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
{
count=0;
for(j=1;j<=m;j++)
{
if((finish[i]=='f')&&(need[i][j]<=work[i]))
count++;
}
if(count==m)
{
for(l=1;l<=m;l++)
work[l]=work[l]+alloc[i][l];
finish[i]='t';
pr[k]=i;
break;
}
}
for(i=1;i<=n;i++)
if(finish[i]=='t')
fcount++;
if(fcount==n)
{
printf("the system is in safe state");
for(i=1;i<=n;i++)
printf("%d",pr[i]);
}
else
printf("the system is not in safe state");
}
OUTPUT
roo@sys5:~/Desktop$ cc bankers.c
roo@sys5:~/Desktop$ ./a.out
Enter the number of process: 5
Enter the number of resources: 3
Enter the total number of resourced: 10 5 7
Enter the max resources required by each pr alloc matrix
753
322
902
222
433
Process allocation matrix
010
200
302
211
002
The system is in safe state 1 2 3 4 5
RESULT: Thus the C program for bankers algorithm was successfully executed and the
output was verified.
Viva Questions:
1. Explain Bankers algorithm?
2. What is meant by deadlock?
3. What is dead lock avoidance?
4. What are the dead lock avoidance methods?
5. How can we prevent deadlocks?
6. What is meant by starvation?
7. When a system is said to be in safe state?
ALGORITHM:
1. Start the program.
2. Get the values of resources and processes.
3. Get the allocation and request matrix value.
4. Get the available instances for each resources
5. Check whether its possible to allocate using below condition.
6. If Need<=Work update work as work=work +allocation
7. Else proceed with next process.
8. If it is possible to allocate then the system is in safe state.
9. Else system is not in safe state.
10. Stop the program.
PROGRAM:
#include <stdio.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]);
St. Joseph’s Institute of Technology 38
C8461-Operating systems Laboratory Department of CSE 2018-2019
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
}
OUTPUT
roo@sys5:~/Desktop$ cc dead.c
roo@sys5:~/Desktop$ ./a.out
Enter total no. of processes : 4
Enter total no. of resources : 5
Enter claim (Max. Need) matrix :
01001
00101
00001
10101
Enter allocation matrix :
10110
11000
00010
00000
Enter resource vector (Total resources) :
21121
Enter availability vector (available resources) :
00001
deadlock causing processes are : 2 3
RESULT: Thus the C program for deadlock detection algorithm was implemented, executed and the
output was verified successfully.
Viva Questions:
1. What is deadlock, starvation? Explain their difference.
2. How can we detect deadlocks?
3. What is deadlock prevention? Explain its methods.
4. What is meant by finite state?
5. What is resource allocation graph
AIM:
To write a program to implement the application of threading and synchronization.
ALGORITHM:
1. Start the process
2. Initialize mutex lock ,thread id and required other variables
3. Create POSIX threads using pthread_create
4. Define thread task by creating new function.
5. Synchronize threads using mutex lock and unlock variables
6. Using pthread_join allow threads to wait until parent process completes
7. Destroy the synchronization variable mutex using pthread_mutex_destroy
8. Stop the process
PROGRAM
//Threading Application Program
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* ThreadFunction(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &ThreadFunction, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(5);
return 0;
}
OUTPUT
[admin@csk Desktop]$ cc 12thread.c -o 12thread -pthread
[admin@csk Desktop]$ ./12thread
Thread created successfully
First thread processing
Thread created successfully
Second thread processing
[admin@csk Desktop]$
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
St. Joseph’s Institute of Technology 41
C8461-Operating systems Laboratory Department of CSE 2018-2019
OUTPUT
[admin@csk Desktop]$ cc 12sync.c -o 12sync -pthread
[admin@csk Desktop]$ ./12sync
Job 1 started
Job 1 finished
Job 2 started
Job 2 finished
[admin@csk Desktop]$
Viva Questions:
1. Define threads?
2. List the types of threads
3. What is multithreading?
4. Define synchronization.
5. How threads can be used for synchronization?
AIM:
To write a program to implementation of the following Memory Allocation Methods for fixed
partition a) First Fit b) Worst Fit c) Best Fit
ALGORITHM:
1. Input memory blocks with size and processes with size.
2. Initialize all memory blocks as free.
3. For First fit memory allocation, Start by picking each process and check if it can be assigned to
current block. If size-of-process <= size-of-block if yes then assign and check for next process. If
not then keep checking the further blocks.
4. For Best fit memory allocation, Start by picking each process and find the minimum block size
that can be assigned to current process i.e., find min(bockSize[1], blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign it to the current process.If not then leave that process
and keep checking the further processes.
5. For Best fit memory allocation,Start by picking each process and find the maximum block size
that can be assigned to current process i.e., find max(bockSize[1],
6. blockSize[2],.....blockSize[n]) > processSize[current], if found then assign it to the current
process. If not then leave that process and keep checking the further processes.
PROGRAM
#include<stdio.h>
main()
{
int i,j,n,l,tb[5],f,t,k=0;
printf("\nEnter the Partitions");
scanf("%d",&n);
printf("\nEnter the no of Processes");
scanf("%d",&l);
int a[n],b[l],par[n],bal[n],ps[l];
printf("\nEnter the space of each partition:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the space required for each processes");
for(i=0;i<l;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
par[i]=a[i];
for(i=0;i<n;i++)
ps[i]=b[i];
for(i=0;i<n;i++)
tb[i]=0;
printf("\nFirst Fit Algorithm");
loop1:
for(i=0;i<n;i++)
bal[i]=0;
i=0;
j=0;
f=0;
printf("\npartition\tprocess\n");
while(j<l)
{
St. Joseph’s Institute of Technology 43
C8461-Operating systems Laboratory Department of CSE 2018-2019
}
OUTPUT:
[admin@csk Desktop]$ cc memory.c
[admin@csk Desktop]$ ./a.out
500 212
600 417
200 112
Memory Unused=959
partition process
300 212
500 417
200 112
600 426
Memory Unused=533
partition process
600 212
500 417
300 112
Memory Unused=959
RESULT: Thus the C program to implementation of the following Memory Allocation Methods
for fixed partition was implemented successfully and the output was verified.
Viva Questions:
1. What do you mean by best fit?
2. What do you mean by first fit?
3. Define secondary memory.
St. Joseph’s Institute of Technology 45
C8461-Operating systems Laboratory Department of CSE 2018-2019
4. Define swapping.
5. What do you mean by worst fit?
AIM:
To implement the Memory management using Paging technique.
ALGORITHM:
1. Read all the necessary input from the keyboard.
2. Pages - Logical memory is broken into fixed - sized blocks.
3. Frames – Physical memory is broken into fixed – sized blocks.
4. Calculate the physical address using the following
1. Physical address = ( Frame number * Frame size ) + offset
5. Display the physical address.
6. Stop the process.
PROGRAM
#include<stdio.h>
#define MAX 50
int main()
{
int page[MAX],i,n,f,ps,off,pno;
clrscr();
printf("\nEnter the no of pages in memory");
scanf("%d",&n);
printf("\nEnter page size");
scanf("%d",&ps);
printf("\nEnter no of frames");
scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any frame)\n\n");
printf("\npageno\tframeno\n-------\t-------");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i);
scanf("%d",&page[i]);
}
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off);
getch();
return 1;
OUTPUT:
[admin@csk Desktop]$ cc 11.c
[admin@csk Desktop]$ ./a.out
Enter the no of pages in memory 4
Enter page size 3
Enter no of frames 4
Enter the page table
(Enter frame no as -1 if that page is not present in any frame)
pageno frameno
------- -------
0 10
1 11
2 12
3 13
RESULT: Thus the C program for paging technique was implemented successfully and the
output was verified.
Viva Questions:
1. Explain paging technique.
2. Define pages, frames, offset in paging technique.
3. What is meant by logical and physical address?
4. What is thrashing?
5. What is meant by page hit, page fault?
AIM:
To implement First in First Out page replacement technique.
ALGORITHM:
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
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;
St. Joseph’s Institute of Technology 48
C8461-Operating systems Laboratory Department of CSE 2018-2019
OUTPUT:
[admin@csk Desktop]$ vi 9a.c
[admin@csk Desktop]$ cc 9a.c
[admin@csk Desktop]$ ./a.out
ENTER THE NUMBER OF PAGES:
20
ENTER THE PAGE NUMBER :
70120304230321201701
ENTER THE NUMBER OF FRAMES :3
ref string page frames
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3
2
1 0 1 3
2 0 1 2
0
1
7 7 1 2
0 7 0 2
1 7 0 1
Page Fault Is 15[admin@csk Desktop]$
RESULT: Thus the C program for FIFO page replacement technique was executed successfully and
the output was verified.
ALGORITHM:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
St. Joseph’s Institute of Technology 49
C8461-Operating systems Laboratory Department of CSE 2018-2019
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++){
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("\t%d",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("\t%d",q[r]);}
printf("\n");}}}
printf("\nThe no of page faults is %d",c);}
OUTPUT:
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2
AIM:
To implement Least Frequently Used page replacement technique.
ALGORITHM:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least frequently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
printf("Enter no of frames : ");
scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);
for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}
OUTPUT
roo@sys5:~/Desktop$ lru.c
roo@sys5:~/Desktop$ ./a.out
Enter no of frames: 3
Enter no of pages:10
Viva Questions:
ALGORITHM:
Step 1: Read the number of directories to be created.
Step 2: Input the number of files for each directory.
Step 3: Give the file names for the files.
Step 4: Display the single level directory containing files in each.
PROGRAM:
#include<stdio.h>
main()
{
int master,s[20];
char f[20][20][20];
char d[20][20];
St. Joseph’s Institute of Technology 53
C8461-Operating systems Laboratory Department of CSE 2018-2019
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");
}
OUTPUT
[admin@csk Desktop]$ cc 6a.c
[admin@csk Desktop]$ ./a.out
enter number of directorios:2
enter names of directories:Directory1 Directory2
enter size of directories:4 3
enter the file names :
f1
f2
f3
f4
f5
f6
f7
f8
directory size filenames
*************************************************
Directory1 4 f1
f2
f3
f4
Directory2 3 f5
f6
f7
RESULT: Thus the C program for single level directory structure was executed successfully
and the output was verified.
Date:
AIM:
To write a program to implement two level directory structure.
ALGORITHM:
Step 1: Read the number of directories to be created.
Step 2: Input the number of subdirectories and names of the subdirectories for each directory.
Step 3: Give the file names for the files under the subdirectories.
Step 4: Display the two level structure for each directory created.
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");
}
St. Joseph’s Institute of Technology 55
C8461-Operating systems Laboratory Department of CSE 2018-2019
OUTPUT:
RESULT: Thus the C program for two level directory structures was executed successfully
and the output was verified.
ALGORITHM:
Step 1: Read the Directory for which the hierarchy structure to be displayed.
Step 2: Write commands to access through the subdirectories and files in the given directory.
Step 3: If subdirectories found print them as next level of the directory
Step 4: If file found print them as next sub level of the sub directory
PROGRAM:
#define _XOPEN_SOURCE 500
#include<ftw.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#include<limits.h>
#define TRUE 1
#define FALSE 0
#define MAXDEPTH 20
static int display_info(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
{
char blanks[PATH_MAX];
const char *filename=fpath+ftwbuf->base;
St. Joseph’s Institute of Technology 56
C8461-Operating systems Laboratory Department of CSE 2018-2019
optind++;
}}
else
{
status=nftw(".",display_info,MAXDEPTH,flags);
if(-1==status)
{
perror("nftw");
exit(EXIT_FAILURE);
}
}
exit(EXIT_SUCCESS);
}
OUTPUT:
[admin@csk Desktop]$ cc 6c.c
[admin@csk Desktop]$ ./a.out Folder1
Folder1
6b.c
5.c
Folder2
Folder4
4.c
3.c
5a.c
5b.c
5c.c
6a.c
6b.c
6c.c
7a.c
7b.c
Folder3
UNIT III Notes.doc
Assignment-1.doc
Unit1.docx
Unit2.docx
Unit3.docx
Unit4.docx
Unit5.docx
Scene.jpg
Data.jpg
[admin@csk Desktop]$
RESULT: Thus the C program for Hierarchy level directory was executed successfully and
output was verified.
Viva Questions:
AIM:
To write a program to implement sequential file allocation.
ALGORITHM:
Step 1: Create a disk space using array.
Step 2: Give the file length and starting block number.
Step 3: Allocate each block of the file sequentially in disk.
Step 4: The disk space is checked to ensure the block is allocated or not.
PROGRAM
#include<stdio.h>
main()
{
int f[50],i,st,j,len,c,k;
X:
for(i=0;i<50;i++)
f[i]=0;
printf("\n Enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
return 0;
}
OUTPUT:
AIM:
To write a program to implement indexed file allocation.
ALGORITHM:
Step 1: Enter the number of files along with their file names.
Step 2: Give the file size and block size for each file.
Step 3: Enter the block numbers for each block in file.
Step 4: Display the indexed file allocation of each file.
PROGRAM
#include<stdio.h>
#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
clrscr();
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n\nEnter blocks for file%d",i+1);
St. Joseph’s Institute of Technology 60
C8461-Operating systems Laboratory Department of CSE 2018-2019
for(j=0;j<b1[i];j++)
{
printf("\n Enter the %dblock ::",j+1);
scanf("%d",&blocks[i][j]);
}
}
do
{
printf("\nEnter the Filename ::");
scanf("%s",S);
for(i=0;i<n;i++)
{
if(strcmp(F[i],S)==0)
{
printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue ::(Y-1:n-0)");
scanf("%c",&ch);
}while(ch!=0);
}
OUTPUT
roo@sys5:~/Desktop$ gcc ifa.c
roo@sys5:~/Desktop$ ./a.out
Enter no. of Files ::2
Enter file 1 name ::fileA
Enter file1 size(in kb)::1
Enter blocksize of File1(in bytes)::543
Enter file 2 name ::fileB
Enter file2 size(in kb)::1
Enter blocksize of File2(in bytes)::489
Enter blocks for file1
Enter the 1block ::1024
Enter blocks for file2
Enter the 1block ::1500
Enter the 2block ::966
Enter the Filename ::fileA
Fname Fsize Bsize Nblocks Blocks
----------------------------------------------------------
fileA 1 543 1 1024->
-----------------------------------------------------------
Do U want to continue ::(Y-1:n-0)
RESULT: Thus the C program for indexed file allocation was executed successfully and the
output was verified.
ALGORITHM:
Step 1: Enter the number of files along with their file names.
Step 2: Give the file size and block size for each file.
Step 3: Enter the block numbers for each block in file.
Step 4: Display the linked file allocation of each file using their block delimiters.
PROGRAM
#include<stdio.h>
#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
int sb[20],eb[20],x;
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n Enter Starting block of file%d::",i+1);
scanf("%d",&sb[i]);
printf("\n Enter Ending block of file%d::",i+1);
scanf("%d",&eb[i]);
printf("\nEnter blocks for file%d::\n",i+1);
for(j=0;j<b1[i]-2;)
{
printf("\n Enter the %dblock ::",j+1);
scanf("%d",&x);
if(x>sb[i]&&x<eb[i])
{
blocks[i][j]=x;
j++;
}
else
printf("\n Invalid block::");
}
}
do
St. Joseph’s Institute of Technology 62
C8461-Operating systems Laboratory Department of CSE 2018-2019
{
printf("\nEnter the Filename ::");
scanf("%s",&S);
for(i=0;i<n;i++)
{
if(strcmp(F[i],S)==0)
{
printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
printf("%d->",sb[i]);
for(j=0;j<b1[i]-2;j++)
printf("%d->",blocks[i][j]);
printf("%d->",eb[i]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue (Y:n)::");
scanf("%d",&ch);
}
while(ch!=0);
}
OUTPUT
roo@sys5:~/Desktop$ gcc lfa.c
roo@sys5:~/Desktop$ ./a.out
Enter no. of Files ::2
Enter file 1 name ::aa
Enter file1 size(in kb)::1
Enter blocksize of File1(in bytes)::512
Enter file 2 name ::bb
Enter file2 size(in kb)::1
Enter blocksize of File2(in bytes)::1024
Enter Starting block of file1::1100
Enter Ending block of file1::1600
Enter blocks for file1::
Enter Starting block of file2::102
Enter Ending block of file2::104
Enter blocks for file2::
Enter the Filename ::aa
Fname Fsize Bsize Nblocks Blocks
-------------------------------------------------------------------
aa 1 512 2 1100->1600->
-------------------------------------------------------------------
Do U want to continue (Y:n)::y
Enter the Filename ::bb
Fname Fsize Bsize Nblocks Blocks
-------------------------------------------------------------------
bb 1 1024 1 102->104->
-------------------------------------------------------------------
Do U want to continue (Y:n)::0
RESULT: Thus the C program for linked file allocation was executed successfully and the
output was verified.
St. Joseph’s Institute of Technology 63
C8461-Operating systems Laboratory Department of CSE 2018-2019
Viva Questions:
1. Explain Linked File allocation?
2. Explain Sequential File allocation?
3. Explain Indexed File allocation?
4. What is the need for efficient file allocation strategies?
5. Advantages of indexed file allocation.
ADDITIONAL EXPERIMENTS
READER WRITER PROBLEM
PROGRAM
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t readCountAccess;
sem_t databaseAccess;
int readCount=0;
void *Reader(void *arg);
void *Writer(void *arg);
int main()
{
int i=0,NumberofReaderThread=0,NumberofWriterThread;
sem_init(&readCountAccess,0,1);
sem_init(&databaseAccess,0,1);
pthread_t Readers_thr[100],Writer_thr[100];
printf(“\nEnter number of Readers thread(MAX 10)”);
scanf(“%d”,&NumberofReaderThread);
printf(“\nEnter number of Writers thread(MAX 10)”);
scanf(“%d”,&NumberofWriterThread);
for(i=0;i<numberofreaderthread;i++)
{
pthread_create(&Readers_thr[i],NULL,Reader,(void *)i);
}
for(i=0;i<numberofwriterthread;i++)
{
pthread_create(&Writer_thr[i],NULL,Writer,(void *)i);
}
for(i=0;i<NumberofWriterThread;i++)
{
pthread_join(Writer_thr[i],NULL);
}
for(i=0;i<NumberofReaderThread;i++)
{
pthread_join(Readers_thr[i],NULL);
}
sem_destroy(&databaseAccess);
St. Joseph’s Institute of Technology 64
C8461-Operating systems Laboratory Department of CSE 2018-2019
sem_destroy(&readCountAccess);
return 0;
}
void * Writer(void *arg)
{
sleep(1);
int temp=(int)arg;
printf(“\nWriter %d is trying to enter into database for modifying the data”,temp);
sem_wait(&databaseAccess);
printf(“\nWriter %d is writting into the database”,temp);
printf(“\nWriter %d is leaving the database”);
sem_post(&databaseAccess);
}
void *Reader(void *arg)
{
sleep(1);
int temp=(int)arg;
printf(“\nReader %d is trying to enter into the Database for reading the data”,temp);
sem_wait(&readCountAccess);
readCount++;
if(readCount==1)
{
sem_wait(&databaseAccess);
printf(“\nReader %d is reading the database”,temp);
}
sem_post(&readCountAccess);
sem_wait(&readCountAccess);
readCount–;
if(readCount==0)
{
printf(“\nReader %d is leaving the database”,temp);
sem_post(&databaseAccess);
}
sem_post(&readCountAccess);
}
OUTPUT
Enter number of Readers thread(MAX 10)2
Enter number of Writers thread(MAX 10)1
Reader 0 is trying to enter into the Database for reading the data
Reader 0 is reading the database
Reader 0 is leaving the database
Reader 1 is trying to enter into the Database for reading the data
Reader 1 is reading the database
Reader 1 is leaving the database
Writer 0 is trying to enter into database for modifying the data
Writer 0 is writing into the database
Writer 0 is leaving the dat