Apple Darwin User Manual Page 47

  • Download
  • Add to my manuals
  • Print
  • Page
    / 68
  • Table of contents
  • BOOKMARKS
  • Rated. / 5. Based on customer reviews
Page view 46
Standard Input and Output
Another common API for interprocess communication is standard input and output. This API provides a pair
of unidirectional streams. Much like socket programming, the stream-based nature of standard input and
output requires you to keep additional state information if you need to associate responses to messages with
the original message. A good way to solve that problem is through the use of message queues, as described
in Message Queues (page 49).
One thing that makes standard input and output convenient is that they are largely set up for you. Every
process in a UNIX-based system has standard input and output automatically. You can take advantage of these
to communicate between a parent process (your main application) and its children (your helper host).
To communicate with child processes in Cocoa, you should use the NSTask API, described in NSTask Class
Reference. For more information on this method, read Creating and Launching an NSTask and Ending an
NSTask.
Alternatively, in BSD tools, you can accomplish the same thing at a file descriptor level using a few low-level
APIs as shown in this example:
#include <stdlib.h>
comm_channel *startchild(char *path)
{
comm_channel *channel = malloc(sizeof(*channel));
pid_t childpid;
int in_descriptors[2];
int out_descriptors[2];
/* Create a pair of file descriptors to use for communication. */
if (pipe(in_descriptors) == -1) {
fprintf(stderr, "pipe creation failed.\n");
goto error_exit;
}
if (pipe(out_descriptors) == -1) {
fprintf(stderr, "pipe creation failed.\n");
goto error_exit;
}
/* Create a new child process. */
if ((childpid = fork()) == -1) {
fprintf(stderr, "fork failed.\n");
Cross-Architecture Plug-in Support
Using Interprocess Communication
2012-12-13 | Copyright © 2004, 2012 Apple Inc. All Rights Reserved.
47
Page view 46
1 2 ... 42 43 44 45 46 47 48 49 50 51 52 ... 67 68

Comments to this Manuals

No comments