parent
6880ae0394
commit
ea016745e8
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,62 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
/* Correction TP1 exo4 by O Guinaldo */
|
||||||
|
|
||||||
|
void codeDuFils(int n) {
|
||||||
|
int i;
|
||||||
|
struct timespec t;
|
||||||
|
|
||||||
|
for (i=0 ; i<10 ; i++) {
|
||||||
|
printf("(numero %d dit :) %d\n", n, i);
|
||||||
|
t.tv_sec=0;
|
||||||
|
t.tv_nsec=300000000+n*100000000;
|
||||||
|
/* il n'affiche pas au meme rythme */
|
||||||
|
nanosleep(&t, NULL);
|
||||||
|
}
|
||||||
|
exit(n); /*le fils retourne son numero comme code de retour */
|
||||||
|
}
|
||||||
|
|
||||||
|
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è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è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);
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,58 @@
|
|||||||
|
#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 codeDuFils(int tube[2]) {
|
||||||
|
char buffer[TAILLE_BUF];
|
||||||
|
unsigned int cpt = 0;
|
||||||
|
while(read(tube[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");
|
||||||
|
close(tube[0]);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void codeDuPere(int tube[2]) {
|
||||||
|
char buffer[TAILLE_BUF];
|
||||||
|
|
||||||
|
while(fgets(buffer, TAILLE_BUF, stdin) != NULL){
|
||||||
|
write(tube[1], buffer, TAILLE_BUF);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(tube[1]);
|
||||||
|
wait(NULL);
|
||||||
|
printf("Pere se termine apès son fils.\n");
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int tube[2];
|
||||||
|
if(pipe(tube)== -1) {
|
||||||
|
perror(" pipe ");
|
||||||
|
exit(errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
@ -0,0 +1,170 @@
|
|||||||
|
#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);
|
||||||
|
}
|
Loading…
Reference in new issue