Compare commits
3 Commits
7665dec314
...
df2ed3acda
Author | SHA1 | Date | |
---|---|---|---|
df2ed3acda | |||
b74b678386 | |||
973b6eacd6 |
84
TD/TD8/exercise1a.c
Normal file
84
TD/TD8/exercise1a.c
Normal file
@ -0,0 +1,84 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define N 20
|
||||
#define PROD 5
|
||||
#define CONS 5
|
||||
#define PROD_MAX 500
|
||||
|
||||
int shared_buffer[N];
|
||||
|
||||
void* producteur(void* args) {
|
||||
int j, product = 0;
|
||||
int count = 0;
|
||||
while (1) {
|
||||
int i = -1;
|
||||
while (++i < N) {
|
||||
if (shared_buffer[i] == -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= N) {
|
||||
sleep(0.05);
|
||||
continue;
|
||||
}
|
||||
product = (rand()%500+product)%20000;
|
||||
j = (i+j)%20000;
|
||||
shared_buffer[i] = product;
|
||||
count++;
|
||||
if (!(count % 5000))
|
||||
printf("produit: %d %d\n", j, product);
|
||||
sleep(0.05);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* consommateur(void* args) {
|
||||
int j, product = 0;
|
||||
int count = 0;
|
||||
while (1) {
|
||||
int i = -1;
|
||||
while (++i < N) {
|
||||
if (shared_buffer[i] != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= N) {
|
||||
sleep(0.05);
|
||||
continue;
|
||||
}
|
||||
product = (shared_buffer[i]+product)%20000;
|
||||
j = (i+j)%20000;
|
||||
shared_buffer[i] = -1;
|
||||
count++;
|
||||
if (!(count %5000))
|
||||
printf("résultat: %d %d %d\n", j, product);
|
||||
sleep(0.05);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
for (int i=0; i < N; i++) {
|
||||
shared_buffer[i] = -1;
|
||||
}
|
||||
|
||||
pthread_t tid[PROD+CONS];
|
||||
for (int i=0; i < PROD; i++) {
|
||||
pthread_create(&(tid[i]), NULL, &producteur, NULL);
|
||||
}
|
||||
|
||||
for (int i=0; i < CONS; i++) {
|
||||
pthread_create(&(tid[i+PROD]), NULL, &consommateur, NULL);
|
||||
}
|
||||
|
||||
for (int i=0; i < PROD+CONS; i++) {
|
||||
pthread_join(tid[i], NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
120
TD/TD8/exercise1b.c
Normal file
120
TD/TD8/exercise1b.c
Normal file
@ -0,0 +1,120 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#define N 10
|
||||
#define PROD 5
|
||||
#define CONS 3
|
||||
#define PROD_MAX 500
|
||||
#define DELAY 1
|
||||
|
||||
#define RED "\033[31m"
|
||||
#define RESET "\033[0m"
|
||||
|
||||
|
||||
sem_t sem_prod;
|
||||
sem_t sem_cons;
|
||||
sem_t sem_renderer;
|
||||
|
||||
int shared_buffer[N];
|
||||
pthread_mutex_t lock;
|
||||
|
||||
void* producteur(void* args) {
|
||||
while (1) {
|
||||
sem_wait(&sem_prod);
|
||||
pthread_mutex_lock(&lock);
|
||||
int i = -1;
|
||||
while (++i < N) {
|
||||
if (shared_buffer[i] == -1) {
|
||||
shared_buffer[i] = rand()%PROD_MAX;
|
||||
sem_post(&sem_cons);
|
||||
sem_post(&sem_renderer);
|
||||
//printf("placed@%d !\n", i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= N) {
|
||||
fprintf(stderr, "Can't place my product !\n");
|
||||
exit(1);
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
sleep(DELAY);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* consommateur(void* args) {
|
||||
while (1) {
|
||||
sem_wait(&sem_cons);
|
||||
pthread_mutex_lock(&lock);
|
||||
int i = -1;
|
||||
while (++i < N) {
|
||||
if (shared_buffer[i] != -1) {
|
||||
shared_buffer[i] = -1;
|
||||
//printf("got@%d !\n", i);
|
||||
sem_post(&sem_prod);
|
||||
sem_post(&sem_renderer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= N) {
|
||||
fprintf(stderr, "Can't get a product !\n");
|
||||
exit(1);
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
sleep(DELAY);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* renderer(void* args) {
|
||||
while (1) {
|
||||
sem_wait(&sem_renderer);
|
||||
pthread_mutex_lock(&lock);
|
||||
printf("\r");
|
||||
for (int i=0; i < N; i++) {
|
||||
if (shared_buffer[i] == -1) {
|
||||
printf(RED "%3d " RESET, shared_buffer[i]);
|
||||
} else {
|
||||
printf("%3d ", shared_buffer[i]);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
for (int i=0; i < N; i++) {
|
||||
shared_buffer[i] = -1;
|
||||
}
|
||||
|
||||
pthread_mutex_init(&lock, NULL);
|
||||
|
||||
pthread_t tid[PROD+CONS+1];
|
||||
sem_init(&sem_prod, 0, N);
|
||||
sem_init(&sem_cons, 0, 0);
|
||||
sem_init(&sem_renderer, 0, 1);
|
||||
for (int i=0; i < PROD; i++) {
|
||||
pthread_create(&(tid[i]), NULL, &producteur, NULL);
|
||||
}
|
||||
|
||||
for (int i=0; i < CONS; i++) {
|
||||
pthread_create(&(tid[i+PROD]), NULL, &consommateur, NULL);
|
||||
}
|
||||
|
||||
// Additional renderer thread to display the state of the market
|
||||
pthread_create(&(tid[CONS+PROD]), NULL, &renderer, NULL);
|
||||
|
||||
|
||||
for (int i=0; i < PROD+CONS+1; i++) {
|
||||
pthread_join(tid[i], NULL);
|
||||
}
|
||||
sem_destroy(&sem_cons);
|
||||
sem_destroy(&sem_prod);
|
||||
|
||||
return 0;
|
||||
}
|
146
TD/TD8/exercise2.c
Normal file
146
TD/TD8/exercise2.c
Normal file
@ -0,0 +1,146 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
Ordre des pipes:
|
||||
AB 0
|
||||
AC 1
|
||||
AD 2
|
||||
BI 3
|
||||
CE 4
|
||||
CF 5
|
||||
DH 6
|
||||
EG 7
|
||||
FG 8
|
||||
GI 9
|
||||
HI 10
|
||||
IA 11
|
||||
*/
|
||||
|
||||
int main() {
|
||||
int* fds[12];
|
||||
|
||||
for (int i=0; i < 12; i++) {
|
||||
fds[i] = malloc(sizeof(int)*2);
|
||||
pipe(fds[i]);
|
||||
}
|
||||
|
||||
int i=0;
|
||||
for (; i < 9; i++) {
|
||||
if (fork()) { break; } // Generate 9 processes
|
||||
if (i == 8) { return 0; }
|
||||
}
|
||||
|
||||
int input[3] = {-1, -1, -1};
|
||||
int output[3] = {-1, -1, -1};
|
||||
|
||||
char id = 'A'+i;
|
||||
switch (id) {
|
||||
case 'A': {// A
|
||||
input[0] = 11;
|
||||
output[0] = 0;
|
||||
output[1] = 1;
|
||||
output[2] = 2;
|
||||
} break;
|
||||
case 'B': {// B
|
||||
input[0] = 0;
|
||||
output[0] = 3;
|
||||
} break;
|
||||
case 'C': {// C
|
||||
input[0] = 1;
|
||||
output[0] = 4;
|
||||
output[1] = 5;
|
||||
} break;
|
||||
case 'D': {// D
|
||||
input[0] = 2;
|
||||
output[0] = 6;
|
||||
} break;
|
||||
case 'E': {// E
|
||||
input[0] = 4;
|
||||
output[0] = 7;
|
||||
} break;
|
||||
case 'F': {// F
|
||||
input[0] = 5;
|
||||
output[0] = 8;
|
||||
} break;
|
||||
case 'G': {// G
|
||||
input[0] = 7;
|
||||
input[1] = 8;
|
||||
output[0] = 9;
|
||||
} break;
|
||||
case 'H': {// H
|
||||
input[0] = 6;
|
||||
output[0] = 10;
|
||||
} break;
|
||||
case 'I': {// I
|
||||
input[0] = 3;
|
||||
input[1] = 9;
|
||||
input[2] = 10;
|
||||
output[0] = 11;
|
||||
} break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE* in_stream[3];
|
||||
FILE* out_stream[3];
|
||||
|
||||
for (int j=0; j < 3; j++) {
|
||||
if (input[j] != -1) {
|
||||
in_stream[j] = fdopen(fds[input[j]][0], "r");
|
||||
}
|
||||
if (output[j] != -1) {
|
||||
out_stream[j] = fdopen(fds[output[j]][1], "w");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
printf("%c: got %d %d %d > %d %d %d\n", id, input[0], input[1], input[2], output[0], output[1], output[2]);
|
||||
sleep(1);
|
||||
|
||||
if (id == 'I') {
|
||||
int c;
|
||||
while (1) { // Get things from stdin
|
||||
c = getchar();
|
||||
for (int j=0; j < 3; j++) {
|
||||
if (output[j] != -1) {
|
||||
fputc(c, out_stream[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
while (1) {
|
||||
printf("%c listening...\n", id);
|
||||
|
||||
int c, d;
|
||||
d = -1;
|
||||
for (int j=0; j < 3; j++) {
|
||||
if (input[j] != -1) {
|
||||
c = fgetc(in_stream[j]);
|
||||
if (d != -1 && c != d) {
|
||||
fprintf(stderr, "%c got two different chars: %c vs %c\n", id, c, d);
|
||||
}
|
||||
d = c;
|
||||
}
|
||||
}
|
||||
printf("%c received %c\n", id, c);
|
||||
for (int j=0; j < 3; j++) {
|
||||
if (output[j] != -1) {
|
||||
printf("%c transmits %c to %d\n", id, c, output[j]);
|
||||
fputc(c, out_stream[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int j=0; j < 3; j++) {
|
||||
if (input[j] != -1) {
|
||||
fclose(in_stream[j]);
|
||||
}
|
||||
if (output[j] != -1) {
|
||||
fclose(out_stream[j]);
|
||||
}
|
||||
}
|
||||
}
|
12
TD/TD8/index.md
Normal file
12
TD/TD8/index.md
Normal file
@ -0,0 +1,12 @@
|
||||
# TD 8: Threads and synchronisation
|
||||
|
||||
## Reasoning operating systems: questions
|
||||
|
||||
1. Différent mécanismes pour différentes utilisations:
|
||||
- mutex locks pour gérer l'accès à une ressource
|
||||
- sémaphores pour gérer l'accès à une quantité de variables
|
||||
- variables de condition
|
||||
- spin lock: comme sémaphore mais avec du temps processus
|
||||
2. Cela indique quelles fourchettes sont utilisées,
|
||||
chaque philosophe n'utilisant que 2 fourchettes ou aucune à la fois
|
||||
3. Deadlock possible
|
69
TD/TD9/exercice1.c
Normal file
69
TD/TD9/exercice1.c
Normal file
@ -0,0 +1,69 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define N 50
|
||||
//#define SHOW_DETAILS
|
||||
|
||||
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
void* access_res(void* args) {
|
||||
int id = *(int*)args;
|
||||
*(int*)args = 0;
|
||||
|
||||
for (int i=0; i < INT_MAX; i++) {
|
||||
pthread_mutex_lock(&lock);
|
||||
*(int*)args += 1;
|
||||
pthread_mutex_unlock(&lock);
|
||||
}
|
||||
*(int*)args = -1;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
pthread_t tid[N];
|
||||
int args[N]; // Reuse it for the number of accesses
|
||||
|
||||
for (int i=0; i < N; i++) {
|
||||
args[i] = i;
|
||||
pthread_create(&(tid[i]), NULL, &access_res, (void*)&(args[i]));
|
||||
}
|
||||
|
||||
bool is_end = false;
|
||||
while (!is_end) {
|
||||
#ifdef SHOW_DETAILS
|
||||
printf("Accesses:\n");
|
||||
#endif
|
||||
int max, min, avg;
|
||||
max = avg = 0;
|
||||
min = INT_MAX;
|
||||
for (int i=0; i < N; i++) {
|
||||
#ifdef SHOW_DETAILS
|
||||
printf("%d ", args[i]);
|
||||
#endif
|
||||
if (args[i] == -1)
|
||||
is_end = true;
|
||||
|
||||
if (args[i] > max)
|
||||
max = args[i];
|
||||
|
||||
if (args[i] < min)
|
||||
min = args[i];
|
||||
|
||||
avg += args[i]/N;
|
||||
}
|
||||
#ifdef SHOW_DETAILS
|
||||
printf("\n");
|
||||
#endif
|
||||
printf("min max avg : %d %d %d\n", min, max, avg);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
for (int i=0; i < N; i++) {
|
||||
pthread_join(tid[i], NULL);
|
||||
}
|
||||
}
|
63
TD/TD9/exercice2.c
Normal file
63
TD/TD9/exercice2.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
|
||||
void* process_fun(void* arg) {
|
||||
int id = *(int*)arg;
|
||||
bool success = false;
|
||||
pthread_mutex_t* mutexs[3];
|
||||
if (id == 0) { // Gives deadlock sometimes
|
||||
mutexs[0] = &mutex1;
|
||||
mutexs[1] = &mutex2;
|
||||
mutexs[2] = &mutex3;
|
||||
} else if (id == 1) {
|
||||
mutexs[0] = &mutex2;
|
||||
mutexs[1] = &mutex1;
|
||||
mutexs[2] = &mutex3;
|
||||
} else if (id == 2) {
|
||||
mutexs[0] = &mutex1;
|
||||
mutexs[1] = &mutex2;
|
||||
mutexs[2] = &mutex3;
|
||||
}
|
||||
|
||||
while (!success) {
|
||||
bool csucc = true;
|
||||
for (int i=0; i < 3; i++) {
|
||||
if (pthread_mutex_trylock(mutexs[i]) != 0) {
|
||||
for (int j=0; j < i; j++) {
|
||||
pthread_mutex_unlock(mutexs[i]);
|
||||
}
|
||||
csucc = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
success = csucc;
|
||||
}
|
||||
printf("I have access\n");
|
||||
pthread_mutex_unlock(mutexs[0]);
|
||||
pthread_mutex_unlock(mutexs[1]);
|
||||
pthread_mutex_unlock(mutexs[2]);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
pthread_t tid[3];
|
||||
int args[3];
|
||||
|
||||
for (int i=0; i < 3; i++) {
|
||||
args[i] = i;
|
||||
pthread_create(&(tid[i]), NULL, &process_fun, &(args[i]));
|
||||
}
|
||||
|
||||
for (int i=0; i < 3; i++) {
|
||||
pthread_join(tid[i], NULL);
|
||||
}
|
||||
}
|
8
TD/TD9/index.md
Normal file
8
TD/TD9/index.md
Normal file
@ -0,0 +1,8 @@
|
||||
# Deadlocks
|
||||
|
||||
## Reasoning operating systems
|
||||
|
||||
1. Les processus 1 et 4 sont en deadlock.
|
||||
2. On n'a pas le graphe des tâches qui vont arriver en avance. Donc c'est plus compliqué dans un monde réel
|
||||
3. 1/6 : same order is deadlock free
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "vmap.h"
|
||||
|
||||
@ -6,10 +7,34 @@ int main() {
|
||||
// Add more tests here
|
||||
VMap* vmap = vmap_init();
|
||||
|
||||
char* data1 = my_malloc(vmap, 30*sizeof(char));
|
||||
char* data2 = my_malloc(vmap, 30*sizeof(char));
|
||||
char* real1 = malloc(sizeof(char)*32);
|
||||
real1[0] = 'h';
|
||||
real1[1] = 'e';
|
||||
real1[2] = 'l';
|
||||
real1[3] = 'l';
|
||||
real1[4] = 'o';
|
||||
real1[5] = '!';
|
||||
real1[6] = '\0';
|
||||
|
||||
my_copy(vmap, data1, data2, 30*sizeof(char));
|
||||
char* data1 = my_malloc(vmap, 32*sizeof(char));
|
||||
char* data2 = my_malloc(vmap, 32*sizeof(char));
|
||||
|
||||
vmap_copy_from_memory(vmap, real1, data1, 8);
|
||||
|
||||
my_copy(vmap, data1, data2, 32*sizeof(char));
|
||||
|
||||
char* real = malloc(sizeof(char)*32);
|
||||
|
||||
vmap_copy_to_memory(vmap, data2, real, 7);
|
||||
|
||||
if (strcmp(real1, real1)) {
|
||||
printf("%d\n", strcmp(real, real1));
|
||||
fprintf(stderr, "copy test failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(real);
|
||||
free(real1);
|
||||
|
||||
my_free(vmap, data1);
|
||||
my_free(vmap, data2);
|
||||
|
@ -24,9 +24,16 @@ VMap* vmap_init();
|
||||
*/
|
||||
PageMapInfo* vmap_get_page(VMap* vmap, void* ptr);
|
||||
|
||||
void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size);
|
||||
/**
|
||||
* From implemented memory to real memory
|
||||
*/
|
||||
void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size);
|
||||
|
||||
/**
|
||||
* From real memory to implemented memory
|
||||
*/
|
||||
void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size);
|
||||
|
||||
void* my_malloc(VMap* vmap, int size);
|
||||
void my_free(VMap* vmap, void* ptr);
|
||||
void my_copy(VMap* vmap, void* src, void* dst, int size);
|
||||
|
14
TP/TP2/rendu/Makefile
Normal file
14
TP/TP2/rendu/Makefile
Normal file
@ -0,0 +1,14 @@
|
||||
SRC = $(wildcard *.c)
|
||||
OBJ = $(filter-out build/main.o build/test.o, $(SRC:%.c=build/%.o))
|
||||
FLAGS = -Wall -Wextra -g -O3
|
||||
|
||||
all: build/test
|
||||
|
||||
build/test: test.c $(OBJ)
|
||||
gcc $^ -o $@ $(FLAGS)
|
||||
|
||||
build/%.o: %.c %.h
|
||||
gcc -c $< -o $@ $(FLAGS)
|
||||
|
||||
clean:
|
||||
rm -f build/*
|
7
TP/TP2/rendu/config.h
Normal file
7
TP/TP2/rendu/config.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef DEF_CONFIG_H
|
||||
#define DEF_CONFIG_H
|
||||
|
||||
#define PAGE_SIZE 4096
|
||||
#define PAGE_COUNT 100
|
||||
|
||||
#endif
|
125
TP/TP2/rendu/linked_list.c
Normal file
125
TP/TP2/rendu/linked_list.c
Normal file
@ -0,0 +1,125 @@
|
||||
// linked list
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "linked_list.h"
|
||||
|
||||
|
||||
linkedList* createLinkedList() {
|
||||
linkedList *list = (linkedList*)malloc(sizeof(linkedList));
|
||||
list->head = NULL;
|
||||
return list;
|
||||
}
|
||||
|
||||
//insert link at the first location
|
||||
void insertFirst(linkedList *list, int key, void *data) {
|
||||
//create a link
|
||||
node *link = (node*) malloc(sizeof(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
|
||||
node* deleteFirst(linkedList *list) {
|
||||
if (!list->head) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//save reference to first link
|
||||
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
|
||||
node* find(linkedList *list, int key) {
|
||||
|
||||
//start from the first link
|
||||
node* current = list->head;
|
||||
|
||||
//if list is empty
|
||||
if(!current) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//navigate through list
|
||||
while(current->key != key) {
|
||||
|
||||
//if it is last node
|
||||
if(!current->next) {
|
||||
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
|
||||
node* deleteElement(linkedList *list, int key) {
|
||||
|
||||
//start from the first link
|
||||
node* current = list->head;
|
||||
node* previous = NULL;
|
||||
|
||||
//if list is empty
|
||||
if(!list->head) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//navigate through list
|
||||
while(current->key != key) {
|
||||
|
||||
//if it is last node
|
||||
if(!current->next) {
|
||||
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(linkedList *list) {
|
||||
return !list->head;
|
||||
}
|
||||
|
||||
int length(linkedList *list) {
|
||||
int length = 0;
|
||||
node *current;
|
||||
|
||||
for(current = list->head; current != NULL; current = current->next) {
|
||||
length++;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
24
TP/TP2/rendu/linked_list.h
Normal file
24
TP/TP2/rendu/linked_list.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef DEF_LLIST_H
|
||||
#define DEF_LLIST_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct node {
|
||||
void *data;
|
||||
int key;
|
||||
struct node *next;
|
||||
} node;
|
||||
|
||||
typedef struct linkedList {
|
||||
node *head;
|
||||
} linkedList;
|
||||
|
||||
linkedList* createLinkedList();
|
||||
void insertFirst(linkedList *list, int key, void *data);
|
||||
node* deleteFirst(linkedList *list);
|
||||
node* find(linkedList *list, int key);
|
||||
node* deleteElement(linkedList *list, int key);
|
||||
bool isEmpty(linkedList *list);
|
||||
int length(linkedList *list);
|
||||
|
||||
#endif
|
181
TP/TP2/rendu/mmap.c
Normal file
181
TP/TP2/rendu/mmap.c
Normal file
@ -0,0 +1,181 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define CHECK_FERROR(f) { if (ferror(f)) {fprintf(stderr, "IO Error"); exit(1);} }
|
||||
|
||||
#include "config.h"
|
||||
#include "mmap.h"
|
||||
|
||||
MMap* mmap_init(){
|
||||
MMap *mmap = malloc(sizeof(MMap));
|
||||
mmap->size = PAGE_SIZE*PAGE_COUNT;
|
||||
mmap->memory = malloc(mmap->size);
|
||||
mmap->count = 0;
|
||||
mmap->support = malloc(sizeof(PageInfo));
|
||||
mmap->support->data = malloc(PAGE_SIZE);
|
||||
mmap->free_list = createLinkedList();
|
||||
// insert PAGE_COUNT pages in the free list
|
||||
for (int i=0; i < PAGE_COUNT; i++) {
|
||||
PageInfo *page = malloc(sizeof(PageInfo));
|
||||
page->data = mmap->memory + i*PAGE_SIZE;
|
||||
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 < PAGE_COUNT; i++) {
|
||||
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
||||
}
|
||||
char tmp2 = '\0';
|
||||
for (int i=0; i < PAGE_COUNT*PAGE_SIZE; i++) {
|
||||
fwrite(&tmp2, 1, 1, mmap->swap);
|
||||
}
|
||||
return mmap;
|
||||
}
|
||||
|
||||
void mmap_clean(MMap *mmap) {
|
||||
free(mmap->memory);
|
||||
fclose(mmap->swap);
|
||||
free(mmap);
|
||||
remove("/tmp/myswap");
|
||||
}
|
||||
|
||||
PageInfo* page_alloc(MMap *mmap) {
|
||||
PageInfo *page = NULL;
|
||||
page = page_free(mmap);
|
||||
if (page) {
|
||||
insertFirst(mmap->alloc_list, page->key, page);
|
||||
mmap->count++;
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
PageInfo* page_free(MMap *mmap) {
|
||||
PageInfo *page = NULL;
|
||||
node *node = deleteFirst(mmap->free_list);
|
||||
if (node) {
|
||||
page = node->data;
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
int check_page_free_list(MMap *mmap) {
|
||||
return length(mmap->free_list);
|
||||
}
|
||||
|
||||
int check_page_alloc(MMap *mmap) {
|
||||
return length(mmap->alloc_list);
|
||||
}
|
||||
|
||||
int move_to_swap(MMap *mmap, PageInfo *page) {
|
||||
int key = -1;
|
||||
fseek(mmap->swap, 0, SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
for (int i=0; i < PAGE_COUNT; i++) {
|
||||
fread(&key, sizeof(int), 1, mmap->swap);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
if (key == -1) {
|
||||
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
fwrite(&page->key, sizeof(int), 1, mmap->swap);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
fwrite(page->data, PAGE_SIZE, 1, mmap->swap);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PageInfo* read_from_swap(MMap* mmap, PageInfo *page) {
|
||||
int key = -1;
|
||||
fseek(mmap->swap, 0, SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
for (int i=0; i < PAGE_COUNT; i++) {
|
||||
(void)fread(&key, sizeof(int), 1, mmap->swap);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
if (key != page->key)
|
||||
continue;
|
||||
|
||||
fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
fread(page->data, PAGE_SIZE, 1, mmap->swap);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
int tmp = -1;
|
||||
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
||||
CHECK_FERROR(mmap->swap);
|
||||
|
||||
return page;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PageInfo* page_lookup(MMap* mmap, int key) {
|
||||
node* data = find(mmap->alloc_list, key);
|
||||
if (data) {
|
||||
return data->data;
|
||||
}
|
||||
|
||||
mmap->support->key = key;
|
||||
PageInfo* page = read_from_swap(mmap, mmap->support);
|
||||
if (page) { // Move one elem from mem to swap
|
||||
PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
|
||||
move_to_swap(mmap, swapped_page);
|
||||
|
||||
memcpy(swapped_page->data, mmap->support->data, PAGE_SIZE);
|
||||
swapped_page->key = key;
|
||||
insertFirst(mmap->alloc_list, key, swapped_page);
|
||||
return page;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void page_remove(MMap *mmap, int key) {
|
||||
PageInfo* page = page_lookup(mmap, key);
|
||||
if (!page)
|
||||
return;
|
||||
|
||||
deleteElement(mmap->alloc_list, key);
|
||||
mmap->count--;
|
||||
}
|
||||
|
||||
PageInfo *page_create(MMap *mmap, bool *moved_to_swap) {
|
||||
if (mmap->count >= 2*PAGE_COUNT)
|
||||
return NULL;
|
||||
|
||||
PageInfo* page = page_alloc(mmap);
|
||||
*moved_to_swap = false;
|
||||
|
||||
if (page)
|
||||
return page;
|
||||
|
||||
// Another page has been moved to swap
|
||||
*moved_to_swap = true;
|
||||
|
||||
page = deleteFirst(mmap->alloc_list)->data;
|
||||
move_to_swap(mmap, page);
|
||||
// Reuse swapped page for the new one
|
||||
page->key = mmap->count;
|
||||
mmap->count++;
|
||||
insertFirst(mmap->alloc_list, page->key, page);
|
||||
|
||||
return page;
|
||||
}
|
36
TP/TP2/rendu/mmap.h
Normal file
36
TP/TP2/rendu/mmap.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef DEF_MMAP_H
|
||||
#define DEF_MMAP_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "linked_list.h"
|
||||
|
||||
typedef struct PageInfo {
|
||||
int key;
|
||||
void *data;
|
||||
} PageInfo;
|
||||
|
||||
typedef struct MMap {
|
||||
void *memory;
|
||||
int size;
|
||||
int count;
|
||||
PageInfo *support;
|
||||
linkedList *free_list;
|
||||
linkedList *alloc_list;
|
||||
FILE *swap;
|
||||
} MMap;
|
||||
|
||||
MMap* mmap_init();
|
||||
void mmap_clean(MMap *mmap);
|
||||
PageInfo* page_alloc(MMap *mmap);
|
||||
PageInfo* page_free(MMap *mmap);
|
||||
int check_page_free_list(MMap *mmap);
|
||||
int check_page_alloc(MMap *mmap);
|
||||
int move_to_swap(MMap *mmap, PageInfo *page);
|
||||
PageInfo* read_from_swap(MMap* mmap, PageInfo *page);
|
||||
PageInfo *page_lookup(MMap *mmap, int key);
|
||||
void page_remove(MMap *mmap, int key);
|
||||
PageInfo *page_create(MMap *mmap, bool *moved_to_swap);
|
||||
|
||||
#endif
|
43
TP/TP2/rendu/test.c
Normal file
43
TP/TP2/rendu/test.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "vmap.h"
|
||||
|
||||
int main() {
|
||||
// Add more tests here
|
||||
VMap* vmap = vmap_init();
|
||||
|
||||
char* real1 = malloc(sizeof(char)*32);
|
||||
real1[0] = 'h';
|
||||
real1[1] = 'e';
|
||||
real1[2] = 'l';
|
||||
real1[3] = 'l';
|
||||
real1[4] = 'o';
|
||||
real1[5] = '!';
|
||||
real1[6] = '\0';
|
||||
|
||||
char* data1 = my_malloc(vmap, 32*sizeof(char));
|
||||
char* data2 = my_malloc(vmap, 32*sizeof(char));
|
||||
|
||||
vmap_copy_from_memory(vmap, real1, data1, 8);
|
||||
|
||||
my_copy(vmap, data1, data2, 32*sizeof(char));
|
||||
|
||||
char* real = malloc(sizeof(char)*32);
|
||||
|
||||
vmap_copy_to_memory(vmap, data2, real, 7);
|
||||
|
||||
if (strcmp(real1, real1)) {
|
||||
printf("%d\n", strcmp(real, real1));
|
||||
fprintf(stderr, "copy test failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(real);
|
||||
free(real1);
|
||||
|
||||
my_free(vmap, data1);
|
||||
my_free(vmap, data2);
|
||||
|
||||
return 0;
|
||||
}
|
162
TP/TP2/rendu/vmap.c
Normal file
162
TP/TP2/rendu/vmap.c
Normal file
@ -0,0 +1,162 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "vmap.h"
|
||||
|
||||
VMap* vmap_init() {
|
||||
VMap* vmap = (VMap*)malloc(sizeof(VMap));
|
||||
vmap->mmap = mmap_init();
|
||||
vmap->pages = createLinkedList();
|
||||
vmap->index = 0;
|
||||
|
||||
return vmap;
|
||||
}
|
||||
|
||||
PageMapInfo* vmap_new_page(VMap* vmap) {
|
||||
PageMapInfo* page = (PageMapInfo*)malloc(sizeof(PageMapInfo));
|
||||
|
||||
bool moved;
|
||||
page->key = page_create(vmap->mmap, &moved)->key;
|
||||
page->start = vmap->index;
|
||||
page->cursor = 0;
|
||||
page->nb_allocs = 0;
|
||||
page->swapped = moved;
|
||||
|
||||
vmap->index += PAGE_SIZE;
|
||||
|
||||
insertFirst(vmap->pages, page->start, page);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
int page_map_available_size(PageMapInfo* page) {
|
||||
int avail = page->start + PAGE_SIZE - page->cursor;
|
||||
assert(avail >= 0);
|
||||
return avail;
|
||||
}
|
||||
|
||||
|
||||
void vmap_free_page(VMap* vmap, PageMapInfo* page) {
|
||||
deleteElement(vmap->pages, page->key);
|
||||
page_remove(vmap->mmap, page->key);
|
||||
free(page);
|
||||
}
|
||||
|
||||
PageMapInfo* vmap_get_page(VMap* vmap, void* ptr) {
|
||||
node* current = vmap->pages->head;
|
||||
while (current) {
|
||||
PageMapInfo* page = current->data;
|
||||
if (page->start <= (int64_t)ptr && page->start+PAGE_SIZE > (int64_t)ptr) {
|
||||
return page;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void vmap_unswap(VMap* vmap, PageMapInfo* page) {
|
||||
if (!page->swapped)
|
||||
return;
|
||||
|
||||
read_from_swap(vmap->mmap, page_lookup(vmap->mmap, page->key));
|
||||
page->swapped = false;
|
||||
}
|
||||
|
||||
|
||||
void* my_malloc(VMap* vmap, int size) {
|
||||
if (size > PAGE_SIZE) {
|
||||
fprintf(stderr, "my_malloc: Requesting too much space: %d > %d\n", size, PAGE_SIZE);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Parcours de la liste pour trouver un candidat. Sinon, nouvelle page
|
||||
node* current = vmap->pages->head;
|
||||
while (current) {
|
||||
PageMapInfo* page = current->data;
|
||||
if (page_map_available_size(page) >= size) {
|
||||
void* ptr = (void*)((uint64_t)page->start+page->cursor);
|
||||
page->cursor += size;
|
||||
page->nb_allocs++;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
PageMapInfo* page = vmap_new_page(vmap);
|
||||
|
||||
void* ptr = (void*)((uint64_t)page->start+page->cursor);
|
||||
page->cursor += size;
|
||||
page->nb_allocs++;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void my_free(VMap* vmap, void* ptr) {
|
||||
PageMapInfo* page = vmap_get_page(vmap, ptr);
|
||||
|
||||
if (!page) {
|
||||
fprintf(stderr, "my_free: Double free or memory corrupted\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
page->nb_allocs--;
|
||||
if (page->nb_allocs == 0) {
|
||||
vmap_free_page(vmap, page);
|
||||
}
|
||||
}
|
||||
|
||||
void my_copy(VMap* vmap, void* src, void* dst, int size) {
|
||||
PageMapInfo* page_map_src = vmap_get_page(vmap, src);
|
||||
PageMapInfo* page_map_dst = vmap_get_page(vmap, dst);
|
||||
|
||||
if (((int64_t)src % PAGE_SIZE) + size >= PAGE_SIZE) {
|
||||
fprintf(stderr, "my_copy: size not available in this src page\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (((int64_t)dst % PAGE_SIZE) + size >= PAGE_SIZE) {
|
||||
fprintf(stderr, "my_copy: size not available in this dst page\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (page_map_src->swapped) {
|
||||
vmap_unswap(vmap, page_map_src);
|
||||
}
|
||||
if (page_map_dst->swapped) {
|
||||
vmap_unswap(vmap, page_map_dst);
|
||||
}
|
||||
|
||||
PageInfo* page_src = page_lookup(vmap->mmap, page_map_src->key);
|
||||
PageInfo* page_dst = page_lookup(vmap->mmap, page_map_dst->key);
|
||||
|
||||
memcpy(page_dst->data+((int64_t)src % PAGE_SIZE), page_src->data+((int64_t)dst % PAGE_SIZE), size);
|
||||
}
|
||||
|
||||
void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size) {
|
||||
PageMapInfo* page_map = vmap_get_page(vmap, src);
|
||||
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
|
||||
|
||||
if (!page) {
|
||||
fprintf(stderr, "vmap_copy_to_memory: page not found\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(page->data + ((int64_t)src % PAGE_SIZE), dst, size);
|
||||
}
|
||||
|
||||
void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size) {
|
||||
PageMapInfo* page_map = vmap_get_page(vmap, dst);
|
||||
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
|
||||
|
||||
if (!page) {
|
||||
fprintf(stderr, "vmap_copy_to_memory: page not found\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(src, page->data + ((int64_t)dst % PAGE_SIZE), size);
|
||||
}
|
41
TP/TP2/rendu/vmap.h
Normal file
41
TP/TP2/rendu/vmap.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef DEF_VMAP_H
|
||||
#define DEF_VMAP_H
|
||||
|
||||
#include "mmap.h"
|
||||
|
||||
typedef struct VMap {
|
||||
MMap* mmap;
|
||||
linkedList* pages;
|
||||
int index; // index of virt memory
|
||||
} VMap;
|
||||
|
||||
typedef struct PageMapInfo {
|
||||
int start;
|
||||
int key; // key for mmap
|
||||
int cursor; // Last allocation end
|
||||
int nb_allocs; // Free page if 0
|
||||
bool swapped;
|
||||
} PageMapInfo;
|
||||
|
||||
VMap* vmap_init();
|
||||
|
||||
/**
|
||||
* Get page hosting a certain pointer
|
||||
*/
|
||||
PageMapInfo* vmap_get_page(VMap* vmap, void* ptr);
|
||||
|
||||
/**
|
||||
* From implemented memory to real memory
|
||||
*/
|
||||
void vmap_copy_to_memory(struct VMap *vmap, void *src, void *dst, int size);
|
||||
|
||||
/**
|
||||
* From real memory to implemented memory
|
||||
*/
|
||||
void vmap_copy_from_memory(struct VMap *vmap, void *src, void *dst, int size);
|
||||
|
||||
void* my_malloc(VMap* vmap, int size);
|
||||
void my_free(VMap* vmap, void* ptr);
|
||||
void my_copy(VMap* vmap, void* src, void* dst, int size);
|
||||
|
||||
#endif
|
BIN
TP/TP2/sujet.pdf
Normal file
BIN
TP/TP2/sujet.pdf
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user