/* Variable partition recycling Algorithm
Insert the recycled memory into the free primary storage queue for use in the next memory allocation. in this simulation program, the ordering principle of the free primary storage queue is arranged by address (that is, the available idle range of the low address is in front of the queue, therefore, the matching placement policy is the first adaptation method ).
There are four types of partition recycling:
1. The free block in the recovery area (hereinafter referred to as the free block) is adjacent to the free primary storage queue. During the recovery, the free block is directly added to the size of the adjacent free zone.
2. the free block has a lower-neighbor free zone. In this case, you need to point the next pointer of the front to the first address of the free block, and direct the next pointer of the free block to the next node in the lower-neighbor free zone, merge size at the same time.
3. the free block has both the upper and lower neighboring areas. In this case, you need to point the next pointer of the adjacent area on the free block to the next pointing address of the adjacent area under the free block, and merge the sizes of the three available partitions.
4. the free block does not have an idle neighboring area. It is an independent block. insert it into the free primary storage queue. because the queue is already placed by address when the partition is merged, there is no need to sort the elements in the queue.
Simulation Implementation:
First, allocate 256 bytes as the simulated primary memory (hereinafter referred to as the virtual primary memory), and then insert four tasks (task1, task2, task3, task4 ). each partition descriptor occupies 6 bytes, and free_head is the header pointer of the free primary storage queue. when the partition is recycled, the system re-resets the task in the virtual primary storage (to facilitate simulation of four recovery situations ). task1 has a neighboring Free Zone (1st cases), task2 has 4th cases, task3 has 2nd cases, and task4 has 3rd cases. the output information starts from the low address of the virtual primary storage, and the available space (including the partition descriptor size) in the queue is output in sequence ).
*/
/* This program is compiled in TC2.0 through -- iron wood box */
# Include <stdio. h>
# Include <stdlib. h>
Typedef struct PD/* partition descriptor Data Structure */
{
Int flag;
Int size;
Struct PD * next;
} Node, * LINK;
LINK free_head;
LINK task1, task2, task3, task4;/* tasks */
Char * p;
Void Init ()/* initialize tasks and idle zones in the simulated memory */
{
Char * tmp;
LINK cursor;/* temp pointer */
Tmp = p;
Task1 = (LINK) (tmp + 30);/* assignment of each job in advance */
Task2 = (LINK) (tmp + 70 );
Task3 = (LINK) (tmp + 130 );
Task4 = (LINK) (tmp + 196 );
Task1-> flag = 1; task1-> size = 40; task1-> next = NULL;
Task2-> flag = 1; task2-> size = 60; task2-> next = NULL;
Task3-> flag = 1; task3-> size = 30; task3-> next = NULL;
Task4-> flag = 1; task4-> size = 35; task4-> next = NULL;
Free_head = (LINK) tmp;/* free memory queue */
Cursor = free_head;
Cursor-> flag = 0; cursor-> size = 30; cursor-> next = (LINK) (tmp + 160 );
Cursor = cursor-> next;
Cursor-> flag = 0; cursor-> size = 36; cursor-> next = (LINK) (tmp + 231 );
Cursor = cursor-> next;
Cursor-> flag = 0; cursor-> size = 25; cursor-> next = NULL;
}
Void PrintInfo (int n)/* print information about the free primary storage queue */
{
LINK temp;
Temp = free_head;
Printf ("nAfter free task % d, free memory are:", n );
While (temp)
{
Printf ("%-3d", temp-> size );
Temp = temp-> next;
}
Printf ("n ");
}
<