<< Chapter < Page | Chapter >> Page > |
As both processes start, they execute an IF-THEN-ELSE and begin to perform different actions in the parent and child. Notice that
globvar and
stackvar are set to 5 in the parent, and then the parent sleeps for two seconds. At this point, the child begins executing. The values for
globvar and
stackvar are unchanged in the child process. This is because these two processes are operating in completely independent memory spaces. The child process sleeps for one second and sets its copies of the variables to 100. Next, the child process calls the
execl( )
function to overwrite its memory space with the UNIX date program. Note that the
execl( )
never returns; the date program takes over all of the resources of the child process. If you were to do a
ps at this moment in time, you still see two processes on the system but process 19336 would be called “date.” The date command executes, and you can see its output.
It’s not uncommon for a human parent process to “fork” and create a human child process that initially seems to have the same identity as the parent. It’s also not uncommon for the child process to change its overall identity to be something very different from the parent at some later point. Usually human children wait 13 years or so before this change occurs, but in UNIX, this happens in a few microseconds. So, in some ways, in UNIX, there are many parent processes that are “disappointed” because their children did not turn out like them!
The parent wakes up after a brief two-second sleep and notices that its copies of global and local variables have not been changed by the action of the child process. The parent then calls the
wait( )
function to determine if any of its children exited. The
wait( )
function returns which child has exited and the status code returned by that child process (in this case, process 19336).
A thread is different from a process. When you add threads, they are added to the existing process rather than starting in a new process. Processes start with a single thread of execution and can add or remove threads throughout the duration of the program. Unlike processes, which operate in different memory spaces, all threads in a process share the same memory space. [link] shows how the creation of a thread differs from the creation of a process. Not all of the memory space in a process is shared between all threads. In addition to the global area that is shared across all threads, each thread has a thread private area for its own local variables. It’s important for programmers to know when they are working with shared variables and when they are working with local variables.
When attempting to speed up high performance computing applications, threads have the advantage over processes in that multiple threads can cooperate and work on a shared data structure to hasten the computation. By dividing the work into smaller portions and assigning each smaller portion to a separate thread, the total work can be completed more quickly.
Notification Switch
Would you like to follow the 'High performance computing' conversation and receive update notifications?