Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Communication between processes

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
Inga_dusha
Asked:
Inga_dusha
  • 4
  • 4
  • 3
1 Solution
 
ozoCommented:
yes, see
perldoc perlipc
0
 
manav_mathurCommented:
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
 
manav_mathurCommented:
sorry, didnt read Ozo's post while commenting.

Manav
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Inga_dushaAuthor Commented:
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
 
ozoCommented:
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
 
manav_mathurCommented:
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
 
Inga_dushaAuthor Commented:
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
 
ozoCommented:
What are you using for ScriptA?
what are you doing with GEN0 and GEN1 ?
0
 
Inga_dushaAuthor Commented:
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
 
ozoCommented:
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
 
Inga_dushaAuthor Commented:
ozo,thanks again
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now