13.1. Running Subprocesses with C Functions

Before immediately resorting to Cocoa solutions for running external scripts and programs, you may be wise to remember one thing: Objective-C is a superset of C. Functions are available in the C standard library that may already be powerful enough to serve your purposes, depending on the problem at hand.

If you do not need to perform any complex data transactions with an external program, you may find the C functions simpler to use than the Cocoa classes, which require a number of initialization steps. This section introduces the C functions available to you for starting external scripts and programs, or processes, from your Cocoa applications.


This chapter is mostly concerned with starting subprocesses from within a Cocoa program. A subprocess is a completely separate program, and does not have access to the same memory address space as the program initiating it.

This contrasts with threads, which are similar, but share the memory space (for example, data structures, variables) of the initiating code.

In short, a thread can use and modify the data stored in your Cocoa application, and a subprocess cannot.

13.1.1. Using the system Function

The system function from the C standard library is probably the easiest way to start a subprocess from your Cocoa application. It takes a single null-terminated C string as an argument, and returns an integer error code. The string is simply a shell script, which will be executed by the Bash ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.