Add TD3 code.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Feb 2018 13:02:21 +0000 (14:02 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Feb 2018 13:02:21 +0000 (14:02 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
.gitignore
TD3/exo2/Makefile [new file with mode: 0644]
TD3/exo2/exo2.c [new file with mode: 0644]
TD3/exo2/monexec/Makefile [new file with mode: 0644]
TD3/exo2/monexec/monexec.c [new file with mode: 0644]
TD3/exo3/Makefile [new file with mode: 0644]
TD3/exo3/exo3.c [new file with mode: 0644]
TD3/exo4/Makefile [new file with mode: 0644]
TD3/exo4/exo4.c [new file with mode: 0644]

index bd64e52c59cc149781611ed7d4adbddc398721c0..c65a3033f0aedee60334d2ca09a6604f3e442551 100644 (file)
@@ -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 (file)
index 0000000..60456d5
--- /dev/null
@@ -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 (file)
index 0000000..0e58bb8
--- /dev/null
@@ -0,0 +1,35 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <wait.h>
+
+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 (file)
index 0000000..9106081
--- /dev/null
@@ -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 (file)
index 0000000..e7b20dd
--- /dev/null
@@ -0,0 +1,19 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+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 (file)
index 0000000..bb4aaf9
--- /dev/null
@@ -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 (file)
index 0000000..2fa2228
--- /dev/null
@@ -0,0 +1,37 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <wait.h>
+
+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 (file)
index 0000000..42a26a6
--- /dev/null
@@ -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 (file)
index 0000000..e835d0d
--- /dev/null
@@ -0,0 +1,42 @@
+#include <unistd.h>
+#include <signal.h>
+#include <wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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;
+}