0% found this document useful (0 votes)
4 views1 page

PDF Segmentation in C Programming

The document contains a C program that implements a simple segment table for managing memory segments. It allows for the initialization of the segment table, adding segments with base and limit values, and checking if a given logical address belongs to any segment. The program outputs the segment number and offset if the logical address is valid, or indicates that it does not belong to any segment otherwise.

Uploaded by

ggottamsindhu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

PDF Segmentation in C Programming

The document contains a C program that implements a simple segment table for managing memory segments. It allows for the initialization of the segment table, adding segments with base and limit values, and checking if a given logical address belongs to any segment. The program outputs the segment number and offset if the logical address is valid, or indicates that it does not belong to any segment otherwise.

Uploaded by

ggottamsindhu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<stdlib.h>
#define MAX_SEGMENTS 10
#define MAX_SEG_SIZE100
struct segment{
int base;
int limit;
};
struct SegmentTable
{
int num_segments;
struct segment segments [MAX_SEGMENTS];
};
struct SegmentTable segment_table;
void initialize_segment_table(){
segment_table.num_segments=0;
}
void add_segment(int base,int limit){
if(segment_table.num_segments<MAX_SEGMENTS){
segment_table.segments[segment_table.num_segments].base=base;
segment_table.segments[segment_table.num_segments].limit=limit;
segment_table.num_segments++;
printf("segment added successfully.\n");
}else{
printf ("segment [Link] addmore segments.\n");
}
}
int main(){
int i,logical_address,segment_number,offset;
initialize_segment_table();
add_segment(0,50);
add_segment(100,30);
logical_address=25;
segment_number=-1;
offset=-1;
for(i=0;i<segment_table.num_segments;++i){
if(logical_address
>=segment_table.segments[i].base&&logical_address<segment_table.segments[i].base+se
gment_table.segments[i].limit){
segment_number=i;
offset=logical_address-segment_table.segments[i].base;
break;
}
}
if (segment_number!=-1){
printf ("logical address%d belongs to segment %d with offset%d.\
n",logical_address,segment_number,offset);
}
else{
printf ("logical address%d does not belong to any segment.\n", logical_address);
}
return 0;
}

You might also like