LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 12-11-2007, 06:14 AM   #1
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 190

Rep: Reputation: 40
Question Bash - named pipes - mathew42 insight please


Totally new to pipes, but an avid reader and wouldn't care to waste your original effort.
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
I am unsure why I should get the error messages from the kills.
Quote:
1 from fifo_one: "1 this is one"; from fifo_two: "1 this is two"
2 from fifo_one: "2 this is one"; from fifo_two: "2 this is two"
3 from fifo_one: "3 this is one"; from fifo_two: "3 this is two"
./pipesh: line 21: kill: %1: no such job
./pipesh: line 22: kill: %2: no such job
I did put a couple of sleeps into the code and hit it with a deft couple of ^C^C and re-ran the code to get
Quote:
mkfifo: cannot create fifo `fifo_one': File exists
mkfifo: cannot create fifo `fifo_two': File exists
1 from fifo_one: "1 this is one"; from fifo_two: "1 this is two"
. . .
This did at least confirms to me that the kills had been working correctly, despite the error messages which caused me some small doubts, and the original pipes seemed to be re-used when it wasn't able to create them anew due to my intervention. Subsequently of course, keeping my fingers out, the runs were clean other than for the unexplained kill error messages.

You might take some comfort from the fact that this is probably going to take me a couple of hours at least to get my mind around fully. I know that I could always re-direct std error to /dev/null, but I would prefer to understand it before rashly sweeping trouble under the carpet.

Having once sorted it all out in my mind, I'll be looking to see where I might naturally use named pipes to advantage. Oh, and I shall be consulting our friend Google also.

Thanks + =
 
Old 12-11-2007, 06:38 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 21,610
Blog Entries: 47

Rep: Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413
Quote:
Originally Posted by PAix View Post
I am unsure why I should get the error messages from the kills.
Because job control is not enabled for non-interactive shell so the jobspec "%1" doesn't refer to anything in particular?
 
Old 12-11-2007, 07:52 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,002

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
In scripts you can get the process ID of the last job running in background by the $! variable, e.g. in your example could be
Code:
# create infinite input to fifos for illustration...
yes |sed 's/y/this is one/' | nl > fifo_one &
pid_one=$!
yes |sed 's/y/this is two/' | nl > fifo_two &
pid_two=$!

# kill backgrounded processes
kill -9 $pid_one $pid_two
$! is the process ID which is different from the job ID (availalble only in interactive shells, as unSpawn already pointed out).
 
Old 12-11-2007, 09:09 AM   #4
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 190

Original Poster
Rep: Reputation: 40
Thanks UnSpawn and Colucix,
All clear to me now, I'm very grateful for the enlightenment from you both.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
named pipes lamtab Programming 12 12-11-2007 10:44 AM
Make tar,zip,rar play nice with named pipes? Pi Man Programming 8 10-22-2007 04:14 PM
Write command in named pipes sahel Programming 7 12-28-2005 06:05 AM
pipes and bash scripts twistedpair Linux - General 0 06-03-2004 09:59 PM
Use of Named Pipes casey0999 Linux - Software 3 08-03-2003 01:21 PM


All times are GMT -5. The time now is 03:03 PM.

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
Open Source Consulting | Domain Registration