tkt
This commit is contained in:
BIN
SCR3.1/TP3/Exo5/V1
Executable file
BIN
SCR3.1/TP3/Exo5/V1
Executable file
Binary file not shown.
BIN
SCR3.1/TP3/Exo5/V2
Executable file
BIN
SCR3.1/TP3/Exo5/V2
Executable file
Binary file not shown.
BIN
SCR3.1/TP3/Exo5/V3
Executable file
BIN
SCR3.1/TP3/Exo5/V3
Executable file
Binary file not shown.
68
SCR3.1/TP3/Exo5/v1.c
Normal file
68
SCR3.1/TP3/Exo5/v1.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#define SIZE 1000
|
||||
|
||||
int search(const unsigned char *t, int start, int end) {
|
||||
int i;
|
||||
for (i = start; i <= end; i++) {
|
||||
if (t[i] == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i;
|
||||
unsigned char arr[SIZE];
|
||||
int pipefd[2];
|
||||
pid_t pid;
|
||||
int found_by_child = 0;
|
||||
int found_by_parent;
|
||||
|
||||
srandom(time(NULL));
|
||||
|
||||
for (i = 0; i < SIZE; i++)
|
||||
arr[i] = (unsigned char)(random() % 255) + 1;
|
||||
|
||||
printf("Entrez un nombre entre 0 et %d: ", SIZE - 1);
|
||||
scanf(" %d", &i);
|
||||
if (i >= 0 && i < SIZE) arr[i] = 0;
|
||||
|
||||
if (pipe(pipefd) == -1) {
|
||||
perror("pipe");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
pid = fork();
|
||||
if (pid == -1) {
|
||||
perror("fork");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (pid == 0) { // Processus fils
|
||||
close(pipefd[0]); // Ferme l'entrée du pipe
|
||||
int result = search(arr, SIZE / 2, SIZE - 1);
|
||||
write(pipefd[1], &result, sizeof(result));
|
||||
close(pipefd[1]); // Ferme la sortie du pipe
|
||||
exit(EXIT_SUCCESS);
|
||||
} else { // Processus père
|
||||
close(pipefd[1]); // Ferme la sortie du pipe
|
||||
found_by_parent = search(arr, 0, SIZE / 2 - 1);
|
||||
read(pipefd[0], &found_by_child, sizeof(found_by_child));
|
||||
close(pipefd[0]);
|
||||
wait(NULL);
|
||||
|
||||
if (found_by_parent || found_by_child)
|
||||
printf("Found !\n");
|
||||
else
|
||||
printf("Not found !\n");
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
83
SCR3.1/TP3/Exo5/v2.c
Normal file
83
SCR3.1/TP3/Exo5/v2.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE 1000
|
||||
|
||||
int search(const unsigned char *t, int start, int end) {
|
||||
int i;
|
||||
for (i = start; i <= end; i++) {
|
||||
if (t[i] == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: %s <number_of_processes>\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int n_procs = atoi(argv[1]);
|
||||
if (n_procs < 1 || n_procs > 100) {
|
||||
fprintf(stderr, "Number of processes must be between 1 and 100.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int i, status;
|
||||
unsigned char arr[SIZE];
|
||||
pid_t pid;
|
||||
int found = 0;
|
||||
int chunk_size = SIZE / n_procs;
|
||||
int remainder = SIZE % n_procs;
|
||||
|
||||
srandom(time(NULL));
|
||||
|
||||
for (i = 0; i < SIZE; i++)
|
||||
arr[i] = (unsigned char)(random() % 255) + 1;
|
||||
|
||||
printf("Entrez un nombre entre 0 et %d: ", SIZE - 1);
|
||||
scanf(" %d", &i);
|
||||
if (i >= 0 && i < SIZE) arr[i] = 0;
|
||||
|
||||
for (i = 0; i < n_procs; i++) {
|
||||
pid = fork();
|
||||
if (pid == -1) {
|
||||
perror("fork");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (pid == 0) { // Processus fils
|
||||
int start = i * chunk_size;
|
||||
int end = start + chunk_size - 1;
|
||||
if (i == n_procs - 1) { // Le dernier fils gère le reste
|
||||
end += remainder;
|
||||
}
|
||||
int result = search(arr, start, end);
|
||||
exit(result);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < n_procs; i++) {
|
||||
waitpid(-1, &status, 0);
|
||||
if (WIFEXITED(status)) {
|
||||
if (WEXITSTATUS(status) == 1) {
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
printf("Found !\n");
|
||||
else
|
||||
printf("Not found !\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
96
SCR3.1/TP3/Exo5/v3.c
Normal file
96
SCR3.1/TP3/Exo5/v3.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define SIZE 1000
|
||||
|
||||
int search(const unsigned char *t, int start, int end) {
|
||||
int i;
|
||||
for (i = start; i <= end; i++) {
|
||||
if (t[i] == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: %s <number_of_processes>\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int n_procs = atoi(argv[1]);
|
||||
if (n_procs < 1 || n_procs > 100) {
|
||||
fprintf(stderr, "Number of processes must be between 1 and 100.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int i, status;
|
||||
unsigned char arr[SIZE];
|
||||
pid_t pids[n_procs];
|
||||
int found = 0;
|
||||
int chunk_size = SIZE / n_procs;
|
||||
int remainder = SIZE % n_procs;
|
||||
|
||||
srandom(time(NULL));
|
||||
|
||||
for (i = 0; i < SIZE; i++)
|
||||
arr[i] = (unsigned char)(random() % 255) + 1;
|
||||
|
||||
printf("Entrez un nombre entre 0 et %d: ", SIZE - 1);
|
||||
scanf(" %d", &i);
|
||||
if (i >= 0 && i < SIZE) arr[i] = 0;
|
||||
|
||||
for (i = 0; i < n_procs; i++) {
|
||||
pids[i] = fork();
|
||||
if (pids[i] == -1) {
|
||||
perror("fork");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (pids[i] == 0) { // Processus fils
|
||||
int start = i * chunk_size;
|
||||
int end = start + chunk_size - 1;
|
||||
if (i == n_procs - 1) {
|
||||
end += remainder;
|
||||
}
|
||||
int result = search(arr, start, end);
|
||||
exit(result);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < n_procs; i++) {
|
||||
pid_t pid = waitpid(-1, &status, 0);
|
||||
if (pid == -1) {
|
||||
perror("waitpid");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (WIFEXITED(status) && WEXITSTATUS(status) == 1) {
|
||||
found = 1;
|
||||
printf("Zéro trouvé ! Envoi de SIGTERM aux autres processus...\n");
|
||||
for (int j = 0; j < n_procs; j++) {
|
||||
if (pids[j] != 0 && pids[j] != pid) {
|
||||
kill(pids[j], SIGTERM);
|
||||
}
|
||||
}
|
||||
break; // Sort de la boucle une fois le zéro trouvé
|
||||
}
|
||||
}
|
||||
|
||||
// Attendre la fin des processus restants (ceux tués par SIGTERM)
|
||||
while (wait(NULL) > 0);
|
||||
|
||||
if (found)
|
||||
printf("Found !\n");
|
||||
else
|
||||
printf("Not found !\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user