diff --git a/.gitignore b/.gitignore index 7140184..dae7a28 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ doc/**/*.pdf build src/03-led-controller/led-controller +src/04-multiprocessing/multiprocessing diff --git a/src/04-multiprocessing/Makefile b/src/04-multiprocessing/Makefile index 0b71c82..71698a0 100644 --- a/src/04-multiprocessing/Makefile +++ b/src/04-multiprocessing/Makefile @@ -1,5 +1,5 @@ EXE=multiprocessing -SRCS=$(wildcard *.c) +SRCS=process.c # Include the standard application Makefile for the CSEL1 labs include ../appl.mk diff --git a/src/04-multiprocessing/main.c b/src/04-multiprocessing/main.c deleted file mode 100644 index ef1d352..0000000 --- a/src/04-multiprocessing/main.c +++ /dev/null @@ -1,100 +0,0 @@ -#include - -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - - -const char * MSG = "Nique ta mère !!\0"; - -static void catch_signal(int signal) { - - switch (signal) { - case SIGHUP: - printf("SIGHUP received\n"); - break; - case SIGINT: - printf("SIGINT received\n"); - break; - case SIGQUIT: - printf("SIGQUIT received\n"); - break; - case SIGTERM: - printf("SIGTERM received\n"); - break; - case SIGABRT: - printf("SIGABRT received\n"); - break; - } - - -} - -static void install_catch_signal() -{ - struct sigaction act = { - .sa_handler = catch_signal, - }; - sigemptyset(&act.sa_mask); - sigaction(SIGHUP, &act, 0); - sigaction(SIGINT, &act, 0); - sigaction(SIGQUIT, &act, 0); - sigaction(SIGTERM, &act, 0); - sigaction(SIGABRT, &act, 0); -} - - -int main(int argc, char* argv[]) { - - install_catch_signal(); - - /* Setup socket for inter-process communication */ - int fd[2]; - int err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd); - if (err == -1) { - perror("socketpair fail"); - exit(1); - } - - /* Fork a child process */ - pid_t pid = fork(); - if (pid == 0) { - /* Parent processus */ - printf("Parent processus: pid=%d\n", pid); - - - - while(1) { - write(fd[0], MSG, strlen(MSG)); - sleep(1); - } - - - } else if (pid > 0) { - /* Child processus */ - printf("Child processus: pid=%d\n", pid); - char buffer[strlen(MSG)]; - - while(1) { - read(fd[1], buffer, strlen(MSG)); - printf("%s\n", buffer); - sleep(1); - } - - } else { - /* error */ - perror("fork fail"); - exit(1); - } - - - return 0; -} diff --git a/src/04-multiprocessing/multiprocessing b/src/04-multiprocessing/multiprocessing deleted file mode 100755 index ecd66d3..0000000 Binary files a/src/04-multiprocessing/multiprocessing and /dev/null differ diff --git a/src/04-multiprocessing/process.c b/src/04-multiprocessing/process.c new file mode 100644 index 0000000..baf4cad --- /dev/null +++ b/src/04-multiprocessing/process.c @@ -0,0 +1,148 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define __USE_GNU // not the define from the professor !!!!!!!!!!!!!!!!!!!!! +#include +#include + +const int NBR_MSG = 5; +const char * MSG[] = { + "Hallo, hallo !\0", + "ça geht !\0", + "Comment vont les olives ?\0", + "Sacré trucs tes trucs là.\0", + "Ta où les vaches !!!!!\0" +}; + +static void catch_signal(int signal) { + + switch (signal) { + case SIGHUP: + printf("SIGHUP received\n"); + break; + case SIGINT: + printf("SIGINT received\n"); + exit(EXIT_SUCCESS); + break; + case SIGQUIT: + printf("SIGQUIT received\n"); + break; + case SIGTERM: + printf("SIGTERM received\n"); + break; + case SIGABRT: + printf("SIGABRT received\n"); + break; + } + + +} + +static void install_catch_signal() +{ + struct sigaction act = { + .sa_handler = catch_signal, + }; + sigemptyset(&act.sa_mask); + sigaction(SIGHUP, &act, 0); + sigaction(SIGINT, &act, 0); + sigaction(SIGQUIT, &act, 0); + sigaction(SIGTERM, &act, 0); + sigaction(SIGABRT, &act, 0); +} + + +int main(int argc, char* argv[]) { + + install_catch_signal(); + + /* Setup socket for inter-process communication */ + int fd[2]; + int err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd); + if (err == -1) { + perror("socketpair fail"); + exit(1); + } + + /* Fork a child process */ + pid_t pid = fork(); + if (pid == 0) { + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(0, &set); + int ret = sched_setaffinity(pid, sizeof(set), &set); + if (ret == -1) { + perror("sched_setaffinity"); + exit(1); + } + + /* Parent processus */ + printf("Parent processus: pid=%d\n", pid); + pid_t child_pid = getpid(); + char buffer[100]; + + for (int i = 0; i < 7; i++) { + read(fd[1], buffer, strlen(MSG[i])); + printf("%s\n", buffer); + memset(buffer, 0, sizeof(buffer)); + } + + int status = 0; + int waited_pid = waitpid(pid, &status, 0); + + if (waited_pid == -1) { + perror("waitpid"); + } else { + if (WIFEXITED(status)) { + printf("Child exited with status %d\n", + WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + printf("Child killed by signal %d\n", + WTERMSIG(status)); + } + } + + } else if (pid > 0) { + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(1, &set); + int ret = sched_setaffinity(pid, sizeof(set), &set); + if (ret == -1) { + perror("sched_setaffinity"); + exit(1); + } + /* Child processus */ + printf("Child processus: pid=%d\n", pid); + + for (int i = 0; i < NBR_MSG; i++) { + if(i==2) sleep(2); + write(fd[0], MSG[i], strlen(MSG[i])); + } + + exit(EXIT_SUCCESS); + + } else { + /* error */ + perror("fork fail"); + exit(EXIT_FAILURE); + } + + /* Test signal handling */ + kill(getpid(), SIGHUP); + kill(getpid(), SIGQUIT); + kill(getpid(), SIGTERM); + kill(getpid(), SIGABRT); + kill(getpid(), SIGINT); + + return 0; +}