Exemples Tubes
This commit is contained in:
22
Exemples/06-Tubes/ecrivain.c
Normal file
22
Exemples/06-Tubes/ecrivain.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
int main (void) {
|
||||
int fd;
|
||||
char message[26];
|
||||
|
||||
sprintf(message, "bonjour du writer [%d]\n", getpid());
|
||||
|
||||
fd = open("my_tube", O_WRONLY);
|
||||
printf("Ici writer [%d]\n", getpid());
|
||||
if (fd != -1) {
|
||||
write(fd, message, strlen(message));
|
||||
}
|
||||
else
|
||||
printf("Désolé, je trouve pas le tube\n");
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
45
Exemples/06-Tubes/exTube.c
Normal file
45
Exemples/06-Tubes/exTube.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
int tube[2], fcntlLecture, fcntlEcriture;
|
||||
char buf[10];
|
||||
|
||||
struct stat statLecture, statEcriture;
|
||||
|
||||
void noeud (struct stat *statDesc) {
|
||||
printf("Quelques caractérisitques du noeud\n");
|
||||
if (S_ISFIFO(statDesc->st_mode)) printf("\t Type : tube\n");
|
||||
else printf("\t Type : autre que tube\n");
|
||||
printf("\t Numéro : %llu\n", statDesc-> st_ino);
|
||||
printf("\t Numéro de disque : %d\n", statDesc-> st_dev);
|
||||
printf("\t Numéro de liens physiques : %d\n", statDesc-> st_nlink);
|
||||
printf("\t Taille : %lld\n", statDesc-> st_size);
|
||||
}
|
||||
|
||||
main() {
|
||||
if (pipe(tube) == -1) { perror("Création ratée"), exit(2); }
|
||||
if ((fcntlLecture = fcntl(tube[0], F_GETFL)) == -1) {
|
||||
perror("Fcntl lecture "); exit(2); }
|
||||
if ((fcntlEcriture = fcntl(tube[1], F_GETFL)) == -1) {
|
||||
perror("Fcntl ecriture "); exit(2); }
|
||||
printf("Attributs de %d (tube[0]) : %d\n", tube[0], fcntlLecture);
|
||||
printf("Attributs de %d (tube[1]) : %d\n", tube[1], fcntlEcriture);
|
||||
write(tube[1], "0123456789", 10);
|
||||
read(tube[0], buf, 5);
|
||||
printf("Chaine lue : %s\n", buf);
|
||||
|
||||
fstat(tube[0], &statLecture);
|
||||
fstat(tube[1], &statEcriture);
|
||||
if (memcmp(&statLecture, &statEcriture, sizeof(struct stat)) != 0) {
|
||||
fprintf(stderr, "Noeuds en lecture et écriture différents\n");
|
||||
printf("Descripteur en lecture\n"); noeud(&statLecture);
|
||||
printf("Descripteur en ecriture\n"); noeud(&statEcriture);
|
||||
} else {
|
||||
printf("Noeud en lecture et ecriture identique\n");
|
||||
noeud(&statEcriture); }
|
||||
}
|
71
Exemples/06-Tubes/exo1.c
Normal file
71
Exemples/06-Tubes/exo1.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define R 0
|
||||
#define W 1
|
||||
|
||||
void action (int sig) {
|
||||
printf("Je capture le signal\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int fdp[2], // père vers le fils
|
||||
fdf[2]; // fils vers père
|
||||
pid_t pid;
|
||||
|
||||
char message[100]; // pour récupérer un message
|
||||
char *phrasef = "message envoyé au père par le fils";
|
||||
char *phrasep = "message envoyé au fils par le père";
|
||||
|
||||
if ((pipe(fdp) != 0) || (pipe(fdf) != 0)) {
|
||||
perror("Pipe pas possible");
|
||||
exit(1);
|
||||
}
|
||||
signal( SIGPIPE, &action);
|
||||
|
||||
if ((pid = fork()) == -1) {
|
||||
perror("Fork pas possible");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
|
||||
if (pid == 0) {
|
||||
|
||||
close(fdf[R]); //
|
||||
close(fdp[W]); //
|
||||
|
||||
if (write( fdf[W], phrasef, strlen(phrasef)+1) > 0) {
|
||||
sleep(4);
|
||||
int n = read(fdp[R], message, 100);
|
||||
// Dans ce cas le signal SIGPIPE est recu => processus tué
|
||||
printf("Valeur de n : %d\n", n);
|
||||
if (n > 0) {
|
||||
printf("J'ai lu : %s\n", message);
|
||||
} else {
|
||||
perror("Lecture pas bon");
|
||||
exit(2);
|
||||
}
|
||||
close(fdf[W]);
|
||||
exit(0);
|
||||
} else {
|
||||
perror("Write pas bon"); exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
close(fdp[R]);
|
||||
close(fdf[W]);
|
||||
|
||||
write (fdp[W], phrasep, strlen(phrasep)+1);
|
||||
sleep(2);
|
||||
read(fdf[R], message, 100);
|
||||
printf("J'ai lu : %s\n", message);
|
||||
// wait(NULL);
|
||||
close(fdp[W]);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
78
Exemples/06-Tubes/exo1a.c
Normal file
78
Exemples/06-Tubes/exo1a.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define R 0
|
||||
#define W 1
|
||||
|
||||
void action (int sig) {
|
||||
printf("Je capture SIGPIPE\n");
|
||||
// quand les read se font sur des tubes sans écrivain
|
||||
}
|
||||
|
||||
int main() {
|
||||
int fdp[2], // père vers le fils
|
||||
fdf[2]; // fils vers père
|
||||
pid_t pid;
|
||||
|
||||
char message[100]; // pour récupérer un message
|
||||
char *phrasef = "message envoyé au père par le fils";
|
||||
char *phrasep = "message envoyé au fils par le père";
|
||||
|
||||
if ((pipe(fdf) == -1) || (pipe(fdp) == -1)) {
|
||||
perror("Pipe erreur");
|
||||
exit(1);
|
||||
}
|
||||
signal(SIGPIPE, &action);
|
||||
|
||||
if ((pid = fork()) == -1) {
|
||||
perror("Fork erreur");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
close(fdf[R]);
|
||||
close(fdp[W]);
|
||||
|
||||
if (write(fdf[W], phrasef, strlen(phrasef)+1) != -1) {
|
||||
sleep(4);
|
||||
|
||||
if (read(fdp[R], message, 100) >= 0) {
|
||||
printf("J'ai lu : %s\n", message);
|
||||
} else {
|
||||
perror("Read erreur"); exit(4);
|
||||
}
|
||||
} else {
|
||||
perror("Write erreur"); exit(3);
|
||||
}
|
||||
|
||||
close(fdp[R]);
|
||||
close(fdf[W]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
close(fdp[R]);
|
||||
close(fdf[W]);
|
||||
|
||||
if (write(fdp[W], phrasep, strlen(phrasep)+1) != -1) {
|
||||
sleep(2);
|
||||
|
||||
if (read(fdf[R], message, 100) >= 0) {
|
||||
printf("J'ai lu : %s\n", message);
|
||||
} else {
|
||||
perror("Read erreur"); exit(4);
|
||||
}
|
||||
} else {
|
||||
perror("Write erreur"); exit(3);
|
||||
}
|
||||
|
||||
|
||||
close(fdf[R]);
|
||||
close(fdp[W]);
|
||||
|
||||
return 0;
|
||||
// A compléter
|
||||
|
||||
}
|
35
Exemples/06-Tubes/fils.c
Normal file
35
Exemples/06-Tubes/fils.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
int main() {
|
||||
int f1, f2;
|
||||
|
||||
char message[100]; // pour récupérer un message
|
||||
char *phrasef = "message envoyé au père par le fils";
|
||||
|
||||
f1 = open("f2p", O_WRONLY);
|
||||
f2 = open("p2f", O_RDONLY);
|
||||
|
||||
if (write(f1, phrasef, strlen(phrasef)+1) != -1) {
|
||||
sleep(4);
|
||||
|
||||
if (read(f2, message, 100) >= 0) {
|
||||
printf("J'ai lu : %s\n", message);
|
||||
} else {
|
||||
perror("Read erreur"); exit(4);
|
||||
}
|
||||
} else {
|
||||
perror("Write erreur"); exit(3);
|
||||
}
|
||||
|
||||
|
||||
close(f1);
|
||||
close(f2);
|
||||
|
||||
return 0;
|
||||
}
|
23
Exemples/06-Tubes/lecteur.c
Normal file
23
Exemples/06-Tubes/lecteur.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int main (void) {
|
||||
int fd, n;
|
||||
char input;
|
||||
|
||||
fd = open("my_tube", O_RDONLY);
|
||||
printf("Ici reader [%d]\n", getpid());
|
||||
if (fd != -1) {
|
||||
printf("Recu par le reader : \n");
|
||||
while ((n = read(fd, &input, 1)) > 0) {
|
||||
printf("%c", input);
|
||||
}
|
||||
printf("Le reader a fini sa mission\n");
|
||||
}
|
||||
else
|
||||
printf("Désolé, je trouve pas le tube\n");
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
35
Exemples/06-Tubes/pere.c
Normal file
35
Exemples/06-Tubes/pere.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
int main() {
|
||||
int f1, f2;
|
||||
|
||||
char message[100]; // pour récupérer un message
|
||||
char *phrasep = "message envoyé au fils par le père";
|
||||
|
||||
f1 = open("f2p", O_RDONLY);
|
||||
f2 = open("p2f", O_WRONLY);
|
||||
|
||||
if (write(f2, phrasep, strlen(phrasep)+1) != -1) {
|
||||
sleep(2);
|
||||
|
||||
if (read(f1, message, 100) >= 0) {
|
||||
printf("J'ai lu : %s\n", message);
|
||||
} else {
|
||||
perror("Read erreur"); exit(4);
|
||||
}
|
||||
} else {
|
||||
perror("Write erreur"); exit(3);
|
||||
}
|
||||
|
||||
|
||||
close(f1);
|
||||
close(f2);
|
||||
|
||||
return 0;
|
||||
}
|
28
Exemples/06-Tubes/upipe.c
Normal file
28
Exemples/06-Tubes/upipe.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define R 0
|
||||
#define W 1
|
||||
|
||||
int main ( ) {
|
||||
int fd[2] ;
|
||||
char message[100] ;
|
||||
int nboctets ;
|
||||
char *phrase = " message envoye au pere par le fils " ;
|
||||
|
||||
pipe ( fd ) ;
|
||||
if (fork() == 0) {
|
||||
close ( fd[R] ) ;
|
||||
write ( fd[W] , phrase , strlen (phrase) + 1 ) ;
|
||||
close ( fd[W] ) ;
|
||||
}
|
||||
else {
|
||||
close ( fd[W] ) ;
|
||||
nboctets = read ( fd[R] , message , 100 ) ;
|
||||
printf ( " Lecture %d octets : %s\n" , nboctets , message ) ;
|
||||
close ( fd [R] ) ;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user