From ee935379e577dd2ab09f34713a17628c989f359f Mon Sep 17 00:00:00 2001 From: iliya <iliya.saroukha@hes-so.ch> Date: Tue, 14 Nov 2023 23:13:02 +0100 Subject: [PATCH] ya hate to see it --- pipes/ex2/.gitignore | 2 + pipes/ex2/Makefile | 26 +++++++++++ pipes/ex2/synchro.c | 98 ++++++++++++++++++++++++++++++++++++++++++ signaux/ex3/prog.c | 93 ++++++++++++++++++++------------------- signaux/ex3/prog.c.bak | 70 ++++++++++++++++++++++++++++++ 5 files changed, 245 insertions(+), 44 deletions(-) create mode 100644 pipes/ex2/.gitignore create mode 100644 pipes/ex2/Makefile create mode 100644 pipes/ex2/synchro.c create mode 100644 signaux/ex3/prog.c.bak diff --git a/pipes/ex2/.gitignore b/pipes/ex2/.gitignore new file mode 100644 index 0000000..0cc4433 --- /dev/null +++ b/pipes/ex2/.gitignore @@ -0,0 +1,2 @@ +*.o +synchro diff --git a/pipes/ex2/Makefile b/pipes/ex2/Makefile new file mode 100644 index 0000000..9d8f91c --- /dev/null +++ b/pipes/ex2/Makefile @@ -0,0 +1,26 @@ +CC := clang +CFLAGS := -g -pedantic -Wall -Wextra -std=c11 +LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm -lpthread +TARGET := synchro + +all: $(TARGET) + +$(TARGET): synchro.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 diff --git a/pipes/ex2/synchro.c b/pipes/ex2/synchro.c new file mode 100644 index 0000000..b0f2815 --- /dev/null +++ b/pipes/ex2/synchro.c @@ -0,0 +1,98 @@ +#include <libgen.h> +#include <signal.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <time.h> +#include <unistd.h> +#define DATA_LEN (1 << 9) + +int main(int argc, char *argv[]) { + if (argc < 2 || argc > 2) { + fprintf(stderr, "Usage:\n"); + fprintf(stderr, "\t./%s <nb_processes>\n", basename(argv[0])); + exit(EXIT_FAILURE); + } + + int nb_processes = atoi(argv[1]); + + fprintf(stdout, "P0 (main) (%d)\n", getpid()); + + for (int i = 0; i < nb_processes - 1; i++) { + pid_t pid = fork(); + + if (pid > 0 && i == 0) { + // int status; + // if (wait(&status) == -1) { + // perror("wait"); + // exit(EXIT_FAILURE); + // } + // + // if (WIFEXITED(status)) { + // fprintf(stdout, "P%d terminated...\n", i + 1); + // } + } else if (pid == 0) { + + int duration = rand() % (15 + 1 - 3) + 3; + sleep(duration); + fprintf(stdout, "P%d (%d) (%d)\t:\t%d sec.\n", i + 1, getpid(), + getppid(), duration); + + } else if (pid == -1) { + perror("fork"); + exit(EXIT_FAILURE); + } + } + + return EXIT_SUCCESS; + + // srand(time(NULL)); + // + // int arr_fd[2] = {0}; + // char buff[DATA_LEN] = {0}; + // int wstatus; + // + // for (size_t i = 0; i < DATA_LEN; i++) { + // buff[i] = rand() % INT8_MAX; + // fprintf(stdout, "%d ", buff[i]); + // } + // fprintf(stdout, "\n"); + // + // if (pipe(arr_fd) == -1) { + // perror("pipe"); + // } + // + // pid_t pid = fork(); + // + // if (pid > 0) { // Parent + // // Parent closes exit FD + // close(arr_fd[0]); + // + // write(arr_fd[1], buff, DATA_LEN); + // close(arr_fd[1]); + // + // wait(&wstatus); + // if (WIFEXITED(wstatus)) { + // fprintf(stdout, "Child has terminated\n"); + // } + // } else if (pid == 0) { // Child + // // Child closes entry FD + // close(arr_fd[1]); + // char child_buff[DATA_LEN] = {0}; + // size_t count = 0; + // size_t bytes_read = 0; + // + // while ((bytes_read = read(arr_fd[0], child_buff, 16))) { + // count += bytes_read; + // } + // + // fprintf(stdout, "Number of bytes read: %lu\n", count); + // } else { // Error + // perror("fork"); + // } + // + // return EXIT_SUCCESS; +} diff --git a/signaux/ex3/prog.c b/signaux/ex3/prog.c index 5e4add3..440bbcc 100644 --- a/signaux/ex3/prog.c +++ b/signaux/ex3/prog.c @@ -1,40 +1,57 @@ +#include <bits/types/sigset_t.h> #include <signal.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> -volatile sig_atomic_t count = 0; -struct sigaction usr1; +volatile sig_atomic_t enabled = 0; -static void handler_usr1(int __attribute__((unused)) signum) { - // if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) { - // fprintf(stdout, "SIGTSTP will be unblocked...\n"); +void sigusr1_handler(int __attribute__((unused)) signum) { + // sigset_t set; sigprocmask(SIG_BLOCK, NULL, &set); // - // if (sigdelset(&usr1.sa_mask, SIGTSTP) == -1) { - // perror("sigdelset"); + // if (sigismember(&set, SIGTSTP) == 1) { + // char msg[] = "SIGTSTP will be unblocked..\n"; + // write(STDOUT_FILENO, msg, strlen(msg)); + // + // sigdelset(&set, SIGTSTP); + // + // if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) { + // perror("sigprocmask"); // } - // } else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) { - // fprintf(stdout, "SIGTSTP will be blocked...\n"); + // } else if (sigismember(&set, SIGTSTP) == 0) { + // char msg[] = "SIGTSTP will be blocked..\n"; + // write(STDOUT_FILENO, msg, strlen(msg)); // - // if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) { - // perror("sigaddset"); + // sigaddset(&set, SIGTSTP); + // if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) { + // perror("sigprocmask"); // } // } else { // perror("sigismember"); // } +} + +void test(void) { + sigset_t set; + sigprocmask(SIG_BLOCK, NULL, &set); + + if (sigismember(&set, SIGTSTP) == 1) { + char msg[] = "SIGTSTP will be unblocked..\n"; + write(STDOUT_FILENO, msg, strlen(msg)); - if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) { - fprintf(stdout, "SIGTSTP will be unblocked...\n"); - if (sigprocmask(SIG_UNBLOCK, &usr1.sa_mask, NULL) == -1) { + sigdelset(&set, SIGTSTP); + + if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) { perror("sigprocmask"); } - sigaddset(&usr1.sa_mask, SIGTSTP); - } else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) { - fprintf(stdout, "SIGTSTP will be blocked...\n"); + } else if (sigismember(&set, SIGTSTP) == 0) { + char msg[] = "SIGTSTP will be blocked..\n"; + write(STDOUT_FILENO, msg, strlen(msg)); - sigaddset(&usr1.sa_mask, SIGTSTP); - if (sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL) == -1) { + sigaddset(&set, SIGTSTP); + if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) { perror("sigprocmask"); } } else { @@ -42,39 +59,27 @@ static void handler_usr1(int __attribute__((unused)) signum) { } } -// static void handler_usr2(int signum) { -// // fprintf(stdout, "Ctrl-C has been pressed %d times\n", counter); -// } +void sigusr2_handler(int __attribute__((unused)) signum) {} int main(void) { - usr1.sa_handler = handler_usr1; - usr1.sa_flags = 0; - sigemptyset(&usr1.sa_mask); + struct sigaction usr1 = {0}; + struct sigaction usr2 = {0}; + // memset(&usr1, 0, sizeof(struct sigaction)); + // memset(&usr2, 0, sizeof(struct sigaction)); - sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL); + usr1.sa_handler = sigusr1_handler; + usr2.sa_handler = sigusr2_handler; - if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) { - perror("sigaddset"); - } - - if (sigprocmask(SIG_SETMASK, &usr1.sa_mask, NULL) == -1) { - perror("sigprocmask"); - exit(EXIT_FAILURE); - } + sigemptyset(&usr1.sa_mask); + sigemptyset(&usr2.sa_mask); if (sigaction(SIGUSR1, &usr1, NULL) == -1) { - perror("sigaction"); - exit(EXIT_FAILURE); + perror("sigaddset"); } - // struct sigaction usr2; - // usr2.sa_handler = handler_usr2; - // usr2.sa_flags = 0; - // sigemptyset(&usr2.sa_mask); - - // if (sigaddset(&usr2.sa_mask, SIGUSR2) == -1) { - // perror("sigaddset"); - // } + if (sigaction(SIGUSR2, &usr2, NULL) == -1) { + perror("sigaddset"); + } while (true) { fprintf(stdout, "hello world\n"); diff --git a/signaux/ex3/prog.c.bak b/signaux/ex3/prog.c.bak new file mode 100644 index 0000000..279c024 --- /dev/null +++ b/signaux/ex3/prog.c.bak @@ -0,0 +1,70 @@ +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +volatile sig_atomic_t count = 0; +struct sigaction usr1; + +static void handler_usr1(int __attribute__((unused)) signum) { + if (sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL) == -1) { + perror("sigprocmask"); + } + + if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) { + fprintf(stdout, "SIGTSTP will be unblocked...\n"); + sigdelset(&usr1.sa_mask, SIGTSTP); + } else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) { + fprintf(stdout, "SIGTSTP will be blocked...\n"); + sigaddset(&usr1.sa_mask, SIGTSTP); + } else { + perror("sigismember"); + } + + if (sigprocmask(SIG_SETMASK, &usr1.sa_mask, NULL) == -1) { + perror("sigprocmask"); + } +} + +// static void handler_usr2(int signum) { +// // fprintf(stdout, "Ctrl-C has been pressed %d times\n", counter); +// } + +int main(void) { + usr1.sa_handler = handler_usr1; + usr1.sa_flags = 0; + sigemptyset(&usr1.sa_mask); + + sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL); + + if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) { + perror("sigaddset"); + } + + if (sigprocmask(SIG_SETMASK, &usr1.sa_mask, NULL) == -1) { + perror("sigprocmask"); + exit(EXIT_FAILURE); + } + + if (sigaction(SIGUSR1, &usr1, NULL) == -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + // struct sigaction usr2; + // usr2.sa_handler = handler_usr2; + // usr2.sa_flags = 0; + // sigemptyset(&usr2.sa_mask); + + // if (sigaddset(&usr2.sa_mask, SIGUSR2) == -1) { + // perror("sigaddset"); + // } + + while (true) { + fprintf(stdout, "hello world\n"); + sleep(1); + } + + return EXIT_SUCCESS; +} -- GitLab