Hello friends
Welcome to Gate Smashers The topic is segmentation. Now, what is the segmentation? Segmentation is a method in which a process will be
divided into parts then we put them into
the main memory. We divide a process
into segments and then we put it in
the main memory. The same concept we
use in the paging. What do we do in paging also By dividing a process
into fixed-sized pages and put them in
the main memory. So what's the difference between
paging and segmentation? Let's understand first what's the difference between
paging and segmentation? In paging whatever
program we have written whatever process
we have written let say there is
a program in C I have saved that program Now what paging will
do to that program without knowing whatever
was written in this it will simply
divide that process and then it is put into
the main memory. So what problem can
emerge due to that One of the problems is
Let say In my program, there is a module or a function of addition. And this is the length
of that function. This will be the length of
the addition module. Now if we are
using paging here paging will simply
divide that module or simply divide that function
into two or further pieces and then we put them
into different locations. Let say this has been
kept in frame f3 and this is kept in
further frame f4. Now, what will happen? When CPU will execute So in that first F3 it executed the instruction
written in F3 But does F3 have a
complete addition model? No! The model of the whole addition
is combined in both F3 and F4. So what will happen When it will execute the addition then he will get
partial code in F3 will not get full code. i.e. immediately
after I execute F3 when the last instruction
will be executed I have to run CPU
and F4 quickly. Means what is written
in frame F4 I have to give that page
to CPU immediately. Otherwise, the addition will
not execute properly here. Here I mean to say that In the paging
without knowing what is the code in it
what's written in the process we simply divide it
into further pages. But segmentation works
from the user's point of view. When we write a program we know that there
is a main program one addition program is there
or addition function is there or subtraction function or
some other functions are there. So what we think All those functions must have
been saved on different pages. but paging doesn't do
anything like that. Paging simply
divide a process or a program on a
fixed-size page. So this problem
may occur there. So maybe CPU has
executed the F3 frame. but it didn't even
get the f4 frame. So here the problem with the page
fault can also occur. So for this, we use
segmentation. What actually segmentation
does is It does not divide the whole
process or program directly It does not
divide the whole process or program
directly like paging. What will it do it will create different segments Let say your program has a function called main. So it has made a
segment of main There can be one
more segment of Let say addition Another segment created Let say one more
segment of subtraction
S2 Stack S3 One more
Square root is there Square root is there. So it will create the segments according
to the code. Means according to
the user's point of view. What users think The number of lines
in the main or the number of lines in
the addition module. Further, it will convert it
in the same segment So here's a
small point In paging, each page
has the same size. But will the size of all segments are
the same here? No! Maybe in the addition module the line of code is low. Maybe the size of
this segment is small. It may have more
line of code. Maybe the size of this
segment is bigger. So what I mean to say Let say we take 5 or 6 Here we take
a total of 6 These are segments Where do we generally
save these segments? We will save these
segments in the main memory. We will put these segments
in the main memory. what we used
to do there We used to divide the
process into pages and we put the pages
in the main memory. But here we are not dividing
into fixed-size pages. Segment can be
of various size this is the important point. But pages are always
of the same size. But segmentation always
run in various sizes. The size of a
the segment is big, some may be small. We put these segments inside the main memory. Now the second point is Like paging does not
know about CPU in the same way
the CPU doesn't have any idea about segmentation. CPU will generate
Logical address We have already discussed that the CPU always
works on logical address. CPU will generate
a logical address I have to assess with
that logical address that which segment
CPU want and for how
far it wants. So through that thing we will be able to find out where is that
segment actually lying in physical or main memory. So here we use two things Whenever we
Let say there is a segment S5 I put S5 here Here 1500 is
representing the base. Base means from where
the segment is starting. After that, here we use the
second point here that is the size. Base address and size What base address
and size is showing that from where that
segment is starting and what is the length of it. and where
it is going. I will explain
this again what are we going to
discuss here first is When CPU will generate
a logical address we have to convert
that logical address into a physical address. Then only we will get the
location of that segment then after fetching
that segment I will give it to the CPU. The same concept
which we use in paging here also our memory management
unit will help in converting logical address we'll convert the logical address
into physical address. Who will help? Again, Memory management unit So how will the memory
management unit convert the logical address into
a physical address? The way to convert it is called a segment table. This means here we will use segment table and that segment table will tell Whatever segment we want how will we fetch it
from the main memory. This is the point. Let's first understand
the segment table There are two things
in segment table base address and size. From where my base
address is starting and this is representing the segment number here. Segment's number. Means 0, 1, 2, 3, 4, 5 Different segments are there. The base address
is representing from where it is starting. Like segment S0 is starting with 3300 Look at here
segment S0 is starting with 3300 and it's limit or size size means 200 we can say 200 byte. i.e 3300+200= 3500 If we add 200 in this i.e the length of
this is up to 200 and its limit is up to 3500. This segment table
will help us how we will convert
the logical address into a physical address. So let's use it. Let say When a CPU will generate a logical
address address That logical address will be divided into two parts. One of them is
segment number and another is
segment size. or it is also called offset. Here the MSB bits
are segment numbers LSB bits will
show segment size. So here logical address whatever bit of the logical
address is coming. Some bits are
segment number and remaining bits
are segment size. If I have s number of bits are representing the
segment number. And d bits are representing
the segment size. So what we will do here is
first of all Whatever logical address
is generated just for example
I am taking Let say logical address is 000/1000 Now whatever this
logical address is First of all,
I have to guess that How many bits are representing
the segment number and how many bits are
representing segment size? As soon as I get to
know these bits are my segment numbers. So let say here I have to access segment 1 where segment 1 is lying
in the main memory I don't know. Because it is non-contiguous Segment can be
randomly anywhere So first of all, I have
to go to segment 1 I went to segment 1 Segment 1 base address of 1800 Here we get to know By going to
address 1800 you will get segment 1. But we also have to keep
another point in mind and that is the d value What d value is
representing here is you have reached 1800 But after that till where you have to
read that data d bits will tell the
length or size. The number of bits
which is given here What they are representing You know the base
address that is 1800 But S1 is saved
from 1800 to 2200. So you want the whole S1 or partially. Means you have to access
only some portion of it So that is the point that is what s and d
is representing here. So I will pick d from here and the value of d
corresponding to S1 that is 400. Here this point representing If d value from here I am
getting d value d value should always
be less than size. d value should always
be less than size. This is what
we are checking. d value means How much CPU
wants to access How far does it
wants to access. If CPU gave the
value of D here let say 500. CPU wants to
read 500 bytes. But when I go to S1 S1 is 1800 and its size is 400. S1 is 400 bytes and CPU wants to
read 500 bytes How it is possible? When the size of segment S1
is only 400 bytes so CPU can read
maximum 400 bytes. Where will the CPU
read 100-byte extras? and obviously, here
we can't access data of other
segments randomly. The problem here is The d value
which is here that d value will always the size which
is given here should be less
than equal to. If the value of
d is 400 then it is fine. This means you want to
access the entire segment. Then there is no problem. What we're checking here is value of d should be
less than equal to size. If it is true then
we will add that value means I know the base
address is 1800 value of d is
let say 400 So I will add
400 in 1800 so what I got is 2200. That means from
1800 to 2200 I have to read
the entire data I have to read
entire segment. So yes
this is what we want. So I have accessed the
entire segment here. If the value of
d was greater then here we
create the trap. The size which is
demanded by the CPU there is no segment
of that size. So, in that case, we have to
create interrupt or trap that is a kind of error. In this case an error
will be created here otherwise normally. Let say the CPU wants
to read segment S3 Segment S3 So here, my earlier bits will represent three values This is just an example.
This is not the actual value. First I went to segment S3 I get to know segment
S3 is on 2300 I went to 2300 There I get to know S3. Now I have to find
value of d Let say value
of d is 200 If value of
d is 200 and here the
corresponding size of segment S3 is 400. That means I don't have to
read entire 400 from that, I have to
read some data In this case 2300 is the base value and what I have added as far as I have to read total 2500. Let say it started from 2300 And till 2500
I read the data and gave it to the CPU. This is the concept How we are using segmentation. What is the advantage of
segmentation over paging? Here we are talking from
the user's point of view. We are not dividing
a process directly without looking
into the program. We not putting it by
simply dividing it. In the segment, we will put related
data in one segment We will divide related data
into proper segments. The second point which is
important here is the size of the segment
maybe different. Size of each segment
can be different but in paging size of
each segment is equal. So this is how we use the
concept of segmentation. Thank you.