Skip to content
Snippets Groups Projects
Commit b71d2051 authored by iliya's avatar iliya
Browse files

feat: serie3 finished

parent 6c29419e
No related branches found
No related tags found
No related merge requests found
*.o
prog
CC := clang
CFLAGS := -g -pedantic -Wall -Wextra -std=c11
LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm -lpthread
TARGET := prog
all: $(TARGET)
$(TARGET): prog.o
@printf "=================== Building executable ===================\n"
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
@printf "\n"
%.o: %.c
@printf "================== Building object files ==================\n"
$(CC) $(CFLAGS) -c $<
@printf "\n"
.PHONY: clean
clean:
rm -f *.o $(TARGET)
.PHONY: rebuild
rebuild: clean all
# Exercice 4
Soit le code suivant :
```c
#define MAX_STRING_SIZE 256
char *strtoupper(char *string) {
static char buffer[MAX_STRING_SIZE];
int index;
for (index = 0; string[index]; index++) {
// on part du principe que toupper est MT-safe et réentrant
buffer[index] = toupper(string[index]);
}
buffer[index] = 0;
return buffer;
}
```
## Questions
1. Le code est-il réentrant ?
2. Le code est-il _thread-safe_ ?
3. Dans la négative, modifiez le code pour qu'il devienne réentrant **et**
thread-safe. À noter que modifier l'interface est autorisé.
## Réponses
1. Le code **n'est pas réentrant** car si l'exécution de la fonction `strtoupper`
est interrompue pendant son exécution est qu'elle est appellée par cette fois-ci
avec une autre chaîne de caractères que l'exécution initiale, le buffer pointera
désormais sur la chaîne de caractères créée sur la base de la seconde exécution
et non pas sur la première exécution. Une fonction réentrante ne doit pas référencer /
retourner de pointeur sur des données statiques ou globales, or dans ce cas elle
le fait avec la variable `buffer` qui a un "comportement" global mais une visibilité
locale.
2. Le code **n'est pas thread-safe** pour la même raison que ci-dessus car la
variable statique `static char buffer` risque d'être modifiée par l'exécution
d'un autre thread.
3. ```c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_SIZE 256
char *strtoupper(char *string) {
char *buffer = calloc(MAX_STRING_SIZE, sizeof(char));
int index;
for (index = 0; string[index]; index++) {
buffer[index] = toupper(string[index]);
}
return buffer;
}
int main(void) {
char *test_str = "Hello World!";
char *res = strtoupper(test_str);
fprintf(stdout, "Result: %s\n", res);
free(res);
return EXIT_SUCCESS;
}
```
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_SIZE 256
char *strtoupper(char *string) {
char *buffer = calloc(MAX_STRING_SIZE, sizeof(char));
int index;
for (index = 0; string[index]; index++) {
buffer[index] = toupper(string[index]);
}
return buffer;
}
int main(void) {
char *test_str = "Hello World!";
char *res = strtoupper(test_str);
fprintf(stdout, "Result: %s\n", res);
free(res);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment