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

feat: final exercise of serie 2

parent 4077757c
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)
%.o: %.c
@printf "=================== Building sources ===================\n"
$(CC) $(CFLAGS) -c $<
.PHONY: clean
clean:
@printf "=================== Cleaning up... ===================\n"
rm -f *.o $(TARGET)
.PHONY: rebuild
rebuild: clean all
# Exercice 4
Soit le code :
```c
int main(int argc, char *argv[]) {
printf("Hello world!");
execlp("sleep", "sleep", "0", (char *)NULL);
}
```
- Expliquez de manière détaillée pourquoi quand on exécute le programme ci-dessus, rien n’est affiché.
- La raison pour laquelle rien ne s'affiche lors de l'exécution du programme
est parce que le buffer de `stdout` ne jamais flushé par un retour à la ligne
`\n`.
- Comment changer le code de manière minimale afin d’obtenir l’affichage attendu “Hello world!” ?
- Unique modification dans le code est le retour à la ligne explicite lors
de l'appel explicite à `printf()`
```c
int main(int argc, char *argv[]) {
printf("Hello world!\n");
execlp("sleep", "sleep", "0", (char *)NULL);
}
```
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
printf("Hello world!\n");
execlp("sleep", "sleep", "0", (char *)NULL);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment