{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


alp-ch03-processes - 04 0430 CH03 10:13 AM Page 45 3...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Processes 3 A RUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS . If you have two terminal windows showing on your screen, then you are probably running the same terminal program twice—you have two terminal processes. Each terminal window is probably running a shell; each running shell is another process.When you invoke a command from a shell, the corresponding program is executed in a new process; the shell process resumes when that process completes. Advanced programmers often use multiple cooperating processes in a single appli- cation to enable the application to do more than one thing at once, to increase application robustness, and to make use of already-existing programs. Most of the process manipulation functions described in this chapter are similar to those on other UNIX systems. Most are declared in the header file <unistd.h> ; check the man page for each function to be sure. 3.1 Looking at Processes Even as you sit down at your computer, there are processes running. Every executing program uses one or more processes. Let’s start by taking a look at the processes already on your computer.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
46 Chapter 3 Processes 3.1.1 Process IDs Each process in a Linux system is identified by its unique process ID , sometimes referred to as pid . Process IDs are 16-bit numbers that are assigned sequentially by Linux as new processes are created. Every process also has a parent process (except the special init process, described in Section 3.4.3,“Zombie Processes”).Thus, you can think of the processes on a Linux system as arranged in a tree, with the init process at its root.The parent process ID ,or ppid , is simply the process ID of the process’s parent. When referring to process IDs in a C or C++ program, always use the pid_t typedef, which is defined in <sys/types.h> .A program can obtain the process ID of the process it’s running in with the getpid() system call, and it can obtain the process ID of its parent process with the getppid() system call. For instance, the program in Listing 3.1 prints its process ID and its parent’s process ID. Listing 3.1 ( print-pid.c ) Printing the Process ID #include <stdio.h> #include <unistd.h> int main () { printf (“The process ID is %d\n”, (int) getpid ()); printf (“The parent process ID is %d\n”, (int) getppid ()); return 0; } Observe that if you invoke this program several times, a different process ID is reported because each invocation is in a new process. However, if you invoke it every time from the same shell, the parent process ID (that is, the process ID of the shell process) is the same. 3.1.2 Viewing Active Processes The ps command displays the processes that are running on your system.The GNU/Linux version of ps has lots of options because it tries to be compatible with versions of ps on several other UNIX variants.These options control which processes are listed and what information about each is shown. By default, invoking
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 16

alp-ch03-processes - 04 0430 CH03 10:13 AM Page 45 3...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online