Unix buffer device

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Unix buffer device

Postby darkspork » Mon Jan 25, 2010 5:50 pm UTC

Is there a virtual device in *nix that acts as a buffer, so that, essentially, whatever input you give it becomes its output? I'm looking for something like the standard cat program, except that it can be mounted like a file, and is shared among all users with read/write access. Does such a thing exist and, if so, what is it called? If not, would it be trivial to create such a virtual device? Can I mount part of RAM as a file? Can I do any of this without su?
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

LikwidCirkel
Posts: 169
Joined: Thu Nov 08, 2007 8:56 pm UTC
Location: on this forum (duh)
Contact:

Re: Unix buffer device

Postby LikwidCirkel » Mon Jan 25, 2010 6:12 pm UTC

bi-directional 2 process/ 2 user pipes or FIFO's are easy to use. If you need something that's readable and writable by multiple users, I know of nothing that's quick and dirty. You could write a pretty simple program though. You might have to create multiple threads, and multiple FIFO nodes for different users though, depending on exactly what you are trying to do. If you create a named FIFO from software, you can then open it just like a file from the shell or other programs and send data back and forth.

Edit: you can do this as a regular user if you create FIFO nodes where you have permission.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Unix buffer device

Postby Berengal » Mon Jan 25, 2010 6:40 pm UTC

Yeah, named pipes are your friends. Alternatively there's unix sockets.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Unix buffer device

Postby lulzfish » Mon Jan 25, 2010 6:44 pm UTC

darkspork wrote:Is there a virtual device in *nix that acts as a buffer, so that, essentially, whatever input you give it becomes its output? I'm looking for something like the standard cat program, except that it can be mounted like a file, and is shared among all users with read/write access. Does such a thing exist and, if so, what is it called? If not, would it be trivial to create such a virtual device? Can I mount part of RAM as a file? Can I do any of this without su?

Yep. You seem to be thinking of a few different kinds of Inter-Process Communication devices.
Here's what I've heard of:

D-Bus: A system communications layer used by GNOME, KDE, etc. to communicate between applications, notify of new hardware, etc. It is shared by all users, and you can use it to send messages between any two programs on the bus. It's basically like a local IM service, but for programs.
Shared memory: A section of RAM that appears as a file. Multiple programs can mount the file and read / write data to it.
RAM-disk / RAMfs: A filesystem based in RAM. I don't know any specific ones, but LiveCDs all use them, so there's at least one or two stable ones out there.
Edit:
Named pipes: I feel obligated to mention these, since everyone else did. Pipes are the simplest communication, and they only have one input and one output. If you create a pipe, you can only have one process writing into it at a time, and the data will disappear once another process reads it out. Pipes are only really good for transferring from one program to another. Multiple inputs or outputs just don't work.

If you only need one file in RAM, it would be best to look into shared memory:
http://en.wikipedia.org/wiki/Shared_memory

But I don't think this is what you're thinking of.

Also, give us more detail. Just tell us what you're trying to accomplish.

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Unix buffer device

Postby thoughtfully » Mon Jan 25, 2010 6:51 pm UTC

FIFOs are character devices, though. A ram disk shouldn't be hard to set up, although I haven't used one since DOS daze, I think. What exactly are you trying to accomplish, anyway? You can create a FIFO for anyone to access (chmod 666, mark of the beast!) as any user, but you'll need to be root to make a ramdisk.

You can do some really neato stuff with FUSE, which will require root to install (but frequently is included in newer distributions). This looks like it might be the sort of thing you are looking for, although it isn't explicitly a ramdisk.

Actually, that looks like a toy I'd enjoy playing with :)

SQLite can run in a memory-only mode. But you'd have to run it as a daemon that mediated access to it, since SQLite doesn't provide a server. No restrictions on permissions, though.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: Unix buffer device

Postby darkspork » Wed Jan 27, 2010 1:32 am UTC

For those wondering, I'm working on a collaboration tool to run on the school's server (It seems to be a custom Linux build) in the terminal via ssh. I can't write to disk between each keystroke, partially because of the naturally slow speed of hard drives. I need communication between multiple programs running under multiple users, and all without root access.

Shared memory was really the thing I was thinking of. I think I'll look into D-Bus too, but I'm not sure if the system has it.
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Unix buffer device

Postby lulzfish » Wed Jan 27, 2010 3:23 am UTC

The problem with shared memory, RAM disks, etc. is that it's more like a block of memory than an actual communication protocol, you'd need to constantly poll for new data, eating up CPU cycles. It might be hard to synchronize, too.

Probably what I would do (Since I'm considering a similar project) is set up a client / server thing, where a server has a single copy of your data, and the clients connect to the server. Whenever a client makes a change, just echo that change to all other clients. The clients would connect over some kind of socket. If everyone's going to SSH in and then run the client on the same computer as the server, you can use Unix sockets. Otherwise, Berkley sockets / any similar network connection.

Pipes are probably not practical since you'd need to automatically create a new pair for each client, and keep track of them all.

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Unix buffer device

Postby phlip » Sat Feb 06, 2010 5:24 am UTC

Why not just use screen? Writing a terminal emulator is a lot more complicated than it sounds, may as well use one that already exists.

For the first user, run "screen -S somescreenname" to start it up. To share the terminal, press Ctrl+A then : to bring up the screen command prompt and enter "multiuser on"... then again press Ctrl+A then : and enter "acladd otheruser". Both of these commands can be added to .screenrc if you want them to run every time. The other user can then run "screen -x -r firstuser/somescreenname". Bam, fully functional terminal sharing. Any user can then press Ctrl+A then D to detach from the screen.

Read screen's man page for a heap more info. Google also finds this which looks good.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]


Return to “Coding”

Who is online

Users browsing this forum: AdsBot [Google] and 8 guests