memory management library called sbmem.a
(buddy memory allocation from shared memory). Processes will use your library to
allocate memory space dynamically, instead of using the well-known malloc function.
A shared memory object, i.e., a shared memory segment, needs to be created first.
Memory space allocations will be made from that segment to the requesting
processes. Your library will implement the necessary initialization, allocation and
deallocation routines. Your library will keep track of the allocated and free spaces in
the memory segment. For that it will use the Buddy memory allocation algoritm [1,
2]. Study buddy algorithm first. It is and old and elegant algorithm. It is easy to grasp.
Your library will implement the following functions. The first two functions will be
used by programs that will create or destroy the shared segment to be used by other
• int sbmem_init (int segsize). This function will create and initialize a shared
memory segment of the given size. The given size is in bytes and must be a power
of 2. Memory will be allocated from that segment to the requesting processes
later. If operation is successful, the function will return 0, otherwise, it will return
-1. You will use POSIX shm_open() and ftruncate() functions in the
implementation of this function. If there is already a shared segment, the function
will destroy the segment first, before creating a new one with the same name.
After initialization, the shared segment will be ready to use. That means processes
can make memory allocation requests, and memory can be allocated from this
segment, if available.
• sbmem_remove (). This function will remove the shared memory segment from
the system. You will use shm_unlink () function to implement this. The function
will do all the necessary cleanup. For example, the created semaphore(s) will be
removed as well.
A process that would like to use the library for memory allocations will use the
following functions that you will implement in your library (sbmemlib.c).
• int sbmem_open(). This function will indicate to the library that the process
would like to use the library. In this way, the library can keep track of the
processes that are interested in using the library. The library will map the shared
segment to the virtual address space of the process using the mmap function. If
there are too many processes using the library at the moment, sem_open will
return -1. Otherwise, if the process can use the library, sem_open will return 0.
• void *sbmem_alloc (int reqsize). This function will allocate memory of size n,
which is the next power of 2 greater than or equal to reqsize. The reqsize is in
bytes. For example, if reqsize is 200 (bytes), then the library will allocate memory
of size 256, because 256 is the next power of 2 greater than or equal to 200. If
allocation succeeds, the function will return a pointer to the allocated space. It is
up to the program what to store in the allocated space. NULL will be returned if
memory could not be allocated. This can happen, for example, when there is not