From 01d700ab53176f4c319136e4ab9023018ffd3721 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 13 Feb 2018 14:02:21 +0100 Subject: [PATCH] Add TD3 code. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .gitignore | 3 ++ TD3/exo2/Makefile | 83 ++++++++++++++++++++++++++++++++++++++ TD3/exo2/exo2.c | 35 ++++++++++++++++ TD3/exo2/monexec/Makefile | 83 ++++++++++++++++++++++++++++++++++++++ TD3/exo2/monexec/monexec.c | 19 +++++++++ TD3/exo3/Makefile | 83 ++++++++++++++++++++++++++++++++++++++ TD3/exo3/exo3.c | 37 +++++++++++++++++ TD3/exo4/Makefile | 83 ++++++++++++++++++++++++++++++++++++++ TD3/exo4/exo4.c | 42 +++++++++++++++++++ 9 files changed, 468 insertions(+) create mode 100644 TD3/exo2/Makefile create mode 100644 TD3/exo2/exo2.c create mode 100644 TD3/exo2/monexec/Makefile create mode 100644 TD3/exo2/monexec/monexec.c create mode 100644 TD3/exo3/Makefile create mode 100644 TD3/exo3/exo3.c create mode 100644 TD3/exo4/Makefile create mode 100644 TD3/exo4/exo4.c diff --git a/.gitignore b/.gitignore index bd64e52..c65a303 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ philosophe/philosophe prodcons/prodcons +TD3/exo2/monexec/monexec +exo[[:digit:]] +!exo[[:digit:]]/ *.static *.dynamic diff --git a/TD3/exo2/Makefile b/TD3/exo2/Makefile new file mode 100644 index 0000000..60456d5 --- /dev/null +++ b/TD3/exo2/Makefile @@ -0,0 +1,83 @@ +# Sample Makefile to build simple project. +# +# This Makefile expect all source files (.c) to be at the same level, in the +# current working directory. +# +# It will automatically generate dependencies, compile all files, and produce a +# binary using the provided name. +# +# Set BINARY_NAME to the name of the binary file to build. +# Set BUILD_TYPE to either debug or release +# +# Automatic dependencies code from: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr +BINARY_NAME=exo2 +BUILD_TYPE=debug +#BUILD_TYPE=release +LDLIBS=-lpthread + +# ==================================== +# DO NOT CHANGE STUFF BEYOND THIS LINE +# ==================================== + +all: $(BINARY_NAME) + +CC=gcc +LD=gcc + +WARN_FLAGS = -Wall -Wextra +STD_FLAG = -std=c11 + +ifeq ($(BUILD_TYPE),debug) +BUILDDIR := .build/debug +DEBUG_FLAG = -g +DEBUG = 1 +STRIP_FLAG = +OPTI_FLAG = -O0 +else +BUILDDIR := .build/release +DEBUG_FLAG = +DEBUG = 0 +STRIP_FLAG = -s +OPTI_FLAG = -O3 +endif + +CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) +LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) + +OBJDIR := $(BUILDDIR)/objs +$(shell mkdir -p $(OBJDIR)) + +SRCS=$(wildcard *.c) +OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS)) + +DEPDIR := $(BUILDDIR)/deps +$(shell mkdir -p $(DEPDIR)) +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(BINARY_NAME): $(OBJS) + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d + @echo "[C ] $*" + @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +$(DEPDIR)/%.d: ; + +.PRECIOUS: $(DEPDIR)/%.d + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) + +clean: + @echo "[CLN]" + -@rm -r $(BUILDDIR) + -@rm $(BINARY_NAME) + +disassemble: $(BINARY_NAME) + objdump -d $< | less + +symbols: $(BINARY_NAME) + objdump -t $< | sort | less diff --git a/TD3/exo2/exo2.c b/TD3/exo2/exo2.c new file mode 100644 index 0000000..0e58bb8 --- /dev/null +++ b/TD3/exo2/exo2.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +int main() +{ + pid_t pid; + + if ((pid = fork()) == -1) { + perror("erreur"); + exit(EXIT_FAILURE); + } else if (pid == 0) { + for (int i = 1; i <= 50; i++) { + printf("%d\n", i); + } + return 0; + } + + // Assure la fin du premier fils avant l'execution du deuxième + wait(NULL); + + if ((pid = fork()) == -1) { + perror("erreur"); + exit(EXIT_FAILURE); + } else if (pid == 0) { + for (int i = 50; i <= 100; i++) { + printf("%d\n", i); + } + return 0; + } + + exit(EXIT_SUCCESS); +} diff --git a/TD3/exo2/monexec/Makefile b/TD3/exo2/monexec/Makefile new file mode 100644 index 0000000..9106081 --- /dev/null +++ b/TD3/exo2/monexec/Makefile @@ -0,0 +1,83 @@ +# Sample Makefile to build simple project. +# +# This Makefile expect all source files (.c) to be at the same level, in the +# current working directory. +# +# It will automatically generate dependencies, compile all files, and produce a +# binary using the provided name. +# +# Set BINARY_NAME to the name of the binary file to build. +# Set BUILD_TYPE to either debug or release +# +# Automatic dependencies code from: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr +BINARY_NAME=monexec +BUILD_TYPE=debug +#BUILD_TYPE=release +LDLIBS=-lpthread + +# ==================================== +# DO NOT CHANGE STUFF BEYOND THIS LINE +# ==================================== + +all: $(BINARY_NAME) + +CC=gcc +LD=gcc + +WARN_FLAGS = -Wall -Wextra +STD_FLAG = -std=c11 + +ifeq ($(BUILD_TYPE),debug) +BUILDDIR := .build/debug +DEBUG_FLAG = -g +DEBUG = 1 +STRIP_FLAG = +OPTI_FLAG = -O0 +else +BUILDDIR := .build/release +DEBUG_FLAG = +DEBUG = 0 +STRIP_FLAG = -s +OPTI_FLAG = -O3 +endif + +CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) +LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) + +OBJDIR := $(BUILDDIR)/objs +$(shell mkdir -p $(OBJDIR)) + +SRCS=$(wildcard *.c) +OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS)) + +DEPDIR := $(BUILDDIR)/deps +$(shell mkdir -p $(DEPDIR)) +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(BINARY_NAME): $(OBJS) + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d + @echo "[C ] $*" + @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +$(DEPDIR)/%.d: ; + +.PRECIOUS: $(DEPDIR)/%.d + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) + +clean: + @echo "[CLN]" + -@rm -r $(BUILDDIR) + -@rm $(BINARY_NAME) + +disassemble: $(BINARY_NAME) + objdump -d $< | less + +symbols: $(BINARY_NAME) + objdump -t $< | sort | less diff --git a/TD3/exo2/monexec/monexec.c b/TD3/exo2/monexec/monexec.c new file mode 100644 index 0000000..e7b20dd --- /dev/null +++ b/TD3/exo2/monexec/monexec.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + pid_t pid; + + if ((pid = fork()) == -1) { + perror("erreur"); + exit(EXIT_FAILURE); + } else if (pid == 0) { + execvp(argv[1], argv + 1); + perror("execvp"); + } + + exit(EXIT_SUCCESS); +} diff --git a/TD3/exo3/Makefile b/TD3/exo3/Makefile new file mode 100644 index 0000000..bb4aaf9 --- /dev/null +++ b/TD3/exo3/Makefile @@ -0,0 +1,83 @@ +# Sample Makefile to build simple project. +# +# This Makefile expect all source files (.c) to be at the same level, in the +# current working directory. +# +# It will automatically generate dependencies, compile all files, and produce a +# binary using the provided name. +# +# Set BINARY_NAME to the name of the binary file to build. +# Set BUILD_TYPE to either debug or release +# +# Automatic dependencies code from: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr +BINARY_NAME=exo3 +BUILD_TYPE=debug +#BUILD_TYPE=release +LDLIBS=-lpthread + +# ==================================== +# DO NOT CHANGE STUFF BEYOND THIS LINE +# ==================================== + +all: $(BINARY_NAME) + +CC=gcc +LD=gcc + +WARN_FLAGS = -Wall -Wextra +STD_FLAG = -std=c11 + +ifeq ($(BUILD_TYPE),debug) +BUILDDIR := .build/debug +DEBUG_FLAG = -g +DEBUG = 1 +STRIP_FLAG = +OPTI_FLAG = -O0 +else +BUILDDIR := .build/release +DEBUG_FLAG = +DEBUG = 0 +STRIP_FLAG = -s +OPTI_FLAG = -O3 +endif + +CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) +LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) + +OBJDIR := $(BUILDDIR)/objs +$(shell mkdir -p $(OBJDIR)) + +SRCS=$(wildcard *.c) +OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS)) + +DEPDIR := $(BUILDDIR)/deps +$(shell mkdir -p $(DEPDIR)) +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(BINARY_NAME): $(OBJS) + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d + @echo "[C ] $*" + @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +$(DEPDIR)/%.d: ; + +.PRECIOUS: $(DEPDIR)/%.d + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) + +clean: + @echo "[CLN]" + -@rm -r $(BUILDDIR) + -@rm $(BINARY_NAME) + +disassemble: $(BINARY_NAME) + objdump -d $< | less + +symbols: $(BINARY_NAME) + objdump -t $< | sort | less diff --git a/TD3/exo3/exo3.c b/TD3/exo3/exo3.c new file mode 100644 index 0000000..2fa2228 --- /dev/null +++ b/TD3/exo3/exo3.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +int main() +{ + pid_t pid; + + if ((pid = fork()) == -1) { + perror("erreur"); + exit(EXIT_FAILURE); + } else if (pid == 0) { + execlp("who", "who", NULL); + perror("execlp"); + exit(1); + } + + // without it just exec with the & + // with it exec with the ; + wait(NULL); + + if ((pid = fork()) == -1) { + perror("erreur"); + exit(EXIT_FAILURE); + } else if (pid == 0) { + execlp("ps", "ps", NULL); + perror("execlp"); + exit(1); + } + + wait(NULL); + + execlp("ls", "ls", "-l", NULL); + perror("execlp"); +} diff --git a/TD3/exo4/Makefile b/TD3/exo4/Makefile new file mode 100644 index 0000000..42a26a6 --- /dev/null +++ b/TD3/exo4/Makefile @@ -0,0 +1,83 @@ +# Sample Makefile to build simple project. +# +# This Makefile expect all source files (.c) to be at the same level, in the +# current working directory. +# +# It will automatically generate dependencies, compile all files, and produce a +# binary using the provided name. +# +# Set BINARY_NAME to the name of the binary file to build. +# Set BUILD_TYPE to either debug or release +# +# Automatic dependencies code from: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr +BINARY_NAME=exo4 +BUILD_TYPE=debug +#BUILD_TYPE=release +LDLIBS=-lpthread + +# ==================================== +# DO NOT CHANGE STUFF BEYOND THIS LINE +# ==================================== + +all: $(BINARY_NAME) + +CC=gcc +LD=gcc + +WARN_FLAGS = -Wall -Wextra +STD_FLAG = -std=c11 + +ifeq ($(BUILD_TYPE),debug) +BUILDDIR := .build/debug +DEBUG_FLAG = -g +DEBUG = 1 +STRIP_FLAG = +OPTI_FLAG = -O0 +else +BUILDDIR := .build/release +DEBUG_FLAG = +DEBUG = 0 +STRIP_FLAG = -s +OPTI_FLAG = -O3 +endif + +CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) +LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) + +OBJDIR := $(BUILDDIR)/objs +$(shell mkdir -p $(OBJDIR)) + +SRCS=$(wildcard *.c) +OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS)) + +DEPDIR := $(BUILDDIR)/deps +$(shell mkdir -p $(DEPDIR)) +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(BINARY_NAME): $(OBJS) + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d + @echo "[C ] $*" + @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +$(DEPDIR)/%.d: ; + +.PRECIOUS: $(DEPDIR)/%.d + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) + +clean: + @echo "[CLN]" + -@rm -r $(BUILDDIR) + -@rm $(BINARY_NAME) + +disassemble: $(BINARY_NAME) + objdump -d $< | less + +symbols: $(BINARY_NAME) + objdump -t $< | sort | less diff --git a/TD3/exo4/exo4.c b/TD3/exo4/exo4.c new file mode 100644 index 0000000..e835d0d --- /dev/null +++ b/TD3/exo4/exo4.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include + +void sigintP() +{ +} + +void sigalrm() +{ +} + +void sigintF() +{ +} + +void sigchld() +{ + int status; + wait(&status); + exit(0); +} + +int main(void) +{ + signal(SIGCHLD, sigchld); + if (fork() == 0) { + signal(SIGINT, sigintF); + while (1) { + printf("ici fils \n"); + sleep(1); + } + } + while (1) { + signal(SIGINT, sigintP); + printf("ici pere \n"); + sleep(1); + } + return 0; +} -- 2.34.1