Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #define PARTITION_FREE 0
- #define PARTITION_ALLOCATED 1
- #define PROCESS_UNLOADED 0
- #define PROCESS_LOADED 1
- #define DEFAULT_SPACING 10
- struct Partition {
- int id;
- int size;
- int freeSpace;
- int status;
- };
- struct Process {
- int id;
- int size;
- int status;
- };
- struct InfoTable {
- int entriesCount;
- int headersCount;
- //number of spaces between columns
- int spacing;
- char **headers;
- char ***entries;
- };
- struct Process createProcess(int id, int size) {
- struct Process newProcess = {id, size, PROCESS_UNLOADED};
- return newProcess;
- }
- struct Partition createPartition(int id, int size) {
- int freeSpace = size;
- struct Partition newPartition = {id, size, freeSpace, PARTITION_FREE};
- return newPartition;
- }
- struct Partition extractPartitionFrom(struct Partition parent, int newPartitionSize) {
- int newPartitionFreeSpace = newPartitionSize;
- //deallocate space from Partition parent to add it to the new Partition
- parent.freeSpace = parent.freeSpace - newPartitionSize;
- return createPartition(parent.id + 1, newPartitionSize);
- }
- void readPartitions(struct Partition partitionList[], int partitionCount) {
- for (int i = 0; i < partitionCount; i++) {
- int size;
- printf("La taille de partition Numero %d =", i + 1);
- scanf("%d", &size);
- partitionList[i] = createPartition(i + 1, size);
- }
- }
- void readProcesses(struct Process processList[], int processCount) {
- for (int i = 0; i < processCount; i++) {
- int size;
- printf("La taille de processus Numero %d demandant l'allocation=", i + 1);
- scanf("%d", &size);
- processList[i] = createProcess(i + 1, size);
- }
- }
- char *resolvePartitionStatus(int statusCode) {
- switch (statusCode) {
- case PARTITION_FREE:
- return "LIBRE";
- case PARTITION_ALLOCATED:
- return "ALLOUE";
- default:
- return "STATE INCONNU";
- }
- }
- char *resolveProcessStatus(int statusCode) {
- switch (statusCode) {
- case PROCESS_UNLOADED:
- return "NON CHARGE";
- case PROCESS_LOADED:
- return "CHARGE";
- default:
- return "STATE INCONNU";
- }
- }
- /**
- *
- * An Utility function to allocate memory for a string
- *
- */
- static void allocateString(char *string, int size) {
- string = malloc((int) ((ceil(log10(size)) + 1) * sizeof(char)));
- }
- char ***createMatrixOfStrings(int rows, int columns, int maxCharacterCount) {
- char ***matrix = malloc(rows * sizeof(char **));
- for (int i = 0; i < rows; ++i) {
- matrix[i] = malloc(columns * sizeof(char *));
- for (int j = 0; j < columns; ++j) {
- matrix[i][j] = malloc(maxCharacterCount * sizeof(char));
- }
- }
- return matrix;
- }
- struct InfoTable
- createPartitionInfoTable(struct Partition partitionList[], int partitionCount) {
- printf("\n");
- int headerCount = 3;
- char **headers = malloc(headerCount * sizeof(char *));
- headers[0] = "Partition N";
- headers[1] = "Size(KO)";
- headers[2] = "State";
- char ***entries = createMatrixOfStrings(partitionCount, headerCount, 50);
- for (int i = 0; i < partitionCount; ++i) {
- struct Partition currentPartition = partitionList[i];
- sprintf(entries[i][0], "%d", currentPartition.id);
- sprintf(entries[i][1], "%d", currentPartition.size);
- sprintf(entries[i][2], "%s", resolvePartitionStatus(currentPartition.status));
- }
- struct InfoTable resultTable = {partitionCount, headerCount, DEFAULT_SPACING, headers, entries};
- return resultTable;
- }
- struct InfoTable createProcessInfoTable(struct Process processList[], int processCount) {
- printf("\n");
- int headerCount = 3;
- char **headers = malloc(headerCount * sizeof(char *));
- headers[0] = "Processus N";
- headers[1] = "Size(KO)";
- headers[2] = "State";
- char ***entries = createMatrixOfStrings(processCount, headerCount, 50);
- for (int i = 0; i < processCount; ++i) {
- struct Process currentProcess = processList[i];
- sprintf(entries[i][0], "%d", currentProcess.id);
- sprintf(entries[i][1], "%d", currentProcess.size);
- sprintf(entries[i][2], "%s", resolveProcessStatus(currentProcess.status));
- }
- struct InfoTable resultTable = {processCount, headerCount, DEFAULT_SPACING, headers, entries};
- return resultTable;
- }
- void displayInfoTable(struct InfoTable infoTable) {
- char **headers = infoTable.headers;
- int headerCount = infoTable.headersCount;
- for (int i = 0; i < headerCount; ++i) {
- printf("%-*s", strlen(headers[i]) + infoTable.spacing, headers[i]);
- }
- printf("\n");
- char ***entries = infoTable.entries;
- int entriesCount = infoTable.entriesCount;
- for (int i = 0; i < entriesCount; ++i) {
- char **entry = entries[i];
- for (int j = 0; j < headerCount; ++j) {
- printf("%-*s", strlen(headers[j]) + infoTable.spacing, entry[j]);
- }
- printf("\n");
- }
- }
- void allocateMemoryForProcess(struct Process process, struct Partition *partitionList, int partitionCount) {
- }
- void onNewPartitionAdded(struct Partition partitionList[], int partitionCount, struct Partition newPartition) {
- }
- void readPartitionCount(int *partitionCount) {
- printf("Veuillez saisir le nombre de partitions :>>");
- scanf("%d", partitionCount);
- }
- void readProcessCount(int *processCount) {
- printf("\nVeuillez saisir le nombre de processus demandant l'allocation d'une partition :>>");
- scanf("%d", processCount);
- }
- struct Partition *
- addPartitionAt(struct Partition *partitionList, int *partitionCount, struct Partition toAdd, int index) {
- int newPartitionCount = *partitionCount + 1;
- struct Partition *newPartitionList = malloc(newPartitionCount * sizeof(*newPartitionList));
- int j = 0;
- for (int i = 0; i < *partitionCount; i++, j++) {
- if (i == index) {
- newPartitionList[i] = toAdd;
- newPartitionList[i].id = index + 1;
- newPartitionList[i + 1] = partitionList[i];
- newPartitionList[i + 1].id = index + 2;
- j++;
- } else {
- newPartitionList[j] = partitionList[i];
- newPartitionList[j].id = j + 1;
- }
- }
- *partitionCount = newPartitionCount;
- return newPartitionList;
- }
- /**
- * Memory allocation strategies (algorithmes)
- * */
- void
- applyFirstFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
- int processCount) {
- }
- void
- applyBestFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
- int processCount) {
- }
- void
- applyWorstFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
- int processCount) {
- }
- void
- applyNestFitStrategy(struct Partition *partitionList, int *partitionCount, struct Process *processList,
- int processCount) {
- }
- int main() {
- int partitionCount = 0, processCount = 0;
- readPartitionCount(&partitionCount);
- struct Partition *partitionList = malloc(partitionCount * sizeof(*partitionList));
- readPartitions(partitionList, partitionCount);
- //struct Partition newPartition = createPartition(48755, 544);
- //partitionList = addPartitionAt(partitionList, &partitionCount, newPartition, 3);
- displayInfoTable(createPartitionInfoTable(partitionList, partitionCount));
- /*readProcessCount(&processCount);
- struct Process *processList = malloc(processCount * sizeof(*processList));
- readProcesses(processList, processCount);
- displayInfoTable(createProcessInfoTable(processList, processCount));*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement