Chapter 11. Tasks, Pipes, and NSTextView

In the last chapter, we built the MathPaper front and back ends. The back end (computational part) was created as a separate program called Evaluator. The front end was created using Cocoa’s powerful multiple-document architecture. In this chapter, we’ll tie the two ends together, learn more about processes, and modify the MathDocument class to display the results of calculations performed by Evaluator.

Processes, Pipes, and Resources

Here’s the big picture of the MathPaper application: one process is responsible for all interaction with the user, while other processes are responsible for performing the actual mathematical calculations. The first process is the MathPaper process itself. The other processes are Evaluator processes that will be created by the MathPaper process using the NSTask class. Communication between the MathPaper and Evaluator processes depends on the NSPipe and NSFileHandle classes. NSTask, NSPipe, and NSFileHandle are Foundation classes that we haven’t used yet; we’ll describe them before using them.

The Unix operating system has always had a rich set of functions dedicated to interprocess communication. Unix uses the fork( ) system function to “spawn” (create) child subprocesses. After a subprocess is spawned, it can change what program it is running by calling the execv( ) system function. One way of communicating with subprocesses is via a pipe , a special kind of file object used by Unix to transmit information between processes. Unix is powerful, but the Unix functions fork( ) and execv( ) can be quite complicated to use. You can learn more about these system calls by typing man fork, man pipe, and man execv at the Terminal command line.

Rather than forcing us to use these functions directly, Cocoa gives us easy-to-use Foundation classes that provide a clean, object-oriented interface.

Objects of the NSTask, NSPipe, and NSFileHandle class type will have specific tasks in MathPaper. Each NSTask object will spawn a child Evaluator process, while NSPipe objects will be used to enable the MathPaper and Evaluator processes to communicate with one another via the Unix pipe structure. (As with pipes in the physical world, anything pushed in one end of a Unix pipe comes out the other end.) To provide for two-way communication between two processes, our MathPaper application will use a pair of pipes: one for sending information from the PaperController to Evaluator, and one for getting the results back from Evaluator.

While creating and using pipes can be complicated, the NSTask and NSPipe classes do all of the hard work for you. As with any class, you need to understand the NSTask and NSPipe class interfaces in order to use them, but you need not be concerned with the details of how these classes actually work.

Each Unix pipe has two ends, represented by a pair of NSFileHandle objects. The NSFileHandle class provides an object-oriented interface for files and communications channels. You can create an NSFileHandle that is associated with a file, with a network connection, or with a pipe.

In summary, each PaperController object in MathPaper will create:

  • One NSTask object that will create and manage an Evaluator process

  • Two NSPipe objects, one for sending information from the PaperController object to an NSTask object, and a second for sending information in the opposite direction between the same two objects

  • Four NSFileHandle objects, two each for the two pipes described in the previous bullet

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.195.225