haikei00X
Messages postés67Date d'inscriptiondimanche 20 juillet 2014StatutMembreDernière intervention11 janvier 2016
-
11 janv. 2016 à 22:03
Bonjour tout le monde.
J'ai un petit problème dans la réception de mon signal dans le processus fils. En effet, je ne reçois pas le signal envoyé d'un autre processus lorsque je mets la fonction exit(0) à la fin du processus fils. Si je la mets en commentaire, je reçois le signal, mais la création des processus fils ne se déroule pas comme prévu.
Je vous mets ci-dessous mon code:
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include <signal.h>
//Declarations
typedef struct{
int type;
pid_t pid;
}camp;
void fct(int nsig) ;
int somme(int n) ;
int chooseType(int somme) ;
void jeterSort( camp Tab[] ) ;
/*************************************/
int main()
{
pid_t child_pid;
int status;
int i ;
int n = 0 ;
int camG=30;
int campB=30;
camp *Tab = mmap(NULL,10* sizeof(camp), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Tab[0].pid = getppid();
Tab[0].type=chooseType( somme(getppid() ) );
int score=10;
for(i=1;i<=6;i++)
{
child_pid = fork();
if(child_pid==0)
{
printf("child=%d\n",getpid());
printf("father=%d\n",getppid());
printf("\n\n") ;
//le traitement de classement des processus
int score=10;
Tab[i].pid = getpid();
Tab[i].type=chooseType( somme(getpid() ) );
n++;
signal(SIGUSR2,fct);
//pause();
//exit(0);
}
waitpid(-1,&status,0);
if(n==6)
{
jeterSort(Tab);
}
}
//printf("***************End of the main process\n***************") ;
return 0;
}
//Implementing funcions
void fct(int nsig) {
printf("I %d process received the signal ^_^\n\n",getpid());
//The attack code here after getting the pid_recep from the pipe
}
/*************************************************************************************/
int somme(int n)
{
int somme=0;
while(n!=0){
somme +=n% 10;
n /= 10 ;
}
return somme;
}
/*************************************************************************************/
int chooseType(int somme)
{
if(somme%2==0)
return 0;
else
return 1;
}
/*************************************************************************************/
void jeterSort( camp Tab[] )
{
srand(time(0));
int AleaAttaq=(rand()%5)+1;
int pid_attack=Tab[AleaAttaq].pid;
int AleaRece=(rand()%5)+1;
int pid_recep=Tab[AleaRece].pid;
//sleep(1);
printf("I %d process sent the signal to %d process\n",getpid(),pid_attack);
kill(pid_attack,SIGUSR2);
/*P.S:Sent signal to id_attack to tell him to start the attack
We need to use a pipe to send the pid_recep to the the process that is going to attack
(the one we sent the signal to pid_attack).
In fct() we implement our attack.
*/
}
Aidez moi s'il vous plait, je ne sais pas qu'est ce que j'ai raté.
Merci d'avance.