FZU memory management 1893

Source: Internet
Author: User

FZU memory management 1893

E-Memory Management

Time Limit: 1000 MS Memory Limit: 32768KB 64bit IO Format: % I64d & % I64u

Description

 

When dynamically allocating memory for a process, the operating system must manage it. One way to manage memory is to maintain a table that records memory usage to Simulate Memory storage. Either of the tables may be one of the following: A memory area allocated to a process or an idle memory area. Each table item in the table contains the following items: Indicators of idle or used memory areas, memory size, and pointers to the next table item.
A table item generally has a parent table item and a successor table item (unless this table item is the bottom or top of the memory ), the pre-table items and subsequent table items of the used Partition Table items may be used Partition Table items or idle Partition Table items, the pre-table items and the subsequent table items of the table items in the free zone can only be used Partition Table items, because if the two table items in the free zone are adjacent to each other, we can merge them into a table item in the free zone, and the memory size is the sum of the table items in the two free zones.
When the system creates a new process, there are many ways to allocate memory for the newly created process. Here we assume that the memory occupied by the new process is known, and it is only possible to allocate continuous memory space to the process. In this question, we use the best adaptation algorithm to allocate memory. The best adaptation algorithm searches for the entire table, finds the smallest available idle zone (if there are multiple, take the top of the table), and assigns the new process to the top of the idle zone, if the remaining memory is still used as the table item in the idle zone, and is the next table item in the new used zone table item.
When the system ends a process, find the corresponding used Partition Table item in the entire table and change it to the idle Partition Table item.
Your task is to write a program to implement such a memory management process. In this question, the memory table has only one table item in the idle memory area, and the memory size is 100 units. Input

 

 

The first act of the input data is an integer T, indicating that there is a T group of input data.
Each row of data in each group is a system command, including "Create", "Delete", "Print", and "End". The parameters following each command are as follows:
Create: two integers: I and size. I indicates the process number of the new process. I> 0. size indicates the memory size occupied by the new process. size> 0, enter the process number to ensure that the new process is different from the existing process number.
Delete: an integer I, I> 0, indicating the process number to end.
Print: No parameter.
End: No parameter. If no processing is performed, the system terminates the command.
Note: Input ensures that the number of table items cannot exceed 100. Output

 

 

For the Create command, if a new process is successfully created, a line "Create process I of size s successfully!" Is output !", Among them, I is the process number of the new process, and s is the memory size occupied by the new process. If there is not enough memory in the idle zone, output a line "No enough memory !".
For the Delete command, if the process is successfully ended, output a line "Delete process I of size s successfully !", Where I is the process Number of the End process, and s is the memory size occupied by the process; if the memory does not contain the process, output a line "No such process !".
For the Print command, the memory table is output sequentially from the header, and each row outputs a table item. The input table item format is "P process number memory size ", the table item in the idle area is in the format of "H memory size ".
Do not perform any processing on the End command to terminate the program. Sample Input

 

2

Create 1 30

Create 2 20

Create 3 30

Print

Create 4 100

Delete 4

Delete 2

Print

Delete 3

Print

End

Create 1 100

Print

End

Sample Output

Create process 1 of size 30 successfully!

Create process 2 of size 20 successfully!

Create process 3 of size 30 successfully!

P 1 30

P 2 20

P 3 30

H 20

No enough memory!

No such process!

Delete process 2 of size 20 successfully!

P 1 30

H 20

P 3 30

H 20

Delete process 3 of size 30 successfully!

P 1 30

H 70

Create process 1 of size 100 successfully!

P 100

 

In fact, this question can be written step by step based on the meaning of the question. It is simple, but annoying. Pay attention to the details, especially the operations on the head and end should be calculated separately, also, do not forget to merge adjacent empty memory. It is found that many people use the linked list to write it, indicating that they haven't learned it yet, so they have written it with vector, which should be well understood.

The Code is as follows:

 

#include
   
    #include
    
     #include
     
      #include#include
      
       using namespace std;char str[100];int arr[105];struct node{int id;char state;int size;};vector
       
         G;int main(){int t, tmp, msize, flag, a, j;scanf("%d", &t);while(t--){node in;in.id = 0;in.state = 'H';in.size = 100;G.push_back(in);while(~scanf("%s", str)){if(strcmp(str, "End") == 0) break;if(strcmp(str, "Create") == 0) {scanf("%d%d", &in.id, &in.size);tmp = 0,  msize = 200;for(int i = 0; i < G.size(); i++){if(G[i].state == 'H'){if(G[i].size >= in.size && G[i].size < msize){msize = G[i].size;tmp = i;}}}if(msize == 200) printf("No enough memory!\n");else{in.state = 'P';G[tmp].size -= in.size;G.insert(G.begin()+tmp, in);if(G[tmp].size == 0){G.erase(G.begin() + tmp);}printf("Create process %d of size %d successfully!\n", in.id, in.size);}}if(strcmp(str, "Print") == 0){for(int i = 0; i < G.size(); i++){if(G[i].state == 'P')printf("%c %d %d\n", G[i].state, G[i].id, G[i].size);elseif(G[i].size != 0)printf("H %d\n", G[i].size);}}if(str[0] == 'D'){flag = 1;scanf("%d", &a);for(j = 0; j < G.size(); j++){if(G[j].id == a && G[j].state == 'P'){printf("Delete process %d of size %d successfully!\n", a, G[j].size);a = 0;G[j].state = 'H';if(j == 0){if(G[1].state == 'H'){G[0].size += G[1].size;G.erase(G.begin() + 1);}}else{if(j == G.size()){if(G[j-1].state == 'H'){G[j-1].size += G[j].size;G.erase(G.begin() + j);}}else{if(G.size() > 1){if(G[j-1].state == 'H'){G[j].size += G[j-1].size;G.erase(G.begin()+j-1);flag = 0;}if(G[j+flag].state == 'H'){G[j+flag-1].size += G[j+flag].size;G.erase(G.begin()+j+flag);}}break;}}}}if(a != 0) printf("No such process!\n");}}G.clear();}return 0;}
       
      
     
    
   


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.