Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. /**-------------------------------------------------------------------------
  2. TP2 - Squelette code exercice 1-V1
  3. Compilation : gcc tp23_exo1-v1_base.c boucler.o -o tp23_exo1-v1 -Wall
  4. --------------------------------------------------------------------------**/
  5.  
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <stdlib.h>
  11. #include "boucler.h"
  12. #include <signal.h>
  13.  
  14. struct data{
  15. int id_process;
  16. int val_immuable;
  17. int val_incr;
  18. };
  19.  
  20. /*------------------------------------------------------------------------
  21. Fonction d'affichage d'une erreur selon la cause de l'echec d'une primitive
  22. Arret du processus avec retour valant codeRetour
  23. ------------------------------------------------------------------------*/
  24. void erreur (char *msg, int codeRetour) {
  25. perror(msg);
  26. exit(codeRetour);
  27. }
  28.  
  29.  
  30.  
  31. /*------------------------------------------------------------------------
  32. Code execute par chaque processus fils
  33. ------------------------------------------------------------------------*/
  34. void fils (/* A compl�ter */) {
  35. int i=0;
  36. while(i<5){
  37. boucler();
  38. }
  39.  
  40. exit(0);
  41. }
  42.  
  43. /*------------------------------------------------------------------------*/
  44. int main(int argc, char *argv[]) {
  45. struct data info;
  46. if (argc !=2){
  47. perror("argc");
  48. exit(1);
  49. }
  50.  
  51. int nbrFils=5;
  52. int debut=0;
  53.  
  54. pid_t fils[nbrFils];
  55.  
  56. int tube[nbrFils][2];
  57. for (int i=0;i<nbrFils;i++)
  58. pipe(tube[i]);
  59.  
  60. info.val_immuable= 234;
  61. info.val_incr=1;
  62.  
  63. /* Cr�éation des fils */
  64. for(int i=0; i<nbrFils; i++ ) {
  65. fils[i]=fork();
  66. if (fils[i]==-1) {
  67. erreur("Echec fork", 1);
  68. exit(99);
  69. }
  70. else if(fils[i]==0){
  71. int in = tube[(i+1)%nbrFils][1];
  72. if(i==0){
  73. int in = tube[0][1];
  74. }
  75. int out = tube[i][0];
  76. //printf("creation tube\n");
  77. for (int j=0;j<nbrFils;++j){
  78. if (tube[j][1] != in)
  79. close(tube[j][1]);
  80. if (tube[j][0] != out)
  81. close(tube[j][0]);
  82. }
  83.  
  84. if (i==0 && debut==0){
  85. printf("Processus de pid %d : n°%d dans l'anneau : j'envoie au n°%d l’info [%d – %d – %d]\n",
  86. getpid(), i, (i+1)%nbrFils, getpid(), info.val_immuable, info.val_incr);
  87. debut++;
  88.  
  89. info.id_process=getpid();
  90. write (in, &info,sizeof info);
  91. close(in);
  92. }
  93.  
  94. if(i!=0){
  95. read(out, &info, sizeof info);
  96. info.val_incr++;
  97. printf("Processus de pid %d : n°%d dans l’anneau : j’ai reçu de %d, j'envoie au n°%d l’info [%d – %d – %d]\n",
  98. getpid(), i, info.id_process, (i+1)%nbrFils, getpid(), info.val_immuable, info.val_incr);
  99.  
  100. close(out);
  101. info.id_process=getpid();
  102. write (in, &info,sizeof info);
  103. close(in);
  104. printf("Processus de pid %d : je me termine (aussi)\n", getpid());
  105. exit(0);
  106. }
  107.  
  108. while(read(out, &info, sizeof info)){
  109.  
  110. if(info.val_incr==nbrFils){
  111. printf("Processus de pid %d : l’information m’est revenue de %d, je peux me terminer\n", getpid(), info.id_process );
  112. exit(0);
  113. }
  114. }
  115.  
  116. /* default : break; */
  117. }
  118.  
  119. }
  120.  
  121. info.id_process=getpid();
  122. write(tube[0][1], &info , sizeof info);
  123.  
  124. /* Attendre �ventuellement la fin de son fils */
  125. wait(NULL);
  126.  
  127. return 0;
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement