Re: [linux-audio-dev] MIDI routing, FIFO's etc.

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: Re: [linux-audio-dev] MIDI routing, FIFO's etc.
From: Benno Senoner (sbenno_AT_gardena.net)
Date: la loka   09 1999 - 21:00:33 EDT


On Sat, 09 Oct 1999, Paul Barton-Davis wrote:
> >Does anyone know if there is a way to make for example /dev/dsp wrapper
> >which works in read/write mode ?
> >maybe by letting the wrapper close the fifo filehandle, and reassign to this
> >filedescriptor a unix socket using dup2() ?
>
> just include read(2) and write(2) front-ends in the LD_PRELOAD
> wrapper, and redirect the relevant file descriptors to the right
> place:
>
> write (int fd, ...) {
> if (fd == read_pipe) {
> fd = write_pipe;
> }
>
> /* do normal stuff */
> }
>
> read (int fd, ...) {
> if (fd == write_pipe) {
> fd = read_pipe;
> }
>
> /* do normal stuff */
> }

Hmm,
I just tried to open a named pipe ( mkfifo /tmp/fifo ) ,
and the behaviour is curious:

consider these 2 simple programs

program A
-----------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

main()
{
  int f,data,res;

f=open("/tmp/fifo10",O_RDWR);
if(f<0)
{
  perror("open");
  exit(1);
}
  data=10;

  res=write(f,&data,4);
  fsync(f);
  printf("write res=%d\n",res);
  data=-1;
  res=read(f,&data,4);
  printf("read res=%d\n",res);
  printf("data=%d\n",data);
}
-----------------------

program B
-----------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

main()
{
  int f,data,res;

  f=open("/tmp/fifo10",O_RDWR);
  if(f<0)
  {
    perror("open");
    exit(1);
  }

  res=read(f,&data,4);
  printf("read res=%d\n",res);
  printf("data=%d\n",data);

  data=20;
  res=write(f,&data,4);
  printf("write res=%d\n",res);
}
-----------------------

If you run A) alone
it reads back the data it wrote to the fifo.

if you run first B) and then A)
B waits for data,
A writes the data to the pipe
B reads the data and writes data to the pipe
A reads the data written by B.

BUT: sometime A reads back his own data, as you would run A alone.

Seems like the fifo uses one single fifo buffer and doesn't care who is the
reader.
that means no reliable fullduplex communication.

But I'm still curious if Paul's read() and write() wrapper is faster
than opening the fifo, and then remap the fifo to a unix domain socket via
dup2() ?

Maybe one could say: what if I issue many read()/write()s to other
filedescriptors ?
In this case the read()/write() wrapper would execute the
if(fd == read_pipe) every time.
I don't know almost nothing about the efficiency of unix domain sockets,
and if they are implemented by using 2 ring buffers to implement
fullduplex communication ( SOCK_STREAM).

Any thoughts ?

regards,
Benno.


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : pe maalis 10 2000 - 07:27:13 EST