Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14534 > unrolled thread

Troubles when processes try to access to a char device

Started byWilly <gruppi@willygroup.org>
First post2012-02-27 08:58 +0100
Last post2012-02-27 14:38 +0100
Articles 8 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Troubles when processes try to access to a char device Willy <gruppi@willygroup.org> - 2012-02-27 08:58 +0100
    Re: Troubles when processes try to access to a char device gwowen <gwowen@gmail.com> - 2012-02-27 05:19 -0800
      Re: Troubles when processes try to access to a char device Willy <gruppi@willygroup.org> - 2012-02-27 14:41 +0100
        Re: Troubles when processes try to access to a char device gwowen <gwowen@gmail.com> - 2012-02-27 07:08 -0800
          Re: Troubles when processes try to access to a char device Willy <gruppi@willygroup.org> - 2012-02-27 16:42 +0100
            Re: Troubles when processes try to access to a char device gwowen <gwowen@gmail.com> - 2012-02-27 09:29 -0800
            Re: Troubles when processes try to access to a char device Dan Oxender <d?n.oxe????@gmail.com> - 2012-02-28 09:06 -0700
    Re: Troubles when processes try to access to a char device Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-02-27 14:38 +0100

#14534 — Troubles when processes try to access to a char device

FromWilly <gruppi@willygroup.org>
Date2012-02-27 08:58 +0100
SubjectTroubles when processes try to access to a char device
Message-ID<4f4b37c7$1@extreme.x-privat.org>
Hi,
I'm building a multi process application under Linux.

In the main process I initialize a char device and put is file 
descriptor in a variable dev_fd in the shared memory area.

## Main Process ##
...
dev_fd = open("/dev/mydev",O_RDWR))
...

In the other processes I want to use the device in this way:

## Other Processes ##
...
read(dev_fd, buffer);
...
write(dev_fd, buffer);
...

but "read" function always returns 255, while "write" function doesn't 
write the device :(

Where is the problem?

Thanks,
-- 
*Willy* - *Contatti*: http://snipurl.com/contatti
* *FAQ* di /it-alt.comp.software.mozilla/: http://www.FAQmozilla.org
* *Gratuiki* - Software Free: http://gratuiki.willygroup.org

[toc] | [next] | [standalone]


#14539

Fromgwowen <gwowen@gmail.com>
Date2012-02-27 05:19 -0800
Message-ID<b3e184a9-e202-4d49-a936-272876efdc51@l14g2000vbe.googlegroups.com>
In reply to#14534
On Feb 27, 7:58 am, Willy <gru...@willygroup.org> wrote:
> Hi,
> I'm building a multi process application under Linux.
>
> In the main process I initialize a char device and put is file
> descriptor in a variable dev_fd in the shared memory area.
>
> ## Main Process ##
> dev_fd = open("/dev/mydev",O_RDWR))

> In the other processes I want to use the device in this way:
>
> ## Other Processes ##
> read(dev_fd, buffer);
> write(dev_fd, buffer);

How are you sharing the file descriptor between processes?

[toc] | [prev] | [next] | [standalone]


#14541

FromWilly <gruppi@willygroup.org>
Date2012-02-27 14:41 +0100
Message-ID<4f4b8821$2@extreme.x-privat.org>
In reply to#14539
Il 27/02/2012 14:19, gwowen ha scritto:
> How are you sharing the file descriptor between processes?

The file descriptor is an attribute of an object which is in a shared 
memory area.

The code is like that:
## shared memory ##
class my_obj
{
    fd;

public:
    init()
    {
     ...
     fd = open("/dev/my_device");
     ...

    };

    int read_data()
    {
      return read(fd, buffer);
    }

    void write_data(data)
    {
       buffer = data;
       write(fd, buffer);

    }
}


-- 
*Willy* - *Contatti*: http://snipurl.com/contatti
* *FAQ* di /it-alt.comp.software.mozilla/: http://www.FAQmozilla.org
* *Gratuiki* - Software Free: http://gratuiki.willygroup.org

[toc] | [prev] | [next] | [standalone]


#14544

Fromgwowen <gwowen@gmail.com>
Date2012-02-27 07:08 -0800
Message-ID<897e745f-3360-4bde-a44a-da19341ba53b@gw9g2000vbb.googlegroups.com>
In reply to#14541
On Feb 27, 1:41 pm, Willy <gru...@willygroup.org> wrote:
> Il 27/02/2012 14:19, gwowen ha scritto:
>
> > How are you sharing the file descriptor between processes?
>
> The file descriptor is an attribute of an object which is in a shared
> memory area.
>
> The code is like that:
> ## shared memory ##
> class my_obj
> {
>     fd;
>

presumably
  int fd;

That'll probably be your issue. By default, file descriptors only have
meaning on a per-process basis.
So if two separate processes use the same integer descriptor, they
won't be reading from the same stream - and all the shared memory
segment shares is the integer.

There are mechanisms for sharing file-descriptors, fork()/exec() or
sendmsg()/recvmsg() are the most common, but they probably won't work
they way you want.

The best bet may be for each process to open their own file descriptor
for the device, and for shared memory / IPC to coordinate the reads /
writes through some sort of mutual exclusion, but it all depends on
the access pattern you need.

[toc] | [prev] | [next] | [standalone]


#14546

FromWilly <gruppi@willygroup.org>
Date2012-02-27 16:42 +0100
Message-ID<4f4ba44d$1@extreme.x-privat.org>
In reply to#14544
Il 27/02/2012 16:08, gwowen ha scritto:
> presumably
>    int fd;
>
> That'll probably be your issue. By default, file descriptors only have
> meaning on a per-process basis.
> So if two separate processes use the same integer descriptor, they
> won't be reading from the same stream - and all the shared memory
> segment shares is the integer.
>
> There are mechanisms for sharing file-descriptors, fork()/exec() or
> sendmsg()/recvmsg() are the most common, but they probably won't work
> they way you want.
>
> The best bet may be for each process to open their own file descriptor
> for the device, and for shared memory / IPC to coordinate the reads /
> writes through some sort of mutual exclusion, but it all depends on
> the access pattern you need.

Thanks :)
I've understood the problem: file descriptor isn't just an int!
Now I'm trying to find out the best solution.

-- 
*Willy* - *Contatti*: http://snipurl.com/contatti
* *FAQ* di /it-alt.comp.software.mozilla/: http://www.FAQmozilla.org
* *Gratuiki* - Software Free: http://gratuiki.willygroup.org

[toc] | [prev] | [next] | [standalone]


#14554

Fromgwowen <gwowen@gmail.com>
Date2012-02-27 09:29 -0800
Message-ID<6d4d8aa3-808e-4dc9-bed5-b47d3398023e@w5g2000vbv.googlegroups.com>
In reply to#14546
On Feb 27, 3:42 pm, Willy <gru...@willygroup.org> wrote:
> Thanks :)
> I've understood the problem: file descriptor isn't just an int!

Right! It's an int that's a handle for an in-kernel per-process
structure.  You can share the int around, but the magic won't follow
it.

[toc] | [prev] | [next] | [standalone]


#14588

FromDan Oxender <d?n.oxe????@gmail.com>
Date2012-02-28 09:06 -0700
Message-ID<1oupk7tomddagdacvdnhj12fbstpi391cu@4ax.com>
In reply to#14546
On Mon, 27 Feb 2012 16:42:06 +0100, Willy <gruppi@willygroup.org>
wrote:


>Thanks :)
>I've understood the problem: file descriptor isn't just an int!
>Now I'm trying to find out the best solution.

two potential solutions:

	new an area of heap which may be used as a "file" and share
the pointer between the processes.

	Open and Close the file as needed in each process.

Dan Oxender

[toc] | [prev] | [next] | [standalone]


#14540

FromAlain Ketterlin <alain@dpt-info.u-strasbg.fr>
Date2012-02-27 14:38 +0100
Message-ID<87aa445rca.fsf@dpt-info.u-strasbg.fr>
In reply to#14534
Willy <gruppi@willygroup.org> writes:

> I'm building a multi process application under Linux.
>
> In the main process I initialize a char device and put is file
> descriptor in a variable dev_fd in the shared memory area.

This can't work: a file descriptor is an index into a process-local
table of open files. It makes no sense to share descriptors between
processes.

BTW, this has absolutely nothing to do with the C++ language. Please
direct your questions to a forum dedicated to basic unix programming.

-- Alain.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web