Now let's see about segmentation in operating system. Segmentation is mainly useful in order to maintain modular structure of a program. So if we want to maintain modular structure of a program, then we use segmentation. The major drawback of paging is in a paging, a module may resides in different pages of the process. let us take a c program any c program is a collection of functions so let we have three functions like the first function is deposit function the second function is withdrawal function the second function is cancel your own function so totally so this is nothing but our process so totally our program or process contains three functions so first function deposit function second function withdrawal function third function cancel function here there is a possibility that deposit function may stores in different pages of a process let it occupies three pages of the process and also here there is a possibility that withdrawal function may resides in some 10 pages of the process so that means in pacing We are not maintaining any modular structure.
So modular structure means each partition should contain only one function. So this partition, this function should not occupy more than one page. So what is the major problem with paging?
It doesn't maintains modular structure. Same method may resides in multiple pages of the process and multiple frames of the main memory. so in order to overcome that problem we use a segmentation so segmentation is a non-contiguous memory allocation just like pacing so we know what is non-contiguous memory allocation here the segments need not be stored in contiguous memory locations wherever there is a free partition then the operating system allocates that partition for the corresponding segment so let's see what is a segment Segment is nothing but a method or a function or some object or some class anything. Segment means a collection of statements.
Generally a function will be called as a segment. Now let us see the memory allocation for segments. Here the memory allocation for the segments is similar to paging process only.
So the given process is divided into a number of partitions. But here the size of a partition. is similar to the size of a segment. So the size of a partition depends upon the size of a segment.
Suppose if a segment size is 10 kb, then 10 kb of the partition will be allocated for the segment. Suppose if a segment size is 30 kb, then 30 kb of the partition will be allocated for the segment. So likewise the main memory is also divided into a number of partitions.
That depends upon the size of the segment. So operating system allocates that. a free frame for the corresponding segment now let's see this example so that we can get the perfect area segmentation hardware cpu generates a logical address in order to execute an instruction so that logical address is divided into two parts the first part specifies segment number the second part specifies offset offset means a location inside the a location inside the segment here the segment number will be given as an index or input to the segment table we know that every segment table contains base address and limit address so let's see this example here this is our process so our process totally contains three segments the first segment is deposit the second segment is withdrawn third segment is some cancelled account cancel here segment is nothing but a method only okay so this is segment zero this is segment one this is segment two if you take the first part of the main memory is operating system so this is segment zero for segment zero what is the base address base address means starting address of the segment for segment zero the base address is hundred whereas this limit specifies length of the segment so what is the length of the segment hundred so let us assume that here cpu is accessing segment one so what is the base address of the segment here the base address is 200. Variance, what is the limit of the segment? So 200, 350. So limit means length of the segment. So 350 minus 200 means?
150 is the limit here. So here while accessing an instruction CPU generates the logical address. Logical address contains segment number. Here what is the segment number here? 1. So this 1 will be passed to the segment table so that we will get the base address and the limit address.
Here let us assume that the CPU is accessing segment 1. Let here the offset is 50. What is the offset here? 50 is the offset here. So here this 50. 50 will be compared with length of the segment offset means a particular location inside the segment here how many locations this segment contains this is the segment totally contains 150 instructions so out of 150 instruction which instruction we are executing that is nothing but offset so let we are executing 50th instruction so the offset is 50 so we have to compare whether this offset is less than the limit or not if this offset is less than the limit less than the limit of the segment so then only the segment is accessing the valid instruction so here so this is nothing but limit so what is the limit of the segment 150 and what we are doing here we are using a comparative less than comparator so here we are checking whether this offset is less than the limit or not here what is the offset here 50 is the offset what is the limit of the segment 150 so 50 is less than 150 d is less than limit so condition is true so we have to execute the exact instruction in the segment so how to get exact instruction here we are executing 50th offset here 50th instruction here so that is nothing but 250 50th instruction is nothing but here the base address is 200 so 250th instruction so how to get 250 here the base address is 200 where he has offset is 50. so if we add 200 with 50 then it will give the exact location in the physical memory main memory so this is nothing but better base address so with the help of yada we are adding the base address with the segment so this d value and this base value are carried together so it will use the exact physical address in the main memory so let us assume that here offset is some 200 let us assume that here offset is 200 whereas what is the length of the segment 150 so these two are compared here so what is d value 200 what is limit value 150 so 200 is less than 150 so condition is false so what will happen it will generate whenever the condition is false then this node will be executed so operating system will generate a trap trap trap means interrupt so that that segment execution will be terminated that means this segment one is trying is trying to access other segments so operating system generates the trap so that that process will be terminated so this is uh this diagram is very very simple let's see one more time so cpu generates a logical address that logical address is divided into two parts segment number and offset segment number will be given as the input to the segment table next with the help of the comparator we have to check whether this offset is less than the limit of the segment or not if this offset is less than the limit of the if this offset is less than the length of the segment then we can say that it is a valid instruction so in order to get the exact physical address we have to add this base address of the segment with the corresponding offset so it will use the exact physical address in the main memory so that cpu executes that instruction so this is about segmentation