?
Solved

Communication between processes

Posted on 2005-03-01
11
Medium Priority
?
266 Views
Last Modified: 2009-07-29
One script (A) call another script (B) during running

my script B is perform something according to his input.
What I want is when script B receive something ,to send this input as message to script A ,that will wait for him and will print on the screen that input .

For example ,script B received "p" ,so script B send to script A "p" ,and Script A should print on the screen "A"

Is this possible to implement ?
Thanks ,for helping.



0
Comment
Question by:Inga_dusha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 13427661
yes, see
perldoc perlipc
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13427664
see
perldoc perlipc
for complex implementations

if you want scriptA to read scriptB's stdout,
##scriptA
open(SCPTB,"scriptB | ") or die("$! : Cant start scriptB") ;
while(<SCPTB>) {
##do soemthing with $_
print ;
}

Manav
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13427669
sorry, didnt read Ozo's post while commenting.

Manav
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Inga_dusha
ID: 13428087
I don't want that script A will read stdout of script B

I want that scriptB will send some message to the scriptA ,not siglnal like Ctrl ^C ,or something like this .

0
 
LVL 84

Accepted Solution

by:
ozo earned 600 total points
ID: 13428540
Something like this?

ScriptA:
use IO::Socket;
 my $sock = new IO::Socket::INET (
                                  LocalHost => '127.0.0.1',
                                  LocalPort => '7070',
                                  Proto => 'tcp',
                                  Listen => 1,
                                  Reuse => 1,
                                 );
 die "Could not create socket: $!\n" unless $sock;

while( my $new_sock = $sock->accept() ){

 while(defined($_=<$new_sock>)) {
   print $_;
 }
}
close($sock);


sccriptB:
use IO::Socket;
 my $sock = new IO::Socket::INET (
                                  PeerAddr => '127.0.0.1',
                                  PeerPort => '7070',
                                  Proto => 'tcp',
                                 );
 die "Could not create socket: $!\n" unless $sock;
 print $sock "Hello there!\n";
 close($sock);
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13428694
Is it very necessary that we use sockets here. The same can be achieved using FIFO list with blocking read.

create a FIFO first using mkfifo command(say 'mkfifio abc')
ScriptA
#####
#!/usr/bin/perl
use strict ;
use warnings ;
while(1) {
open(FIF,"< abc") or die("$! : Cant open abc") ;
while(<FIF>) {
print "Entered : $_" ;
}
close(FIF) ;
}

Then run this scriptA in the background.
In scriptB,just write to this file. Whenever a write is done on this, scriptA will spew "Entered : <the_line_put_by_script_b>"

Sample run
########
[oracle@localhost temp]$ ./p1.pl&
[1] 11174
[oracle@localhost temp]$ ps
  PID TTY          TIME CMD
10821 pts/1    00:00:00 bash
11174 pts/1    00:00:00 p1.pl
11175 pts/1    00:00:00 ps
[oracle@localhost temp]$ echo "abc" > abc
Entered : abc
[oracle@localhost temp]$ echo "efg" > abc
Entered : efg
[oracle@localhost temp]$ echo "This is another line" > abc
Entered : This is another line
[oracle@localhost temp]$


Manav
0
 

Author Comment

by:Inga_dusha
ID: 13430381
Thanks a lot ,ozo ,
your solution is perfect for me ,
maybe you know ,

why I receive the following output on the screen

accept() on closed socket GEN0 at /usr/lib/perl5/5.8.0/i586-linux-thread-multi/IO/Socket.pm line 183, <GEN1> line 4.


manav_mathur, also thanks for your time .

0
 
LVL 84

Expert Comment

by:ozo
ID: 13435398
What are you using for ScriptA?
what are you doing with GEN0 and GEN1 ?
0
 

Author Comment

by:Inga_dusha
ID: 13436867
I am really new in Perl ,
In Script A i create with fork another proccess that call script B ,
if scriptB send "q" than I make close to the socket in this way:

while( $new_sock = $sock->accept() ){
while(defined($_=<$new_sock>)) {
print $_;
if ($_ eq "A\n") { print "description A\n"; }
elsif ($_ eq "B\n") { print "description B\n"; }
elsif ($_ eq "C\n") {  print "description C\n"; }
else {close($sock); } #Here i am receive an error
}
}

0
 
LVL 84

Expert Comment

by:ozo
ID: 13436895
SOCK: while( $new_sock = $sock->accept() ){
    while( defined($_=<$new_sock>) ){
        print $_;
        if ($_ eq "A\n") { print "description A\n"; }
        elsif ($_ eq "B\n") { print "description B\n"; }
        elsif ($_ eq "C\n") {  print "description C\n"; }
        elsif( /^q/i ){ last SOCK; }
    }
}
close $sock;
0
 

Author Comment

by:Inga_dusha
ID: 13437893
ozo,thanks again
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question