Advertisement
HoussemNa12

Untitled

Dec 12th, 2020
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. #define PARTITION_FREE 0
  7. #define PARTITION_ALLOCATED 1
  8.  
  9. #define PROCESS_UNLOADED 0
  10. #define PROCESS_LOADED 1
  11.  
  12. #define DEFAULT_SPACING 10
  13.  
  14. struct Partition {
  15.     int id;
  16.     int size;
  17.     int freeSpace;
  18.     int status;
  19. };
  20.  
  21. struct Process {
  22.     int id;
  23.     int size;
  24.     int status;
  25. };
  26.  
  27. struct InfoTable {
  28.     int entriesCount;
  29.     int headersCount;
  30.     //number of spaces between columns
  31.     int spacing;
  32.     char **headers;
  33.     char ***entries;
  34. };
  35.  
  36. struct Process createProcess(int id, int size) {
  37.     struct Process newProcess = {id, size, PROCESS_UNLOADED};
  38.     return newProcess;
  39. }
  40.  
  41. struct Partition createPartition(int id, int size) {
  42.     int freeSpace = size;
  43.  
  44.     struct Partition newPartition = {id, size, freeSpace, PARTITION_FREE};
  45.     return newPartition;
  46. }
  47.  
  48. struct Partition extractPartitionFrom(struct Partition parent, int newPartitionSize) {
  49.     int newPartitionFreeSpace = newPartitionSize;
  50.     //deallocate space from Partition parent to add it to the new Partition
  51.     parent.freeSpace = parent.freeSpace - newPartitionSize;
  52.  
  53.     return createPartition(parent.id + 1, newPartitionSize);
  54. }
  55.  
  56. void readPartitions(struct Partition partitionList[], int partitionCount) {
  57.     for (int i = 0; i < partitionCount; i++) {
  58.         int size;
  59.         printf("La taille de partition Numero %d =", i + 1);
  60.         scanf("%d", &size);
  61.  
  62.         partitionList[i] = createPartition(i + 1, size);
  63.     }
  64. }
  65.  
  66. void readProcesses(struct Process processList[], int processCount) {
  67.     for (int i = 0; i < processCount; i++) {
  68.         int size;
  69.         printf("La taille de processus Numero %d demandant l'allocation=", i + 1);
  70.         scanf("%d", &size);
  71.  
  72.         processList[i] = createProcess(i + 1, size);
  73.     }
  74. }
  75.  
  76. char *resolvePartitionStatus(int statusCode) {
  77.     switch (statusCode) {
  78.         case PARTITION_FREE:
  79.             return "LIBRE";
  80.         case PARTITION_ALLOCATED:
  81.             return "ALLOUE";
  82.         default:
  83.             return "STATE INCONNU";
  84.     }
  85. }
  86.  
  87. char *resolveProcessStatus(int statusCode) {
  88.     switch (statusCode) {
  89.         case PROCESS_UNLOADED:
  90.             return "NON CHARGE";
  91.         case PROCESS_LOADED:
  92.             return "CHARGE";
  93.         default:
  94.             return "STATE INCONNU";
  95.     }
  96. }
  97.  
  98. /**
  99.  *
  100.  * An Utility function to allocate memory for a string
  101.  *
  102.  */
  103. static void allocateString(char *string, int size) {
  104.     string = malloc((int) ((ceil(log10(size)) + 1) * sizeof(char)));
  105. }
  106.  
  107. char ***createMatrixOfStrings(int rows, int columns, int maxCharacterCount) {
  108.     char ***matrix = malloc(rows * sizeof(char **));
  109.     for (int i = 0; i < rows; ++i) {
  110.         matrix[i] = malloc(columns * sizeof(char *));
  111.         for (int j = 0; j < columns; ++j) {
  112.             matrix[i][j] = malloc(maxCharacterCount * sizeof(char));
  113.         }
  114.     }
  115.     return matrix;
  116. }
  117.  
  118. struct InfoTable
  119. createPartitionInfoTable(struct Partition partitionList[], int partitionCount) {
  120.     printf("\n");
  121.  
  122.     int headerCount = 3;
  123.     char **headers = malloc(headerCount * sizeof(char *));
  124.     headers[0] = "Partition N";
  125.     headers[1] = "Size(KO)";
  126.     headers[2] = "State";
  127.  
  128.     char ***entries = createMatrixOfStrings(partitionCount, headerCount, 50);
  129.  
  130.     for (int i = 0; i < partitionCount; ++i) {
  131.         struct Partition currentPartition = partitionList[i];
  132.         sprintf(entries[i][0], "%d", currentPartition.id);
  133.         sprintf(entries[i][1], "%d", currentPartition.size);
  134.         sprintf(entries[i][2], "%s", resolvePartitionStatus(currentPartition.status));
  135.     }
  136.     struct InfoTable resultTable = {partitionCount, headerCount, DEFAULT_SPACING, headers, entries};
  137.  
  138.     return resultTable;
  139.  
  140. }
  141.  
  142. struct InfoTable createProcessInfoTable(struct Process processList[], int processCount) {
  143.     printf("\n");
  144.  
  145.     int headerCount = 3;
  146.     char **headers = malloc(headerCount * sizeof(char *));
  147.     headers[0] = "Processus N";
  148.     headers[1] = "Size(KO)";
  149.     headers[2] = "State";
  150.  
  151.     char ***entries = createMatrixOfStrings(processCount, headerCount, 50);
  152.  
  153.     for (int i = 0; i < processCount; ++i) {
  154.         struct Process currentProcess = processList[i];
  155.         sprintf(entries[i][0], "%d", currentProcess.id);
  156.         sprintf(entries[i][1], "%d", currentProcess.size);
  157.         sprintf(entries[i][2], "%s", resolveProcessStatus(currentProcess.status));
  158.     }
  159.     struct InfoTable resultTable = {processCount, headerCount, DEFAULT_SPACING, headers, entries};
  160.  
  161.     return resultTable;
  162. }
  163.  
  164. void displayInfoTable(struct InfoTable infoTable) {
  165.     char **headers = infoTable.headers;
  166.     int headerCount = infoTable.headersCount;
  167.  
  168.     for (int i = 0; i < headerCount; ++i) {
  169.         printf("%-*s", strlen(headers[i]) + infoTable.spacing, headers[i]);
  170.     }
  171.  
  172.     printf("\n");
  173.     char ***entries = infoTable.entries;
  174.     int entriesCount = infoTable.entriesCount;
  175.     for (int i = 0; i < entriesCount; ++i) {
  176.         char **entry = entries[i];
  177.         for (int j = 0; j < headerCount; ++j) {
  178.             printf("%-*s", strlen(headers[j]) + infoTable.spacing, entry[j]);
  179.         }
  180.         printf("\n");
  181.     }
  182. }
  183.  
  184. void allocateMemoryForProcess(struct Process process, struct Partition *partitionList, int partitionCount) {
  185.  
  186. }
  187.  
  188. void onNewPartitionAdded(struct Partition partitionList[], int partitionCount, struct Partition newPartition) {
  189.  
  190. }
  191.  
  192. void readPartitionCount(int *partitionCount) {
  193.     printf("Veuillez saisir le nombre de partitions :>>");
  194.     scanf("%d", partitionCount);
  195. }
  196.  
  197. void readProcessCount(int *processCount) {
  198.     printf("\nVeuillez saisir le nombre de processus demandant l'allocation d'une partition :>>");
  199.     scanf("%d", processCount);
  200. }
  201.  
  202. struct Partition *
  203. addPartitionAt(struct Partition *partitionList, int *partitionCount, struct Partition toAdd, int index) {
  204.     int newPartitionCount = *partitionCount + 1;
  205.     struct Partition *newPartitionList = malloc(newPartitionCount * sizeof(*newPartitionList));
  206.     int j = 0;
  207.     for (int i = 0; i < *partitionCount; i++, j++) {
  208.         if (i == index) {
  209.             newPartitionList[i] = toAdd;
  210.             newPartitionList[i].id = index + 1;
  211.             newPartitionList[i + 1] = partitionList[i];
  212.             newPartitionList[i + 1].id = index + 2;
  213.             j++;
  214.         } else {
  215.             newPartitionList[j] = partitionList[i];
  216.             newPartitionList[j].id = j + 1;
  217.         }
  218.     }
  219.  
  220.     *partitionCount = newPartitionCount;
  221.  
  222.     return newPartitionList;
  223. }
  224.  
  225.  
  226. /**
  227.  * Memory allocation strategies (algorithmes)
  228.  * */
  229. void
  230. applyFirstFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
  231.                       int processCount) {
  232.  
  233. }
  234.  
  235. void
  236. applyBestFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
  237.                      int processCount) {
  238.  
  239. }
  240.  
  241. void
  242. applyWorstFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
  243.                       int processCount) {
  244.  
  245. }
  246.  
  247. void
  248. applyNestFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
  249.                      int processCount) {
  250.  
  251. }
  252.  
  253. int main() {
  254.     int partitionCount = 0, processCount = 0;
  255.  
  256.     readPartitionCount(&partitionCount);
  257.     struct Partition *partitionList = malloc(partitionCount * sizeof(*partitionList));
  258.  
  259.     readPartitions(partitionList, partitionCount);
  260.     //struct Partition newPartition = createPartition(48755, 544);
  261.     //partitionList = addPartitionAt(partitionList, &partitionCount, newPartition, 3);
  262.  
  263.     displayInfoTable(createPartitionInfoTable(partitionList, partitionCount));
  264.  
  265.     /*readProcessCount(&processCount);
  266.     struct Process *processList = malloc(processCount * sizeof(*processList));
  267.  
  268.     readProcesses(processList, processCount);
  269.  
  270.     displayInfoTable(createProcessInfoTable(processList, processCount));*/
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement