0

I have some other trouble using the "signal" function. I have 5 children processes and its father.

Only one process should be running at a time, and that process should change to another aleatory process with some delay (from 1 to 10 seconds). For those changes its mandatory to use SIGUSER1 signal. This is my code and the error I'm having:

struct compartido{
   int pid1, pid2, pid3, pid4, pid5;
   int propietario;     
   int contador;            
   int pidpadre;            
};

   struct compartido* var;  

void manejador(int prop, int actual){
   if(signal(SIGUSR1,SIG_IGN)==SIG_ERR){
      perror("Error en el Signal.\n");
      exit(2);
   }
   printf("Proceso %d al mando en el manejador.\n",prop);

   kill(prop,SIGCONT);
   if(actual==getpid()) kill(actual,SIGSTOP);

   sleep(rand() % 11 + 1);
}

int main(int argc, char *argv[]) {

    key_t llave1,llavesem;  
    int idmem,idsem;    


    idmem=shmget(llave1,sizeof(struct compartido),IPC_CREAT|0600);
    if (idmem==-1) {
        perror ("shmget");
        return 1;
    }
    var=(struct compartido*) shmat(idmem,0,0);
    var->pidpadre=getpid();

    var->propietario=var->pid1;
    /*Next line gives the error*/
    if(var->pid1==getpid()) signal(SIGUSR1,manejador(var->propietario, getpid()));

    /*Doing more things inside...*/

    /*Next line gives the error*/
   signal(SIGUSR1,manejador(var->propietario, getpid()));
}

As far as I know, the function called "manejador" could be used with one int value void manejador(int s){ printf("Signal received %d\n",s); }

But I need 2 variables inside, because I should stop the current process and relaunch the new one.

The exact error I'm having says:

error: invalid use of void expression

Can you please, help me?

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
user1660559
  • 33
  • 1
  • 2
  • 6

1 Answers1

0
signal(SIGUSR1,manejador(var->propietario, getpid()));
  • You need to pass a function identifier instead of calling a function, i.e. just leave in manejdor

  • Signal handlers have very specific layouts that you must respect: a signal handler must take one int parameter and return void

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • If I just put "manejador" I get this alert: warning: passing argument 2 of signal from incompatible pointer type [enabled by default]. /usr/include/signal.h:101:23: note: expected '__sighandler_t' but argument is of type 'void (*)(int, int)' – user1660559 Sep 10 '12 at 16:33