Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CS 2257
LTPC
0032
1. Write programs using the following system calls of UNIX operating system:
fork, exec, getpid, exit, wait, close, stat, opendir, readdir
.c
om
2. Write programs using the I/O System calls of UNIX operating system (open, read,
write, etc).
3. Write C programs to simulate UNIX commands like ls, grep, etc.
4. Given the list of processes, their CPU burst times and arrival times. Display/print the
Gantt chart for FCFS and SJF. For each of the scheduling policies, compute and print
the average waiting time and average turnaround time.
ul
5. Given the list of processes, their CPU burst times and arrival times. Display/print the
Gantt chart for Priority and Round robin. For each of the scheduling policies, compute
and print the average waiting time and average turnaround time.
pa
jin
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
INDEX
.c
om
.re
4a
4b
4c
4d
5a
5b
5c
5d
5e
ul
3a
3b
3c
3d
pa
2a
2b
2c
jin
1a
1b
1c
1d
1e
Session#
6a
6b
6c
6d
7a
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
The fork system call is used to create a new process called child process.
o The return value is 0 for a child process.
o The return value is negative if process creation is unsuccessful.
o For the parent process, return value is positive
The child process is an exact copy of the parent process.
Both the child and parent continue to execute the instructions following fork call.
The child can start execution before the parent or vice-versa.
wait()
pa
ul
The wait system call causes the parent process to be blocked until a child terminates.
When a process terminates, the kernel notifies the parent by sending the SIGCHLD
signal to the parent.
Without wait, the parent may finish first leaving a zombie child, to be adopted by init
process
execl()
.re
exit()
jin
The exec family of function (execl, execv, execle, execve, execlp, execvp) is used by the
child process to load a program and execute.
execl system call requires path, program name and null pointer
The exit system call is used to terminate a process either normally or abnormally
Closes all standard I/O streams.
stat()
The stat system call is used to return information about a file as a structure.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 1a
Aim
To create a new child process using fork system call.
Algorithm
Result
.re
jin
pa
ul
.c
om
Thus a child process is created with copy of its parent's address space.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Process creation - fork.c */
<stdio.h>
<stdlib.h>
<unistd.h>
<sys/types.h>
.c
om
#include
#include
#include
#include
main()
{
pid_t pid;
int x = 5;
pid = fork();
x++;
.re
jin
pa
ul
if (pid < 0)
{
printf("Process creation error");
exit(-1);
}
else if (pid == 0)
{
printf("Child process:");
printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of parent is %d\n", getppid());
}
else
{
printf("\nParent process:");
printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of shell is %d\n", getppid());
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc fork.c
.c
om
$ ./a.out
Child process:
Process id is 19499
Value of x is 6
Process id of parent is 19498
.re
jin
pa
ul
Parent process:
Process id is 19498
Value of x is 6
Process id of shell is 3266
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 1b
Aim
To block a parent process until child completes using wait system call.
Algorithm
jin
pa
ul
.c
om
.re
Result
Thus using wait system call zombie child processes were avoided.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Wait for child termination - wait.c */
<stdio.h>
<stdlib.h>
<unistd.h>
<sys/types.h>
<sys/wait.h>
.c
om
#include
#include
#include
#include
#include
main()
{
int i, status;
pid_t pid;
.re
jin
pa
ul
pid = fork();
if (pid < 0)
{
printf("\nProcess creation failure\n");
exit(-1);
}
else if(pid > 0)
{
wait(NULL);
printf ("\nParent starts\nEven Nos: ");
for (i=2;i<=10;i+=2)
printf ("%3d",i);
printf ("\nParent ends\n");
}
else if (pid == 0)
{
printf ("Child starts\nOdd Nos: ");
for (i=1;i<10;i+=2)
printf ("%3d",i);
printf ("\nChild ends\n");
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc wait.c
8 10
.re
jin
pa
ul
Parent starts
Even Nos:
2
Parent ends
.c
om
$ ./a.out
Child starts
Odd Nos:
1
Child ends
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 1c
Aim
To load an executable program in a child processes exec system call.
.c
om
Algorithm
Result
.re
jin
pa
ul
Thus the child process loads a binary executable file into its address space.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Load a program in child process - exec.c */
<stdio.h>
<sys/types.h>
<unistd.h>
<stdlib.h>
.c
om
#include
#include
#include
#include
pa
ul
if (argc != 3)
{
printf("\nInsufficient arguments to load program");
printf("\nUsage: ./a.out <path> <cmd>\n");
exit(-1);
}
jin
switch(pid = fork())
{
case -1:
printf("Fork failed");
exit(-1);
.re
case 0:
printf("Child process\n");
i = execl(argv[1], argv[2], 0);
if (i < 0)
{
printf("%s program not loaded using exec system
call\n", argv[2]);
exit(-1);
}
default:
wait(NULL);
printf("Child Terminated\n");
exit(0);
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc exec.c
$ ./a.out
ul
consumer.c
ex6b.c
exec.c
fcreate.c
hello
pri.c
simls.c
wait.c
pa
$ ./a.out /bin/ls ls
Child process
a.out
cmdpipe.c
dirlist.c
ex6a.c
ex6c.c
ex6d.c
fappend.c
fcfs.c
fork.c
fread.c
list
list.c
producer.c
rr.c
sjf.c
stat.c
Child Terminated
.c
om
jin
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 1d
Aim
To display file status using stat system call.
.c
om
Algorithm
Get filename as command line argument.
If filename does not exist then stop.
Call stat system call on the filename that returns a structure
Display members st_uid, st_gid, st_blksize, st_block, st_size, st_nlink, etc.,
Convert time members such as st_atime, st_mtime into time using ctime function
Compare st_mode with mode constants such as S_IRUSR, S_IWGRP, S_IXOTH and
display file permissions.
7. Stop
pa
ul
1.
2.
3.
4.
5.
6.
Result
.re
jin
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* File status - stat.c */
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
pa
ul
.c
om
.re
jin
printf("Permissions : ");
printf( (S_ISDIR(file.st_mode)) ? "d" : "-");
printf( (file.st_mode & S_IRUSR) ? "r" : "-");
printf( (file.st_mode & S_IWUSR) ? "w" : "-");
printf( (file.st_mode & S_IXUSR) ? "x" : "-");
printf( (file.st_mode & S_IRGRP) ? "r" : "-");
printf( (file.st_mode & S_IWGRP) ? "w" : "-");
printf( (file.st_mode & S_IXGRP) ? "x" : "-");
printf( (file.st_mode & S_IROTH) ? "r" : "-");
printf( (file.st_mode & S_IWOTH) ? "w" : "-");
printf( (file.st_mode & S_IXOTH) ? "x" : "-");
printf("\n");
if(file.st_mode
printf("File
if(file.st_mode
printf("File
& S_IFREG)
type : Regular\n");
& S_IFDIR)
type : Directory\n");
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc stat.c
.re
jin
pa
ul
.c
om
$ ./a.out fork.c
User id : 0
Group id : 0
Block size : 4096
Blocks allocated : 8
Inode no. : 16627
Last accessed : Fri Feb 22 21:57:09 2013
Last modified : Fri Feb 22 21:56:13 2013
File size : 591 bytes
No. of links : 1
Permissions : -rw-r--r-File type : Regular
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 1e
Aim
To display directory contents using readdir system call.
ul
1.
2.
3.
4.
5.
6.
7.
.c
om
Algorithm
pa
Result
.re
jin
Thus files and subdirectories in the directory was listed that includes hidden files.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Directory content listing - dirlist.c */
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
.c
om
ul
if (argc != 2)
{
printf("Usage: ./a.out <dirname>\n");
exit(-1);
}
pa
jin
while(dptr=readdir(dname))
printf("%s\n", dptr->d_name);
closedir(dname);
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc dirlist.c
.re
jin
pa
ul
.c
om
$ ./a.out vijai
wait.c
a.out
..
stat.c
dirlist.c
fork.c
.
exec.c
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
ul
read()
pa
jin
write()
close()
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 2a
Aim
To create a file and to write contents.
.c
om
Algorithm
pa
ul
jin
Result
.re
Thus a file has been created with input from the user. The process can be verified by
using cat command.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* File creation - fcreate.c */
<stdio.h>
<stdlib.h>
<string.h>
<fcntl.h>
.c
om
#include
#include
#include
#include
pa
ul
if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}
jin
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc fcreate.c
.re
jin
pa
ul
.c
om
$ ./a.out hello
File I/O
Open system call is used to either open or create a file.
creat system call is used to create a file. It is seldom used.
^D
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 2b
Aim
To read the given file and to display file contents.
.c
om
Algorithm
Declare a character buffer buf to store 100 bytes.
Get existing filename as command line argument.
Open the file for reading using open system call with O_RDONLY option.
Check the file descriptor.
a) If file does not exist, then stop.
5. Read until end-of-file using read system call.
a) Read 100 bytes (max.) from file and print it
6. Close the file using close system call.
7. Stop
pa
ul
1.
2.
3.
4.
Result
.re
jin
Thus the given file is read and displayed on the console. The process can be verified by
using cat command.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* File Read
- fread.c */
.c
om
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
ul
jin
pa
fd = open(argv[1], O_RDONLY);
if(fd == -1)
{
printf("%s file does not exist\n", argv[1]);
exit(-1);
}
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc fread.c
.re
jin
pa
ul
.c
om
$ ./a.out hello
File I/O
open system call is used to either open or create a file.
creat system call is used to create a file. It is seldom used.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 2c
Aim
To append content to an existing file.
.c
om
Algorithm
Declare a character buffer buf to store 100 bytes.
Get exisiting filename as command line argument.
Create a file with the given name using open system call with O_APPEND option.
Check the file descriptor.
a) If value is negative, then stop.
5. Get input from the console until user types Ctrl+D
a) Read 100 bytes (max.) from console and store onto buf using read system call
b) Write length of buf onto file using write system call.
6. Close the file using close system call.
7. Stop
pa
ul
1.
2.
3.
4.
jin
Result
.re
Thus contents have been written to end of the file. The process can be verified by using
cat command.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* File append - fappend.c */
<stdio.h>
<string.h>
<stdlib.h>
<fcntl.h>
.c
om
#include
#include
#include
#include
pa
ul
if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}
jin
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc fappend.c
.re
jin
pa
ul
.c
om
$ ./a.out hello
read system call is used to read from file or console
write system call is used to write to file.
^D
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
COMMAND SIMULATION
.re
jin
pa
ul
.c
om
Using UNIX system calls, most commands can be emulated in a similar manner.
Simulating a command and all of its options is an exhaustive exercise.
Command simulation harnesses one's programming skills.
Command simulation helps in development of standard routines to be
customized to the application needs.
Generally file I/O commands are simulated.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 3a
ls command
Aim
To simulate ls command using UNIX system calls.
3.
4.
ul
1.
2.
.c
om
Algorithm
Result
.re
jin
pa
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* ls command simulation - list.c */
#include <stdio.h>
#include <dirent.h>
.c
om
main()
{
struct dirent **namelist;
int n,i;
char pathname[100];
getcwd(pathname);
n = scandir(pathname, &namelist, 0, alphasort);
pa
ul
if(n < 0)
printf("Error\n");
else
for(i=0; i<n; i++)
if(namelist[i]->d_name[0] != '.')
printf("%-20s", namelist[i]->d_name);
.re
jin
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc list.c -o list
consumer.c
ex6b.c
exec.c
fcreate.c
hello
pri.c
simls.c
wait.c
.c
om
cmdpipe.c
ex6a.c
ex6d.c
fcfs.c
fread.c
list.c
rr.c
stat.c
.re
jin
pa
ul
$ ./list
a.out
dirlist.c
ex6c.c
fappend.c
fork.c
list
producer.c
sjf.c
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 3b
grep command
Aim
To simulate grep command using UNIX system call.
ul
6.
7.
pa
1.
2.
3.
4.
5.
.c
om
Algorithm
Result
.re
jin
Thus the program simulates grep command by listing lines containing the search text.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* grep command simulation - mygrep.c */
.c
om
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
pa
ul
.re
jin
fd = fopen(argv[2],"r");
if(fd == NULL)
{
printf("%s is not exist\n",argv[2]);
exit(-1);
}
while(!feof(fd))
{
i = 0;
while(1)
{
c = fgetc(fd);
if(feof(fd))
{
str[i++] = '\0';
break;
}
if(c == '\n')
{
str[i++] = '\0';
break;
}
str[i++] = c;
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
.re
jin
pa
ul
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc mygrep.c -o mygrep
.re
jin
pa
ul
.c
om
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 3c
cp command
Aim
To simulate cp command using UNIX system call.
jin
8.
9.
pa
ul
1.
2.
3.
4.
5.
6.
7.
.c
om
Algorithm
Result
.re
Thus a file is copied using file I/O. The cmp command can be used to verify that
contents of both file are same
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* cp command simulation - copy.c */
<stdio.h>
<stdlib.h>
<fcntl.h>
<sys/stat.h>
.c
om
#include
#include
#include
#include
pa
ul
if (argc != 3)
{
printf("Usage: gcc copy.c -o copy\n");
printf("Usage: ./copy <filename> <newfile> \n");
exit(-1);
}
.re
jin
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc copy.c -o copy
.re
jin
pa
ul
.c
om
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 3d
rm command
Aim
To simulate rm command using UNIX system call.
ul
1.
2.
3.
4.
5.
6.
.c
om
Algorithm
pa
Result
.re
jin
Thus files can be deleted in a manner similar to rm command. The deletion of file can be
verified by using ls command.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* rm command simulation - del.c */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
.c
om
jin
pa
fd = open(argv[1], O_RDONLY);
if (fd != -1)
{
close(fd);
unlink(argv[1]);
}
else
perror(argv[1]);
ul
if (argc != 2)
{
printf("Usage: gcc del.c -o del\n");
printf("Usage: ./del <filename>\n");
exit(-1);
}
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc del.c -o del
.re
jin
pa
ul
.c
om
$ ./del hello.txt
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
PROCESS SCHEDULING
.c
om
ul
jin
pa
.re
Round Robin
All processes are processed one by one as they have arrived, but in rounds.
Each process cannot take more than the time slice per round.
Round robin is a fair preemptive scheduling algorithm.
A process that is yet to complete in a round is preempted after the time slice
and put at the end of the queue.
When a process is completely processed, it is removed from the queue.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 4a
FCFS Scheduling
Aim
.c
om
To schedule snapshot of processes queued according to FCFS (First Come First Serve)
scheduling.
Algorithm
Define an array of structure process with members pid, btime, wtime & ttime.
Get length of the ready queue, i.e., number of process (say n)
Obtain btime for each process.
The wtime for first process is 0.
Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
6. Compute average waiting time awat and average turnaround time atur
7. Display the btime, ttime and wtime for each process.
8. Display GANTT chart for the above scheduling
9. Display awat time and atur
10. Stop
jin
pa
ul
1.
2.
3.
4.
5.
.re
Result
Thus waiting time & turnaround time for processes based on FCFS scheduling was
computed and the average waiting time was determined.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* FCFS Scheduling
- fcfs.c */
#include <stdio.h>
ul
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
.c
om
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10];
jin
pa
.re
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
printf("\n
FCFS Scheduling\n\n");
for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");
for(i=0; i<n; i++)
printf("\n P%d\t%4d\t%3d\t%2d",
p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");
for(i=0; i<28; i++)
printf("-");
.re
jin
pa
ul
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$./a.out
Enter no. of process :
Burst time for process
Burst time for process
Burst time for process
Burst time for process
4
P1
P2
P3
P4
(in
(in
(in
(in
ms)
ms)
ms)
ms)
:
:
:
:
pa
ul
FCFS Scheduling
10
4
11
6
.c
om
$ gcc fcfs.c
jin
GANTT Chart
---------------------------------------|
P1
| P2 |
P3
|
P4 |
---------------------------------------0
10
14
25
31
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 4b
SJF Scheduling
Aim
.c
om
Algorithm
Define an array of structure process with members pid, btime, wtime & ttime.
Get length of the ready queue, i.e., number of process (say n)
Obtain btime for each process.
Sort the processes according to their btime in ascending order.
a. If two process have same btime, then FCFS is used to resolve the tie.
5. The wtime for first process is 0.
6. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
7. Compute average waiting time awat and average turn around time atur.
8. Display btime, ttime and wtime for each process.
9. Display GANTT chart for the above scheduling
10. Display awat and atur
11. Stop
Result
.re
jin
pa
ul
1.
2.
3.
4.
Thus waiting time & turnaround time for processes based on SJF scheduling was
computed and the average waiting time was determined.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* SJF Scheduling sjf.c */
#include <stdio.h>
ul
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
.c
om
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10], temp;
jin
pa
.re
}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
pa
ul
.c
om
printf("\n
SJF Scheduling\n\n");
for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");
for(i=0; i<n; i++)
printf("\n P%-4d\t%4d\t%3d\t%2d",
p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");
for(i=0; i<28; i++)
printf("-");
.re
jin
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$./a.out
Enter no. of process :
Burst time for process
Burst time for process
Burst time for process
Burst time for process
Burst time for process
5
P1
P2
P3
P4
P5
(in
(in
(in
(in
(in
ms)
ms)
ms)
ms)
ms)
:
:
:
:
:
pa
ul
SJF Scheduling
10
6
5
6
9
.c
om
$ gcc sjf.c
.re
jin
GANTT Chart
----------------------------------------------| P3 |
P2 |
P4 |
P5
|
P1
|
----------------------------------------------0
5
11
17
26
36
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 4c
Priority Scheduling
Aim
To schedule snapshot of processes queued according to Priority scheduling.
.c
om
Algorithm
Define an array of structure process with members pid, btime, pri, wtime & ttime.
Get length of the ready queue, i.e., number of process (say n)
Obtain btime and pri for each process.
Sort the processes according to their pri in ascending order.
a. If two process have same pri, then FCFS is used to resolve the tie.
5. The wtime for first process is 0.
6. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
7. Compute average waiting time awat and average turn around time atur
8. Display the btime, pri, ttime and wtime for each process.
9. Display GANTT chart for the above scheduling
10. Display awat and atur
11. Stop
Result
.re
jin
pa
ul
1.
2.
3.
4.
Thus waiting time & turnaround time for processes based on Priority scheduling was
computed and the average waiting time was determined.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Priority Scheduling
- pri.c */
#include <stdio.h>
ul
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
.c
om
struct process
{
int pid;
int btime;
int pri;
int wtime;
int ttime;
} p[10], temp;
.re
jin
pa
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
pa
ul
.re
jin
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc pri.c
Priority Scheduling
ms) : 10
ms) : 7
ms) : 6
ms) : 13
ms) : 5
.c
om
(in
3
(in
1
(in
3
(in
4
(in
2
ul
$ ./a.out
Enter no. of process : 5
Burst time for process P1
Priority for process P1 :
Burst time for process P2
Priority for process P2 :
Burst time for process P3
Priority for process P3 :
Burst time for process P4
Priority for process P4 :
Burst time for process P5
Priority for process P5 :
.re
jin
pa
GANTT Chart
---------------------------------------------------|
P2
| P5 |
P1
|
P3 |
P4
|
---------------------------------------------------0
7
12
22
28
41
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 4d
Aim
To schedule snapshot of processes queued according to Round robin scheduling.
.c
om
Algorithm
Result
.re
jin
pa
ul
1.
2.
3.
4.
5.
6.
Thus waiting time and turnaround time for processes based on Round robin scheduling
was computed and the average waiting time was determined.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Round robin scheduling
- rr.c */
#include <stdio.h>
.c
om
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,j=0;
float awat,atur;
pa
ul
.re
jin
printf("\nGANTT Chart\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] -= t;
a[j+1] = a[j] + t;
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
pa
}
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] -= 1;
printf(" P%d |",x+1);
}
ul
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];
s++;
}
j++;
b[x] -= 1;
printf(" P%d
|", x+1);
.c
om
jin
.re
printf("\n");
for(i=0;i<m;i++)
printf("--------");
printf("\n");
for(i=0; i<n;
{
for(j=i+1;
{
if(p[i]
{
temp
p[i]
p[j]
i++)
j<n; j++)
> p[j])
= p[i];
= p[j];
= temp;
temp = k[i];
k[i] = k[j];
k[j] = temp;
}
}
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
pa
ul
printf("\n\n");
for(i=0; i<30; i++)
printf("-");
printf("\nProcess\tBurst\tTrnd\tWait\n");
for(i=0; i<30; i++)
printf("-");
for (i=0; i<n; i++)
printf("\nP%-4d\t%4d\t%4d\t%4d", p[i]+1, bur1[i],
tur[i],wat[i]);
printf("\n");
for(i=0; i<30; i++)
printf("-");
jin
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nAverage waiting time
: %.2f ms", awat);
printf("\nAverage turn around time : %.2f ms\n", atur);
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ ./a.out
Enter no. of process : 5
Burst time for process P1 : 10
Burst time for process P2 : 29
Burst time for process P3 : 3
Burst time for process P4 : 7
Burst time for process P5 : 12
Enter the time slice (in ms) : 10
Round Robin Scheduling
.c
om
$ gcc rr.c
pa
ul
GANTT Chart
-------------------------------------------------------------P1
| P2
| P3 | P4 | P5 | P2
| P5
|
P2 |
-------------------------------------------------------------0
10
20
23
30
40
50
52
61
.re
jin
-----------------------------Process Burst
Trnd
Wait
-----------------------------P1
10
10
0
P2
29
61
32
P3
3
23
20
P4
7
30
23
P5
12
52
40
------------------------------
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
INTERPROCESS COMMUNICATION
Inter-Process communication (IPC), is the mechanism whereby one process can
communicate with another process, i.e exchange data.
IPC in linux can be implemented using pipe, shared memory, message queue,
semaphore, signal or sockets.
.c
om
Pipe
Pipes are unidirectional byte streams which connect the standard output from
one process into the standard input of another process.
A pipe is created using the system call pipe that returns a pair of file descriptors.
The descriptor pfd[0] is used for reading and pfd[1] is used for writing.
Can be used only between parent and child processes.
Shared memory
pa
ul
.re
jin
Producer-Consumer problem
A producer process produces information to be consumed by a consumer process
A producer can produce one item while the consumer is consuming another one.
With bounded-buffer size, consumer must wait if buffer is empty, whereas
producer must wait if buffer is full.
The buffer can be implemented using any IPC facility.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 5a
Aim
To generate 25 fibonacci numbers and determine prime amongst them using pipe.
.c
om
Algorithm
jin
pa
ul
1.
2.
3.
Result
.re
Thus fibonacci numbers that are prime is determined using IPC pipe.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<unistd.h>
<sys/types.h>
pa
if(pipe(pfd) == -1)
{
printf("Error in pipe");
exit(-1);
}
ul
main()
{
pid_t pid;
int pfd[2];
int i,j,flg,f1,f2,f3;
static unsigned int ar[25],br[25];
.c
om
.re
jin
pid=fork();
if (pid == 0)
{
printf("Child process generates Fibonacci series\n" );
f1 = -1;
f2 = 1;
for(i = 0;i < 25; i++)
{
f3 = f1 + f2;
printf("%d\t",f3);
f1 = f2;
f2 = f3;
ar[i] = f3;
}
write(pfd[1],ar,25*sizeof(int));
}
else if (pid > 0)
{
wait(NULL);
read(pfd[0], br, 25*sizeof(int));
printf("\nParent prints Fibonacci that are Prime\n");
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
ul
.c
om
pa
}
else
{
printf("Process creation failed");
exit(-1);
}
.re
jin
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc fibprime.c
8
377
17711
13
610
28657
.c
om
$ ./a.out
Child process generates Fibonacci series
0
1
1
2
3
5
21
34
55
89
144
233
987
1597
2584
4181
6765
10946
46368
Parent prints Fibonacci that are Prime
2
3
5
13
89
233
28657
.re
jin
pa
ul
1597
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 5b
who | wc -l
Aim
To determine number of users logged in using pipe.
.c
om
Algorithm
jin
pa
ul
1.
2.
Result
.re
Thus standard output of who is connected to standard input of wc using pipe to compute
number of users logged in.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pfds[2];
pipe(pfds);
.re
jin
pa
ul
if (!fork())
{
close(1);
dup(pfds[1]);
close(pfds[0]);
execlp("who", "who", NULL);
}
else
{
close(0);
dup(pfds[0]);
close(pfds[1]);
execlp("wc", "wc", "-l", NULL);
}
.c
om
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc cmdpipe.c
.re
jin
pa
ul
.c
om
$ ./a.out
15
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 5c
Chat Messaging
Aim
To exchange message between server and client using message queue.
Algorithm
1.
2.
3.
jin
6.
7.
pa
ul
4.
5.
.c
om
Server
1.
2.
3.
.re
Client
4.
5.
6.
Result
Thus chat session between client and server was done using message queue.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
Server
/* Server chat process - srvmsg.c */
<stdio.h>
<stdlib.h>
<string.h>
<sys/types.h>
<sys/ipc.h>
<sys/msg.h>
.c
om
#include
#include
#include
#include
#include
#include
pa
jin
main()
{
int msqid, len;
key_t key = 2013;
ul
struct mesgq
{
long type;
char text[200];
} mq;
.re
}
msgctl(msqid, IPC_RMID, NULL);
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Client
/* Client chat process - climsg.c */
<stdio.h>
<stdlib.h>
<string.h>
<sys/types.h>
<sys/ipc.h>
<sys/msg.h>
.c
om
#include
#include
#include
#include
#include
#include
pa
main()
{
int msqid, len;
key_t key = 2013;
ul
struct mesgq
{
long type;
char text[200];
} mq;
.re
jin
}
printf("Server Disconnected\n");
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
Server
ul
Client
pa
$ ./srvmsg
Enter text, ^D to quit:
hi
From Client: "hello"
Where r u?
From Client: "I'm where i am"
bye
From Client: "ok"
^D
.c
om
.re
jin
$ ./climsg
Client ready:
From Server: "hi"
hello
From Server: "Where r u?"
I'm where i am
From Server: "bye"
ok
Server Disconnected
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 5d
Shared Memory
Aim
To demonstrate communication between process using shared memory.
Algorithm
.c
om
Server
jin
pa
ul
1.
2.
3.
1.
2.
3.
.re
Client
4.
5.
6.
7.
Result
Thus contents written onto shared memory by the server process is read by the client
process.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
Server
/* Shared memory server - shms.c */
<stdio.h>
<stdlib.h>
<sys/un.h>
<sys/types.h>
<sys/ipc.h>
<sys/shm.h>
.c
om
#include
#include
#include
#include
#include
#include
pa
main()
{
char c;
int shmid;
key_t key = 2013;
char *shm, *s;
ul
#define shmsize 27
.re
jin
memset(shm, 0, shmsize);
s = shm;
printf("Writing (a-z) onto shared memory\n");
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = '\0';
while (*shm != '*');
printf("Client finished reading\n");
if(shmdt(shm) != 0)
fprintf(stderr, "Could not close memory segment.\n");
shmctl(shmid, IPC_RMID, 0);
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Client
/* Shared memory client - shmc.c */
<stdio.h>
<stdlib.h>
<sys/types.h>
<sys/ipc.h>
<sys/shm.h>
.c
om
#include
#include
#include
#include
#include
#define shmsize 27
ul
main()
{
int shmid;
key_t key = 2013;
char *shm, *s;
jin
pa
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
Server
.c
om
Client
ul
.re
jin
pa
$ ./shmc
Accessing shared memory id : 196611
Shared memory contents:
abcdefghijklmnopqrstuvwxyz
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 5e
Producer-Consumer problem
Aim
To synchronize producer and consumer processes using semaphore.
Algorithm
6.
7.
8.
.c
om
ul
Result
pa
5.
jin
4.
.re
1.
2.
3.
Thus synchronization between producer and consumer process for access to a shared
memory segment is implemented.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Producer-Consumer problem using semaphore pcsem.c */
<stdio.h>
<stdlib.h>
<sys/types.h>
<sys/ipc.h>
<sys/shm.h>
<sys/sem.h>
.c
om
#include
#include
#include
#include
#include
#include
#define N 5
#define BUFSIZE 1
#define PERMS 0666
/* semaphore variables */
ul
int *buffer;
int nextp = 0, nextc = 0;
int mutex, full, empty;
.re
jin
pa
void producer()
{
int data;
if(nextp == N)
nextp = 0;
printf("Enter data for producer to produce : ");
scanf("%d",(buffer + nextp));
nextp++;
}
void consumer()
{
int g;
if(nextc == N)
nextc = 0;
g = *(buffer + nextc++);
printf("\nConsumer consumes data %d", g);
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
s.val = initval;
if((semval = semctl(semid, 0, SETVAL, s)) < 0)
printf("\nError in executing semctl");
}
pa
.re
main()
{
int shmid, i;
pid_t pid;
jin
ul
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
.re
jin
pa
ul
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc pcsem.c
produce : 8
produce : 4
produce : 2
produce : 9
.re
jin
pa
ul
.c
om
$ ./a.out
Enter data for producer to produce : 5
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
MEMORY MANAGEMENT
The first-fit, best-fit, or worst-fit strategy is used to select a free hole from the set
of available holes.
First fit
.c
om
ul
Worst fit
pa
jin
FIFO
.re
Page replacement is based on when the page was brought into memory.
When a page should be replaced, the oldest one is chosen.
Generally, implemented using a FIFO queue.
Simple to implement, but not efficient.
Results in more page faults.
The page-fault may increase, even if frame size is increased (Belady's anomaly)
LRU
Pages used in the recent past are used as an approximation of future usage.
The page that has not been used for a longer period of time is replaced.
LRU is efficient but not optimal.
Implementation of LRU requires hardware support, such as counters/stack.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 6a
Aim
To allocate memory requirements for processes using first fit allocation.
Algorithm
jin
7.
8.
9.
pa
ul
2.
3.
4.
5.
6.
Declare structures hole and process to hold information about set of holes and
processes respectively.
Get number of holes, say nh.
Get the size of each hole
Get number of processes, say np.
Get the memory requirements for each process.
Allocate processes to holes, by examining each hole as follows:
a. If hole size > process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
b. Otherwise check the next from the set of hole
Print the list of process and their allocated holes or unallocated status.
Print the list of holes, their actual and current availability.
Stop
.c
om
1.
Result
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* First fit allocation - ffit.c */
#include <stdio.h>
pa
main()
{
int i, np, nh, j;
ul
.c
om
struct process
{
int size;
int flag;
int holeid;
} p[10];
struct hole
{
int size;
int actual;
} h[10];
.re
jin
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
jin
pa
ul
printf("\n\tFirst fit\n");
printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
.re
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", i, h[i].actual, h[i].size);
printf("\n");
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
Enter
enter
enter
enter
enter
Holes : 5
H0 : 100
H1 : 500
H2 : 200
H3 : 300
H4 : 600
number of process :
the size of process
the size of process
the size of process
the size of process
4
P0
P1
P2
P3
:
:
:
:
pa
First fit
212
417
112
426
ul
$ ./a.out
Enter the number of
Enter size for hole
Enter size for hole
Enter size for hole
Enter size for hole
Enter size for hole
.c
om
$ gcc ffit.c
PSize
212
417
112
426
Hole
H1
H4
H1
Not allocated
Hole
H0
H1
H2
H3
H4
Actual
100
500
200
300
600
Available
100
176
200
300
183
.re
jin
Process
P0
P1
P2
P3
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 6b
Aim
To allocate memory requirements for processes using best fit allocation.
Algorithm
jin
7.
8.
9.
pa
ul
2.
3.
4.
5.
6.
Declare structures hole and process to hold information about set of holes and
processes respectively.
Get number of holes, say nh.
Get the size of each hole
Get number of processes, say np.
Get the memory requirements for each process.
Allocate processes to holes, by examining each hole as follows:
a. Sort the holes according to their sizes in ascending order
b. If hole size > process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
c. Otherwise check the next from the set of sorted hole
Print the list of process and their allocated holes or unallocated status.
Print the list of holes, their actual and current availability.
Stop
.c
om
1.
.re
Result
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Best fit allocation - bfit.c */
#include <stdio.h>
ul
.c
om
struct process
{
int size;
int flag;
int holeid;
} p[10];
struct hole
{
int hid;
int size;
int actual;
} h[10];
pa
main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
.re
jin
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
.c
om
pa
ul
printf("\n\tBest fit\n");
printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
.re
jin
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual,
h[i].size);
printf("\n");
}
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
Enter
enter
enter
enter
enter
Holes : 5
H0 : 100
H1 : 500
H2 : 200
H3 : 300
H4 : 600
number of process :
the size of process
the size of process
the size of process
the size of process
4
P0
P1
P2
P3
212
417
112
426
pa
Best fit
:
:
:
:
ul
$ ./a.out
Enter the number of
Enter size for hole
Enter size for hole
Enter size for hole
Enter size for hole
Enter size for hole
.c
om
$ gcc bfit.c
PSize
212
417
112
426
Hole
H3
H1
H2
H4
Hole
H1
H3
H2
H0
H4
Actual
500
300
200
100
600
Available
83
88
88
100
174
.re
jin
Process
P0
P1
P2
P3
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 6c
Aim
To implement demand paging for a reference string using FIFO method.
Algorithm
.c
om
jin
8.
9.
pa
ul
1.
2.
3.
4.
5.
6.
7.
.re
Result
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* FIFO page replacement - fifopr.c */
#include <stdio.h>
.c
om
main()
{
int i,j,l,rs[50],frame[10],nf,k,avail,count=0;
pa
ul
.re
jin
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc fifopr.c
2
2
3
3
4
4
6
6
6
6
1
1
1
1
3
2
2
2
4
4
3
3
5
5
.c
om
5
5
5
7
jin
1
6
-1
-1
-1
-1
ul
Page frames
1 -1 -1 -1
1
2 -1 -1
1
2
3 -1
1
2
3
4
.re
Ref. str
1
2
3
4
2
1
5
6
2
1
2
3
7
6
3
2
1
2
3
6
ref. string : 20
string :
2 1 2 3 7 6 3 2 1 2 3 6
frames : 5
pa
$ ./a.out
Enter length of
Enter reference
1 2 3 4 2 1 5 6
Enter number of
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 6d
Aim
To implement demand paging for a reference string using LRU method.
Algorithm
Get length of the reference string, say len.
Get reference string and store it in an array, say rs.
Get number of frames, say nf.
Create access array to store counter that indicates a measure of recent usage.
Create a function arrmin that returns position of minimum of the given array.
Initalize frame array upto length nf to -1.
Initialize position of the page replacement, say j to 0.
Initialize freq to 0 to track page frequency
Initialize no. of page faults, say count to 0.
For each page in reference string in the given order, examine:
a. Check whether page exist in the frame array.
b. If page exist in memory then
i. Store incremented freq for that page position in access array.
c. If page does not exist in memory then
i. Check for any empty frames.
ii. If there is an empty frame,
Assign that frame to the page
Store incremented freq for that page position in access array.
Increment count.
iii. If there is no free frame then
Determine page to be replaced using arrmin function.
Store incremented freq for that page position in access array.
Increment count.
iv. Display pages in frame array.
11. Print count.
12. Stop
.re
jin
pa
ul
.c
om
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Result
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* LRU page replacement - lrupr.c */
#include <stdio.h>
int arrmin(int[], int);
.c
om
main()
{
int i,j,len,rs[50],frame[10],nf,k,avail,count=0;
int access[10], freq=0, dm;
jin
pa
ul
.re
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
if(dm == 1)
{
frame[k] = rs[i];
access[k] = ++freq;
count++;
}
else
{
j = arrmin(access, nf);
frame[j] = rs[i];
access[j] = ++freq;
count++;
}
for(k=0; k<nf; k++)
printf("%4d", frame[k]);
.c
om
.re
jin
pa
ul
}
}
printf("\n\nTotal no. of page faults : %d\n", count);
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc lrupr.c
3
6
4
4
1
1
2
2
6
6
3
3
5
5
ul
2
2
5
7
jin
1
1
-1
-1
-1
-1
pa
Page frames
1 -1 -1 -1
1
2 -1 -1
1
2
3 -1
1
2
3
4
.re
Ref. str
1
2
3
4
2
1
5
6
2
1
2
3
7
6
3
2
1
2
3
6
.c
om
$ ./a.out
Length of Reference string : 20
Enter reference string :
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6
Enter no. of frames : 5
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
FILE ALLOCATION
.c
om
Contiguous
ul
Linked
jin
pa
.re
In indexed allocation, all pointers are put in a single block known as index
block.
The directory contains address of the index block.
The ith entry in the index block points to ith block of the file.
Indexed allocation supports direct access.
It suffers from pointer overhead, i.e wastage of space in storing pointers.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Exp# 7a
Contiguous Allocation
Aim
To implement file allocation on free disk space in a contiguous manner.
Algorithm
.c
om
ul
4.
5.
6.
pa
1.
2.
3.
Result
.re
jin
Thus contiguous allocation is done for files with the available free blocks.
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Program
/* Contiguous Allocation - cntalloc.c */
.c
om
#include <stdio.h>
#include <string.h>
jin
pa
void display()
{
int i;
for(i=0; i<20; i++)
printf("%4d",i);
printf("\n");
for(i=0; i<20; i++)
printf("%4s", a[i]);
}
ul
void directory()
{
int i;
printf("\nFile Start Length\n");
for(i=0; i<num; i++)
printf("%-4s %3d %6d\n",fid[i],start[i],length[i]);
}
.re
main()
{
int i,n,k,temp,st,nb,ch,flag;
char id[4];
do
{
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
pa
ul
start[num] = st;
for(i=st; i<(st+nb); i++)
strcpy(a[i], id);;
printf("Allocation done\n");
num++;
.c
om
jin
.re
printf("\n\t\t\tContiguous Allocation\n");
printf("Directory:");
directory();
printf("\nDisk space after allocation:\n");
display();
printf("\n");
http://cseannauniv.blogspot.com
Vijai Anand
CS2257
Output
$ gcc cntalloc.c
$ ./a.out
Disk space before allocation:
0
1
2
3
4
5
6
10
11
12
13
14
15
16
17
18
19
.c
om
ul
pa
jin
.re
Directory:
File Start Length
ls
3
4
cp
14
3
tr
10
3
mv
0
2
ps
7
3
Disk space after allocation:
0
1
2
3
4
5
6
mv mv
ls ls ls ls
http://cseannauniv.blogspot.com
7
ps
8
ps
9
ps
10
tr
11
tr
12
tr
13
14
cp
15
cp
16
cp
17
18
19
Vijai Anand