diff --git a/SCR3.1/TP3/Exo1/ex1-stdio.c b/SCR3.1/TP3/Exo1/ex1-stdio.c new file mode 100644 index 0000000..4bc5de2 --- /dev/null +++ b/SCR3.1/TP3/Exo1/ex1-stdio.c @@ -0,0 +1,12 @@ +#include +#include +#include + + +int main(){ + + printf("NON"); + if (fork()) { + printf("OUI\n"); + } +} diff --git a/SCR3.1/TP3/Exo1/ex1-syscall.c b/SCR3.1/TP3/Exo1/ex1-syscall.c new file mode 100644 index 0000000..1b46b6a --- /dev/null +++ b/SCR3.1/TP3/Exo1/ex1-syscall.c @@ -0,0 +1,14 @@ +#include +#include +#include + + +int main(){ + + //printf("NON"); + write(STDOUT_FILENO,"NON",3); + if (fork()) { + //printf("OUI\n"); + write(STDOUT_FILENO,"OUI\n",4); + } +} diff --git a/SCR3.1/TP3/Exo1/stdio b/SCR3.1/TP3/Exo1/stdio new file mode 100755 index 0000000..e24fefe Binary files /dev/null and b/SCR3.1/TP3/Exo1/stdio differ diff --git a/SCR3.1/TP3/Exo1/syscall b/SCR3.1/TP3/Exo1/syscall new file mode 100755 index 0000000..3386f12 Binary files /dev/null and b/SCR3.1/TP3/Exo1/syscall differ diff --git a/SCR3.1/TP3/Exo2/banane1 b/SCR3.1/TP3/Exo2/banane1 new file mode 100644 index 0000000..e015745 --- /dev/null +++ b/SCR3.1/TP3/Exo2/banane1 @@ -0,0 +1 @@ +je suis le pereje suis le fils !!! \ No newline at end of file diff --git a/SCR3.1/TP3/Exo2/banane2 b/SCR3.1/TP3/Exo2/banane2 new file mode 100644 index 0000000..8e23c20 --- /dev/null +++ b/SCR3.1/TP3/Exo2/banane2 @@ -0,0 +1 @@ +je suis le fils !!! \ No newline at end of file diff --git a/SCR3.1/TP3/Exo2/fork_and_fd1 b/SCR3.1/TP3/Exo2/fork_and_fd1 new file mode 100755 index 0000000..c9c51cf Binary files /dev/null and b/SCR3.1/TP3/Exo2/fork_and_fd1 differ diff --git a/SCR3.1/TP3/Exo2/fork_and_fd1.c b/SCR3.1/TP3/Exo2/fork_and_fd1.c new file mode 100644 index 0000000..2c529b9 --- /dev/null +++ b/SCR3.1/TP3/Exo2/fork_and_fd1.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define msg1 "je suis le pere" +#define msg2 "je suis le fils !!!" + +int main(int argc,char * argv[]){ + + int outfd; + pid_t p; + + if (argc != 2){ + + printf("%s file\n",argv[0]); + exit(1); + } + + //p=fork(); + outfd = open(argv[1],O_WRONLY|O_CREAT,0644); + assert(outfd >= 0); + p=fork(); + switch(p){ + case (pid_t)-1 : + perror(NULL); + exit(2); + + case (pid_t)0 : + write(outfd,msg2,strlen(msg2)); + break; + default : + write (outfd,msg1,strlen(msg1)); + break; + } + close(outfd); +} diff --git a/SCR3.1/TP3/Exo2/fork_and_fd2 b/SCR3.1/TP3/Exo2/fork_and_fd2 new file mode 100755 index 0000000..2bbb9e6 Binary files /dev/null and b/SCR3.1/TP3/Exo2/fork_and_fd2 differ diff --git a/SCR3.1/TP3/Exo2/fork_and_fd2.c b/SCR3.1/TP3/Exo2/fork_and_fd2.c new file mode 100644 index 0000000..7a63fe5 --- /dev/null +++ b/SCR3.1/TP3/Exo2/fork_and_fd2.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define msg1 "je suis le pere" +#define msg2 "je suis le fils !!!" + +int main(int argc,char * argv[]){ + + int outfd; + pid_t p; + + if (argc != 2){ + + printf("%s file\n",argv[0]); + exit(1); + } + + p=fork(); + outfd = open(argv[1],O_WRONLY|O_CREAT,0644); + assert(outfd >= 0); + //p=fork(); + switch(p){ + case (pid_t)-1 : + perror(NULL); + exit(2); + + case (pid_t)0 : + write(outfd,msg2,strlen(msg2)); + break; + default : + write (outfd,msg1,strlen(msg1)); + break; + } + close(outfd); +} diff --git a/SCR3.1/TP3/Exo3/banane1 b/SCR3.1/TP3/Exo3/banane1 new file mode 100644 index 0000000..a13ba16 --- /dev/null +++ b/SCR3.1/TP3/Exo3/banane1 @@ -0,0 +1 @@ +jvhbdnsqcvhbsdhhdbkjabd,v diff --git a/SCR3.1/TP3/Exo3/banane2 b/SCR3.1/TP3/Exo3/banane2 new file mode 100644 index 0000000..a13ba16 --- /dev/null +++ b/SCR3.1/TP3/Exo3/banane2 @@ -0,0 +1 @@ +jvhbdnsqcvhbsdhhdbkjabd,v diff --git a/SCR3.1/TP3/Exo3/banane3 b/SCR3.1/TP3/Exo3/banane3 new file mode 100644 index 0000000..a13ba16 --- /dev/null +++ b/SCR3.1/TP3/Exo3/banane3 @@ -0,0 +1 @@ +jvhbdnsqcvhbsdhhdbkjabd,v diff --git a/SCR3.1/TP3/Exo3/copy1byte b/SCR3.1/TP3/Exo3/copy1byte new file mode 100755 index 0000000..6b13615 Binary files /dev/null and b/SCR3.1/TP3/Exo3/copy1byte differ diff --git a/SCR3.1/TP3/Exo3/copy1byte.c b/SCR3.1/TP3/Exo3/copy1byte.c new file mode 100644 index 0000000..fab6100 --- /dev/null +++ b/SCR3.1/TP3/Exo3/copy1byte.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define msg1 "je suis le pere" +#define msg2 "je suis le fils !!!" + +int main(int argc,char * argv[]){ + + int infd,outfd; + ssize_t nbread; + char buf[1]; + pid_t p; + + if (argc != 3){ + + printf("%s infile outfile\n",argv[0]); + exit(1); + } + + infd = open(argv[1],O_RDONLY); + assert(infd >= 0); + outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644); + assert(outfd >= 0); + + p=fork(); // <- decommentez cette ligne + + while(1){ + + nbread=read(infd,buf,sizeof(buf)); + if (nbread <=0 ) break; + write(outfd,buf,sizeof(buf)); + + } + + + close(infd); + close(outfd); +} diff --git a/SCR3.1/TP3/Exo3/copy1byte2 b/SCR3.1/TP3/Exo3/copy1byte2 new file mode 100755 index 0000000..f7693af Binary files /dev/null and b/SCR3.1/TP3/Exo3/copy1byte2 differ diff --git a/SCR3.1/TP3/Exo4/Exo4.c b/SCR3.1/TP3/Exo4/Exo4.c new file mode 100644 index 0000000..4a8e0b5 --- /dev/null +++ b/SCR3.1/TP3/Exo4/Exo4.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +int main() { + pid_t pid; + int status; + + pid = fork(); + + if (pid < 0) { + // Création du processus échoué + perror("fork"); + return 1; + } else if (pid == 0) { + // Processus fils + printf("Fils: %d\n", pid); + printf("Fils: Mon PID %d\n", getpid()); + printf("Fils: Celui du père %d\n", getppid()); + printf("Fils: je m'endors pour 4 secondes...\n"); + sleep(4); + exit(2); + } else { + // Processus père + printf("Père: fork() %d (le PID du fils)\n", pid); + printf("Père: mon PID %d\n", getpid()); + printf("Père: PID de mon père %d\n", getppid()); + printf("Père: attends la fin de mon fils...\n"); + wait(&status); + printf("Père: Code de retour : %d\n", WEXITSTATUS(status)); + + printf("Père: processus actifs...\n"); + execl("/bin/ps", "ps", "-ef", NULL); + + //Sera exécutée que si execl échoue + perror("execl"); + return 1; + } + + return 0; +} diff --git a/SCR3.1/TP3/Exo4/Exo4prof.c b/SCR3.1/TP3/Exo4/Exo4prof.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/SCR3.1/TP3/Exo4/Exo4prof.c @@ -0,0 +1 @@ + diff --git a/SCR3.1/TP3/Exo4/a.out b/SCR3.1/TP3/Exo4/a.out new file mode 100755 index 0000000..276b626 Binary files /dev/null and b/SCR3.1/TP3/Exo4/a.out differ diff --git a/SCR3.1/TP3/Exo5/V1 b/SCR3.1/TP3/Exo5/V1 new file mode 100755 index 0000000..7c1da1a Binary files /dev/null and b/SCR3.1/TP3/Exo5/V1 differ diff --git a/SCR3.1/TP3/Exo5/V2 b/SCR3.1/TP3/Exo5/V2 new file mode 100755 index 0000000..5252b03 Binary files /dev/null and b/SCR3.1/TP3/Exo5/V2 differ diff --git a/SCR3.1/TP3/Exo5/V3 b/SCR3.1/TP3/Exo5/V3 new file mode 100755 index 0000000..e04c614 Binary files /dev/null and b/SCR3.1/TP3/Exo5/V3 differ diff --git a/SCR3.1/TP3/Exo5/v1.c b/SCR3.1/TP3/Exo5/v1.c new file mode 100644 index 0000000..5fddadd --- /dev/null +++ b/SCR3.1/TP3/Exo5/v1.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +#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; +} diff --git a/SCR3.1/TP3/Exo5/v2.c b/SCR3.1/TP3/Exo5/v2.c new file mode 100644 index 0000000..d8bde23 --- /dev/null +++ b/SCR3.1/TP3/Exo5/v2.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include + +#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 \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; +} + diff --git a/SCR3.1/TP3/Exo5/v3.c b/SCR3.1/TP3/Exo5/v3.c new file mode 100644 index 0000000..23d50b2 --- /dev/null +++ b/SCR3.1/TP3/Exo5/v3.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#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 \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; +}