add PP, droit, english an system

master
Antoine PEREDERII 2 years ago
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.

@ -1,172 +1,336 @@
-- ? 1 - Écrire une fonction qui pour un match donné calcule le nombre de rebonds
-- ? pris par les joueurs qui nont pas débuté la rencontre. Proposez deux versions,
-- ? une utilisant les fonctions dagrégation et une autre utilisant un curseur mais aucune fonction dagrégation.
\! clear
-- -- ? 1 - Écrire une fonction qui pour un match donné calcule le nombre de rebonds
-- -- ? pris par les joueurs qui nont pas débuté la rencontre. Proposez deux versions,
-- -- ? une utilisant les fonctions dagrégation et une autre utilisant un curseur mais aucune fonction dagrégation.
-- CREATE OR REPLACE FUNCTION calcul_rebonds(match Game.id%TYPE) RETURNS integer AS $$
-- DECLARE
-- sumRebounds integer;
-- BEGIN
-- SELECT sum(gd.rebounds) INTO sumRebounds
-- FROM GameDetail gd, Game g
-- WHERE g.id = match AND gd.idGame = g.id AND gd.startPosition is NULL;
-- RETURN sumRebounds;
-- END;
-- $$ LANGUAGE plpgsql;
-- SELECT calcul_rebonds('22100979');
-- -- CREATE FUNCTION
-- -- calcul_rebonds
-- -- ----------------
-- -- 32
-- -- (1 row)
-- CREATE OR REPLACE FUNCTION calcul_rebonds_curs(match Game.id%TYPE) RETURNS GameDetail.rebounds%TYPE AS $$
-- DECLARE
-- sumRebounds GameDetail.rebounds%TYPE := 0;
-- rbs GameDetail.rebounds%TYPE := 0;
-- curs cursor FOR SELECT rebounds
-- FROM GameDetail
-- WHERE idGame = match AND startPosition is NULL AND rebounds is NOT NULL;
-- BEGIN
-- OPEN curs;
-- FETCH curs INTO rbs;
-- WHILE FOUND LOOP
-- sumRebounds = sumRebounds + rbs;
-- FETCH curs INTO rbs;
-- END LOOP;
-- CLOSE curs;
-- RETURN sumRebounds;
-- END;
-- $$ LANGUAGE plpgsql;
-- SELECT calcul_rebonds_curs('22100979');
-- -- CREATE FUNCTION
-- -- calcul_rebonds_curs
-- -- ---------------------
-- -- 32
-- -- (1 row)
-- -- ? 2 - Vérifier que vos deux versions retournent les mêmes résultats en affichant le nombre total de rebonds
-- -- ? pris par les joueurs nayant pas débuté la rencontre pour tous les matchs ayant eu lieu le 12 mars 2022.
-- SELECT calcul_rebonds(id)
-- FROM Game
-- WHERE dateGame = '2022-03-12';
-- -- calcul_rebonds
-- -- ----------------
-- -- 39
-- -- 20
-- -- 43
-- -- 40
-- -- 41
-- -- 25
-- -- 24
-- -- (7 rows)
-- SELECT calcul_rebonds_curs(id)
-- FROM Game
-- WHERE dateGame = '2022-03-12';
-- -- calcul_rebonds_curs
-- -- ---------------------
-- -- 39
-- -- 20
-- -- 43
-- -- 40
-- -- 41
-- -- 25
-- -- 24
-- -- (7 rows)
-- -- ? 3 - Écrire une fonction qui calcule la même chose mais pour les joueurs ayant débuté la rencontre,
-- -- ? autrement dit qui calcule, pour un match donné, le nombre de rebonds pris par les joueurs ayant débuté la rencontre.
-- CREATE OR REPLACE FUNCTION calcul_rebonds_t(match Game.id%TYPE) RETURNS integer AS $$
-- DECLARE
-- sumRebounds integer;
-- BEGIN
-- SELECT sum(gd.rebounds) INTO sumRebounds
-- FROM GameDetail gd, Game g
-- WHERE g.id = match AND gd.idGame = g.id AND gd.startPosition is NOT NULL;
-- IF sumRebounds is NULL THEN
-- RETURN 0;
-- END IF;
-- RETURN sumRebounds;
-- END;
-- $$ LANGUAGE plpgsql;
-- SELECT calcul_rebonds_t('22100979');
-- -- CREATE FUNCTION
-- -- calcul_rebonds_t
-- -- ------------------
-- -- 57
-- -- (1 row)
-- CREATE OR REPLACE FUNCTION calcul_rebonds_curs_t(match Game.id%TYPE) RETURNS GameDetail.rebounds%TYPE AS $$
-- DECLARE
-- sumRebounds GameDetail.rebounds%TYPE := 0;
-- rbs GameDetail.rebounds%TYPE := 0;
-- curs cursor FOR SELECT rebounds
-- FROM GameDetail
-- WHERE idGame = match AND startPosition is NOT NULL AND rebounds is NOT NULL;
-- BEGIN
-- OPEN curs;
-- FETCH curs INTO rbs;
-- WHILE FOUND LOOP
-- sumRebounds = sumRebounds + rbs;
-- FETCH curs INTO rbs;
-- END LOOP;
-- CLOSE curs;
CREATE OR REPLACE FUNCTION calcul_rebonds(match Game.id%TYPE) RETURNS integer AS $$
DECLARE
sumRebounds integer;
BEGIN
SELECT sum(gd.rebounds) INTO sumRebounds
FROM GameDetail gd, Game g
WHERE g.id = match AND gd.idGame = g.id AND gd.startPosition is NULL;
-- RETURN sumRebounds;
-- END;
-- $$ LANGUAGE plpgsql;
RETURN sumRebounds;
END;
$$ LANGUAGE plpgsql;
-- SELECT calcul_rebonds_curs_t('22100979');
SELECT calcul_rebonds('22100979');
-- -- CREATE FUNCTION
-- -- calcul_rebonds_curs_t
-- -- -----------------------
-- -- 57
-- -- (1 row)
-- CREATE FUNCTION
-- calcul_rebonds
-- ----------------
-- 32
-- (1 row)
-- SELECT calcul_rebonds_t('10300004');
-- SELECT calcul_rebonds_curs_t('10300004');
CREATE OR REPLACE FUNCTION calcul_rebonds_curs(match Game.id%TYPE) RETURNS GameDetail.rebounds%TYPE AS $$
DECLARE
sumRebounds GameDetail.rebounds%TYPE := 0;
rbs GameDetail.rebounds%TYPE := 0;
curs cursor FOR SELECT rebounds
FROM GameDetail
WHERE idGame = match AND startPosition is NULL AND rebounds is NOT NULL;
BEGIN
OPEN curs;
FETCH curs INTO rbs;
WHILE FOUND LOOP
sumRebounds = sumRebounds + rbs;
FETCH curs INTO rbs;
END LOOP;
CLOSE curs;
-- -- ? 4 - Trouver le match (abréviation des équipes et date) pendant lequel les joueurs
-- -- ? ayant débuté la rencontre ont réalisé le plus de rebonds.
RETURN sumRebounds;
END;
$$ LANGUAGE plpgsql;
-- SELECT t1.abbreviation, t2.abbreviation, g.dateGame, calcul_rebonds_t(g.id)
-- FROM Game g, Team t1, Team t2
-- WHERE g.idHomeTeam = t1.id AND g.idVisitorTeam = t2.id AND calcul_rebonds_t(g.id) = (SELECT max(calcul_rebonds_t(id))
-- FROM Game);
SELECT calcul_rebonds_curs('22100979');
-- -- abbreviation | abbreviation | dategame | calcul_rebonds_t
-- -- --------------+--------------+------------+------------------
-- -- POR | DEN | 2019-05-03 | 101
-- -- (1 row)
-- CREATE FUNCTION
-- calcul_rebonds_curs
-- ---------------------
-- 32
-- (1 row)
-- -- ? 5 - Y a til des matchs pour lesquels les données sur les rebonds sont incohérentes?
-- -- ? Vérifier en comparant les valeurs obtenus grâce aux fonctions précédentes avec les valeurs contenues dans la table Game.
-- -- ! SELECT t1.abbreviation, t2.abbreviation, g.dateGame, (calcul_rebonds_t(g.id) + calcul_rebonds(g.id)) as fct, g.reboundsHome, g.reboundsAway
-- -- ! FROM Game g, Team t1, Team t2
-- -- ! WHERE g.idHomeTeam = t1.id AND g.idVisitorTeam = t2.id AND calcul_rebonds_t(g.id) = (SELECT max(calcul_rebonds_t(id))
-- -- ! FROM Game);
-- ? 2 - Vérifier que vos deux versions retournent les mêmes résultats en affichant le nombre total de rebonds
-- ? pris par les joueurs nayant pas débuté la rencontre pour tous les matchs ayant eu lieu le 12 mars 2022.
SELECT calcul_rebonds(id)
FROM Game
WHERE dateGame = '2022-03-12';
-- -- ? 6 - Écrire une fonction qui pour un match et une équipe donnée calcule le total des points des joueurs de cet équipe pendant le match,
-- -- ? à partir des données contenues dans la table GameDetail.
-- calcul_rebonds
-- ----------------
-- 39
-- 20
-- 43
-- 40
-- 41
-- 25
-- 24
-- (7 rows)
-- CREATE OR REPLACE FUNCTION PtsTotJoueur(match Game.id%TYPE, team Team.id%TYPE)
-- RETURNS integer as $$
-- DECLARE total integer;
-- BEGIN
-- SELECT sum(points) INTO total
-- FROM GameDetail
-- WHERE idTeam = team AND idGame = match AND points IS NOT NULL;
-- IF total is NULL THEN
-- RETURN 0;
-- END IF;
SELECT calcul_rebonds_curs(id)
FROM Game
WHERE dateGame = '2022-03-12';
-- RETURN total;
-- END;
-- $$ LANGUAGE plpgsql;
-- calcul_rebonds_curs
-- ---------------------
-- 39
-- 20
-- 43
-- 40
-- 41
-- 25
-- 24
-- (7 rows)
-- SELECT PtsTotJoueur('22101006','1610612741');
-- ? 3 - Écrire une fonction qui calcule la même chose mais pour les joueurs ayant débuté la rencontre,
-- ? autrement dit qui calcule, pour un match donné, le nombre de rebonds pris par les joueurs ayant débuté la rencontre.
-- -- ? 7 - Utiliser cette fonction pour vérifier si les valeurs ptsHome et ptsAway de la table Game sont correctes.
CREATE OR REPLACE FUNCTION calcul_rebonds_t(match Game.id%TYPE) RETURNS integer AS $$
DECLARE
sumRebounds integer;
-- SELECT g.id AS idGame, t.id AS idTeam, g.ptsHome, g.ptsAway, PtsTotJoueur(g.id, g.idHomeTeam) AS ptsHome1, PtsTotJoueur(g.id, g.idVisitorTeam) AS ptsAway2
-- FROM GAME g, Team t
-- WHERE g.idVisitorTeam = t.id;
-- -- ? 8 - Quelle table contient des données incorrectes/incomplètes ? Game ou GameDetail ? Vérifier grâce aux résultats officiels des matchs.
-- ? 9 - Ecrire une fonction isBestAtHome qui retourne un booléen indiquant si une équipe donnée a
-- ? gagné au moins autant de matchs à domicile quà lextérieur lors de la saison passée en paramètre.
CREATE OR REPLACE FUNCTION isBestAtHome(IdTeam Team.id%TYPE, GameSeason Game.season%TYPE) RETURNS numeric AS $$
DECLARE
home_wins integer;
away_wins integer;
BEGIN
SELECT sum(gd.rebounds) INTO sumRebounds
FROM GameDetail gd, Game g
WHERE g.id = match AND gd.idGame = g.id AND gd.startPosition is NOT NULL;
SELECT COUNT(*) INTO home_wins
FROM GAME g
WHERE g.season = GameSeason AND g.idHomeTeam = IdTeam AND g.ptsHome > g.ptsAway;
SELECT COUNT(*) INTO away_wins
FROM GAME g
WHERE g.season = GameSeason AND g.idVisitorTeam = IdTeam AND g.ptsAway > g.ptsHome;
IF sumRebounds is NULL THEN
IF (home_wins >= away_wins) THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
$$ LANGUAGE plpgsql;
-- ? 10 - Vérifier que léquipe des Spurs a été meilleure à domicile quà lextérieur pendant la saison 2021.
RETURN sumRebounds;
SELECT isBestAtHome(id, '2021') AS best_at_home
FROM Team
WHERE abbreviation = 'SAS';
-- ? 11 - Ecrire une fonction bestAtHome qui retourne une table avec les équipes (ids, abbréviations, noms et villes)
-- ? qui ont gagné au moins autant de matchs à domicile quà lextérieur lors de la saison passée en paramètre.
CREATE OR REPLACE FUNCTION BestAtHome(GameSeason Game.season%TYPE)
RETURNS TABLE(id Team.id%TYPE, abbreviation Team.abbreviation%TYPE, nom Team.nickname%TYPE, ville Team.city%TYPE) AS $$
BEGIN
RETURN QUERY SELECT DISTINCT t.id, t.abbreviation, t.nickname, t.city
FROM Team t
WHERE isBestAtHome(t.id, GameSeason) = 1;
END;
$$ LANGUAGE plpgsql;
SELECT calcul_rebonds_t('22100979');
-- ? 12 - Quelles équipes ont gagné au moins autant de matchs à domicile quà lextérieur en 2021?
-- CREATE FUNCTION
-- calcul_rebonds_t
-- ------------------
-- 57
-- (1 row)
SELECT BestAtHome(2021);
CREATE OR REPLACE FUNCTION calcul_rebonds_curs_t(match Game.id%TYPE) RETURNS GameDetail.rebounds%TYPE AS $$
DECLARE
sumRebounds GameDetail.rebounds%TYPE := 0;
rbs GameDetail.rebounds%TYPE := 0;
curs cursor FOR SELECT rebounds
FROM GameDetail
WHERE idGame = match AND startPosition is NOT NULL AND rebounds is NOT NULL;
-- ? 13 - Ecrire une fonction qui retourne un booléen indiquant si une équipe donnée à gagner au moins autant de matchs à
-- ? domiciles quà lextérieur pendant au moins n saisons consécutives, où n est un paramètre de la fonction.
-- ? Cette fonction devra lever une exception personnalisée si n nest pas une valeur possible.
CREATE OR REPLACE FUNCTION isBestAtHomeDuring(nbSeason numeric) RETURNS numeric AS $$
DECLARE
home_wins integer;
away_wins integer;
BEGIN
OPEN curs;
FETCH curs INTO rbs;
SELECT COUNT(*) INTO home_wins
FROM GAME g, Team t
WHERE g.idHomeTeam = t.id AND isBestAtHome(t.id, g.season) = 1;
WHILE FOUND LOOP
sumRebounds = sumRebounds + rbs;
FETCH curs INTO rbs;
END LOOP;
CLOSE curs;
SELECT COUNT(*) INTO away_wins
FROM GAME g, Team t
WHERE g.idVisitorTeam = t.id AND isBestAtHome(t.id, g.season) = 0;
RETURN sumRebounds;
IF (home_wins >= away_wins) THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
$$ LANGUAGE plpgsql;
SELECT calcul_rebonds_curs_t('22100979');
-- SELECT isBestAtHomeDuring(5);
-- ? 14 - Y a til des équipes qui ont gagné au moins autant de matchs à domicile quà lextérieur
-- ? pendant 2 saisons consécutives ? Pendant 3 saisons consécutives ?
-- CREATE FUNCTION
-- calcul_rebonds_curs_t
-- -----------------------
-- 57
-- (1 row)
SELECT calcul_rebonds_t('10300004');
SELECT calcul_rebonds_curs_t('10300004');
-- ? 15 - Écrire une fonction qui calcule lid de léquipe ayant le meilleur pourcentage moyen de paniers
-- ? à 3 points dune saison donnée.
CREATE OR REPLACE FUNCTION idTeam3Points(GameSeason Game.season%TYPE) RETURNS numeric AS $$
DECLARE
idTeam Team.id%TYPE;
BEGIN
SELECT t.id INTO idTeam
FROM Team t, GameDetail gd, Game g
WHERE gd.idTeam = t.id AND gd.idGame = g.id AND g.season = GameSeason AND gd.threePointsPrctage >= ALL(SELECT threePointsPrctage
FROM GameDetail gd1
WHERE gd1.idTeam = t.id);
RETURN idTeam;
END;
$$ LANGUAGE plpgsql;
-- ? 16 - Utiliser cette fonction pour afficher la meilleure équipe (abbréviation, nom et ville) de la saison 2021 en pourcentage moyen de paniers à 3 points
SELECT t.id, t.abbreviation, t.nickname, t.city
FROM Team t
WHERE idTeam3Points('2021') = t.id;
-- ? 17 - Écrire une fonction qui calcule combien de paniers à trois points ont été marqué par un joueur donné, pendant une saison donnée.
CREATE OR REPLACE FUNCTION idTeam3Points(idPlayer Player.id%TYPE , GameSeason Game.season%TYPE) RETURNS numeric AS $$
DECLARE
idTeam Team.id%TYPE;
BEGIN
SELECT t.id INTO idTeam
FROM Team t, GameDetail gd, Game g, Player p
WHERE gd.idTeam = t.id AND gd.idGame = g.id AND p.id = idPlayer AND g.season = GameSeason AND gd.threePointsPrctage >= ALL(SELECT threePointsPrctage
FROM GameDetail gd1
WHERE gd1.idTeam = t.id);
RETURN idTeam;
END;
$$ LANGUAGE plpgsql;
-- ? 18 - Écrire une fonction qui calcule lid du joueur ayant marqué le plus de paniers à trois points pendant une saison donnée.
-- ? 4 - Trouver le match (abréviation des équipes et date) pendant lequel les joueurs
-- ? ayant débuté la rencontre ont réalisé le plus de rebonds.
SELECT t1.abbreviation, t2.abbreviation, g.dateGame, calcul_rebonds_t(g.id)
FROM Game g, Team t1, Team t2
WHERE g.idHomeTeam = t1.id AND g.idVisitorTeam = t2.id AND calcul_rebonds_t(g.id) = (SELECT max(calcul_rebonds_t(id))
FROM Game);
-- abbreviation | abbreviation | dategame | calcul_rebonds_t
-- --------------+--------------+------------+------------------
-- POR | DEN | 2019-05-03 | 101
-- (1 row)
-- ? 19 - En utilisant les fonctions précédement créées, écrire un bloc anonyme qui affiche pour chaque saison, par ordre chronologique,
-- ? le nom du joueur ayant marqué le plus de paniers à trois points ainsi que le nombres de paniers à trois points marqués.
-- ? 5 - Y a til des matchs pour lesquels les données sur les rebonds sont incohérentes?
-- ? Vérifier en comparant les valeurs obtenus grâce aux fonctions précédentes avec les valeurs contenues dans la table Game.
-- ! SELECT t1.abbreviation, t2.abbreviation, g.dateGame, (calcul_rebonds_t(g.id) + calcul_rebonds(g.id)) as fct, g.reboundsHome, g.reboundsAway
-- ! FROM Game g, Team t1, Team t2
-- ! WHERE g.idHomeTeam = t1.id AND g.idVisitorTeam = t2.id AND calcul_rebonds_t(g.id) = (SELECT max(calcul_rebonds_t(id))
-- ! FROM Game);
-- ? 20 - Ce calcul est très long. Pour effectuer un calcul plus efficace, nous allons créer une table supplémentaire permettant de stocker des statistiques.
-- ? Créer la table Stats(season, player, threePoints) contenant le nombre de paniers à trois points marqués par chaque joueur pendant une saison et
-- ? la remplir avec les données contenues dans GameDetail.
-- ? △! Penser à éliminer les valeurs NULL.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -29,7 +29,7 @@
"fini ?\n",
" - si on ne peut plus inserer en tete ou en queue les dominos de la pioche.\n",
"- Est-il éventuellement utile d'écrire certaines sous-fonctions, afin de clarifier le code ? \n",
"Ca peut être plus simple à comprendre le code, notamment pour le comptage de Y."
" - Ca peut être plus simple à comprendre le code, notamment pour le comptage de Y."
]
},
{

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…
Cancel
Save