From 42787211fd039f38e7d4c1a8b2c0160ea94df323 Mon Sep 17 00:00:00 2001 From: EmpereurWaza Date: Tue, 26 Sep 2023 21:11:50 +0200 Subject: [PATCH] =?UTF-8?q?Avanc=C3=A9es=20du=20dernier=20TP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SCR3.1/TP3/ex5_v1.c | 55 +++++++++++++++++++++++++++++++++++++++++++++ SCR3.1/TP3/ex5_v2.c | 44 ++++++++++++++++++++++++++++++++++++ SCR3.1/TP3/ex7.c | 36 +++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 SCR3.1/TP3/ex5_v1.c create mode 100644 SCR3.1/TP3/ex5_v2.c create mode 100644 SCR3.1/TP3/ex7.c diff --git a/SCR3.1/TP3/ex5_v1.c b/SCR3.1/TP3/ex5_v1.c new file mode 100644 index 0000000..e126941 --- /dev/null +++ b/SCR3.1/TP3/ex5_v1.c @@ -0,0 +1,55 @@ +#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_SUCCESS; +} + + diff --git a/SCR3.1/TP3/ex5_v2.c b/SCR3.1/TP3/ex5_v2.c new file mode 100644 index 0000000..583568c --- /dev/null +++ b/SCR3.1/TP3/ex5_v2.c @@ -0,0 +1,44 @@ +#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, status, partition = ceil((start + end) / n); + for (proc = 1; proc <= n;proc++){ + p = fork(); + if (p == 0){ + for (i = 0 + partition * proc + } +} + +int main(int argc , char * argv[]) +{ + int i, n; + unsigned char arr[SIZE]; + if (argc != 2) + printf("Usage : %s ", argv[0]); + 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; +} + + diff --git a/SCR3.1/TP3/ex7.c b/SCR3.1/TP3/ex7.c new file mode 100644 index 0000000..a982c8f --- /dev/null +++ b/SCR3.1/TP3/ex7.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include + +void fils(){ + srand(getpid()); + exit(rand()%128); +} + +int main(int argc, char** argv){ + int i, n, max, status; + pid_t p; + if (argc != 2){ + printf("Usage : %s ", argv[0]); + exit(1); + } + n = (int) strtod(argv[1], NULL); + for (i = 1; i <= n; i++){ + p = fork(); + assert(p != 1); + if (p == 0) + fils(); + } + wait(&status); + max = WEXITSTATUS(status); + for (i = 2; i <= n; i++){ + wait(&status); + if (WEXITSTATUS(status) > max) + max = WEXITSTATUS(status); + } + printf("Le plus grand nombre choisi par les processus est %d\n", max); + exit(0); +}