Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- // create the linked list structure of the memory block
- struct MemoryBlock {
- int beginAddr;
- int endAddr;
- int hole;
- int Pro_ID;
- struct MemoryBlock* next;
- };
- typedef struct MemoryBlock node;
- // declare the starting pointer of the linked list as NULL
- node* start = NULL;
- //initialize and return a free memory block/node
- node* createNode(int beginAddr, int endAddr)
- { //takes the starting address and the ending address as input
- node* temp = (node*)malloc(sizeof(node)); //allocate memory using malloc() function
- temp->beginAddr = beginAddr;
- temp->endAddr = endAddr;
- temp->hole = 1; //hole
- temp->Pro_ID = -1; //means there's no process going on
- return temp; //returns the
- }
- //if the size of the memory block/node >= the size of the process, a process is assigned to the block
- void addProcess(int size, int pid) {
- node* ptr = start;
- while(ptr != NULL) {
- if ((ptr->endAddr - ptr->beginAddr) >= size && ptr->hole==1)
- {
- node* new_node = createNode(ptr->beginAddr + size, ptr->endAddr);
- ptr->endAddr = (ptr->beginAddr + size-1);
- ptr->hole = 0; //Memory block is occupied
- ptr->Pro_ID = pid; //the Process id of the Process occupied
- new_node->next = ptr->next;
- ptr->next = new_node; // points to the next node/block
- break;
- }
- ptr = ptr->next;
- }
- }
- //aadds the created node to the list
- void addNode(node* new_node) {
- if (start == NULL) {
- start = new_node;
- }
- else
- {
- node* ptr = start;
- while (ptr->next != NULL) {
- ptr = ptr->next;
- }
- ptr->next = new_node;
- }
- }
- int deleteProcess(node *start, int del_pid)
- {
- node* prePtr = start;
- node* ptr = start;
- if(del_pid<0)
- {
- printf("Invalid Process ID!\n");
- return 0;
- }
- while(ptr != NULL)
- {
- if (ptr->Pro_ID == del_pid)
- {
- ptr->hole = 1;
- ptr->Pro_ID = -1;
- if(ptr==prePtr)
- {
- return 1;
- }
- if(ptr->next == NULL)
- {
- return 1;
- }
- return 1;
- }
- prePtr = ptr;
- ptr = ptr->next;
- }
- printf("No match for Process ID %d\n", del_pid);
- return 0;
- }
- //Snapshot of the current memory blocks
- void printSnapshot(node *start){
- node* temp = start;
- int size;
- printf("Process ID\tSize\tBegin Address\t\tEnd Address\tHole\n");
- printf("-----------------------------------------------------------------------\n");
- printf("OS\t\t"); //ProcessID of the node
- printf("400\t\t");
- printf("0\t\t"); //Starting address of the node
- printf("399\t\t");
- printf("0\t\n");
- while(temp != NULL) //while the node is not empty
- {
- printf("%d\t\t", temp->Pro_ID); //ProcessID of the node
- printf("%d\t\t", temp->endAddr - (temp->beginAddr - 1)); //Size of the node
- printf("%d\t\t", temp->beginAddr);
- printf("%d\t\t", temp->endAddr);
- printf("%d\t\t\n", temp->hole);
- temp = temp->next;
- }
- }
- int main(){
- int choice;
- int pid, size;
- int del_pid;
- node* tmp=createNode(400, 2560); //Initialize memory block
- addNode(tmp);
- //printSnapshot(start);
- addProcess(600,1);
- //printSnapshot(start);
- addProcess(200,2);
- printSnapshot(start);
- //deleteProcess(start,1);
- //printSnapshot(start);
- //addProcess(700,3);
- //printSnapshot(start);
- //deleteProcess(start,1);
- //printSnapshot(start);
- printf("\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement