From 7376474b67eeda6e44e0d4f246febab74c6dda73 Mon Sep 17 00:00:00 2001 From: Tom Moguljak Date: Thu, 28 Sep 2023 23:10:20 +0200 Subject: [PATCH] Ajout exo 5 TP3 --- TP3/Exo5/exo5_1.c | 57 +++++++++++++++++++++++++++++++++++++++ TP3/Exo5/exo5_2.c | 63 +++++++++++++++++++++++++++++++++++++++++++ TP3/Exo5/exo5_3.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 TP3/Exo5/exo5_1.c create mode 100644 TP3/Exo5/exo5_2.c create mode 100644 TP3/Exo5/exo5_3.c diff --git a/TP3/Exo5/exo5_1.c b/TP3/Exo5/exo5_1.c new file mode 100644 index 0000000..5e67563 --- /dev/null +++ b/TP3/Exo5/exo5_1.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include + +#define SIZE 1000 +int search(const unsigned char *t, int start, int end) +{ + pid_t p; + int i, status, moitie = ceil((start + end) / 2); + p = fork(); + switch (p) + { + case -1: + exit(1); + case 0: + for (i = 0; i < moitie; i++) + { + if (t[i] == 0) + exit(1); + } + exit(0); + default: + for (i = moitie; i <= end; i++) + { + if (t[i] == 0) + return 1; + } + wait(&status); + return WEXITSTATUS(status); + } +} + +int main(int argc, char *argv[]) +{ + int i; + unsigned char arr[SIZE]; + + srandom(time(NULL)); + + for (i = 0; i < SIZE; i++) + arr[i] = (unsigned char)(random() % 255) + 1; + + printf("Enter a number between 0 and %d: ", SIZE); + scanf(" %d", &i); + if (i >= 0 && i < SIZE) + arr[i] = 0; + + if (search(arr, 0, SIZE - 1)) + printf("Found !\n"); + else + printf("Not found !\n"); + return EXIT_SUCCES; +} \ No newline at end of file diff --git a/TP3/Exo5/exo5_2.c b/TP3/Exo5/exo5_2.c new file mode 100644 index 0000000..569ed63 --- /dev/null +++ b/TP3/Exo5/exo5_2.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include + +#define SIZE 1000 +int search(const unsigned char *t, int start, int end, int n) +{ + pid_t p; + int i, proc, res = 0, status, partition = ceil((start + end) / n); + for (proc = 1; proc <= n, proc++) + { + p = fork(); + if (p == 0) + { + for (i = 0 + partition * proc; i < partition * (proc + 1); i++) + { + if (t[i] == 0) + exit(1); + } + exit(0); + } + } + + for (i = 0; i < n; i++) + { + wait(&status); + if (WEXITSTATUS(status)) + res = 1; + } + return res; +} + +int main(int argc, char *argv[]) +{ + int i, n; + unsigned char arr[SIZE]; + if (argc != 2) + { + printf("Usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + n = (int)strtod(argv[1], NULL); + srandom(time(NULL)); + + for (i = 0; i < SIZE; i++) + arr[i] = (unsigned char)(random() % 255) + 1; + + printf("Enter a number between 0 and %d: ", SIZE); + scanf(" %d", &i); + if (i >= 0 && i < SIZE) + arr[i] = 0; + + if (search(arr, 0, SIZE - 1, n)) + printf("Found !\n"); + else + printf("Not found !\n"); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/TP3/Exo5/exo5_3.c b/TP3/Exo5/exo5_3.c new file mode 100644 index 0000000..7568d71 --- /dev/null +++ b/TP3/Exo5/exo5_3.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include + +#define SIZE 1000 +int search(const unsigned char *t, int start, int end, int n) +{ + pid_t p; + int i, proc, res = 0, status, partition = ceil((start + end) / n); + + for (proc = 1; proc <= n; proc++) + { + p = fork(); + if (p == 0) + { + for (i = 0 + partition * proc; i < partition * (proc + 1); i++) + { + if (t[i] == 0) + { + exit(1); + kill(0, SIGTERM); + } + } + exit(0); + } + } + + for (i = 0; i < n; i++) + { + wait(&status); + if (WEXITSTATUS(status)) + res = 1; + } + return res; +} + +int main(int argc, char *argv[]) +{ + int i, n; + unsigned char arr[SIZE]; + if (argc != 2) + { + printf("Usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + n = (int)strtod(argv[1], NULL); + srandom(time(NULL)); + + for (i = 0; i < SIZE; i++) + arr[i] = (unsigned char)(random() % 255) + 1; + + printf("Enter a number between 0 and %d: ", SIZE); + scanf(" %d", &i); + + if (i >= 0 && i < SIZE) + arr[i] = 0; + + if (search(arr, 0, SIZE - 1, n)) + printf("Found !\n"); + else + printf("Not found !\n"); + return EXIT_SUCCESS; +} \ No newline at end of file