1

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.

alk
  • 69,737
  • 10
  • 105
  • 255
noname
  • 261
  • 1
  • 4
  • 11
  • How do you create server and client processes? – user2155932 Apr 19 '13 at 08:25
  • on same pc and 2 terminals gcc -c server.c gcc -o serv server.o ./serv same for client – noname Apr 19 '13 at 08:29
  • 3
    You are passing pointer to string between process. The string data is allocated in client's address space and this pointer isn't valid in server's address space (it may be, if one process is spawned by the other using `fork`, however, I see that it is not the case). You should use other IPC, see [this](http://stackoverflow.com/questions/404604/comparing-unix-linux-ipc) question. – user2155932 Apr 19 '13 at 08:34
  • Hmmmm i see. Thank you :). fifo was my second option . I thought this one was easier . – noname Apr 19 '13 at 08:41

1 Answers1

2

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.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82