Você está na página 1de 20

Practical No.

2 :
Thread:
A thread is a single sequence stream within in a process. Because threads have s
ome of the properties of processes, they are sometimes called lightweight proces
ses.
1. USER LEVEL THREAD:
User thread are implemented by users.OS doesnt recognized user level threads.Impl
ementation of User threads is easy.Context switch time is less.Context switch re
quires no hardware support.If one user level thread perform blocking operation t
hen entire process will be blocked.
2. KERNEL LEVEL THREAD:
kernel threads are implemented by OS.Kernel threads are recognized by OS.Impleme
ntation of Kernel thread is complicated.Context switch time is more.Hardware sup
port is needed.If one kernel thread perform blocking operation then another thre
ad can continue execution.
Syntax:
int pthread_create(Pthread_t *pthread)
Stopping thread:
int pthread_cancel(Pthread_t thread)
----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// A normal C function that is executed as a thread when its name
// is specified in pthread_create()
void *myThreadFun(void *vargp)
{
sleep(1);
printf("Printing GeeksQuiz from Thread \n");
return NULL;
}
int main()
{
pthread_t tid;
printf("Before Thread\n");
pthread_create(&tid, NULL, myThreadFun, NULL);
pthread_join(tid, NULL);
printf("After Thread\n");
exit(0);
}
----------------------------------------------------------------------
How to compile above code :
gfg@ubuntu:~/$ gcc multithread.c -lpthread
gfg@ubuntu:~/$ ./a.out
Before Thread
Printing GeeksQuiz from Thread
After Thread
gfg@ubuntu:~/$
---------------------------------END--------------------------------------
--------------------------------------------------------------------------
Practical No. 3
--------------------------------------------------------------------------
First Come First Served (FCFS) is a Non-Preemptive scheduling algorithm. FIFO (F
irst In First Out) strategy assigns priority to process in the order in which th
ey request the processor. The process that requests the CPU first is allocated t
he CPU first. This is easily implemented with a FIFO queue for managing the task
s. As the process come in, they are put at the end of the queue. As the CPU fini
shes each task, it removes it from the start of the queue and heads on to the ne
xt task.
-----------------------------------------------------------------------
#include<stdio.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 Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
--------------------------------------------------------------
OUTPUT:
Enter total number of processes(maximum 20):3
Enter Process Burst Time
P[1]:24
P[2]:3
P[3]:3
Process Burst Time Waiting Time Turnaround Time
P[1] 24 0 24
P[2] 3 24 27
P[3] 3 27 30
Average Waiting Time:17
Average Turnaround Time:27
Process returned 0 (0x0) execution time : 7.661s
-----------------------------------------------------------------
-----------------------------------------------------------------
Practical No. 4
-----------------------------------------------------------------
First Come First Serve (FCFS)
Jobs are executed on first come, first serve basis.
It is a non-preemptive, pre-emptive scheduling algorithm.
Easy to understand and implement.
Its implementation is based on FIFO queue.
Poor in performance as average wait time is high.
These algorithms are either non-preemptive or preemptive. Non-preemptive algorit
hms are designed so that once a process enters the running state, it cannot be p
reempted until it completes its allotted time, whereas the preemptive scheduling
is based on priority where a scheduler may preempt a low priority running proce
ss anytime when a high priority process enters into a ready state.
------------------------------------------------------------------
C Program to implement preemptive shortest job first algoritm:
#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Details of %d Processes\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nEnter Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_t
ime[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest];
}
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);
printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);
return 0;
}
--------------------------------------------------------------------------
gcc test.c
./a.out
OUTPUT:
Enter the Total Number of Processes: 3
Enter Details of 3 Processes
Enter Arrival Time: 0
Enter Burst Time: 20
Enter Arrival Time: 3
Enter Burst Time: 10
Enter Arrival Time: 7
Enter Burst Time: 30
Average Waiting Time: 11.000000
Average Turnaround Time: 31.000000
-----------------------------------------------------------------
-----------------------------------------------------------------
SJF CPU Scheduling Algorithm C Program using Non-Preemptive Algorithm
---------------------------------------------------------------------
#include<stdio.h>
int main()
{
int temp, i, j, limit, sum = 0, position;
float average_wait_time, average_turnaround_time;
int burst_time[20], process[20], waiting_time[20], turnaround_time[20];
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
for(i = 0; i < limit; i++)
{
printf("Enter Burst Time For Process[%d]:\t", i + 1);
scanf("%d", &burst_time[i]);
process[i] = i + 1;
}
for(i = 0; i < limit; i++)
{
position = i;
for(j = i + 1; j < limit; j++)
{
if(burst_time[j] < burst_time[position])
{
position = j;
}
}
temp = burst_time[i];
burst_time[i] = burst_time[position];
burst_time[position] = temp;
temp = process[i];
process[i] = process[position];
process[position] = temp;
}
waiting_time[0] = 0;
for(i = 1; i < limit; i++)
{
waiting_time[i] = 0;
for(j = 0; j < i; j++)
{
waiting_time[i] = waiting_time[i] + burst_time[j];
}
sum = sum + waiting_time[i];
}
average_wait_time = (float)sum / limit;
sum = 0;
printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n");
for(i = 0; i < limit; i++)
{
turnaround_time[i] = burst_time[i] + waiting_time[i];
sum = sum + turnaround_time[i];
printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i]
, waiting_time[i], turnaround_time[i]);
}
average_turnaround_time = (float)sum / limit;
printf("\nAverage Waiting Time:\t%f\n", average_wait_time);
printf("\nAverage Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}
----------------------------------------------------------------------------
gcc test.c
./a.out
Output:
Enter Total Number of Processes: 5
Enter Burst Time For Process[1]: 10
Enter Burst Time For Process[3]: 05
Enter Burst Time For Process[4]: 25
Enter Burst Time For Process[5]: 18
Enter Burst Time For Process[6]: 23
Process ID Burst Time Waiting Time Turnaround Time
process[2] 5 0 5
process[1] 10 5 15
process[4] 18 15 33
process[5] 23 33 56
process[3] 25 56 81
Average Waiting Time: 21.799999
Average Turnaround Time: 38.000000
--------------------------------------------------------
--------------------------------------------------------------------
Practical No. 5
-------------------------------------------------------------------
Round Robin is the preemptive process scheduling algorithm.
Each process is provided a fix time to execute, it is called a quantum.
Once a process is executed for a given time period, it is preempted and other pr
ocess executes for a given time period.
Context switching is used to save states of preempted processes.
------------extra part
1. The queue structure in ready queue is of First In First Out (FIFO) type.
2. A fixed time is allotted to every process that arrives in the queue. This fix
ed time is known as time slice or time quantum.
3. The first process that arrives is selected and sent to the processor for exec
ution. If it is not able to complete its execution within the time quantum provi
ded, then an interrupt is generated using an automated timer.
4. The process is then stopped and is sent back at the end of the queue. However
, the state is saved and context is thereby stored in memory. This helps the pro
cess to resume from the point where it was interrupted.
5. The scheduler selects another process from the ready queue and dispatches it
to the processor for its execution. It is executed until the time Quantum does n
ot exceed.
6. The same steps are repeated until all the process are finished.
--------------------------------------------------------------------
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",c
ount+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[co
unt]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
-----------------------------------------------------------------
OUTPUT:
Enter Total Process: 4
Enter Arrival Time and Burst Time for Process Process Number 1 :0 9
Enter Arrival Time and Burst Time for Process Process Number 2 :1 5
Enter Arrival Time and Burst Time for Process Process Number 3 :2 3
Enter Arrival Time and Burst Time for Process Process Number 4 :3 4
Enter Time Quantum: 5
Process |Turnaround time|waiting time
P[2] | 9 | 4
P[3] | 11 | 8
P[4] | 14 | 10
P[1] | 21 | 12
Average Waiting Time= 8.500000
Avg Turnaround Time = 13.750000
---------------------------------------------------------------------
---------------------------------------------------------------------
Practical No. 6
--------------------------------------------------------------------------
In priority scheduling algorithm each process has a priority associated with it
and as each process hits the queue, it is stored in based on its priority so tha
t process with higher priority are dealt with first. It should be noted that equ
al priority processes are scheduled in FCFS order.
----------extra
To prevent high priority processes from running indefinitely the scheduler may d
ecrease the priority of the currently running process at each clock tick (i.e.,
at each clock interrupt). If this action causes its priority to drop below that
of the next highest process, a process switch occurs. Alternatively, each proces
s may be assigned a maximum time quantum that it is allowed to run. When this qu
antum is used up, the next highest priority process is given a chance to run.
Limitations
The problem occurs when the operating system gives a particular task a very low
priority, so it sits in the queue for a larger amount of time, not being dealt w
ith by the CPU. If this process is something the user needs, there could be a ve
ry long wait, this process is known as Starvation or Infinite Blocking.
Solution
Many operating systems use a technique called aging, in which a low priority proce
ss slowly gains priority over time as it sits in the queue. Even if, the priorit
y of the process is low, there is a surety of its execution.
----------------------------------------------------------------------
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat
;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order using s
election sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}
----------------------------------------------------------------------------
Output:
Enter Total Number of Process:4
Enter Burst Time and Priority
P[1]
Burst Time:6
Priority:3
P[2]
Burst Time:2
Priority:2
P[3]
Burst Time:14
Priority:1
P[4]
Burst Time:6
Priority:4
Proces Burst Time Waiting Time Turnaround Time
process[3] 14 0 14
process[2] 2 14 16
process[1] 6 16 22
process[4] 6 22 28
Average Waiting Time=13
Average Turnaround Time=20
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Practical No. 7
--------------------------------------------------------------------------
When a page fault occurs, the OS has to remove a page from the memory so that it
can fit in another page in the memory. These page replacement algorithms are us
ed in operating systems that support virtual memory management.
FIFO Page Replacement technique is one of the simplest one to implement amongst
other page replacement algorithms. It is a conservative algorithm. It is a low-o
verhead algorithm that maintains a queue to keep a track of all the pages in a m
emory. When a page needs to be replaced, the page at the FRONT of the Queue will
be replaced. The FIFO Page Replacement technique is not implemented in operatin
g systems nowadays.
----------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &reference_string[i]);
}
printf("\nEnter Total Number of Frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
{
temp[m] = reference_string[m];
}
else if(s == 0)
{
temp[(page_faults - 1) % frames] = reference_string[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
}
----------------------------------------------------------------------------
gcc demo.c -lm
./a.out
OUTPUT:
Enter Total Number of Pages: 5
Enter values of Reference String:
Value No. [1]: 4
Value No. [2]: 1
Value No. [3]: 2
Value No. [4]: 4
Value No. [5]: 5
Enter Total Number of Frames: 3
4 -1 -1
4 1 -1
4 1 2
4 1 2
5 1 2
Total Page Faults: 4
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Practical No.8
----------------------------------------------------------------------------
Optimal page replacement algorithm says that if page fault occurs then that page
should be removed that will not be used for maximum time in future.
Optimal page replacement is perfect, but not possible in practice as operating s
ystem cannot know future requests. The use of Optimal Page replacement is to set
up a benchmark so that other replacement algorithms can be analyzed against it.
----------extra for viva
Page Fault:
A page fault is a type of interrupt, raised by the hardware when a running progr
am accesses a memory page that is mapped into the virtual address space, but not
loaded in physical memory.
First In First Out:
This is the simplest page replacement algorithm. In this algorithm, operating sy
stem keeps track of all pages in the memory in a queue, oldest page is in the fr
ont of the queue. When a page needs to be replaced page in the front of the queu
e is selected for removal.
Least Recently Used:
In this algorithm page will be replaced which is least recently used.
----------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2
, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page 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]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
----------------------------------------------------------------------------
Output:
Enter number of frames: 3
Enter number of pages: 10
Enter page reference string: 2 3 4 2 1 3 7 5 4 3
2 -1 -1
2 3 -1
2 3 4
2 3 4
1 3 4
1 3 4
7 3 4
5 3 4
5 3 4
5 3 4
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Practical No. 9
Least Recently Used (LRU) page replacement algorithm works on the concept that t
he pages that are heavily used in previous instructions are likely to be used he
avily in next instructions. And the page that are used very less are likely to b
e used less in future. Whenever a page fault occurs, the page that is least rece
ntly used is removed from the memory frames. Page fault occurs when a referenced
page in not found in the memory frames.
ALGORITHM :
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
-----------------------------------------------------------------------------
#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;
}
}
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;
}
}
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;
}
---------------------------------------------------------------------------
Output:
Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3
5 -1 -1
5 7 -1
5 7 -1
5 7 6
5 7 6
3 7 6
Total Page Faults = 4
--------------------------------------------------------------------------
OR
---------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int frames[10], temp[10], pages[10];
int total_pages, m, n, position, k, l, total_frames;
int a = 0, b = 0, page_fault = 0;
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frames[m] = -1;
}
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Values for Reference String:\n");
for(m = 0; m < total_pages; m++)
{
printf("Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
for(n = 0; n < total_pages; n++)
{
a = 0, b = 0;
for(m = 0; m < total_frames; m++)
{
if(frames[m] == pages[n])
{
a = 1;
b = 1;
break;
}
}
if(a == 0)
{
for(m = 0; m < total_frames; m++)
{
if(frames[m] == -1)
{
frames[m] = pages[n];
b = 1;
break;
}
}
}
if(b == 0)
{
for(m = 0; m < total_frames; m++)
{
temp[m] = 0;
}
for(k = n - 1, l = 1; l <= total_frames - 1; l++, k--)
{
for(m = 0; m < total_frames; m++)
{
if(frames[m] == pages[k])
{
temp[m] = 1;
}
}
}
for(m = 0; m < total_frames; m++)
{
if(temp[m] == 0)
position = m;
}
frames[position] = pages[n];
page_fault++;
}
printf("\n");
for(m = 0; m < total_frames; m++)
{
printf("%d\t", frames[m]);
}
}
printf("\nTotal Number of Page Faults:\t%d\n", page_fault);
return 0;
}
----------------------------------------------------------------------------
Questions:
OS:
An Operating System (OS) is an interface between a computer user and computer ha
rdware. An operating system is a software which performs all the basic tasks lik
e
1. File management,
2. Memory management,
3. Process management,
4. Handling input and output, and
5. Controlling peripheral devices such as disk drives and printers.
Types of OS:
Batch operating system:
Time-sharing operating systems:
Distributed operating System:
Network operating System:
Real Time operating System:
Multitasking:
Multitasking is when multiple jobs are executed by the CPU simultaneously by swi
tching between them. Switches occur so frequently that the users may interact wi
th each program while it is running.
Multiprogramming:
Sharing the processor, when two or more programs reside in memory at the same ti
me, is referred as multiprogramming.
Spooling:
Spooling is an acronym for simultaneous peripheral operations on line. Spooling
refers to putting data of various I/O jobs in a buffer. This buffer is a special
area in memory or hard disk which is accessible to I/O devices.
Process Control Block (PCB):
A Process Control Block is a data structure maintained by the Operating System f
or every process. The PCB is identified by an integer process ID (PID)
--------------------------------------------------------------------------
Schedulers
Schedulers are special system software which handle process scheduling in variou
s ways. Their main task is to select the jobs to be submitted into the system an
d to decide which process to run. Schedulers are of three types
Long Term Scheduler:
It is also called a job scheduler. A long term scheduler determines which progra
ms are admitted to the system for processing. It selects processes from the queu
e and loads them into memory for execution.
Short Term Scheduler:
It is also called as CPU scheduler. Its main objective is to increase system per
formance in accordance with the chosen set of criteria. It is the change of read
y state to running state of the process. CPU scheduler selects a process among t
he processes that are ready to execute and allocates CPU to one of them.
Medium Term Scheduler:
Medium term scheduling is a part of swapping. It removes the processes from the
memory. It reduces the degree of multiprogramming.
What is a Deadlock?
Deadlocks are a set of blocked processes each holding a resource and waiting to
acquire a resource held by another process.
How to avoid Deadlocks:
1. Mutual Exclusion
2. Hold and Wait
3. No Preemption
4. Circular Wait
Handling Deadlock:
Preemption
We can take a resource from one process and give it to other. This will resolve
the deadlock situation, but sometimes it does causes problems.
Rollback
In situations where deadlock is a real possibility, the system can periodically
make a record of the state of each process and when deadlock occurs, roll everyt
hing back to the last checkpoint, and restart, but allocating resources differen
tly so that deadlock does not occur.
Kill one or more processes
This is the simplest way, but it works.
Logical and Physical Addresses:
The set of all logical addresses generated by a program is referred to as a logi
cal address space. The set of all physical addresses corresponding to these logi
cal addresses is referred to as a physical address space.
Swapping:
Swapping is a mechanism in which a process can be swapped temporarily out of mai
n memory (or move) to secondary storage (disk) and make that memory available to
other processes. At some later time, the system swaps back the process from the
secondary storage to main memory.
Fragmentation:
As processes are loaded and removed from memory, the free memory space is broken
into little pieces. It happens after sometimes that processes cannot be allocat
ed to memory blocks considering their small size and memory blocks remains unuse
d. This problem is known as Fragmentation.
Paging:
A computer can address more memory than the amount physically installed on the s
ystem. This extra memory is actually called virtual memory and it is a section o
f a hard that's set up to emulate the computer's RAM. Paging technique plays an
important role in implementing virtual memory.
Paging is a memory management technique in which process address space is broken
into blocks of the same size called pages (size is power of 2, between 512 byte
s and 8192 bytes). The size of the process is measured in the number of pages.
Similarly, main memory is divided into small fixed sized blocks of (physical) me
mory called frames and the size of a frame is kept the same as that of a page to
have optimum utilization of the main memory and to avoid external fragmentation
.
Segmentation:
Segmentation is a memory management technique in which each job is divided into
several segments of different sizes, one for each module that contains pieces th
at perform related functions. Each segment is actually a different logical addre
ss space of the program.
When a process is to be executed, its corresponding segmentation are loaded into
non contiguous memory though every segment is loaded into a contiguous block of
available memory.
Segmentation memory management works very similar to paging but here segments ar
e of variable length where as in paging pages are of fixed size.
Virtual Memory:
A computer can address more memory than the amount physically installed on the s
ystem. This extra memory is actually called virtual memory and it is a section o
f a hard disk that's set up to emulate the computer's RAM.
The main visible advantage of this scheme is that programs can be larger than ph
ysical memory. Virtual memory serves two purposes. First, it allows us to extend
the use of physical memory by using disk. Second, it allows us to have memory p
rotection, because each virtual address is translated to a physical address.
Page Replacement Algorithm:
Page replacement algorithms are the techniques using which an Operating System d
ecides which memory pages to swap out, write to disk when a page of memory needs
to be allocated. Paging happens whenever a page fault occurs and a free page ca
nnot be used for allocation purpose accounting to reason that pages are not avai
lable or the number of free pages is lower than required pages.
First In First Out (FIFO) algorithm:
Oldest page in main memory is the one which will be selected for replacement.
Easy to implement, keep a list, replace pages from the tail and add new pages at
the head.
Optimal Page algorithm:
An optimal page replacement algorithm has the lowest page fault rate of all algo
rithms. An optimal page replacement algorithm exists, and has been called OPT or
MIN.
Replace the page that will not be used for the longest period of time. Use the t
ime when a page is to be used.
Least Recently Used (LRU) algorithm:
Page which has not been used for the longest time in main memory is the one whic
h will be selected for replacement.
Easy to implement, keep a list, replace pages by looking back into time.
Least frequently Used(LFU) algorithm:
The page with the smallest count is the one which will be selected for replaceme
nt.
This algorithm suffers from the situation in which a page is used heavily during
the initial phase of a process, but then is never used again.
Direct Memory Access (DMA):
Direct Memory Access (DMA) means CPU grants I/O module authority to read from or
write to memory without involvement. DMA module itself controls exchange of dat
a between main memory and the I/O device.
Interrupts I/O:
An alternative scheme for dealing with I/O is the interrupt driven method. An in
terrupt is a signal to the microprocessor from a device that requires attention.
A device controller puts an interrupt signal on the bus when it needs CPUs attent
ion when CPU receives an interrupt, It saves its current state and invokes the a
ppropriate interrupt handler using the interrupt vector (addresses of OS routine
s to handle various events).
Kernel I/O Subsystem
Kernel I/O Subsystem is responsible to provide many services related to I/O. Fol
lowing are some of the services provided.
Scheduling Kernel schedules a set of I/O requests to determine a good order in w
hich to execute them. When an application issues a blocking I/O system call, the
request is placed on the queue for that device. The Kernel I/O scheduler rearra
nges the order of the queue to improve the overall system efficiency and the ave
rage response time experienced by the applications.
Buffering Kernel I/O Subsystem maintains a memory area known as buffer that stor
es data while they are transferred between two devices or between a device with
an application operation. Buffering is done to cope with a speed mismatch betwee
n the producer and consumer of a data stream or to adapt between devices that ha
ve different data transfer sizes.
Caching Kernel maintains cache memory which is region of fast memory that holds
copies of data. Access to the cached copy is more efficient than access to the o
riginal.
Spooling and Device Reservation A spool is a buffer that holds output for a devi
ce, such as a printer, that cannot accept interleaved data streams. The spooling
system copies the queued spool files to the printer one at a time. In some oper
ating systems, spooling is managed by a system daemon process. In other operatin
g systems, it is handled by an in kernel thread.
Error Handling An operating system that uses protected memory can guard against
many kinds of hardware and application errors.

File Access Mechanisms:


File access mechanism refers to the manner in which the records of a file may be
accessed. There are several ways to access files
Sequential access
Direct/Random access
Indexed sequential access
Space Allocation:
Files are allocated disk spaces by operating system. Operating systems deploy fo
llowing three main ways to allocate disk space to files.
Contiguous Allocation:
Each file occupies a contiguous address space on disk.
Assigned disk address is in linear order.
Linked Allocation:
Each file carries a list of links to disk blocks.
Directory contains link / pointer to first block of a file.
Indexed Allocation:
Each file has its own index block which stores the addresses of disk space occup
ied by the file.
Directory contains the addresses of index blocks of files.

Você também pode gostar