Ca a terminé l'exo 5

This commit is contained in:
Arnaud NGWALA NGWALA 2023-09-26 21:54:54 +02:00
parent 42787211fd
commit 8386f31be1
2 changed files with 77 additions and 3 deletions

View File

@ -10,20 +10,33 @@
int search(const unsigned char * t,int start,int end, int n) int search(const unsigned char * t,int start,int end, int n)
{ {
pid_t p; pid_t p;
int i, proc, status, partition = ceil((start + end) / n); int i, proc, res = 0, status, partition = ceil((start + end) / n);
for (proc = 1; proc <= n;proc++){ for (proc = 1; proc <= n;proc++){
p = fork(); p = fork();
if (p == 0){ if (p == 0){
for (i = 0 + partition * proc 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 main(int argc , char * argv[])
{ {
int i, n; int i, n;
unsigned char arr[SIZE]; unsigned char arr[SIZE];
if (argc != 2) if (argc != 2){
printf("Usage : %s <nombre de fils à créer entre 1 et 100>", argv[0]); printf("Usage : %s <nombre de fils à créer entre 1 et 100>", argv[0]);
return EXIT_FAILURE;
}
n = (int) strtod(argv[1], NULL); n = (int) strtod(argv[1], NULL);
srandom(time(NULL)); srandom(time(NULL));

61
SCR3.1/TP3/ex5_v3.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <math.h>
#include <signal.h>
#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 <nombre de fils à créer entre 1 et 100>\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;
}