You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
3.8 KiB
171 lines
3.8 KiB
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <sys/wait.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
# define TAILLE_BUF 80 // 80 = taille d'une ligne
|
|
|
|
|
|
void fermeTubesPere(int N, int **tubes) {
|
|
int i;
|
|
for (i=0 ; i<N ; i++) {
|
|
close(tubes[i][0]);
|
|
}
|
|
}
|
|
|
|
void fermeTubesFils(int N, int **tubes) {
|
|
int i;
|
|
for (i=0 ; i<N ; i++) {
|
|
close(tubes[i][1]);
|
|
}
|
|
}
|
|
|
|
void fermeAutresTubes(int N, int **tubes, int numTube) {
|
|
int i;
|
|
for (i=0 ; i<N ; i++) {
|
|
if(i != numTube) {
|
|
close(tubes[i][0]);
|
|
}
|
|
}
|
|
}
|
|
|
|
void fermeTubes(int N, int **tubes) {
|
|
int i;
|
|
for (i=0 ; i<N ; i++) {
|
|
close(tubes[i][0]);
|
|
close(tubes[i][0]);
|
|
}
|
|
}
|
|
|
|
void codeDuFils(int N, int **tubes, int numFils) {
|
|
char buffer[TAILLE_BUF];
|
|
unsigned int cpt = 0;
|
|
// for(i=0;)
|
|
while(read(tubes[i][0], buffer, TAILLE_BUF)){
|
|
cpt ++;
|
|
printf("\t\e[1;33mFils : %d : lecture de\e[1;31m %s\e[0m\n", cpt, buffer);
|
|
}
|
|
printf("Fils se termine sur fin de lecteure du tube\n");
|
|
fermeTubesFils(N, tubes);
|
|
fermeAutresTubes(N, tubes, numFils);
|
|
exit(0);
|
|
}
|
|
|
|
void codeDuPere(int N, int **tubes) {
|
|
char buffer[TAILLE_BUF];
|
|
|
|
while(fgets(buffer, TAILLE_BUF, stdin) != NULL){
|
|
int i;
|
|
for (i=0 ; i<N ; i++) {
|
|
write(tubes[i][1], buffer, TAILLE_BUF);
|
|
}
|
|
}
|
|
|
|
fermeTubes(N, tubes);
|
|
wait(NULL);
|
|
printf("Pere se termine apès son fils.\n");
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int i, etat, N;
|
|
pid_t pid;
|
|
int tubes[100][2];
|
|
int *tmp;
|
|
|
|
if (argc!=2) {
|
|
fputs("Donner un arg entier\n", stderr);
|
|
exit(1);
|
|
}
|
|
N=atoi(argv[1]);
|
|
if (N <= 0 || N > 100) {
|
|
fprintf(stderr, "N doit être un entier positif ou inférieur à 100.\n");
|
|
exit(1);
|
|
}
|
|
if(pipe(tubes)== -1) {
|
|
perror(" pipe ");
|
|
exit(errno);
|
|
}
|
|
tmp = (int*)malloc(sizeof(int) * 2 * N);
|
|
|
|
if(tmp == NULL){
|
|
printf("Erreur Malloc");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
/* le pere va creer N fils */
|
|
for (i=0 ; i<N ; i++) {
|
|
if((pid=fork())==-1) {
|
|
perror("pb fork");
|
|
exit(errno);
|
|
}
|
|
else if (pid==0) {
|
|
close(tubes[i][1]);
|
|
codeDuFils(N, tubes, i);
|
|
exit(0);
|
|
/* le ieme fils ne doit pas retourner dans la boucle */
|
|
}
|
|
}
|
|
|
|
fermeTubesPere(N, tubes);
|
|
codeDuPere();
|
|
|
|
|
|
// switch(fork()) {
|
|
// case -1 :
|
|
// perror(" fork ");
|
|
// exit(errno);
|
|
// case 0 : // le fils
|
|
// close(tube[1]);
|
|
// codeDuFils(tube);
|
|
// exit(0);
|
|
// default : // le pere
|
|
// close(tube[0]);
|
|
// codeDuPere(tube);
|
|
// }
|
|
return 0;
|
|
}
|
|
|
|
int main (int argc, char* argv[]){
|
|
|
|
int i, etat, N;
|
|
pid_t pid;
|
|
|
|
if (argc!=2) {
|
|
fputs("Donner un arg entier\n", stderr);
|
|
exit(1);
|
|
}
|
|
/* le atoi ne g<E8>re pas les erreurs
|
|
preferer le sscanf (un scanf dans une chaine) */
|
|
N=atoi(argv[1]);
|
|
|
|
/* le pere va creer N fils */
|
|
for (i=0 ; i<N ; i++) {
|
|
if((pid=fork())==-1) {
|
|
perror("pb fork");
|
|
exit(errno);
|
|
}
|
|
else if (pid==0) {
|
|
codeDuFils(i);
|
|
exit(0);
|
|
/* le ieme fils ne doit pas retourner dans la boucle */
|
|
}
|
|
}
|
|
|
|
/* la suite n'est faite que par le p<E8>re */
|
|
for (i=0 ; i<N ; i++) {
|
|
if ((pid=wait(&etat))==-1) {perror("pb wait"); exit(errno);}
|
|
if (WIFEXITED(etat))
|
|
printf("(pere:) fils %d a retourne le code %d\n", pid, WEXITSTATUS(etat));
|
|
else
|
|
printf("(pere:) fils %d s'est mal termine\n", pid);
|
|
}
|
|
puts("(pere:) appli terminee");
|
|
|
|
exit(0);
|
|
}
|