Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <semaphore.h>
- #include <sys/time.h>
- #include <time.h>
- #include <sys/resource.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <sched.h>
- #include <setjmp.h>
- #include <mqueue.h>
- #define NB_PIECE 2
- #define CP 1
- #define C1 2
- #define C2 3
- #define PV 4
- #define PA 5
- #define AA 6
- #define GA 7
- #define TROU 8
- #define ENCOCHE 9
- #define PRISE 10
- #define STOCKTROU 11
- #define NBPIECE 12
- struct ACTUATEURS
- {
- unsigned Cp : 2;
- unsigned c1 : 1;
- unsigned c2 : 1;
- unsigned Pv : 1;
- unsigned Pa : 1;
- unsigned Aa : 1;
- unsigned Ga : 1;
- } ;
- union
- {
- struct ACTUATEURS act ;
- unsigned char byte ;
- } u_act ;
- struct CAPTEURS
- {
- unsigned L1 : 1;
- unsigned L2 : 1;
- unsigned t : 1;
- unsigned s : 1;
- unsigned Cs : 1;
- unsigned Ap : 1;
- unsigned Pp : 1;
- unsigned De : 1;
- } ;
- union
- {
- struct CAPTEURS capt ;
- unsigned char byte ;
- } u_capt ;
- int fd_petra_in, fd_petra_out ;
- pid_t idPlongeur,tapis1,tapis2;
- caddr_t shm_ptr;
- void fctPlongeur();
- void fctTapis1(int);
- void fctTapis2(int);
- void handlerSIGINT(int);
- sem_t *ptr_Fin;
- sem_t *ptr_Tapis1;
- sem_t *ptr_Tapis2;
- sem_t *ptr_PrendrePiece;
- sem_t *ptr_Mauvaise;
- int main()
- {
- struct timespec tim;
- int shm;
- int i;
- int ret_val;
- tim.tv_sec = 0;
- tim.tv_nsec = 50000000;
- ptr_Fin = sem_open("FIN", O_CREAT, 0777, 0);
- ptr_Tapis1 = sem_open("TAPIS1", O_CREAT, 0777, 0);
- ptr_Tapis2 = sem_open("TAPIS2", O_CREAT, 0777, 0);
- ptr_PrendrePiece = sem_open("PRENDREPIECE", O_CREAT, 0777, 1);
- struct sigaction act;
- act.sa_flags = 0;
- act.sa_handler = &handlerSIGINT;
- sigaction(SIGINT, &act, NULL );
- fd_petra_in = open ( "/dev/capteursPETRA", O_RDONLY );
- fd_petra_out = open ( "/dev/actuateursPETRA", O_WRONLY );
- shm = shm_open("/tmp/SHM", (O_CREAT | O_RDWR), 0);
- if(shm == -1)
- {
- perror("Creation SHM failed");
- }
- ftruncate(shm, 30);
- shm_ptr = mmap(0, 30, PROT_WRITE|PROT_READ,MAP_SHARED, shm, 0);
- if(*shm_ptr == -1)
- {
- perror("MMAP failed");
- }
- for(i=0;i<13;i++)
- {
- *(shm_ptr+i)=0;
- }
- idPlongeur=fork();
- if(!idPlongeur)
- {
- fctPlongeur(getpid());
- }
- tapis1=fork();
- if(!tapis1)
- {
- fctTapis1(getpid());
- }
- tapis2=fork();
- if(!tapis2)
- {
- fctTapis2(getpid());
- }
- do
- {
- u_act.act.Cp =*(shm_ptr + CP);
- u_act.act.c1 =*(shm_ptr + C1);
- u_act.act.c2 =*(shm_ptr + C2);
- u_act.act.Aa =*(shm_ptr + AA);
- u_act.act.Ga =*(shm_ptr + GA);
- u_act.act.Pa =*(shm_ptr + PA);
- u_act.act.Pv =*(shm_ptr + PV);
- write(fd_petra_out, &u_act.byte, 1);
- nanosleep(&tim,0);
- }while(sem_trywait(ptr_Fin)==-1);
- puts(" Fin des processus : tapis1, tapis2, plongeur");
- kill(idPlongeur,SIGKILL);
- kill(tapis1,SIGKILL);
- kill(tapis2,SIGKILL);
- u_act.act.Cp =0;
- u_act.act.c1 =0;
- u_act.act.c2 =0;
- u_act.act.Aa =0;
- u_act.act.Ga =0;
- u_act.act.Pa =0;
- u_act.act.Pv =0;
- write(fd_petra_out, &u_act.byte, 1);
- puts(" De-allocation m�moire partagee");
- ret_val=munmap(shm_ptr,30);
- if ( ret_val == -1 )
- {
- perror ( "\n\rMAIN : munmap failed !!!" );
- }
- puts(" Fermeture m�moire partagee");
- ret_val = close ( shm );
- if ( ret_val == -1 )
- {
- perror ( "\n\rMAIN : close failed !!!" );
- }
- puts(" Effacement de l'objet m�moire partagee");
- ret_val = shm_unlink ( "/tmp/SHM" );
- if ( ret_val == -1 )
- {
- perror ( "\n\rMAIN : shm_unlink failed !!!" );
- }
- puts(" Destruction des semaphores");
- /*sem_destroy(ptr_Fin);
- sem_destroy(ptr_Tapis1);
- sem_destroy(ptr_Tapis2);
- sem_destroy(ptr_PrendrePiece);*/
- ret_val = sem_unlink ( "FIN" );
- if ( ret_val == -1 )
- perror ( "\n\rMAIN : sem_unlink failed !!!" ) ;
- ret_val = sem_unlink ( "TAPIS1" );
- if ( ret_val == -1 )
- perror ( "\n\rMAIN : sem_unlink failed !!!" ) ;
- ret_val = sem_unlink ( "TAPIS2" );
- if ( ret_val == -1 )
- perror ( "\n\rMAIN : sem_unlink failed !!!" ) ;
- ret_val = sem_unlink ( "PRENDREPIECE" );
- if ( ret_val == -1 )
- perror ( "\n\rMAIN : sem_unlink failed !!!" ) ;
- puts(" Fermeture programme");
- close (fd_petra_out);
- close (fd_petra_in);
- return 0;
- }
- void fctPlongeur(pid_t pid)
- {
- struct timespec tim;
- tim.tv_sec = 0;
- tim.tv_nsec = 300000000;
- while(sem_wait(ptr_PrendrePiece)==0)
- {
- tim.tv_sec = 0;
- tim.tv_nsec = 500000000;
- read (fd_petra_in , &u_capt.byte , 1);
- while(u_capt.capt.De == 1&&*(shm_ptr + PRISE)==0)
- {
- read (fd_petra_in , &u_capt.byte , 1);
- nanosleep(&tim,0);
- }
- if(*(shm_ptr + PRISE)==1)
- {
- tim.tv_nsec = 100000000;
- tim.tv_sec = 0;
- *(shm_ptr + CP) = 3;
- nanosleep(&tim,NULL);
- //boucle tant que plongeur n'est Pas stable
- do
- {
- read (fd_petra_in , &u_capt.byte , 1);
- }while(u_capt.capt.Cs != 0);
- *(shm_ptr + PA) = 1; //descendre
- nanosleep(&tim,NULL);
- //tant que le plongeur n'est Pas arriv� en bas on boucle
- do
- {
- read (fd_petra_in , &u_capt.byte , 1);
- }while(u_capt.capt.Pp != 1);
- sleep(1);
- *(shm_ptr + PV) = 1; // asspire la pi�ce
- nanosleep(&tim,NULL);
- *(shm_ptr + PA) = 0; //remonte le plongeur
- //boucle tant que le plongeur n'est Pas en position haute
- do
- {
- read (fd_petra_in , &u_capt.byte , 1);
- }while(u_capt.capt.Pp != 0);
- *(shm_ptr + CP) = 2; //envoie le plongeur en position 2 au dessus de la poubelle
- nanosleep(&tim,NULL);
- //boucle tant que plongeur n'est Pas en position 3
- do
- {
- read (fd_petra_in , &u_capt.byte , 1);
- }while(u_capt.capt.Cs != 0);
- *(shm_ptr + PV) = 0;
- *(shm_ptr + CP) = 0;
- sleep(3);
- if(*(shm_ptr +NBPIECE)>=2)
- *(shm_ptr+TROU)=*(shm_ptr+STOCKTROU);
- puts("UNE PIECE SUPPRIME");
- *(shm_ptr+NBPIECE)=*(shm_ptr+NBPIECE)-1;
- *(shm_ptr+PRISE)=0;
- }
- else
- {
- sleep(3);
- *(shm_ptr + PA) = 1;
- sleep(2);
- *(shm_ptr + PV) = 1;
- sleep(1);
- *(shm_ptr + PA) = 0;
- sleep(2);
- *(shm_ptr + CP) = 1;
- sleep(2);
- do
- {
- read (fd_petra_in , &u_capt.byte , 1);
- nanosleep(&tim,NULL);
- }while(u_capt.capt.Cs != 0);
- nanosleep(&tim,NULL);
- *(shm_ptr + PV) = 0;
- nanosleep(&tim,NULL);
- *(shm_ptr + CP) = 0;
- sem_post(ptr_Tapis1);
- }
- }
- }
- void fctTapis1(pid_t pid) // (2)
- {
- struct timespec tim;
- tim.tv_sec = 0;
- tim.tv_nsec = 300000000;
- int compteur;
- int cmp;
- int slotprec;
- while(sem_wait(ptr_Tapis1)==0)
- {
- tim.tv_nsec = 100000000;
- tim.tv_sec = 0;
- while(*(shm_ptr+10)==1)
- {
- nanosleep(&tim,NULL);
- }
- nanosleep(&tim,NULL);
- puts("NOUVELLE PIECE");
- *(shm_ptr +NBPIECE)=*(shm_ptr+NBPIECE)+1;
- *(shm_ptr + AA) = 0;
- *(shm_ptr + C1) = 1;
- compteur=0;
- cmp=0;
- if(*(shm_ptr +NBPIECE)>=2)
- {
- *(shm_ptr+STOCKTROU)=0;
- }
- else
- {
- *(shm_ptr+TROU)=0;
- }
- sem_post(ptr_PrendrePiece);
- while(compteur != 80) //Verification du slot si 4 changements alors slot ok
- {
- slotprec = u_capt.capt.s;
- read (fd_petra_in , &u_capt.byte , 1);
- if(u_capt.capt.s != slotprec)
- {
- cmp++;
- }
- nanosleep(&tim,NULL);
- compteur++;
- }
- if(cmp == 4)
- {
- if(*(shm_ptr +NBPIECE)>=2)
- {
- *(shm_ptr+STOCKTROU)=1;
- puts("STOCKTROU");
- }
- else
- {
- *(shm_ptr+TROU) = 1;
- *(shm_ptr+STOCKTROU)=1; //trouok
- puts(" TROU OK");
- }
- }
- else
- {
- puts(" TROU NOK");
- }
- *(shm_ptr + C1) = 0;
- *(shm_ptr + GA) = 1;
- sleep(1);
- *(shm_ptr + AA) = 1;
- do
- {
- read (fd_petra_in , &u_capt.byte , 1);
- nanosleep(&tim,NULL);
- }while(u_capt.capt.Ap != 1);
- sleep(1);
- sem_post(ptr_Tapis2);
- }
- }
- void fctTapis2(pid_t pid) // (3)
- {
- struct timespec tim;
- tim.tv_nsec = 500000000;
- tim.tv_sec = 0;
- int compteur;
- while(sem_wait(ptr_Tapis2)==0)
- {
- tim.tv_nsec = 100000000;
- tim.tv_sec = 0;
- while(*(shm_ptr+10)==1)
- {
- nanosleep(&tim,NULL);
- }
- *(shm_ptr + C2) = 1;
- *(shm_ptr + GA) = 0;
- compteur=0;
- *(shm_ptr+ENCOCHE)=1;
- while(compteur != 58) //Verification de l'encoche
- {
- read (fd_petra_in , &u_capt.byte , 1);
- while(u_capt.capt.L1 == 1)
- {
- read (fd_petra_in , &u_capt.byte , 1);
- if(u_capt.capt.L2 == 1 && u_capt.capt.L1 == 1)
- {
- *(shm_ptr+ENCOCHE) = 0;
- puts(" ENCOCHE MAUVAISE ");
- fflush(stdin);
- }
- }
- nanosleep(&tim,NULL);
- compteur++;
- }
- if(*(shm_ptr + ENCOCHE)==0 || *(shm_ptr + TROU) == 0)
- {
- *(shm_ptr + PRISE)=1;
- sleep(1);
- *(shm_ptr + C2) = 0;
- sem_post(ptr_PrendrePiece); // D�marre le grappin pour la mauvaise pi�ce
- }
- else
- {
- sleep(2);
- puts("UNE PIECE SUPPRIMEE");
- if(*(shm_ptr +NBPIECE)>=2)
- *(shm_ptr+TROU)=*(shm_ptr+STOCKTROU);
- *(shm_ptr +NBPIECE)=*(shm_ptr +NBPIECE)-1;
- *(shm_ptr + C2) = 0;
- }
- }
- }
- void handlerSIGINT(int sig)
- {
- puts(" FIN PROGRAMME");
- sem_post(ptr_Fin);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement