Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SUBMITTED TO:
RAMANDEEP SIR
SUBMITTED BY :
ANJANI KUNWAR
RA1803A10
10807973
B.TECH(CSE)-H
Design Problems 1
1. Windows.
2. Macintosh
3 Linux
Expectation:
SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class cpuschedule
int n,Bu[20];
float Twt,Awt,A[10],Wt[10],w;
public:
void Getdata();
void Fcfs();
void Sjf();
void SjfP();
void SjfNp();
//Round Robin Algorithm
void RoundRobin();
//Priority Algorithm
void Priority();
};
#include "cpuh.h"
void cpuschedule::Getdata()
int i;
cout<<"
cin>>n;
for(i=1;i<=n;i++)
cout<<"
cin>>Bu[i];
}
//First come First served Algorithm
void cpuschedule::Fcfs()
int i,B[10];
Twt=0.0;
for(i=1;i<=n;i++)
B[i]=Bu[i];
cout<<"
cout<<B[i];
Wt[1]=0;
for(i=2;i<=n;i++)
Wt[i]=B[i-1]+Wt[i-1];
for(i=1;i<=n;i++)
Twt=Twt+Wt[i];
Awt=Twt/n;
cout<<"
Total Weighting Time="<<Twt;
cout<<"
";
void cpuschedule::Sjf()
int i,j,temp,B[10];
Twt=0.0;
for(i=1;i<=n;i++)
B[i]=Bu[i];
cout<<"
cout<<B[i];
for(i=n;i>=1;i--)
for(j=1;j<=n;j++)
if(B[j-1]>B[j])
{
temp=B[j-1];
B[j-1]=B[j];
B[j]=temp;
Wt[1]=0;
for(i=2;i<=n;i++)
Wt[i]=B[i-1]+Wt[i-1];
for(i=1;i<=n;i++)
Twt=Twt+Wt[i];
Awt=Twt/n;
cout<<"
cout<<"
";
}
//Shortest job First Algorithm with NonPreemption
void cpuschedule::SjfNp()
int i,B[10],Tt=0,temp,j;
char S[10];
float A[10],temp1,t;
Twt=0.0;
w=0.0;
for(i=1;i<=n;i++)
B[i]=Bu[i];
cout<<"
cout<<B[i];
S[i]='T';
Tt=Tt+B[i];
cout<<"
cin>>A[i];
}
for(i=n;i>=1;i--)
for(j=3;j<=n;j++)
if(B[j-1]>B[j])
temp=B[j-1];
temp1=A[j-1];
B[j-1]=B[j];
A[j-1]=A[j];
B[j]=temp;
A[j]=temp1;
for(i=1;i<=n;i++)
cout<<"
w=w+B[1];
t=w;
S[1]='F';
while(w<Tt)
i=2;
while(i<=n)
if(S[i]=='T'&&A[i]<=t)
Wt[i]=w;
cout<<"
WT"<<i<<"="<<Wt[i];
S[i]='F';
w=w+B[i];
t=w;
i=2;
else
i++;
}
}
for(i=1;i<=n;i++)
cout<<"
Wt"<<i<<"=="<<Wt[i];
for(i=1;i<=n;i++)
Twt=Twt+(Wt[i]-A[i]);
Awt=Twt/n;
";
";
//Priority Algorithm
void cpuschedule::Priority()
int i,B[10],P[10],j;
w=0.0;
int max;
Twt=0.0;
max=1;
for(i=1;i<=n;i++)
B[i]=Bu[i];
cout<<"
cout<<B[i];
cout<<"
cin>>P[i];
if(max<P[i])
max=P[i];
j=1;
while(j<=max)
i=1;
while(i<=n)
if(P[i]==j)
{
Wt[i]=w;
w=w+B[i];
i++;
j++;
for(i=1;i<=n;i++)
Twt=Twt+Wt[i];
Awt=Twt/n;
";
";
void cpuschedule::SjfP()
int i,j,m,Wt[10],k,B[10],A[10],Tt=0,Wtm[10],temp;
char S[20],start[20];
int max=0,Time=0,min;
float Twt=0.0,Awt;
for(i=1;i<=n;i++)
B[i]=Bu[i];
cout<<"
if(B[i]>max)
max=B[i];
Wt[i]=0;
S[i]='T';
start[i]='F';
Tt=Tt+B[i];
cout<<"
cin>>A[i];
if(A[i]>Time)
Time=A[i];
//cout<<"
Max="<<max;
int w=0,flag=0,t=0;
i=1;
while(t<Time)
if(flag==0)
Wt[i]=Wt[i]+w;
cout<<"
Wt["<<i<<"]="<<Wt[i];
B[i]=B[i]-1;
if(B[i]==0)
S[i]='F';
start[i]='T';
t++;
w=w+1;
if(S[i]!='F')
j=1;flag=1;
{
flag=0;
Wt[i]=Wt[i]-w;
i=j;
else
flag=1;
j++;
else
i++;
j=1;
while(A[j]<=t &&j<=n)
flag=0;
i=j;
j++;
}
else
if(flag==0)
i++;
cout<<"
";
for(i=1;i<=n;i++)
cout<<"
B["<<i<<"]="<<B[i];
cout<<"
";
while(w<Tt)
min=max+1;
i=1;
while(i<=n)
{
min=B[i];
j=i;
i++;
i=j;
w=w+B[i];
S[i]='F';
else
Wt[i]=Wt[i]+w;
w=w+B[i];
S[i]='F';
cout<<"Weight info
";
for(i=1;i<=n;i++)
cout<<"
WT["<<i<<"]="<<Wt[i];
";
for(i=1;i<=n;i++)
Wt[i]=Wt[i]-A[i];
cout<<"
WT["<<i<<"]="<<Wt[i];
for(i=1;i<=n;i++)
Twt=Twt+Wt[i];
Awt=Twt/n;
cout<<"
}
//Round Robin Algorithm
void cpuschedule::RoundRobin()
int i,j,tq,k,B[10],Rrobin[10][10],count[10];
int max=0;
int m;
Twt=0.0;
for(i=1;i<=n;i++)
B[i]=Bu[i];
cout<<"
cout<<B[i];
if(max<B[i])
max=B[i];
Wt[i]=0;
cout<<"
cin>>tq;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
Rrobin[i][j]=0;
i=1;
while(i<=n)
j=1;
while(B[i]>0)
if(B[i]>=tq)
B[i]=B[i]-tq;
Rrobin[i][j]=tq;
j++;
}
else
Rrobin[i][j]=B[i];
B[i]=0;
j++;
count[i]=j-1;
i++;
cout<<"Display
";
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
cout<<"Rr["<<i<<","<<j<<"]="<<Rrobin[i][j];
cout<<" ";
cout<<"
";
}
//calculating weighting time
int x=1;
i=1;
while(x<=n)
for(int a=1;a<x;a++)
Wt[x]=Wt[x]+Rrobin[a][i];
i=1;
int z=x;
j=count[z];
k=1;
while(k<=j-1)
if(i==n+1)
i=1;
k++;
else
if(i!=z)
{
Wt[z]=Wt[z]+Rrobin[i][k];
i++;
x++;
for(i=1;i<=n;i++)
cout<<"
for(i=1;i<=n;i++)
Twt=Twt+Wt[i];
Awt=Twt/n;
cout<<"
cout<<"
";
}
//Application file for cpu Scheduling
#include "cpuh.h"
void main()
int ch,cho;
cpuschedule c;
do
cout<<" MENU
";
cout<<"1.Getting BurstTime
";
cout<<"2.FirstComeFirstServed
";
cout<<"3.ShortestJobFirst
";
cout<<"4.RoundRobin
";
cout<<"5.Priority
";
cout<<"6.EXIT
";
cout<<"Enter your choice
";
cin>>ch;
switch(ch)
case 1:
c.Getdata();
break;
case 2:
";
c.Fcfs();
break;
case 3:
";
do
cout<<"1.SJF-Normel
";
cout<<"2.SJF-Preemptive
";
cout<<"3.SJF-NonPreemptive
";
";
cin>>cho;
switch(cho)
case 1:
c.Sjf();
break;
case 2:
c.SjfP();
break;
case 3:
c.SjfNp();
break;
}while(cho<=3);
break;
case 4:
";
c.RoundRobin();
break;
case 5:
cout<<"PRIORITY SCHEDULING
";
c.Priority();
break;
case 6:
break;
}while(ch<=5);
}
CPU Scheduling and process scheduling
Make sure your scheduling strategy is good enough with the following criteria:
The Scheduling algorithms can be divided into two categories with respect to how
they deal with clock interrupts.
Many of scheduling algorithems are available and each of them are either
Schedule Algorithms
• FCFS Scheduling
• Round Robin Scheduling
• SJF Scheduling
• SRT Scheduling
• Priority Scheduling
• Multilevel Queue Scheduling
• Multilevel Feedback Queue Scheduling
Note: While the FIFO algorithm is easy to implement, it ignores the service
time request and all other criteria that may influence the performance with
respect to turnaround or waiting time.
Solution: Limit the amount of time a process can execute without a context switch.
This time is called a time slice.
The FCFS scheduling algorithm is nonpreemptive. Once the CPU has been
allocated to a process, that process keeps the CPU until it wants to release the
CPU, either by terminating or by requesting I/O. The FCFS algorithm is
particularly troublesome for time-sharing systems. where it is important that each
user get a share of the CPU at regular intervals. It would be disastrous to allow one
process to keep the CPU for an extended period.
RR (Round Robin) is One of the oldest, simplest, fairest and most widely used
algorithm.The round-robin(RR) scheduling algorithm is designed especially for
time-sharing systems. A small unit of time, called a time quantum or time-slice is
defined. A time quantum is generally from 10 to 100 milliseconds. The ready
queue is treated as a circular queue. The CPU scheduler goes around the ready
queue, allocating the CPU to each process for a time interval of up to 1 time
quantum.
One of two things will then happen. The process may have a CPU burst of less
than 1 time quantum. In this case, the process itself will release the CPU
voluntarily. The scheduler will then proceed to the next process in the ready queue.
Otherwise, it the CPU burst of the currently running process is greater than 1 time
quantum, the timer will go off and will cause an interrupt to the operating system.
A context switch will be executed, and the process will be put at the tail of the
ready queue. The CPU scheduler will then select the next process from the ready
queue.
In the RR scheduling algorithm, no process is allocated the CPU for more than 1
time quantum in a row. If a process's CPU burst exceeds 1 time quantum, that
process os preempted and is put back in the ready queue. The RR scheduling
algorithm is inherently preemptive.
For operating systems, we need to consider the effect of context switching on the
performance of RR scheduling. Let use assume that we have only 1 process of 10
time units. If the quantum is 12 time units, the process finishes in less than 1 time
quantum, with no overhead. If the quantum is 6 time units, however, the process
will require 2 quanta, resulting in a context switch. If the time quantum is 1 time
unit, then 9 context switches will occur, slowing the execution of the process
accordingly.
Thus, we want the time quantum to be large with respect to the context switch
time. If the context switch time is approximately 5 percent of the time quantum,
then about 5 percent of the CPU time will be spent in context switch.
Shortest-Job-First Scheduling(SJF)
Maintain the Ready queue in order of increasing job lengths. When a job comes in,
insert it in the ready queue based on its length. When current process is done, pick
the one at the head of the queue and run it. This algorithm associates with each
process the length of the next CPU burst. When the CPU is available, it is assigned
to the process that has the next smallest CPU burst. If two processes have the same
length CPU burst, FCFS scheduling is used to break the tie.
Say the estimated time (burst) for a process is E0, suppose the actual
time is measured to be T0.
typically a=1/2.
The operating system makes the system appear as if it has a larger amount of
memory than it actually has. The virtual memory can be many times larger
than the physical memory in the system,
Protection
Each process in the system has its own virtual address space. These virtual
address spaces are completely separate from each other and so a process
running one application cannot affect another. Also, the hardware virtual
memory mechanisms allow areas of memory to be protected against writing.
This protects code and data from being overwritten by rogue applications.
Memory Mapping
Memory mapping is used to map image and data files into a processes
address space. In memory mapping, the contents of a file are linked directly
into the virtual address space of a process.
Demang paging:;;
Linux uses demand paging to load executable images into a processes virtual
memory. Whenever a command is executed, the file containing it is opened
and its contents are mapped into the processes virtual memory.
Swapping
Linux uses a Least Recently Used (LRU) page aging technique to fairly
choose pages which might be removed from the system. This scheme
involves every page in the system having an age which changes as the page
is accessed. The more that a page is accessed, the younger it is; the less that
it is accessed the older and more stale it becomes. Old pages are good
candidates for swapping.
Physical addressing mode requires no page tables and the processor does not
attempt to perform any address translations in this mode. The Linux kernel is
linked to run in physical address space.
The Alpha AXP processor does not have a special physical addressing mode.
Instead, it divides up the memory space into several areas and designates two of
them as physically mapped addresses. This kernel address space is known as
KSEG address space and it encompasses all addresses .
Caches
Buffer Cache
The buffer cache contains data buffers that are used by the block device
drivers.
These buffers are of fixed sizes (for example 512 bytes) and contain blocks
of information that have either been read from a block device or are being
written to it. All hard disks are block devices.
Page Cache
Swap Cache
Hardware Caches
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;
SJF
Advantages
Disadvantages
Advantages
Disadvantages
Priority Based
Advantages
1) Static priorities work well for real time systems
2) Dynamic priorities work well for general workloads
Disadvantages
locations.
• Compaction.
• External fragmentation.
Paging
address
• Logical and physical addresses are the same in compile-time and loadtime