csignalssigqueue

Can I send char* with sigqueue to another process


Here is a part of my code in client program

      union sigval toServer;   

      char *test = "dummy";
      serverPID = atol(buf2);
      toServer.sival_ptr = (void *)test;


      // Register to server
      if (sigqueue(serverPID, SIGUSR1, toServer) == -1) {  // register
         fprintf(stderr," Server isn't ready!\n");
         return 1;
      }

Here is my handler in server program

      static void register_handler(int signo, siginfo_t* info, void *context) {   

      registeredProgramID = info->si_pid;

      if(info->si_value.sival_ptr != NULL)
       fprintf(stderr," sent value is  = %s \n" ,(char *)info->si_value.sival_ptr);
      }

There is no error but i can't get what i sent. It prints something weird.


Solution

  • No you cannot.

    You can send char*, but the receiving process normally doesn't normally have access to the memory of the sending process and the memory may be mapped differently. When you access the memory address pointed to by the received pointer the result is undefined (i.e. platform specific and not necessarily repeatable). The most likely result is either some kind of memory protection error or that the read memory contains a random value.