Pipes Implementation

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Pipes Implementation

Post by liquid.silver »

I'm currently busy implementing pipes in my os and i was wondering how they are dealt with in other oses. I specifically refering here to situations where pipes are used for ipc.

Say process A creates a pipe and sends the two ends to processes B and C. Can they communicate with each other? And what happens if process A terminates? How does the os know when to close a pipe? How does it know another process won't use it again? Would B and C actually just be able to use the pipe? What if process D guessed the pipe id? Could it use the pipe without authorization?

I think these questions show what i'm unsure of. I have tried to find out how other oses do it, but with on success. I realise i can just decide how it will be done in my os, but i want to first know how others do it. I'm refering to both hobbie oses and proprietary ones here.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Pipes Implementation

Post by jal »

liquid.silver wrote:I'm currently busy implementing pipes in my os and i was wondering how they are dealt with in other oses. I specifically refering here to situations where pipes are used for ipc.
In Unix, a pipe is just an ordinary file (from the point of the programmer) you can write to and read from. You open a pipe like a file, and read from it or write to it like a file. A named pipe actually resides in the file system and can be used by any process that may access the pipe, I don't recall how unnamed pipes are accessed.

I really don't understand what you mean by a process A sending the 'ends' of a pipe to processes B and C. If I understand what you are saying, aren't you talking more about message channels?


JAL
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Pipes Implementation

Post by JamesM »

I don't recall how unnamed pipes are accessed.
IIRC they are given a filename in /tmp and accessed as named pipes.
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post by liquid.silver »

Process A would be using some sort of handle to access each end of the pipe. I was referring to just passing that handle. Also i'm referring to unnamed pipes.

In unix when do the pipes disappear? Obviously if they are specifically closed, but if they aren't, and the process terminates and leaves them open, what happens?
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

liquid.silver wrote:Process A would be using some sort of handle to access each end of the pipe. I was referring to just passing that handle. Also i'm referring to unnamed pipes.
Yeah, but then they'd have to be global handles, instead of local ones (which is more usual).
In unix when do the pipes disappear? Obviously if they are specifically closed, but if they aren't, and the process terminates and leaves them open, what happens?
Unix, like other decent OSes, cleans up after processes that leave resources open.


JAL
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post by liquid.silver »

But what i'm getting at is when does the os cleanup the unused resources? How does it know that the resources are unused?
didroe
Posts: 6
Joined: Wed Nov 28, 2007 9:05 am

Post by didroe »

If you stored a list of open files/handles for each process, add to it when someone does fileopen/pipeopen or whatever and remove from it when they do fileclose. When the process ends, make implicit calls to fileclose for all handles remaining in the process' list.
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post by liquid.silver »

Is that how other oses like linux and windows do it? I was planning on doing something similar to that in my os, i just want to know other methods.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

liquid.silver wrote:Is that how other oses like linux and windows do it? I was planning on doing something similar to that in my os, i just want to know other methods.
Yes, even DOS did this, although I'm not sure whether it actually cleaned up after a program. Unix does so yes, and Windows as well. In general, it is a good idea to do resource tracking, an OS should never rely on a program closing file handles, freeing memory, etc.


JAL
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post by liquid.silver »

So if process A makes a named pipe and sends the name to process B. Then A terminates. Can process B then use the pipe? And if an unnamed pipe was made?
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

The standard way to create a pipe on Unix, is to call mkpipe() which "returns" two file descriptors. See the manpage, but basicly one end can be written to, and the other end read from..

Internally the pipe is basicly a ringbuffer and a couple of semaphores. Nothing fancy. The size is typically around 4k or so.. thought that isn't really all that important.

Such a pipe exists as long as either end of the pipe is open. It's cleaned after you close() the both ends. There's a couple of ways to move file descriptors from a process to another on a typical unix system, but most common way is to not:

when you write a pipe command line, it's the shell that calls mkpipe() and then when it fork()s the processes to do the work, it does some filedescriptor renaming (see dup()) to get the right descriptors at the right places before it finally exec()s the programs. edit: remember fork()ed childs inherit the parents open file descriptors by default

Named pipes are similar, except they sit somewhere in the filesystem, and if you open the named pipe repeatedly, you get the same pipe every time.. anyway, just more ringbuffers kludged to have a filename. :)
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post by liquid.silver »

And with the named pipe, how long does the entry in the filesystem exist after noone has it open?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

liquid.silver wrote:And with the named pipe, how long does the entry in the filesystem exist after noone has it open?
It remains until you delete it... ;)
I wrote:[brynet@ttyp0]~/named-pipe: $ mkfifo happy-pipe
[brynet@ttyp0]~/named-pipe: $ ls -li
total 0
467721 prw-r--r-- 1 brynet brynet 0 Dec 6 17:00 happy-pipe
[brynet@ttyp0]~/named-pipe: $
Notice the p in the permissions..
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post by liquid.silver »

Thanx. I think i understand what i wanted to know about implementations of pipes
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Anyone care to add some of this info to the wiki? ('Unix Pipes')
C8H10N4O2 | #446691 | Trust the nodes.
Post Reply