Correct format for PtGetRcvidPid() to get the PID of the process ID

Description: The start_child_process1() spawns the child process ‘process1’ and also adds one Process Input Handler which accepts every non-photon message as per “https://www.qnx.com/developers/docs/6.3.2/photon/lib_ref/pt/ptappaddinput.html”. The processInputHandler() recieves the pulses from the process1 and process2, but the pid returned from the

PtGetRcvidPid(rcvd) is always -1.

As per “https://www.qnx.com/developers/docs/6.3.2/photon/lib_ref/pt/ptgetrcvidpid.html”, the PtGetRcvidPid() should return the PID of the process ID.

Please help.

Code Snippet Start

pid_t process1_pid;

extern pid_t process2_pid;

int start_child_process1() {

  int found = 0, attempts = 5;

  char *tmp[] = {NULL, NULL, NULL, NULL, NULL};

  tmp[0] = "../../_process/process1";

  process1_pid = spawnve(P_NOWAITO, "../../_process/process1", tmp, environ);

  while (!found && attempts) {
        if ((process1Coid = name_open("PROCESS_1", 0)) != -1) {
              found = 1;
        } else {
              attempts--;
              delay(SEC);
        }
  }

  if (!found) {
        perror("[ MainApp ] - Unable to create child process: process1");
        return NOT_WORKING;
  }

if (PtAppAddInput(NULL, 0, processInputHandler, NULL) == NULL) {  // all pulses are captured
    perror("[ MainApp ] - Unable to connect to process Input Handler");
    return NOT_WORKING;
}

  return WORKING_OK;

}
static int processInputHandler(void* data, int rcvd, void* message, size_t size) {

struct _pulse* rcvdPulse = (struct _pulse*)message;

pid_t recvPid;

recvPid = PtGetRcvidPid(rcvd);

  if ((recvPid == process1_pid) && (rcvdPulse->code == PROCESS1_MESSAGE_PULSE1)) {
        function1();
  }
  else if ((recvPid == process2_pid) && (rcvdPulse->code == PROCESS2_MESSAGE_PULSE2)) {
        function2();
  }

return (Pt_CONTINUE);

}
Code Snippet End

Thank you.

When MsgReceive() gets a pulse the return is not a rcvid. From QNX docs:

“On success, both functions return a positive rcvid if they received a message, or EOK if they received a pulse. The only difference between MsgReceive() and MsgReceive_r() is the way they indicate errors:”

That’s why PtGetRcvidPid(rcvid) is always -1.

I can suggest two work arounds.

  1. Stuff the PID in the pulse. There’s just enough room.

  2. Why use a pulse at all? Have the child send a message and then reply immediately. There are legitimate uses for a pulse, but I don’t see one here.

1 Like

Hi Maschoen,

Greetings of the day and thank you for the response.

The system sends pulses from many processes using MsgSendPulse(), where the processInputHandler() need to handle the pulse messages from two different processes.

Here I am trying to filter the pulse messages based on the process ID, so that processInputHandler() can only handles the pulses from the required processes. In many cases, the MsgSendPulse(coid, priority, code, value) inserted some data as “value” argument to pass, that is why I am not able to stuff the PID in the pulse message.

The actual code is below and I am trying to modify as above.
Code Snippet Start
static int processInputHandler(void* data, int rcvd, void* message, size_t size) {
struct _pulse* rcvdPulse = (struct _pulse*)message;

if (rcvdPulse->code == PROCESS1_MESSAGE_PULSE1) {
	function1();
}
else if (rcvdPulse->code == PROCESS2_MESSAGE_PULSE2) {
	function2();
}

return (Pt_CONTINUE);

}
Code Snippet End

Note: QNX OS version 6.3.2

Thank you.

So why don’t you just send a message instead of a pulse and reply to it immediately?

An alternative would be to have a separate receive thread for each process that will send a pulse.

Currently I am stuffing the PID in the MsgSendPulse(), which are required to handle the processInputHandler(). This way processInputHandler() filter the processes which have valid process ID and handles pulses.

Is there anyway to find out the source of the pulse/message sent which triggers the below input handler?
Currently some unknown source (garbage) is triggering the below input handler and trigger the abnormal behavior on power-up.

There’s a somewhat complicated way. The intrumented kernel can be set up to record kernel events. I believe the package is recorded in the recording.

The issue is resolved. Thank you for your support.

In the system, Process-3 sends one message with some data to the Process-4 where process 4 has one ‘process input handler’ tied to process4. As the process 4 input handler uses the “Pt_CONTINUE” at the end of the function, OS searches for any other process input handlers tied to process4 or global process input handlers. Then the above mentioned global process input handler receives the message sent by process-3. Coincidentally the data received from the process-3 matches with the “PROCESS1_MESSAGE_PULSE1” value and the “function1();” called.

Solution:

  1. Use Pt_HALT in the process 4 input handler
  2. As you mention filter out messages based on process-ID
  3. Also filter out the PULSEs and Messages.

Code Snippet Start

static int processInputHandler(void* data, int rcvd, void* message, size_t size) {
  struct _pulse* rcvdPulse = (struct _pulse*)message;
  recvPid = (pid_t)rcvdPulse->value.sival_ptr;

  if ((_PULSE_TYPE == rcvdPulse->type) && (_PULSE_SUBTYPE == rcvdPulse->subtype)){
    if ((recvPid == process1_pid) && ( PROCESS1_MESSAGE_PULSE1 == rcvdPulse->code)) {
          function1();
      }
      else if ((recvPid == process2_pid) && (PROCESS2_MESSAGE_PULSE2 == rcvdPulse->code)) {
          function2();
      }
  }
  return (Pt_CONTINUE);
}

Code Snippet End