From mboxrd@z Thu Jan 1 00:00:00 1970 References: Message-ID: <1319490255.1417.YahooMailNeo@domain.hid> Date: Mon, 24 Oct 2011 14:04:15 -0700 (PDT) From: Tom Z MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="1498488837-480329272-1319490255=:1417" Subject: Re: [Xenomai-help] address spaces of real-time task and standard linux process Reply-To: Tom Z List-Id: Help regarding installation and common use of Xenomai List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Thomas Lockhart Cc: "\"xenomai@xenomai.org\"" --1498488837-480329272-1319490255=:1417 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Thanks for Thomas' answer.=0A=0AThe previous code I showed was truncated fo= r simplicity. The actual code is a little more complicated.=A0=0A=0AI am wr= iting a C++ program on Robotic Operating System (www.ROS.org) on Ubuntu 10.= 10 patched with Xenomai 2.5.6. This program processes video captured from a= USB camera. It has 3 major functions:=0A=0Aint main (int argc, char ** arg= v): Initializes the whole program, create a message pipe for communication = between the standard Linux process and a real-time (Xenomai) task.=0A=0Avoi= d imageCb (const sensor_msgs::ImageConstPtr& msg): A callback function runn= ing in the *standard Linux Process*. It is called whenever an image frame i= s available, and it sends a message to the real-time task mentioned above.= =0A=0Avoid rtImageProcessing (void * arg): This is the real-time (Xenomai) = task that is supposed to process images.=0A=0AThe above functions are as fo= llows: (For simplicity, I omitted some unimportant codes)=0A=0A1) int main = (int argc, char ** argv){=0A=A02)=A0=0A=A03) =A0 =A0 rt_print_auto_init(1);= =0A=A04) =A0 =A0 mlockall(MCL_CURRENT|MCL_FUTURE);=0A=A05) =A0 =A0 //Create= a message pipe for the real-time task=0A=A06) =A0 =A0 rt_pipe_create(&pipe= _desc, NULL, PIPE_MINOR, 0);=0A=A07) =A0 =A0 //Initialize the real-time tas= k=0A=A08) =A0 =A0 rt_task_create(&task_desc, "RealTimeImageProcessing", 409= 6, 99, T_FPU|T_CPU(0));=0A=A09) =A0 =A0 rt_task_start(&task_desc, &rtImageP= rocessing, NULL);=0A10)=A0=0A11) =A0 =A0 //Create a message pipe for the st= andard Linux process=0A12) =A0 =A0 char devname[32];=A0=0A13) =A0 =A0 sprin= tf(devname, "/dev/rtp%d", PIPE_MINOR);=A0=0A14) =A0 =A0 pipe_fd =3D open(de= vname, O_RDWR);=0A15)=A0=0A16)=A0=0A17) =A0 =A0 //Setup the callback functi= on=0A18) =A0 =A0 //....=0A19)=A0=0A20) =A0 =A0 pause(); //Waiting=0A21)=A0= =0A22) =A0 =A0 //Clean up the tasks & pipes=0A23) }=0A24)=A0=0A25)=A0=0A26)= void imageCb(const sensor_msgs::ImageConstPtr& msg){=0A27)=A0=0A28) =A0 = =A0 int len =3D sizeof(sensor_msgs::ImageConstPtr *);=0A29) =A0 =A0 int ptr= _as_int =3D (int)&msg;=0A30) =A0 =A0 //Write the address of msg to the pipe= , so rtImageProcessing() gets msg's address, and interprets this address as= a pointer to msg.=0A31) =A0 =A0 write(pipe_fd, (const void*)&ptr_as_int, l= en);=A0=0A32) }=0A33) //sensor_msgs::ImageConstPtr is a class defined in RO= S libraries, and it looks like this:=0A34) //typedef boost::shared_ptr< ::s= ensor_msgs::Image const> ImageConstPtr;=0A35) //where ::sensor_msgs::Image = is a class for an image in ROS. Not sure if it matters how ::sensor_msgs::I= mage is defined, so the definition is omitted for simplicity=0A36)=A0=0A37)= void rtImageProcessing (void * arg){=0A38) =A0 =A0 char read_buf[8];=0A39)= =A0 =A0 int ptr_as_int;=0A40) =A0 =A0 int len =3D sizeof(sensor_msgs::Imag= eConstPtr *);=0A41) =A0 =A0 sensor_msgs::ImageConstPtr * p_msg;=0A42)=A0=0A= 43) =A0 =A0 while (1) {=0A44) =A0 =A0 =A0 =A0 rt_pipe_read(&pipe_desc, (voi= d *)read_buf, len, TM_INFINITE);=0A45) =A0 =A0 =A0 =A0 ptr_as_int =3D *((in= t*)read_buf);=0A46) =A0 =A0 =A0 =A0 p_msg =3D (sensor_msgs::ImageConstPtr *= )ptr_as_int;=0A47) =A0 =A0 =A0 =A0=A0=0A48) =A0 =A0 =A0 =A0 cv_bridge::CvIm= agePtr cv_ptr;=0A49) =A0 =A0 =A0 =A0 cv_ptr =3D cv_bridge::toCvCopy(*p_msg,= enc::BGR8);=0A50) =A0 =A0 =A0 =A0 //... More image processing codes go her= e =A0=A0=0A51) =A0 }=0A52) }=0A=0A=0AIn=A0rtImageProcessing() I can use (*p= _msg) as a pointer to msg to access some basic info of msg, e.g., (*p_msg)-= >height. But the program has a segment fault when executing Line 49). I hav= e made sure that the codes after Line 49) can, if running in the standard L= inux process w/ msg as an argument (that is, do not use a real-time task fo= r image processing), run correctly, so it is not the poor design of image p= rocessing codes that cause this problem. I suspect that the problem comes f= rom the accessing of a smart pointer across different processes, but I am n= ot sure, and I am still struggling with this problem...=0A=0A=0AAny ideas o= r suggestions will be highly appreciated.=0ATomZ=0A=0A=0A=0AOn Mon, Oct 24,= 2011 at 11:25 AM, Thomas Lockhart wrote:=0A= =0A=A0 =A0 On 10/23/2011 01:25 PM, haitaozhumail-disc@domain.hid wrote:=0A= =0A=A0 =A0 =A0 =A0 Hi All,=0A=0A=A0 =A0 =A0 =A0 Do a standard Linux process= and a real-time task (spawned by the=0A=A0 =A0 =A0 =A0 standard Linux proc= ess with rt_task_create and rt_task_start ) share the=0A=A0 =A0 =A0 =A0 sam= e address space? More specifically, I have a C++ program like this:=A0=0A= =0A=A0 =A0 ...=0A=0A=A0 =A0 =A0 =A0 Can the function demo() correctly acces= s the object created in main()?=0A=A0 =A0 =A0 =A0 What if pA is a smart poi= nter defined in Boost library?=A0=0A=0A=A0 =A0 Yes, yes, and yes (though I = didn't look at the actual code, the address space is shared).=0A=0A=A0 =A0 = For things like smart pointers, just make sure that someone is keeping a re= ference to the object so the reference count does not go to zero.=0A=0A=A0 = =A0 hth=0A=0A=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0- Tom=A0 --1498488837-480329272-1319490255=:1417 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable
Thanks for Thomas' answer.

The previous code I showed was truncated fo= r simplicity. The actual code is a little more complicated. 

I am writing a C++ program on Robotic Op= erating System=0A (www.ROS.org) on Ubuntu 10.10 patched with Xenomai 2.5.6.= This progr= am processes video captured from a USB camera. It has 3 major functions:

int main (int argc, char ** argv): Initializes the whole program, create a= message pipe for communication between the standard Linux process and a re= al-time (Xenomai) task.
void imageCb (const sensor_msgs::ImageConstPtr& msg): A callback f= unction running in the *standard Linux Process*. It is called whenever an i= mage frame is available, and it sends a message to the real-time task menti= oned above.
void rtImageProcessing (void * arg): This i= s the real-time (Xenomai) task that is supposed to process images.

The above functions are as follows: (For simplicity, I omitted som= e unimportant codes)

=
1) int main (int argc, char ** argv){
<= div id=3D"yiv617584859yui_3_2_0_15_131947690534840" class=3D"yui_3_2_0_14_1= 31947716813670">&n= bsp;2) 
 3)     rt_print_auto_init(1)= ;
 4)     mlockall(MCL_CURRENT|MCL_FUTUR= E);
 5)     //Create a message pipe for th= e real-time task
 6)     rt_pipe_create(&a= mp;pipe_desc, NULL, PIPE_MINOR, 0);
 7)     //Initialize the real-time = task
 8)     rt_task_create(&task_desc= , "RealTimeImageProcessing", 4096, 99, T_FPU|T_CPU(0));
=
&= nbsp;9)     rt_task_start(&task_desc, &rtImageProcessing,= NULL);
1= 0) 
11)     //Create a message pipe for th= e standard Linux process
12)     char devname[3= 2]; 
13)     sprintf(devname, "/dev/rtp%d"= , PIPE_MINOR); 
14)     pipe_fd =3D open(de= vname, O_RDWR);
15) 
16) 
1= 7)     //Setup the callback function
18) &nb= sp;   //....
19) 
20)   =   pause(); //Waiting
21) 
22)     //Clean= up the tasks & pipes
23) }
24)&n= bsp;
25)&n= bsp;
26) void imageCb(const sensor_msgs::ImageConstPtr&am= p; msg){
27) 
28)     int len =3D sizeof(sensor_msgs::Imag= eConstPtr *);
29)     int ptr_as_int =3D (int)&= amp;msg;
30)     //Write the address of msg to = the pipe, so rtImageProcessing() gets msg's address, and interprets this ad= dress as a pointer to msg.
31)     write(pipe_fd, (const void*)&ptr= _as_int, len); 
32) }
33) //s= ensor_msgs::ImageConstPtr is a class defined in ROS libraries, and it looks= like this:
34) //typedef boost::shared_ptr< ::sensor_= msgs::Image const> ImageConstPtr;
35) //where ::sensor_msgs::Image is a cla= ss for an image in ROS. Not sure if it matters how ::sensor_msgs::Image is = defined, so the definition is omitted for simplicity
36)&= nbsp;
37) void rtImageProcessing (void * arg){
38) &= nbsp;   char read_buf[8];
39)     int ptr_= as_int;
40)     int len =3D sizeof(sensor_msgs:= :ImageConstPtr *);
4= 1)     sensor_msgs::ImageConstPtr * p_msg;
42)&= nbsp;
43)     while (1) {
44)=         rt_pipe_read(&pipe_desc, (void *)read_buf,= len, TM_INFINITE);
45)         ptr_as_int= =3D *((int*)read_buf);
46)         p= _msg =3D (sensor_msgs::ImageConstPtr *)ptr_as_int;
47) &n= bsp;       
48)         cv_bridge::CvImagePtr= cv_ptr;
49)         cv_ptr =3D cv_br= idge::toCvCopy(*p_msg, enc::BGR8);
50)     &nbs= p;   //... More image processing codes go here   
51)   }
52) }


In rtImageProcessing() I can use (*p_msg) as a pointer= to msg to access some basic info of msg, e.g., (*p_msg)->height. But the program has a segment faul= t when executing Line 49). I have made sure that the codes after Line 49) c= an, if running in the standard Linux process w/ msg as an argument (that is= , do not use a real-time task for image processing), run correctly, so it i= s not the poor design of image processing codes that cause this problem. I = suspect that the problem comes from the accessing of a smart pointer across= different processes, but I am not sure, and I am still struggling with thi= s problem...

<= /span>
Any ideas or su= ggestions will be highly appreciated.
TomZ


On Mon, Oct 24, 2011 at 11= :25 AM, Thomas Lockhart <Thomas.Lockhart@domain.hid> wrote:<= /div>
    On 10/23/2011 01:25 PM, haitaozhumail-disc@= yahoo.com wrote:

       = ; Hi All,
=
        Do a s= tandard Linux process and a real-time task (spawned by the
        standard Linux process with= =0A rt_task_create and rt_task_start ) share the
   =     same address space? More specifically, I have a C++ program = like this: 

    ...

=         Can the function demo() correctly acces= s the object created in main()?
        Wh= at if pA is a smart pointer defined in Boost library? 
    Yes, yes, and yes (though I didn't look at th= e actual code, the address space is shared).

=
&nb= sp;   For things like smart pointers, just make sure that someone is k= eeping a reference to the object so=0A the reference count does not go to z= ero.

    hth

    &= nbsp;                - Tom 
--1498488837-480329272-1319490255=:1417--