TP1: Add solution from zip

This commit is contained in:
augustin64 2024-03-26 17:06:15 +01:00
parent 26d2405496
commit 0f6830b34d
7 changed files with 335 additions and 0 deletions

16
TP/TP1/rendu/Makefile Normal file
View File

@ -0,0 +1,16 @@
all: build/test
build/test: test.c build/vmap.o build/mmap.o build/linked_list.o
gcc -o test test.c vmap.o mmap.o linked_list.o -w
build/mmap.o: mmap.c mmap.h linked_list.c linked_list.h
gcc -c mmap.c
build/vmap.o: vmap.c vmap.h
gcc -c vmap.c
build/linked_list.o: linked_list.c linked_list.h
gcc -c linked_list.c
clean:
rm -f build/*

125
TP/TP1/rendu/linked_list.c Normal file
View File

@ -0,0 +1,125 @@
// linked list
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linked_list.h"
struct linkedList* createLinkedList() {
struct linkedList *list = (struct linkedList*) malloc(sizeof(struct linkedList));
list->head = NULL;
return list;
}
//insert link at the first location
void insertFirst(struct linkedList *list, int key, void *data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
//point it to old first node
link->next = list->head;
//point first to new first node
list->head = link;
}
//delete first item
struct node* deleteFirst(struct linkedList *list) {
if (list->head == NULL) {
return NULL;
}
//save reference to first link
struct node *tempLink = list->head;
//mark next to first link as first
list->head = list->head->next;
//return the deleted link
return tempLink;
}
//find a link with given key
struct node* find(struct linkedList *list, int key) {
//start from the first link
struct node* current = list->head;
//if list is empty
if(list->head == NULL) {
return NULL;
}
//navigate through list
while(current->key != key) {
//if it is last node
if(current->next == NULL) {
return NULL;
} else {
//go to next link
current = current->next;
}
}
//if data found, return the current Link
return current;
}
//delete a link with given key
struct node* deleteElement(struct linkedList *list, int key) {
//start from the first link
struct node* current = list->head;
struct node* previous = NULL;
//if list is empty
if(list->head == NULL) {
return NULL;
}
//navigate through list
while(current->key != key) {
//if it is last node
if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
//found a match, update the link
if(current == list->head) {
//change first to point to next link
list->head = list->head->next;
} else {
//bypass the current link
previous->next = current->next;
}
return current;
}
//is list empty
bool isEmpty(struct linkedList *list) {
return list->head == NULL;
}
int length(struct linkedList *list) {
int length = 0;
struct node *current;
for(current = list->head; current != NULL; current = current->next) {
length++;
}
return length;
}

View File

@ -0,0 +1,19 @@
#include <stdbool.h>
struct node {
void *data;
int key;
struct node *next;
};
struct linkedList {
struct node *head;
};
struct linkedList* createLinkedList();
void insertFirst(struct linkedList *list, int key, void *data);
struct node* deleteFirst(struct linkedList *list);
struct node* find(struct linkedList *list, int key);
struct node* deleteElement(struct linkedList *list, int key);
bool isEmpty(struct linkedList *list);
int length(struct linkedList *list);

140
TP/TP1/rendu/mmap.c Normal file
View File

@ -0,0 +1,140 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "mmap.h"
struct MMap* mmap_init(){
struct MMap *mmap = malloc(sizeof(struct MMap));
mmap->size = 4096*100;
mmap->memory = malloc(mmap->size);
mmap->count = 0;
mmap->support = malloc(sizeof(struct PageInfo));
mmap->support->data = malloc(4096);
mmap->free_list = createLinkedList();
// insert 100 pages in the free list
for (int i = 0; i < 100; i++) {
struct PageInfo *page = malloc(sizeof(struct PageInfo));
page->data = mmap->memory + i*4096;
page->key = i;
insertFirst(mmap->free_list, i, page);
}
mmap->alloc_list = createLinkedList();
mmap->swap = fopen("/tmp/myswap", "wb+");
int tmp = -1;
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
fwrite(&tmp, sizeof(int), 1, mmap->swap);
}
char tmp2 = '\0';
for (int i = 0; i < 100*4096; i++) {
fwrite(&tmp2, 1, 1, mmap->swap);
}
return mmap;
}
void mmap_clean(struct MMap *mmap) {
free(mmap->memory);
fclose(mmap->swap);
free(mmap);
remove("/tmp/myswap");
}
struct PageInfo* page_alloc(struct MMap *mmap) {
struct PageInfo *page = NULL;
page = page_free(mmap);
if(page!=NULL) {
insertFirst(mmap->alloc_list, page->key, page);
mmap->count++;
}
return page;
}
struct PageInfo* page_free(struct MMap *mmap) {
struct PageInfo *page = NULL;
struct node *node = deleteFirst(mmap->free_list);
if (node != NULL) {
page = node->data;
}
return page;
}
int check_page_free_list(struct MMap *mmap) {
return length(mmap->free_list);
}
int check_page_alloc(struct MMap *mmap) {
return length(mmap->alloc_list);
}
int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
int key = -1;
fseek(mmap->swap, 0, SEEK_SET);
if( ferror(mmap->swap) ) {
return -1;
}
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
fread(&key, sizeof(int), 1, mmap->swap);
if( ferror(mmap->swap) ) {
return -1;
}
if (key == -1) {
fseek(mmap->swap, i, SEEK_SET);
if( ferror(mmap->swap) ) {
return -1;
}
fwrite(&page->key, sizeof(int), 1, mmap->swap);
if( ferror(mmap->swap) ) {
return -1;
}
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
if( ferror(mmap->swap) ) {
return -1;
}
fwrite(page->data, 4096, 1, mmap->swap);
if( ferror(mmap->swap) ) {
return -1;
}
return 0;
}
}
return -1;
}
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
int key = -1;
fseek(mmap->swap, 0, SEEK_SET);
if( ferror(mmap->swap) ) {
return NULL;
}
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
int ret = fread(&key, sizeof(int), 1, mmap->swap);
if( ferror(mmap->swap) ) {
return NULL;
}
if (key == page->key) {
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
if( ferror(mmap->swap) ) {
return NULL;
}
fread(page->data, 4096, 1, mmap->swap);
if( ferror(mmap->swap) ) {
return NULL;
}
fseek(mmap->swap, i, SEEK_SET);
if( ferror(mmap->swap) ) {
return NULL;
}
int tmp = -1;
fwrite(&tmp, sizeof(int), 1, mmap->swap);
if( ferror(mmap->swap) ) {
return NULL;
}
return page;
} else {
return NULL;
}
}
return NULL;
}

27
TP/TP1/rendu/mmap.h Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include "linked_list.h"
struct MMap {
void *memory;
int size;
int count;
struct PageInfo *support;
struct linkedList *free_list;
struct linkedList *alloc_list;
FILE *swap;
};
struct PageInfo {
int key;
void *data;
};
struct MMap* mmap_init();
void mmap_clean(struct MMap *mmap);
struct PageInfo* page_alloc(struct MMap *mmap);
struct PageInfo* page_free(struct MMap *mmap);
int check_page_free_list(struct MMap *mmap);
int check_page_alloc(struct MMap *mmap);
int move_to_swap(struct MMap *mmap, struct PageInfo *page);
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page);

8
TP/TP1/rendu/test.c Normal file
View File

@ -0,0 +1,8 @@
#include <string.h>
#include "mmap.h"
int main(int argc, char **argv) {
// Some really cool tests!
return 0;
}

BIN
TP/TP1/sujet.pdf Normal file

Binary file not shown.