Skip navigation.
Home
The QNX Community Portal

View topic - seperate channel after fork

seperate channel after fork

anything that doesn't fit to other groups.

seperate channel after fork

Postby wiescho » Thu Feb 26, 2015 2:28 pm

Hello,

I wrote a programm which forks two or more processes. each process creates a channel and a timer.Each process should now wait till its coresponding timer elapsed and a pulse is received. the problem is that the call to ChannelCreate() returns the same channelID to all processes. Is this a typical behavior? I was thinking that each process would crate its own ChannelID. how do i get two seperate channel IDs in to seperate Processes?


Code: Select all
//function will be called in a while(1) after fork()
inline int set_AfterActivate(double* after, double all, double until, double during, long* exec_counter)
{
   #ifdef __QNX_
   static struct sigevent  event;      // event to deliver
   static int    coid;                // connection back to ourselves
   static struct itimerspec   timer;      // the timer data structure
   static MessageT msg;           // the message itself
   static timer_t             timerid;    // timer ID for timer
   static int rcvid;              // process ID of the sender
   static int     chid;               // channel ID (global)
    // Task wurde noch nicht ausgeführt Setze timmervariablen und befülle sie mit after und all
    if((*exec_counter) <= 0){
       //set Timer
      if ((chid = ChannelCreate (0)) == -1) { //return the same ID in two seperate processes
         perror (NULL);
         exit (EXIT_FAILURE);
      }
      // create a connection back to ourselves
      coid = ConnectAttach (0, 0, chid, 0, 0);
      if (coid == -1) {
         perror (NULL);
         exit (EXIT_FAILURE);
      }
      // set up the kind of event that we want to deliver -- a pulse
      SIGEV_PULSE_INIT (&event, coid, SIGEV_PULSE_PRIO_INHERIT, CODE_TIMER, 0);
                
      // First time execution create timer
       if (timer_create (CLOCK_REALTIME, &event, &timerid) == -1) {
         perror (NULL);
         exit (EXIT_FAILURE);
      }   
      timer.it_value.tv_sec = (long)((*after)/(double)MILLION);
      timer.it_value.tv_nsec = (long)((*after) * (double)TOUSEND);
      timer.it_interval.tv_sec = (((int)all)/MILLION);
      timer.it_interval.tv_nsec = ((int)all) * TOUSEND;
      //start timer
      timer_settime (timerid, 0, &timer, NULL);
      
      //if after not used exit function
      if( ((int)(*after)) == UNUSED || ((int)(*after)) == 0 ) {
         (*exec_counter)++;
         return 0;
      }
      else{ //wait for pulse
         rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);
         (*exec_counter)++;
         return 0;
      }
    }
wiescho
New Member
 
Posts: 1
Joined: Wed Feb 25, 2015 3:35 pm

Re: seperate channel after fork

Postby Tim » Thu Feb 26, 2015 3:12 pm

This is normal.

Each process does have its own set of channelId's. They do however all start at the same number. This is similar to how each process has it's own set of open file id's but they all start at the same number (stdin, stdout, stderr use 0, 1, 2 and if you do an fopen() call the returned fd will be identical across multiple processes).

If you want to send a message/pulse to a specific process you do that in the ConnectAttach() call by specifying a non-zero pid value in argument 2 (a 0 says you are connecting to your own channel).

// Connecting to yourself
coid = ConnectAttach (0, 0, chid, 0, 0);

// Connecting to someone else (could still be yourself if you specify your own pid)
coid = ConnectAttach (0, pid, chid, 0, 0);

Tim
Tim
Senior Member
 
Posts: 1390
Joined: Wed Mar 10, 2004 12:28 am


Return to General Programming

Who is online

Users browsing this forum: No registered users and 3 guests

cron