Skip to contentSkip to conversation

writing from NRT to a rt_pipe

Xenomai-help NZG 2 participants 11 messages Jun 7, 2007

lore
Share & tools
lore

NZG

author
#0 lore raw

When writing from NRT to a real time pile (open, write, close). Are message boundaries preserved based upon the length of the write call?

It's not working for me. The code below returns success, but calling

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); or
err = rt_pipe_read(comp,k,sizeof(kommand_t),100);

from RT space on the same pipe never returns any data

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); always returns err
0x0b, Resource temporarily unavailable

err = rt_pipe_read(comp,k,sizeof(kommand_t),100);
always returns err:
0x6e, Connection timed out

NZG

code specifics follow...............

		int err;
		kommand_t k = {
		.type = KILLCYCLE,	
		};
	   
	    int cfd = open(COMMANDPIPE, O_WRONLY|O_SYNC);
	   
	   if(cfd<0){
	   	printf("can't open %s (%s)\n",COMMANDPIPE, strerror(-cfd));
	   	fflush(stdout);
	   	return;
	   }
	   	
	   printf("sizeof kommand_t = %u\n",sizeof(kommand_t));
	   
       err=write(cfd,&k,sizeof(kommand_t));
       	
       if(err>=0)printf("wrote %u bytes to %s\n",err,COMMANDPIPE); 
       	
       if(err<0)printf("err writing to %s (%s)\n",COMMANDPIPE, 
strerror(errno));
       
       
       if(close(cfd)<0)printf("couldn't close %s\n",COMMANDPIPE);
author
#1 lore raw

Further experimentations appear to show that the problem is that I'm closing the NRT pipe before data is received in the RT thread, this appears to cause the loss of data in the pipe.

Is this the desired behavior?

NZG

Quoted 55 lines · NZG

When writing from NRT to a real time pile (open, write, close). Are message boundaries preserved based upon the length of the write call?

It's not working for me. The code below returns success, but calling

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); or
err = rt_pipe_read(comp,k,sizeof(kommand_t),100);

from RT space on the same pipe never returns any data

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); always returns err
0x0b, Resource temporarily unavailable

err = rt_pipe_read(comp,k,sizeof(kommand_t),100);
always returns err:
0x6e, Connection timed out

NZG

code specifics follow...............

		int err;
		kommand_t k = {
		.type = KILLCYCLE,
		};

	    int cfd = open(COMMANDPIPE, O_WRONLY|O_SYNC);

	   if(cfd<0){
	   	printf("can't open %s (%s)\n",COMMANDPIPE, strerror(-cfd));
	   	fflush(stdout);
	   	return;
	   }

	   printf("sizeof kommand_t = %u\n",sizeof(kommand_t));

       err=write(cfd,&k,sizeof(kommand_t));

       if(err>=0)printf("wrote %u bytes to %s\n",err,COMMANDPIPE);

       if(err<0)printf("err writing to %s (%s)\n",COMMANDPIPE,

strerror(errno));

       if(close(cfd)<0)printf("couldn't close %s\n",COMMANDPIPE);

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

#2 lore raw
Quoted 5 lines · NZG

Further experimentations appear to show that the problem is that I'm closing the NRT pipe before data is received in the RT thread, this appears to cause the loss of data in the pipe.

Is this the desired behavior?

Yes, clearly.

Quoted 65 lines

NZG

Quoted 51 lines · NZG

When writing from NRT to a real time pile (open, write, close). Are message boundaries preserved based upon the length of the write call?

It's not working for me. The code below returns success, but calling

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); or
err = rt_pipe_read(comp,k,sizeof(kommand_t),100);

from RT space on the same pipe never returns any data

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); always returns err
0x0b, Resource temporarily unavailable

err = rt_pipe_read(comp,k,sizeof(kommand_t),100);
always returns err:
0x6e, Connection timed out

NZG

code specifics follow...............

		int err;
		kommand_t k = {
		.type = KILLCYCLE,
		};

	    int cfd = open(COMMANDPIPE, O_WRONLY|O_SYNC);

	   if(cfd<0){
	   	printf("can't open %s (%s)\n",COMMANDPIPE, strerror(-cfd));
	   	fflush(stdout);
	   	return;
	   }

	   printf("sizeof kommand_t = %u\n",sizeof(kommand_t));

       err=write(cfd,&k,sizeof(kommand_t));

       if(err>=0)printf("wrote %u bytes to %s\n",err,COMMANDPIPE);

       if(err<0)printf("err writing to %s (%s)\n",COMMANDPIPE,

strerror(errno));

       if(close(cfd)<0)printf("couldn't close %s\n",COMMANDPIPE);
Quoted 4 lines

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Signature · 1 line
Philippe.
author
#3 lore raw
Quoted 1 line

Yes, clearly.

Ah, that in conjunction with the
"can't use message pipe - Cannot allocate memory"
thread resolves my issues then.

My assumption was that data would exist in the pipe after the write until the pipe was destroyed, irrespective of the "openness" of the NRT side.

It's possible that this assumption is due to a lack of experience with pipes in general, I don't know.

thank you for all you help,
NZG

author
#4 lore raw

from: man:pipe(7)
If all file descriptors referring to the write end of a pipe have been closed, then an attempt to read(2) from the pipe will see end-of-file

well there ya go, when all else fails read the manual

thx again,
NZG

#5 lore raw
In reply to NZG
Quoted 8 lines · NZG
Quoted 1 line

Yes, clearly.

Ah, that in conjunction with the
"can't use message pipe - Cannot allocate memory"
thread resolves my issues then.

My assumption was that data would exist in the pipe after the write until the pipe was destroyed, irrespective of the "openness" of the NRT side.

Nope, as a general rule, the read-side of an endpoint is flushed when the other end closes. Then, you have also to take into account the priority of the thread waiting on the endpoint wrt the priority of the one which closes the other side.

For instance, if a RT thread is waiting for data (e.g. currently sleeping on rt_pipe_read()) while the NRT side closes, then the RT thread will be awaken, and a rescheduling will occur in the pipe release routine before the input queue is eventually flushed, leaving the opportunity for the RT side to consume the pending messages (since the read-side has priority over the closing side in this situation).

What happened in your code is likely that the RT side was not actually waiting for data, but maybe handling other duties while the NRT side closed its endpoint. Yeah, that's a bit tricky, I agree, but in a strictly prioritized system, this makes some sense.

Quoted 3 lines

It's possible that this assumption is due to a lack of experience with pipes in general, I don't know.

Not really, fact is that we do have to deal with an asymmetric view of the communication, i.e. RT / NRT, so there is room for specific implementations. OTOH, regular Linux pipes won't give you more guarantee on the read-side, you could just get a 0-bytes return from a read() to notify the broken pipe condition.

Btw, do you happen to have CONFIG_XENO_OPT_NATIVE_PIPE_BUFSZ set to some value in your .config?

Quoted 3 lines

thank you for all you help,
NZG

Signature · 1 line
Philippe.
author
#6 lore raw
Quoted 2 lines · Philippe Gerum

Btw, do you happen to have CONFIG_XENO_OPT_NATIVE_PIPE_BUFSZ set to some value in your .config?

CONFIG_XENO_OPT_NATIVE_PIPE_BUFSZ=4096

NZG

author
#7 lore raw

[Xenomai-help] writing from NRT to a rt_pipe-> easy wait for completion

In reply to Philippe Gerum
Quoted 7 lines · Philippe Gerum
Quoted 6 lines · NZG
Quoted 1 line

Yes, clearly.

Ah, that in conjunction with the
"can't use message pipe - Cannot allocate memory"
thread resolves my issues then.

Quoted 7 lines
Quoted 2 lines

My assumption was that data would exist in the pipe after the write until the pipe was destroyed, irrespective of the "openness" of the NRT side.

Nope, as a general rule, the read-side of an endpoint is flushed when the other end closes. Then, you have also to take into account the priority of the thread waiting on the endpoint wrt the priority of the one which closes the other side.

So, if I wanted to use standard NRT calls in user space to wait for data to actually be delivered through the rt pipe before closing the fd, what would be the best method? Select? Or is there a simple call for this I'm missing?

thx,
NZG

#8 lore raw

Re: [Xenomai-help] writing from NRT to a rt_pipe-> easy wait for completion

Quoted 11 lines · NZG
Quoted 10 lines · Philippe Gerum
Quoted 8 lines · NZG
Quoted 1 line

Yes, clearly.

Ah, that in conjunction with the
"can't use message pipe - Cannot allocate memory"
thread resolves my issues then.

My assumption was that data would exist in the pipe after the write until the pipe was destroyed, irrespective of the "openness" of the NRT side.

Quoted 7 lines
Quoted 4 lines

Nope, as a general rule, the read-side of an endpoint is flushed when the other end closes. Then, you have also to take into account the priority of the thread waiting on the endpoint wrt the priority of the one which closes the other side.

So, if I wanted to use standard NRT calls in user space to wait for data to actually be delivered through the rt pipe before closing the fd, what would be the best method? Select? Or is there a simple call for this I'm missing?

select() is supported, but it won't allow you to wait for the output queue from the NRT-side to drain, it will only allow you to wait for messages sent from the RT endpoint.

There is no simple call, I'm afraid. One way would be to make your RT side always wait on the read-side (and process the messages as they arrive); since the RT receiver would always have precedence over the NRT sender, you would have the guarantee that all the messages sent have been consumed before the NRT side closes its endpoint.

Another simple way would be to extend the pipe driver in order to provide a new ioctl() function, returning the status of the RT input queue, in which case, you would just have to loop issuing this ioctl() until it reports that the RT side has consumed all the pending messages. Not pretty, but would work.

The best way would be to extend our support for select(), in the poll method the message pipe driver provides.

Quoted 10 lines

thx,
NZG

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Signature · 1 line
Philippe.
author
#9 lore raw

Re: [Xenomai-help] writing from NRT to a rt_pipe-> easy wait for completion

Quoted 5 lines · Philippe Gerum

There is no simple call, I'm afraid. One way would be to make your RT side always wait on the read-side (and process the messages as they arrive); since the RT receiver would always have precedence over the NRT sender, you would have the guarantee that all the messages sent have been consumed before the NRT side closes its endpoint

I'm thinking this is the cleanest way, just spawn a separate RT thread of lesser priority to my main rt loop, set it up to do nothing but read and process messages, and put some locks on it.

thx,
NZG

#10 lore raw
In reply to NZG
Quoted 3 lines · NZG

When writing from NRT to a real time pile (open, write, close). Are message boundaries preserved based upon the length of the write call?

It should yes, that's the main difference between this interface and other RT fifo implementations. rt_pipe_stream() works the other way, though (i.e. bytestream mode).

Quoted 8 lines

It's not working for me. The code below returns success, but calling

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); or
err = rt_pipe_read(comp,k,sizeof(kommand_t),100);

from RT space on the same pipe never returns any data

Quoted 8 lines

err = rt_pipe_read(comp,k,sizeof(kommand_t),TM_NONBLOCK); always returns err
0x0b, Resource temporarily unavailable

err = rt_pipe_read(comp,k,sizeof(kommand_t),100);
always returns err:
0x6e, Connection timed out

Please send a compilable standalone test, so that we get a clear view of what fails, and of the current runtime conditions when that fails.

Quoted 36 lines

NZG

code specifics follow...............

		int err;
		kommand_t k = {
		.type = KILLCYCLE,	
		};
	   
	    int cfd = open(COMMANDPIPE, O_WRONLY|O_SYNC);
	   
	   if(cfd<0){
	   	printf("can't open %s (%s)\n",COMMANDPIPE, strerror(-cfd));
	   	fflush(stdout);
	   	return;
	   }
	   	
	   printf("sizeof kommand_t = %u\n",sizeof(kommand_t));
	   
       err=write(cfd,&k,sizeof(kommand_t));
       	
       if(err>=0)printf("wrote %u bytes to %s\n",err,COMMANDPIPE); 
       	
       if(err<0)printf("err writing to %s (%s)\n",COMMANDPIPE, 
strerror(errno));
       
       
       if(close(cfd)<0)printf("couldn't close %s\n",COMMANDPIPE);

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Signature · 1 line
Philippe.