OS Lab File
OS Lab File
Name_________________________________________
Roll No________________________________________
Year___________________________________________
Content: An operating system acts as an intermediary between the user of a computer and computer
hardware. The purpose of an operating system is to provide an environment in which a user can execute
programs conveniently and efficiently.
An operating system is a program that controls the execution of application programs and acts as an
interface between the user of a computer and the computer hardware.
There are various types of Operating System used throughout the world and this depends mainly on the
type of operations performed. Most commonly used operating systems are as follows:
● Windows 10, 8, 7, XP
● LINUX
● UNIX
● Mac OS
2. WINDOWS XP
The minimum hardware requirements for Windows XP Home Edition are:
i. Pentium 233-megahertz (MHz) processor or faster (300 MHz is recommended)
ii. At least 64 megabytes (MB) of RAM (128 MB is recommended)
iii. At least 1.5 gigabytes (GB) of available space on the hard disk
iv. CD-ROM or DVD-ROM drive
v. Keyboard and a Microsoft Mouse or some other compatible pointing device
vi. Video adapter and monitor with Super VGA (800 x 600) or higher resolution
vii. Sound card
viii. Speakers or headphones
3. UNIX OS
i. RAM: 1 GB
ii. Processor: IBM 604e processor with a clock speed of 375 MHz or faster
iii. Free disk space: /tmp must have 1 GB free disk space. If Tivoli Identity Manager installs
WebSphere Application Server, (WAS_HOMEI must have 800 MB free disk space and /var must
have 300 MB free disk space. Allocate 500 MB for /itim45.
4. LINUX
i. 32-bit Intel-compatible processor running at 2 GHz or greater
ii. 512 MB RAM
iii. Disk space: 2.5 GB for Pipeline Pilot server plus components
iv. A DVD-ROM drive.
Program No: 02
AIM: Execute various UNIX system calls for:
Process management
File management
Input/output Systems calls
Content: The interface between a process and an operating system is provided by system calls. In
general, system calls are available as assembly language instructions. They are also included in the
manuals used by the assembly level programmers.
UNIX System Calls
System calls in Unix are used for file system control, process control, interprocess communication etc.
Access to the Unix kernel is only available through these system calls. Generally, system calls are similar
to function calls; the only difference is that they remove the control from the user process.
There are around 80 system calls in the Unix interface currently. Details about some of the important ones
are given as follows -
access() This checks if a calling process has access to the required file
chdir() The chdir command changes the current directory of the system
kill() This system call sends kill signal to one or more processes
link() A new file name is linked to an existing file using link system call.
pause() The pause call suspends a file until a particular signal occurs.
alarm() The alarm system call sets the alarm clock of a process
#include <stdio.h>
int main()
{
int i;
extern int errno, sys_nerr;
for (i = 0; i < sys_nerr; ++i)
{
fprintf(stderr, "%3d",i);
errno = i;
perror(" ");
}
exit (0);
}
/* errmsg2.c print all system error messages using the global error message table. */
#include <stdio.h>
int main()
{
int i;
extern int sys_nerr;
extern char *sys_errlist[];
/* creat.c */
#include <stdio.h>
#include <sys/types.h> /* defines types used by sys/stat.h */
#include <sys/stat.h> /* defines S_IREAD & S_IWRITE */
int main()
{
int fd;
fd = creat("[Link]", S_IREAD | S_IWRITE);
if (fd == -1)
printf("Error in opening [Link]\n");
else
{
printf("[Link] opened for read/write access\n");
printf("[Link] is currently empty\n");
}
close(fd);
exit (0);
}
The following is a sample of the manifest constants for the mode
argument as defined in /usr/include/sys/stat.h:
Next is the open() system call. open() lets you open a file for
reading, writing, or reading and writing.
The prototype for the open() system call is:
#include <fcntl.h>
int open(file_name, option_flags [, mode])
char *file_name;
int option_flags, mode;
Where file_name is a pointer to the character string that names the file, option flags represent the type of
channel, and mode defines the file's access permissions if the file is being [Link] allowable
option_flags as defined in "/usr/include/fcntl.h" are:
#define O_RDONLY 0 /* Open the file for reading only */
#define O_WRONLY 1 /* Open the file for writing only */
#define O_RDWR 2 /* Open the file for both reading and writing*/
#define O_NDELAY 04 /* Non-blocking I/O */
#define O_APPEND 010 /* append (writes guaranteed at the end) */
#define O_CREAT 00400 /*open with file create (uses third open arg) */
#define O_TRUNC 01000 /* open with truncation */
#define O_EXCL 02000 /* exclusive open */
if (read(fd, buffer, sizeof(message)) == sizeof(message))
printf("\"%s\" was written to [Link]\n", buffer);
else
printf("*** error reading [Link] ***\n");
close (fd);
}
else
printf("*** [Link] already exists ***\n");
exit (0);
Program No: 03
Aim: Implement CPU Scheduling Policies:
I. SJF
II. Priority
III. FCFS
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("nProcesst Burst Time tWaitingTimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \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],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("nEnter Process Burst Timen");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("nProcessttBurstTimetWaitingTimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("nP[%d]tt%dtt%dtt%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("nnAverage Waiting Time:%d",avwt);
printf("nAverage Turnaround Time:%d",avtat);
getch();
}
Output:
Output:
Enter no of files: 2
Enter no. of blocks occupied by file1 4
Enter the starting block of file1 2
Enter no. of blocks occupied by file2 10
Enter the starting block of file2 5
Filename Start block length
124
2 5 10
Enter file name: rajesh
File name is:12803 length is:0blocks occupied
2. Linked File Allocation method
Program:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter [Link] blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
Output:
Program:
#include<stdio.h>
void main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for(i = 0; i< 10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i<bno; i++)
scanf("%d", &bsize[i]);
Program:
#include<stdio.h>
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np &&parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++) {printf("Block %d:",i);scanf("%d",&b[i]);}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++) {printf("File %d:",i);scanf("%d",&f[i]);}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
Output:
There are mainly two types of fragmentation in the operating system. These are as follows:
1. Internal Fragmentation
2. External Fragmentation
Internal Fragmentation
When a process is allocated to a memory block, and if the process is smaller than the amount of memory
requested, a free space is created in the given memory block. Due to this, the free space of the memory
block is unused, which causes internal fragmentation.
3. External Fragmentation:
External fragmentation happens when there’s a sufficient quantity of area within the memory to
satisfy the memory request of a method. However, the process’s memory request cannot be
fulfilled because the memory offered is in a non-contiguous manner. Whether you apply a first-fit
or best-fit memory allocation strategy it’ll cause external fragmentation.
Difference between Internal fragmentation and External fragmentation
Internal fragmentation happens when the External fragmentation happens when the
2. method or process is smaller than the memory. method or process is removed.
Internal fragmentation occurs with paging and External fragmentation occurs with segmentation
6. fixed partitioning. and dynamic partitioning.
It occurs in worst fit memory allocation It occurs in best fit and first fit memory
8. method. allocation method
Program No: 07
Aim: Implementation of compaction for the continually changing memory layout and calculate total
movement of data.
Content: Compaction is a technique to collect all the free memory present in form of fragments into one
large chunk of free memory, which can be used to run other processes.
It does that by moving all the processes towards one end of the memory and all the available free space
towards the other end of the memory so that it becomes contiguous.
It is not always easy to do compaction. Compaction can be done only when the relocation is dynamic
and done at execution time. Compaction cannot be done when relocation is static and is performed at
load time or assembly time.
Before Compaction
Before compaction, the main memory has some free space between occupied space. This condition is
known as external fragmentation. Due to less free space between occupied spaces, large processes
cannot be loaded into them.
Main Memory
Occupied space
Free space
Occupied space
Occupied space
Free space
After Compaction
After compaction, all the occupied space has been moved up and the free space at the bottom. This
makes the space contiguous and removes external fragmentation. Processes with large memory
requirements can be now loaded into the main memory.
Main Memory
Occupied
space
Occupied
space
Occupied
space
Free space
Free space
#include<stdio.h>
#include<conio.h>
void create(int,int);
void del(int);
void compaction();
void display();
int fname[10],fsize[10],fstart[10],freest[10],freesize[10],m=0,n=0,start;
int main()
{
int name,size,ch,i;
int *ptr;
// clrscr();
ptr=(int *)malloc(sizeof(int)*100);
start=freest[0]=(int)ptr;
freesize[0]=500;
printf("\n\n");
for(i=0;i<=m;i++)
printf("%d%d\n",freest[i],freesize[i]);
printf("\n\n");
while(1)
{
printf("[Link].\n");
printf("[Link].\n");
printf("[Link].\n");
printf("[Link].\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the name of file: ");
scanf("%d",&name);
printf("\nEnter the size of the file: ");
scanf("%d",&size);
create(name,size);
break;
case 2:
printf("\nEnter the file name which u want to delete: ");
scanf("%d",&name);
del(name);
break;
case 3:
compaction();
printf("\nAfter compaction the tables will be:\n");
display();
break;
case 4:
exit(1);
default:
printf("\nYou have entered a wrong choice.\n");
}
}
fsize[j]=size;
fstart[j]=freest[a];
freest[a]=freest[a]+size;
freesize[a]=freesize[a]-size;
printf("\n The memory map will now be: \n\n");
display();
}
else
{
printf("\nNo enough space is [Link] compaction......");
flag=1;
compaction();
display();
for(i=0;i<=m;i++)
if( freesize[i] >= size)
a=i,flag=0;
if(!flag)
{
for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]+=size;
freesize[a]-=size;
printf("\n The memory map will now be: \n\n");
display();
}
else
printf("\nNo enough space.\n");
}
}
void del(int name)
{
int i,j,k,flag=1;
for(i=0;i<n;i++)
if(fname[i]==name)
break;
if(i==n)
{
flag=0;
printf("\nNo such process exists......\n");
}
else
{
m++;
freest[m]=fstart[i];
freesize[m]=fsize[i];
for(k=i;k<n;k++)
{
fname[k]=fname[k+1];
fsize[k]=fsize[k+1];
fstart[k]=fstart[k+1];
}
n--;
}
if(flag)
{
printf("\n\n After deletion of this process the memory map will be : \n\n");
display();
}
}
void compaction()
{
int i,j,size1=0,f_size=0;
if(fstart[0]!=start)
{
fstart[0]=start;
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
else
{
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
f_size=freesize[0];
for(j=0;j<=m;j++)
size1+=freesize[j];
freest[0]=freest[0]-(size1-f_size);
freesize[0]=size1;
m=0;
}
void display()
{
int i;
Content: The resource allocation graph is the pictorial representation of the state of a system. As its
name suggests, the resource allocation graph is the complete information about all the processes which are
holding some resources or waiting for some resources. It also contains the information about all the
instances of all the resources whether they are available or being used by the processes.
In RAG vertices are mainly of two types, Resource and process. Each of them will be represented by a
different shape. Circle represents process while rectangle represents resource. There are two types of
edges in RAG –
1. Assign Edge – If you already assign a resource to a process then it is called Assign edge.
2. Request Edge – It means in future the process might want some resource.
Example of Single Instances RAG
In the above single instances RAG example, it can be seen that P2 is holding the R1 and waiting
for R3. P1 is waiting for R1 and holding R2 and, P3 is holding the R3 and waiting for R2. So, it can be
concluded that none of the processes will get executed. In the case of Multiple Instances RAG, it becomes
difficult to analyze from the RAG that the system is in a safe state or not.
Output:
One such deadlock detection algorithm makes use of a wait-for graph to track which other processes a
process is currently blocking on. In a wait-for graph, processes are represented as nodes, and an edge
from process Pi to Pj implies Pj is holding a resource that Pi needs and thus Pi is waiting for Pj to
release its lock on that resource. There may be processes waiting for more than a single resource
to become available. Graph cycles imply the possibility of a deadlock.
INPUT/s:
STEPS TO PERFORM:
OUTPUT