An application as a set of processes
This idea of using a set of cooperating processes isn't
limited to the OS system processes.
Your
applications should be written in exactly the same way. You
might have some driver process that gathers data from some
hardware and then needs to pass that data on to other
processes, which then act on that data.
Let's use the example of an application that's monitoring the level of water in a reservoir. Should the water level rise too high, then you'll want to alert an operator as well as open some flow-control valve.
In terms of hardware, you'll have some water-level sensor tied to an I/O board in a computer. If the sensor detects some water, it will cause the I/O board to generate an interrupt.
The software consists of a driver process that talks to the I/O board and contains an interrupt service thread (IST) to deal with the board's interrupt. You'll also have a GUI process that will display an alarm window when told to do so by the driver, and finally, another driver process that will open/close the flow-control valve.
Why break this application into multiple processes? Why not have everything done in one process? There are several reasons:
- Each process lives in its own protected memory space.
If there's a bug such that a pointer has a value
that isn't valid for the process, then when the pointer is
next used, the hardware will generate a fault, which the
kernel handles (the kernel will set the SIGSEGV signal on the process).
This approach has two benefits. The first is that a stray pointer won't cause one process to overwrite the memory of another process. The implications are that one process can go bad while other processes keep running.
The second benefit is that the fault will occur precisely when the pointer is used, not when it's overwriting some other process's memory. If a pointer were allowed to overwrite another process's memory, then the problem wouldn't manifest itself until later and would therefore be much harder to debug.
- It's very easy to add or remove processes from an application as need be. This implies that applications can be made scalable—adding new features is simply a matter of adding processes.
- Processes can be started and stopped on the fly, which comes in handy for dynamic upgrading or simply for stopping an offending process.
- Processing can be easily distributed across multiple processors in a networked environment.
- The code for a process is much simpler if it concentrates on doing a single job. For example, a single process that acts as a driver, a GUI front-end, and a data logger would be fairly complex to build and maintain. This complexity would increase the chances of a bug, and any such bug would likely affect all the activities being done by the process.
- Different programmers can work on different processes without fear of overwriting each other's work.