Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: IPC Design problem

by anonymized user 468275 (Curate)
on Aug 25, 2015 at 10:52 UTC ( [id://1139812]=note: print w/replies, xml ) Need Help??


in reply to IPC Design problem

It's not obvious what happens when a child hits the end of the infinite while loop, but I would expect it will keep looping and become a parent of its own children. It's usually correct for child processes to explicitly exit when they are done DWIMming.

One world, one people

Replies are listed 'Best First'.
IPC named pipe issue with nstore_fd
by QuillMeantTen (Friar) on Aug 25, 2015 at 13:41 UTC

    So I think I made some headway. I have most of the main messaging daemon code down but I'm having a bit of a problem with nstore...

    here is Messaging.pm :

    here is Messenger (the little daemon class)



    and finaly the code I'm using to test them. I first run messaging.pm with one parameter : the filename for the named pipe

    then I run the following script :

    has soon as it is done writing to the pipe and has closed it I receive the following error message from my messaging daemon :

    Magic number checking on storable file failed at /usr/local/lib/perl/5.14.2/Storable.pm line 401, <$fh> chunk 1, at ../lib/Security/Monitoring/Messaging.pm line 115

    I find it most disturbing because it means that my previous tests on the Messaging.pm file must have been cheating someway, if they had not they would have created the same kind of error yet they run fine! ->those tests, I mean


    If you have an inkling of what's wrong, even an intuition I would be most grateful because I'm quite lost...

    Also in the test file I used a shotgun to scatter some kill commands everywhere in the subroutine because if I did not the test output will be out of sequence and appear three times.
    I tried to find out how I could prevent my children from doing that whatever the scheduler decided but I could not, so if you have any better/more elegant way to do it I'm all ears!

    Thank you for reading :)

      I think your read loop in the Messaging class is a little too greedy:

      ABORT:while(<$fh>){ my $data = $_; my $params = fd_retrieve($fh); ...

      This first reads a line, then does an fd_retrieve(). Your test script first does an nstore_fd(), then writes some text, i.e. exactly the other way around.

      This is not the only problem with your code. Apart from style/idiom issues, the fact that you are mixing plain text with binary text in the same channel is asking for trouble. If you reverse the writing order in your test script, the fact that the text string in our test script does not contain a newline will most likely trip up the <$fh> statement and slurp up the nstore-d data as well. (And if you have a string with more than one newline you also have a problem.) You should really consider using some appropriate encapsulation, like a hash with both the data and the parameters, written out with nstore:

      # Writing a message: my $message = { data => "Some message\nthat can contain newlines\n", params => \%params, }; nstore_fd($message, $fh); # Reading a message: my $message = fd_retrieve($fh); my $params = $message->{'params'}; my $data = $message->{'data'};

      ... Or something like that.

      Hope this helps!

      -- Steven

        Thanks for the tips, would you please expand on the "style/idiom issues"?

        I did not put much effort in applying best practices in the test script but I tried to do better in the code, what should I try to improve?

        also, I finaly nailed it down I think. I am still looking for a way to send serialized hash without bad things happening due to newline though...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1139812]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-26 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found