• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTask interactive question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask interactive question


  • Subject: Re: NSTask interactive question
  • From: Chris Suter <email@hidden>
  • Date: Thu, 30 Nov 2006 09:39:06 +1100

One thing to be wary of (although it might not occur in your case), is that both writeData and availableData can block if there isn't sufficient buffer space. I'm guessing that Ruby's gets, puts and $stdout.flush might also block. So, in certain circumstances, you could get a deadlock.

You could demonstrate the deadlock by making s have more than one suitably large lines. Of course this may not be possible in your case but I think there would still be other circumstances where you'd get a deadlock (they're slightly more difficult to demonstrate).

- Chris

On 30/11/2006, at 7:17 AM, Shawn Erickson wrote:

On 11/29/06, Matt Neuburg <email@hidden> wrote:
I have an NSTask which is a Ruby interactive script. The Ruby script just
loops on standard input, like this:


while (s = gets)
  puts gk.crunch(s) # never mind what that does
  $stdout.flush
end

I'm launching this NSTask and then I'm repeatedly sending input at it and
retrieving the output, all in a single routine, without using notifications,
like this:


NSString* input = [NSString stringWithFormat: @"%@\n", s];
NSFileHandle* fh = [[task standardInput] fileHandleForWriting];
[fh writeData:[input dataUsingEncoding: NSMacOSRomanStringEncoding]];
NSFileHandle* fh2 = [[task standardOutput] fileHandleForReading];
NSString* output = [[[NSString alloc] initWithData: [fh2 availableData]
encoding: NSUTF8StringEncoding] autorelease];


I'm just doing that over and over again, supplying input and retrieving
output in a single step. It's working fine. My question is: even though it's
working, am I in fact cruising for a bruising? Must I instead use the more
complicated readInBackgroundAndNotify architecture? m.

Nothing in the above is ensuring that you get a complete response from the task since availableData may be a subset of what the task has so far been able to generate after you send out the input. Also nothing ensures that you might not flood the task with input so that the output you read is unrelated to what you just sent down.

You don't need to use read in background but you do need to ensure you
are in sync with your task in some fashion (note read in background
wouldn't solve this issue for you either).

-Shawn
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
systems.com


This email sent to email@hidden


--
Coriolis Systems Limited is a limited company incorporated in England and Wales
Company No. 5061807 Registered Office: 10 Oxford Street, Southampton, SO14 3DJ



Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Re: NSTask interactive question
      • From: "Shawn Erickson" <email@hidden>
References: 
 >NSTask interactive question (From: Matt Neuburg <email@hidden>)
 >Re: NSTask interactive question (From: "Shawn Erickson" <email@hidden>)

  • Prev by Date: Re: Multithreading/Memory Management Problem
  • Next by Date: Re: Re: NSTask interactive question
  • Previous by thread: Re: NSTask interactive question
  • Next by thread: Re: Re: NSTask interactive question
  • Index(es):
    • Date
    • Thread