The above segment descriptor describes a memory segment in


following online course first:
• C Tutorial

Further information about C is available here:

• C and C++ in 5 days
• https://www2.its.strath.ac.uk/courses/c/
In this exercise, we simulate a memory allocation (we just want to see the effect of fragmentation and of compacting memory fragments into larger ones). For this, we manage a list of memory segments in our program:

typedef struct segment {
int start ;
int size ;
int status ;
struct segment * next ;
} segment_t ;
The above segment descriptor describes a memory segment in terms of its start address, its size and whether it is ALLOCATED or FREE. This descriptor is part of a list (contains a pointer to next segment descriptor). Initially, there is only one segment with status == FREE, describing the complete memory as one segment.

Two functions have to be implemented: mem_alloc() and mem_free(). With mem_alloc(), a junk of memory is allocated: it takes as a parameter the size of memory required and returns a pointer to the segment created or NULL, if not enough free memory can be found. With mem_free(), a segment can be freed up again. If there are two free segments adjacent to each other, they have to be combined into one segmengmentation and of compacting memory fragments into larger ones). For this, we manage a list of memory segments in our program:
typedef struct segment {
int start ;
int size ;
int status ;
struct segment * next ;
} segment_t ;
The above segment descriptor describes a memory segment in terms of its start address, its size and whether it is ALLOCATED or FREE. This descriptor is part of a list (contains a pointer to next segment descriptor). Initially, there is only one segment with
status == FREE, describing the complete memory as one segment.

Two functions have to be implemented: mem_alloc() and mem_free(). With mem_alloc(), a junk of memory is allocated: it takes as a parameter the size of memory required and returns a pointer to the segment created or NULL, if not enough free memory can be found. With mem_free(), a segment can be freed up again. If there are two free segments adjacent to each other, they have to be combined into one segment (one of the segment nodes has to be removed from the list).

The prototype for mem_alloc(): segment_t * mem_alloc( int size ) A pointer to the allocated segment is returned.
The prototype for mem_free(): void mem_free ( segment_t * segment ) It takes the segment to be freed as a parameter.
Over time, the memory will become fragmented and a call to mem_alloc() will fail, as no segment large enough may be available. Thefunction mem_alloc() therefore has to initiate a compaction of free memory : all allocated segments should be moved towards the top of the list and all free segments to the bottom of the list. When all free segments

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: The above segment descriptor describes a memory segment in
Reference No:- TGS01245179

Expected delivery within 24 Hours