1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include stdio.h
#include signal.h
#include stdlib.h
#include unistd.h
// Numero de processus actuel, et pid correspondants :
int procactuel,pid1,pid2;
// Donne la main aux processsus l'un apres l'autre :
void swap(int s)
{
if(procactuel==0)
{
printf("Main au processus 2\n");
kill(pid2,SIGUSR1);
procactuel=1;
}
else
{
printf("Main au processus 1\n");
kill(pid1,SIGUSR1);
procactuel=0;
}
// Reprise :
alarm(1);
signal(s,swap);
}
void detruit(int s)
{
// L'ordonnanceur entraine dans sa chute les deux
// processus fils :
kill(pid1,SIGINT);
kill(pid2,SIGINT);
int mypid=getpid();
alarm(0);
kill(mypid,SIGKILL);
}
void usage(char *nomcommande)
{
printf("Usage : %s pid1 pid2\n",nomcommande);
printf("Execute de maniere alternative pid1 et pid2.\n");
exit(EXIT_FAILURE);
}
int main(int argc, char * argv[])
{
if (argc!=3)
usage(argv[0]);
// Recuperation des parametres :
pid1=atoi(argv[1]);
pid2=atoi(argv[2]);
// Verification des deux pid :
if(pid1#139;=0 || pid2#139;=0)
usage(argv[0]);
// Declaration des fonctions de traitement d'interruption :
signal(SIGALRM,swap);
signal(SIGINT,detruit);
// Initialisation :
printf("Pressez CTRL-C pour quitter !\n");
printf("Mini-ordonnanceur pret.\n");
procactuel=0;
kill(pid1,SIGUSR1);
alarm(1);
// Boucle infinie :
while(1) {pause();}
return 0;
} |