LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-06-2007, 02:34 PM   #1
lamtab
Member
 
Registered: Nov 2007
Posts: 31

Rep: Reputation: 15
named pipes


Hi all! I was wondering if someone could give me an example of the usage of named pipes for communication of father with multiple children processes in c code. Thanks in advance
 
Old 12-06-2007, 04:37 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,246

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
1st result from google: http://developers.sun.com/solaris/ar...med_pipes.html
google is your friend...
 
Old 12-07-2007, 01:32 AM   #3
lamtab
Member
 
Registered: Nov 2007
Posts: 31

Original Poster
Rep: Reputation: 15
I'm looking for a parent-multiple children example. I have an example similar to that. thx anyway
 
Old 12-07-2007, 02:16 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Consider this:
Code:
#!/bin/bash

mkfifo fifo_one
mkfifo fifo_two

# create infinite input to fifos for illustration...
yes |sed 's/y/this is one/' | nl > fifo_one &
yes |sed 's/y/this is two/' | nl > fifo_two &

n=1
while [ $n -le 3 ]; do
        read -u 3 one_data
        read -u 4 two_data
        echo "$n  from fifo_one: \"$one_data\"; from fifo_two: \"$two_data\""
        let n+=1
done 3< fifo_one 4< fifo_two

# kill backgrounded processes
kill -9 %1
kill -9 %2

# wait will they're dead
wait

# clean up fifos
rm -f fifo_one fifo_two
 
Old 12-07-2007, 02:17 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Oh, C code... Doh!
 
Old 12-07-2007, 03:10 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
you are better off using sockets,
no more difficult to use and then you can also
distribute your app over a network.

maybe
 
Old 12-07-2007, 07:40 AM   #7
lamtab
Member
 
Registered: Nov 2007
Posts: 31

Original Poster
Rep: Reputation: 15
I know i can use sockets but i have to use fifos
 
Old 12-07-2007, 07:47 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
well, it's easy, make a fifo
one process opens it for reading one for writing.
just like ordinary files.

e.g: even easier
open two xterms.

term 1
Code:
mkfifo
cat FIFO
term 2
Code:
cat > FIFO
... start typing here ...
 
Old 12-07-2007, 08:44 AM   #9
lamtab
Member
 
Registered: Nov 2007
Posts: 31

Original Poster
Rep: Reputation: 15
thx man but i want c code and communication between father and his 3 children
 
Old 12-07-2007, 08:58 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
well, that is an example of how simple it is if you can do it with shell.

is the communication two way or one way?
well assume it's two way you must have two FIFOs per pair,
parents read/write child write/read which is 6 pipes.
but then the parent will probly need to use select to check which is ready.
(A pipe can only connect with ONE reader and ONE writer at a time)

or, you could have one pair of FIFOs and say use the PID of the child as an
IP address as it were to prepend each message,
simpler solution.




is this homework?

Last edited by bigearsbilly; 12-07-2007 at 08:59 AM.
 
Old 12-07-2007, 10:16 AM   #11
lamtab
Member
 
Registered: Nov 2007
Posts: 31

Original Poster
Rep: Reputation: 15
Didnt know about select i'll try it. No it's not homework just learning about fifos
 
Old 12-10-2007, 03:48 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
ok, no offence.

an interesting use for a FIFO often seen in books, is for your
mail .signature file (if this applies).
you replace it with a FIFO and a serving process that gives a
random quote (often the fortune program) every time the
FIFO is read, nifty eh?

you might like to try it.
 
Old 12-11-2007, 10:44 AM   #13
lamtab
Member
 
Registered: Nov 2007
Posts: 31

Original Poster
Rep: Reputation: 15
no offense taken. thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make tar,zip,rar play nice with named pipes? Pi Man Programming 8 10-22-2007 04:14 PM
FC4 System Crash bcs chown -R named:named extend joangopan Fedora 1 09-09-2007 02:46 AM
chown -R named:named /var/named crash the system? joangopan Fedora 2 09-09-2007 02:46 AM
Write command in named pipes sahel Programming 7 12-28-2005 06:05 AM
Use of Named Pipes casey0999 Linux - Software 3 08-03-2003 01:21 PM


All times are GMT -5. The time now is 12:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration