tkt
This commit is contained in:
12
SCR3.1/TP3/Exo1/ex1-stdio.c
Normal file
12
SCR3.1/TP3/Exo1/ex1-stdio.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
printf("NON");
|
||||
if (fork()) {
|
||||
printf("OUI\n");
|
||||
}
|
||||
}
|
||||
14
SCR3.1/TP3/Exo1/ex1-syscall.c
Normal file
14
SCR3.1/TP3/Exo1/ex1-syscall.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
//printf("NON");
|
||||
write(STDOUT_FILENO,"NON",3);
|
||||
if (fork()) {
|
||||
//printf("OUI\n");
|
||||
write(STDOUT_FILENO,"OUI\n",4);
|
||||
}
|
||||
}
|
||||
BIN
SCR3.1/TP3/Exo1/stdio
Executable file
BIN
SCR3.1/TP3/Exo1/stdio
Executable file
Binary file not shown.
BIN
SCR3.1/TP3/Exo1/syscall
Executable file
BIN
SCR3.1/TP3/Exo1/syscall
Executable file
Binary file not shown.
1
SCR3.1/TP3/Exo2/banane1
Normal file
1
SCR3.1/TP3/Exo2/banane1
Normal file
@@ -0,0 +1 @@
|
||||
je suis le pereje suis le fils !!!
|
||||
1
SCR3.1/TP3/Exo2/banane2
Normal file
1
SCR3.1/TP3/Exo2/banane2
Normal file
@@ -0,0 +1 @@
|
||||
je suis le fils !!!
|
||||
BIN
SCR3.1/TP3/Exo2/fork_and_fd1
Executable file
BIN
SCR3.1/TP3/Exo2/fork_and_fd1
Executable file
Binary file not shown.
42
SCR3.1/TP3/Exo2/fork_and_fd1.c
Normal file
42
SCR3.1/TP3/Exo2/fork_and_fd1.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include<assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
BIN
SCR3.1/TP3/Exo2/fork_and_fd2
Executable file
BIN
SCR3.1/TP3/Exo2/fork_and_fd2
Executable file
Binary file not shown.
42
SCR3.1/TP3/Exo2/fork_and_fd2.c
Normal file
42
SCR3.1/TP3/Exo2/fork_and_fd2.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include<assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
1
SCR3.1/TP3/Exo3/banane1
Normal file
1
SCR3.1/TP3/Exo3/banane1
Normal file
@@ -0,0 +1 @@
|
||||
jvhbdnsqcvhbsdhhdbkjabd,v
|
||||
1
SCR3.1/TP3/Exo3/banane2
Normal file
1
SCR3.1/TP3/Exo3/banane2
Normal file
@@ -0,0 +1 @@
|
||||
jvhbdnsqcvhbsdhhdbkjabd,v
|
||||
1
SCR3.1/TP3/Exo3/banane3
Normal file
1
SCR3.1/TP3/Exo3/banane3
Normal file
@@ -0,0 +1 @@
|
||||
jvhbdnsqcvhbsdhhdbkjabd,v
|
||||
BIN
SCR3.1/TP3/Exo3/copy1byte
Executable file
BIN
SCR3.1/TP3/Exo3/copy1byte
Executable file
Binary file not shown.
45
SCR3.1/TP3/Exo3/copy1byte.c
Normal file
45
SCR3.1/TP3/Exo3/copy1byte.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include<assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
BIN
SCR3.1/TP3/Exo3/copy1byte2
Executable file
BIN
SCR3.1/TP3/Exo3/copy1byte2
Executable file
Binary file not shown.
42
SCR3.1/TP3/Exo4/Exo4.c
Normal file
42
SCR3.1/TP3/Exo4/Exo4.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
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;
|
||||
}
|
||||
1
SCR3.1/TP3/Exo4/Exo4prof.c
Normal file
1
SCR3.1/TP3/Exo4/Exo4prof.c
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
BIN
SCR3.1/TP3/Exo4/a.out
Executable file
BIN
SCR3.1/TP3/Exo4/a.out
Executable file
Binary file not shown.
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