Manual With Programs
Manual With Programs
Manual With Programs
OPERATING SYSTEMS
Lab Record (212CSE3303)
1
BASIC LINUX COMMANDS
EX. NO. 1a WORKING WITH FILES AND DIRECTORIES
AIM:
To study the UNIX commands for accessing files and directories.
COMMANDS DESCRIPTION:
2
Ex. No. 1b General Purpose Utility Commands
AIM:
To work with some of the general purpose utility commands in UNIX.
COMMANDS DESCRIPTION:
3
• rev - reverse lines of a file or files
EXECUTION & OUTPUT:
SYSTEM CALLS
Ex. No. 2a Fork System Call
4
INSTRUCTIONS:
System call fork() is used to create processes. It takes no arguments and returns a process ID. The
purpose of fork() is to create a new process, which becomes the child process of the caller. After a
new child process is created, both processes will execute the next instruction following
the fork() system call. Therefore, we have to distinguish the parent from the child. This can be
done by testing the returned value of fork():
• If fork() returns a negative value, the creation of a child process was unsuccessful.
• fork() returns a zero to the newly created child process.
• fork() returns a positive value, the process ID of the child process, to the parent. The
returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an
integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to
this process.
AIM:
To write a program to create a child process using the system calls –fork.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
pid_t pid = fork();
if(pid == 0) {
printf("Child => PPID: %d PID: %d\n", getppid(), getpid());
exit(EXIT_SUCCESS);
5
}
else if(pid > 0) {
13
printf("Parent => PID: %d\n", getpid());
printf("Waiting for child process to finish.\n");
wait(NULL);
printf("Child process finished.\n");
}
else {
printf("Unable to create child process.\n");
}
return EXIT_SUCCESS;
}
EXECUTION & OUTPUT:
6
7
Ex. No. 2b EXEC System Call
DESCRIPTION:
EXEC is a functionality of an operating system that runs an executable file in the context of
an already existing process, replacing the previous executable. This act is also referred to as
an overlay. It is especially important in Unix-like systems, although exists elsewhere. As a new
process is not created, the process identifier (PID) does not change, but the machine code, data, heap,
and stack of the process are replaced by those of the new program.
AIM:
To write a program to display time and date using exec system call.
PROGRAM:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
execlp("date","date", NULL);
execv("./hello", args);
printf("Back to example.c");
return 0;
}
8
9
Ex.No. 2c STAT System Call
DESCRIPTION:
stat() is a Unix system call that returns file attributes about an inode. The semantics
of stat() vary between operating systems. As an example, Unix command ls uses this system call to
retrieve information on files that includes:
AIM:
PROGRAM:
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
struct stat sb;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (stat(argv[1], &sb) == -1) {
perror("stat");
10
exit(EXIT_FAILURE);
}
printf("File type: ");
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long) sb.st_ino);
printf("Mode: %lo (octal)\n",
(unsigned long) sb.st_mode);
printf("Link count: %ld\n", (long) sb.st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid);
printf("Preferred I/O block size: %ld bytes\n",
(long) sb.st_blksize);
printf("File size: %lld bytes\n",
(long long) sb.st_size);
printf("Blocks allocated: %lld\n",
(long long) sb.st_blocks);
printf("Last status change: %s", ctime(&sb.st_ctime));
printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime));
11
exit(EXIT_SUCCESS);
}
EXECUTION & OUTPUT:
12
Ex. No. 2d Wait System Call
DESCRIPTION:
The system call wait() is blocks the calling process until one of its child processes exits or
a signal is received. For our purpose, we shall ignore signals. wait() takes the address of an integer
variable and returns the process ID of the completed process. Some flags that indicate the
completion status of the child process are passed back with the integer pointer. One of the main
purposes of wait() is to wait for completion of child processes.
1. If there are at least one child processes running when the call to wait() is made, the caller
will be blocked until one of its child processes exits. At that moment, the caller resumes its
execution.
2. If there is no child process running when the call to wait() is made, then this wait() has no
effect at all. That is, it is as if no wait() is there.
AIM:
To write a program using wait system call.
PROGRAM:
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
24
#include<sys/wait.h>
int main()
{
pid_t p;
printf("before fork\n");
13
p=fork();
if(p==0)//child
{
printf("I am child having id %d\n",getpid());
printf("My parent's id is %d\n",getppid());
}
else//parent
{
wait(NULL);
printf("My child's id is %d\n",p);
printf("I am parent having id %d\n",getpid());
}
printf("Common\n");
}
14
Ex. No. 2e Input Output system Call
DESCRIPTION:
- creat(name, permissions) – Used to create a file with the name and mode specified. Here,
permission would be a number. 0666 means read write permissions.
- open(name, mode) – Used to open a file name in the mode (read or write) specified. 0 is for
opening in read mode, 1 for writing and 2 for both.
- lseek(fd, offest, whence) - Move the read/write pointer to the specified location.
AIM:
To write a program to implement the concept of I/O call.
PROGRAM:
// C program to illustrate
// open system call
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
extern int errno;
int main()
{
// if file does not have in directory
// then file foo.txt is created.
15
int fd = open("foo.txt", O_RDONLY | O_CREAT);
int close(int fd);
printf("fd = %d/n", fd);
if (fd ==-1)
{
// print which type of error have in a code
printf("Error Number % d\n", errno);
// print program detail "Success or failure"
perror("Program");
}
return 0;
}
16
SCHEDULING ALGORITHMS
AIM:
To write a program using C in UNIX environment to implement the first come first serve
scheduling
PROGRAM:
#include<stdio.h>
struct process
{
int id,wait,ser,tottime;
}p[20];
main()
{
int i,n,j,totalwait=0,totalser=0,avturn,avwait;
printf("Enter number of process:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter process_id:");
scanf("%d",&p[i].id);
printf("Enter process service time:");
scanf("%d",&p[i].ser);
}
p[1].wait=0;
p[1].tottime=p[1].ser;
17
for(i=2;i<=n;i++)
{
for(j=1;j<i;j++)
{
p[i].wait=p[i].wait+p[j].ser;
}
totalwait=totalwait+p[i].wait;
p[i].tottime=p[i].wait+p[i].ser;
totalser=totalser+p[i].tottime;
}
avturn=totalser/n;
avwait=totalwait/n;
printf("Id\tservice\twait\ttotal");
for(i=1;i<=n;i++)
{
printf("\n%d\t%d\t%d\t%d\n",p[i].id,p[i].ser,p[i].wait,p[i].tottime);
}
printf("average waiting time %d\n",avwait);
printf("average turnaround time %d\n",avturn);
}
18
19
Ex. No. 3b Shortest Job First Scheduling
AIM:
To write a program using C in UNIX environment to implement the shortest job next
concept.
PROGRAM:
#include<stdio.h>
struct ff
{
int pid,ser,wait;
}p[20];
struct ff tmp;
main()
{
int i,n,j,tot=0,avwait,totwait=0,tturn=0,aturn;
printf("enter the number of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process id");
scanf("%d",&p[i]);
printf("enter service time");
scanf("%d",&p[i].ser); p[i].wait=0;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
20
if(p[i].ser>p[j].ser)
{
tmp=p[i]; p[i]=p[j]; p[j]=tmp;
}
}
}
printf("PID\tSER\tWAIT\tTOT\n");
for(i=0;i<n;i++)
{
tot=tot+p[i].ser;
tturn=tturn+tot;
p[i+1].wait=tot;
totwait=totwait+p[i].wait;
printf("%d\t%d\t%d\t%d\n",p[i].pid,p[i].ser,p[i].wait,tot);
}
avwait=totwait/n;
aturn=tturn/n;
printf("TOTAL WAITING TIME :%d\n",totwait);
printf("AVERAGE WAITING TIME : %d\n",avwait);
printf("TOTAL TURNAROUND TIME :%d\n",tturn);
printf("AVERAGE TURNAROUND TIME:%d\n",aturn);
}
21
22
Ex. No. 3c Round Robin Scheduling
AIM:
To write a program using C in UNIX environment to implement Round-robin scheduling
concept.
PROGRAM:
#include <stdio.h>
struct roundRobin
{
int pburst,pburst1,wtime,endtime,arrivt,boolean,flagcntl; char pname[5];
}a[5];
int n,tq;
void input();
void initialize();
void calculate();
void display_waittime();
int main()
{
input();
initialize();
calculate();
display_waittime();
//getch();
//return 0;
}
void input()
23
{
int i;
printf("Enter Total no. of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name:");
scanf("%s",&a[i].pname);
printf("Enter process burst time:");
scanf("%d",&a[i].pburst);
printf("Enter process arrival time:");
scanf("%d",&a[i].arrivt);
}
38
printf("\nEnter the time quantum/Time Slice:");
scanf("%d",&tq);
}
void initialize()
{ int i;
for(i=0;i<n;i++)
{
a[i].pburst1=a[i].pburst;
a[i].wtime=0;
a[i].endtime=0;
a[i].boolean=0;
a[i].flagcntl=0;
}
}
24
void calculate()
{
int i,j=0,k=0,flag=1,count=0;
printf("\n---GANTT CHART---\n");
printf("0 | ");
while(flag)
{
for(i=0;i<n;i++)
{
if((k<n)&&(a[i].arrivt<=count)&&(a[i].flagcntl==0)) //calculating waiting time for first time
{
39
a[i].wtime=count-a[i].arrivt;
a[i].endtime=count;
a[i].boolean=1;
a[i].flagcntl=1;
k++;
}
if((a[i].pburst1>tq)&&(a[i].arrivt<=count))
{
if(a[i].boolean==1)
a[i].boolean=0;
else
a[i].wtime=a[i].wtime+(count-a[i].endtime);
count=count+tq;
a[i].pburst1=a[i].pburst1-tq; a[i].endtime=count;
printf("%d %s| ",count,a[i].pname);
}
25
else if((a[i].pburst1>0) && (a[i].pburst1<=tq) && (a[i].arrivt<=count))
{
if(a[i].boolean==1)
a[i].boolean=0;
else
a[i].wtime=a[i].wtime+(count-a[i].endtime);
count=count+a[i].pburst1;
a[i].endtime=count;
printf("%d %s| ",count,a[i].pname);
a[i].pburst1=0;
j++;
}
40
else if(j==n) flag=0;
}//end of for loop
}//end of while loop
}
void display_waittime()
{
int i,tot=0,turn=0;
for(i=0;i<n;i++)
{
printf("\n\nWaiting time for Process %s is %d",a[i].pname,a[i].wtime);
tot=tot+a[i].wtime;
turn=turn+a[i].endtime-a[i].arrivt;
}
printf("\n\n\tAverage waiting time=%f",(float)tot/(float)n);
printf("\n\tAverage turnaround time=%f\n",(float)turn/(float)n); }
26
Ex.No. 3d Priority Scheduling
AIM:
To implement the priority scheduling using C in UNIX environment.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
//flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
27
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
28
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
EXECUTION & OUTPUT:
29
Ex.No : 4 SIMULATATION of IPC in UNIX
AIM:
DESCRIPTION:
In the discussion of the fork() system call, we mentioned that a parent and its children have
separate address spaces. While this would provide a more secured way of executing parent and
children processes (because they will not interfere each other), they shared nothing and have no
way to communicate with each other. A shared memory is an extra piece of memory that is
attached to some address spaces for their owners to use. As a result, all of these processes share
the same memory segment and have access to it. Consequently, race conditions may occur if
memory accesses are not handled properly. The following figure shows two processes and their
address spaces. The yellow rectangle is a shared memory attached to both address spaces and both
process 1 and process 2 can have access to this shared memory as if the shared memory is part of
its own address space. In some sense, the original address spaces is "extended" by attaching this
shared memory.
Shared memory is a feature supported by UNIX System V, including Linux, SunOS and Solaris.
One process must explicitly ask for an area, using a key, to be shared by other processes. This
process will be called the server. All other processes, the clients, that know the shared area can
access it. However, there is no protection to a shared memory and any process that knows it can
30
access it freely. To protect a shared memory from being accessed at the same time by several
processes, a synchronization protocol must be setup.
A shared memory segment is identified by a unique integer, the shared memory ID. The shared
memory itself is described by a structure of type shmid_ds in header file sys/shm.h. To use this
file, files sys/types.h and sys/ipc.h must be included.
PROGRAM:
Program:
#include <stdio.h>
int main()
gets(str);
shmdt(str);
return 0;
31
EXECUTION & OUTPUT:
PROGRAM:
#include <stdio.h>
int main()
32
// shmat to attach to shared memory
shmdt(str);
shmctl(shmid,IPC_RMID,NULL);
return 0;
33
EXECUTION & OUTPUT:
34
Ex.No: 5 IMPLEMENTATION OF DEADLOCK - BANKERS ALGORITHM
AIM:
To write a UNIX C Program for the Implementation of Banker's
PROBLEM DESCRIPTION:
The Banker’s Algorithm was designed and developed by a Dutch Computer Scientist,
Edsger Djikstra. The Banker’s Algorithm is a Resource Allocation and a Deadlock Avoidance
Algorithm.
This algorithm takes analogy of an actual bank where clients request to withdraw cash. The
Banking Authorities have some data according to which the cash is lent to the client. The Banker
cannot give more cash than the client’s request and the total cash available in the bank.
1. Safety Test Algorithm: This algorithm checks the current state of the system to maintain its
Safe State.
35
2. Resource Request Handling Algorithm: This algorithm verifies if the requested resources,
after their allocation to the processes affects the Safe State of the System. If it does, then the
request of the process for the resource is denied, thereby maintaining the Safe State.
A State is considered to be Safe if it is possible for all the Processes to Complete its Execution
without causing any Deadlocks. An Unsafe State is the one in which the Processes cannot
complete its execution.
PROGRAM:
#include <stdio.h>
int main()
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
f[k] = 0;
int need[n][m];
int y = 0;
if (f[i] == 0) {
int flag = 0;
flag = 1;
break;
}}
if (flag == 0) {
ans[ind++] = i;
avail[y] += alloc[i][y];
f[i] = 1;
37
}}}}
return (0);
38
39
Ex. No.:6 PAGE REPLACEMENT ALGORITHMS
AIM:
To write a program in C to implement page replacement algorithm FIFO
PROBLEM DESCRIPTION:
Page replacement algorithms are used to decide what pages to page out when a page needs
to be allocated. This happens when a page fault occurs and free page cannot be used to satisfy
allocation
LRU:
“Replace the page that had not been used for a longer sequence of time”. The frames are
empty in the beginning and initially no page fault occurs so it is set to zero. When a page fault
occurs the page reference sting is brought into the memory. The operating system keeps track of all
pages in the memory, thereby keeping track of the page that had not been used for longer sequence
of time. If the page in the page reference string is not in memory, the page fault is incremented and
the page that had not been used for a longer sequence of time is replaced. If the page in the page
reference string is in the memory take the next page without calculating the next page. Take the
next page in the page reference string and check if the page is already present in the memory or
not. Repeat the process until all pages are referred and calculate the page fault for all those pages
in the page references string for the number of available frames.
Program
#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
40
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j,
pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
41
if(flag1 == 0){
for(j=0;j<no_of_frames;++j){
if(frames[j]==-1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}}}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
EXECUTION & OUTPUT:
42
Optimal replacement algorithm
PROGRAM:
#include <stdio.h>
int main()
{
//variable declaration and initialization
int frames_number, pages_number, frames[10],faults, pages[30], temp[10], flag1, flag2, flag3, i, j,
k, pos, max, miss = 0;
//code to input the frame number
printf("Enter number of frames: ");
scanf("%d", & frames_number);
//code to input number of pages
printf("Enter number of pages: ");
scanf("%d", &pages_number);
//code to define reference string, page numbers, and frame numbers
printf("Enter page reference string: ");
for(i = 0; i < pages_number; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < frames_number; ++i){
43
frames[i] = -1;
}
for(i = 0; i < pages_number; ++i){
flag1 = flag2 = 0;
for(j = 0; j < frames_number; ++j){
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}
//definition of the flag at the starting of the string
if(flag1 == 0){
for(j = 0; j < frames_number; ++j){
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
// definition of the flag at the mid position
if(flag2 == 0){
flag3 =0;
for(j = 0; j < frames_number; ++j){
temp[j] = -1;
for(k = i + 1; k < pages_number; ++k){
44
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < frames_number; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
//definition of flag at the rear position
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < frames_number; ++j){ if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
miss++;
}
printf("\n");
for(j = 0; j < frames_number; ++j){
45
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page miss = %d", miss);
return 0;
}
EXECUTION & OUTPUT:
FIFO algorithm
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 :");
46
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++){
if(frame[k]==a[i])
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
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;
}
EXECUTION & OUTPUT:
47
48
MEMORY MANAGEMENT FUNCTIONS
50
}
EXECUTION & OUTPUT
51
Ex. No. 7b Memory Management Scheme using First Fit.
AIM:
To write a C program to implement the memory management scheme using first fit
PROGRAM:
#include<stdio.h>
int main()
{
static int block_arr[10], file_arr[10];
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp;
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d", &number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
73
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
52
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
file_arr[m] = n;
break;
}
}
}
fragments[m] = temp;
block_arr[file_arr[m]] = 1;
}
printf("\nFile Number\tBlock Number\tFile Size\tBlock Size\tFragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file_arr[m], files[m], blocks[file_arr[m]],
fragments[m]);
}
printf("\n");
return 0;
}
53
54
Ex. No. 7c Memory Management Scheme using Worst Fit.
AIM:
To write a C program to implement the memory management scheme using worst fit.
PROGRAM:
#include<stdio.h>
int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
55
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]],
fragments[m]);
}
printf("\n");
56
return 0;
}
57
Ex No: 8 IMPLEMENTATION OF DISK SCHEDULING ALGORITHMS
AIM:
To write a ‘C’ program to implement the Disk Scheduling algorithm for First Come First
Served (FCFS), Shortest Seek Time First (SSTF), and SCAN.
PROBLEM DESCRIPTION:
Disk Scheduling is the process of deciding which of the cylinder request is in the ready
queue is to be accessed next.
The access time and the bandwidth can be improved by scheduling the servicing of disk
I/O requests in good order.
Access Time:
The access time has two major components: Seek time and Rotational Latency.
Seek Time:
Seek time is the time for disk arm to move the heads to the cylinder containing the
desired sector.
Rotational Latency:
Rotational latency is the additional time waiting for the disk to rotate the desired sector to
the disk head.
Bandwidth:
The disk bandwidth is the total number of bytes transferred, divided by the total time
between the first request for service and the completion of the last transfer.
PROCEDURE:
1. Input the maximum number of cylinders and work queue and its head starting position.
2. First Come First Serve Scheduling (FCFS) algorithm – The operations are performed in
order requested
58
6. Shortest Seek Time First Scheduling (SSTF) algorithm – This algorithm selects the
request with the minimum seek time from the current head position.
7. Since seek time increases with the number of cylinders traversed by the head, SSTF chooses
the pending request closest to the current head position.
9. SCAN Scheduling algorithm –The disk arm starts at one end of the disk, and moves toward
the other end, servicing requests as it reaches each cylinder, until it gets to the other end of the
disk.
10. At the other end, the direction of head movement is reversed and servicing continues.
11. The head continuously scans back and forth across the disk.
ALGORITHM:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. head is the position of disk head.
2. Let us one by one take the tracks in default order and calculate the absolute distance of the track
4. Currently serviced track position now becomes the new head position.
5.Go to step 2 until all tracks in request array have not been serviced.
Program :
#include<stdio.h>
int main()
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
59
scanf("%d",&max);
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
avg=seek/(float)n;
return 0;
60
SSTF:
ALORITHM:
1. Let Request array represents an array storing indexes of tracks that have been requested. head is
2. Find the positive distance of all tracks in the request array from head.
3. Find a track from requested array which has not been accessed/serviced yet and has minimum
5. Currently serviced track position now becomes the new head position.
6.Go to step 2 until all tracks in request array have not been serviced
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
61
printf("* SSTF Disk Scheduling Algorithm *\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&queue[i]);
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(t[i]>t[j])
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
85
62
}
for(i=1;i<n-1;i++)
seek=seek+abs(head-queue[i]);
head=queue[i];
avg=seek/(float)n;
return 0;
SCAN:
ALGORITHM:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. head is the position of disk head.
2. Let direction represents whether the head is moving towards left or right.
3.In the direction in which head is moving service all tracks one by one.
6. Currently serviced track position now becomes the new head position.
63
7,Go to step 3 until we reach at one of the ends of the disk.
If we reach at the end of the disk reverse the direction and go to step 2 until all tracks in request
array have not been serviced.
PROGRAM:
#include<stdio.h>
#include<math.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
64
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
65
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
EXECUTION & OUTPUT:
66
67
Ex No: 9 IMPLEMENTATION OF ACCESS CONTROL MECHANISM
AIM:
Access control is the traditional center of gravity of computer security. It is where security
engineering meets computer science. Its function is to control which principals (persons, processes,
machines, . . .) have access to which resources in the system—which files they can read, which
programs they can execute, how they share data with other principals, and so on.
The access control mechanisms, which the user sees at the application level, may express a
very rich and complex security policy. A modern online business could assign staff to one of
dozens of different roles, each of which could initiate some subset of several hundred possible
transactions in the system. Some of these (such as credit card transactions with customers) might
require online authorization from a third party while others (such as refunds) might require dual
control.The applications may be written on top of middleware, such as a database management
system or bookkeeping package, which enforces a number of protection properties. For example,
bookkeeping software may ensure that a transaction that debits one ledger for a certain amount
must credit another ledger for the same amount
The middleware will use facilities provided by the underlying operating system. As this constructs
resources such as files and communications ports from lower-level components, it acquires the
responsibility for providing ways to control access to them.
Finally, the operating system access controls will usually rely on hardware features provided by
the processor or by associated memory management hardware. These control which memory
addresses a given process can access
Based on the restrictions and access control to systems, we come across three main types:
discretionary access control (DAC), role based access (RBAC) and mandatory access control
(MAC).
Discretionary Access control, DAC: Discretionary access control is a means of restricting access
to objects based on the identity of subjects that try to operate or access them. The most
representative example is the mechanism of permissions established by the owner (user/group) of
the subject. Therefore, it’s the owner of the object who determines which users and with which
privileges they access his/her objects. This access mechanism is present in the majority of
operating systems. In this type of access control its normal to use attributes (read, write, execute,
etc) to mark the permissions applied to the object. In the same way, there is an owner/user and a
set of options that can be shared (groups, other users…).
68
DAC permissions in a Windows file -
In the particular case of UNIX/Linux systems, the objects of the system (files, directories, etc)
include three attributes: read (r), write (w) and execute (x). These permissions are assigned by the
user for the group and others (users that are not owners and don’t belong the group).
In Linux, with the ls -l command we can visualize the basic DAC permissions. In the following
example we can see a system, where user1, who belongs to group1, has fixed the
read/write/execute permissions (rwx) to himself as the owner. The users that belong to the same
group (grupo1) have the same read and write permissions (rw-) and any other user only has a read
permission (r--):
69
- usuario2 can read but not modify usuario1’s fichero1 because of the assigned permissions -
Mandatory Access Control, MAC: This access mechanism is a compliment of the previous ones
and adds another safety layer for access and privilege control. MAC bases itself on “tagging”
every element in the system that will then undergo the access control policies that have been
configured. Therefore, in any operation by a subject on an object the tags will be verified and the
established MAC policies will be applied to determine if the operation is allowed, even when it has
complied with other security controls. In contrast with the discretionary access control, the user
can modify permissions and tags but can’t fix access controls that suppose a violation of the
system’s MAC policies. It’s the responsibility of a privileged user to establish the MAC’s central
policies that will govern the controls to be applied depending on the established tags. Examples of
these control methods are SELinux in Linux/Redhat/Centos/Fedora distributions or AppArmor in
Linux SuSE.
.
70
Types of access control -
The file read operation in C can be performed using functions fscanf() or fgets(). Both the
functions performed the same operations as that of scanf and gets but with an additional
parameter, the file pointer. There are also other functions we can use to read from a file. Such
functions are listed below:
Function Description
71
Function Description
fscanf() Use formatted string and variable arguments list to take input from a file.
Write to a File
The file write operations can be performed by the functions fprintf() and fputs() with
similarities to read operations. C programming also provides some other functions that can be
used to write data to a file such as:
Function Description
Similar to printf(), this function use formatted string and varible arguments list to print
fprintf()
output to the file.
fputs() Prints the whole line in the file and a newline at the end.
fwrite() This functions write the specified amount of bytes to the binary file.
72