Advanced Linux Programming: 3-Processes
Số trang: 16
Loại file: pdf
Dung lượng: 236.09 KB
Lượt xem: 8
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu advanced linux programming: 3-processes, công nghệ thông tin, hệ điều hành phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Advanced Linux Programming: 3-Processes 3 ProcessesA RUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have twoterminal windows showing on your screen, then you are probably running thesame terminal program twice—you have two terminal processes. Each terminalwindow is probably running a shell; each running shell is another process.When youinvoke a command from a shell, the corresponding program is executed in a newprocess; 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 increaseapplication robustness, and to make use of already-existing programs. Most of the process manipulation functions described in this chapter are similar tothose on other UNIX systems. Most are declared in the header file ; checkthe man page for each function to be sure.3.1 Looking at ProcessesEven as you sit down at your computer, there are processes running. Every executingprogram uses one or more processes. Let’s start by taking a look at the processesalready on your computer.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 . 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 #include int main () { printf (“The process ID is %d ”, (int) getpid ()); printf (“The parent process ID is %d ”, (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 ps displays the processes controlled by the terminal or terminal window in which ps is invoked. For example: % ps PID TTY TIME CMD 21693 pts/8 00:00:00 bash 21694 pts/8 00:00:00 ps 3.1 Looking at Processes 47This invocation of ps shows two processes.The first, bash, is the shell running on thisterminal.The second is the running instance of the ps program itself.The first col-umn, labeled PID, displays the process ID of each. For a more detailed look at what’s running on your GNU/Linux system, invokethis: % ps -e -o pid,ppid,commandThe -e option instructs ps to display all processes running on the system.The-o pid,ppid,command option tells ps what information to show about each process—in this case, the process ID, the parent process ID, and the command running in thisprocess. ps Output Formats With the -o option to the ps command, you specify the information about processes that you want in the output as a comma-separated list. For example, ps -o pid,user,start_time,command displays the process ID, the name of the user owning the process, the wall clock time at which the process started, and the command running in the process. See the man page for ps for the full list of field codes. You can use the -f (full listing), -l (long listing), or -j (jobs listing) options instead to get three differ- ...
Nội dung trích xuất từ tài liệu:
Advanced Linux Programming: 3-Processes 3 ProcessesA RUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have twoterminal windows showing on your screen, then you are probably running thesame terminal program twice—you have two terminal processes. Each terminalwindow is probably running a shell; each running shell is another process.When youinvoke a command from a shell, the corresponding program is executed in a newprocess; 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 increaseapplication robustness, and to make use of already-existing programs. Most of the process manipulation functions described in this chapter are similar tothose on other UNIX systems. Most are declared in the header file ; checkthe man page for each function to be sure.3.1 Looking at ProcessesEven as you sit down at your computer, there are processes running. Every executingprogram uses one or more processes. Let’s start by taking a look at the processesalready on your computer.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 . 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 #include int main () { printf (“The process ID is %d ”, (int) getpid ()); printf (“The parent process ID is %d ”, (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 ps displays the processes controlled by the terminal or terminal window in which ps is invoked. For example: % ps PID TTY TIME CMD 21693 pts/8 00:00:00 bash 21694 pts/8 00:00:00 ps 3.1 Looking at Processes 47This invocation of ps shows two processes.The first, bash, is the shell running on thisterminal.The second is the running instance of the ps program itself.The first col-umn, labeled PID, displays the process ID of each. For a more detailed look at what’s running on your GNU/Linux system, invokethis: % ps -e -o pid,ppid,commandThe -e option instructs ps to display all processes running on the system.The-o pid,ppid,command option tells ps what information to show about each process—in this case, the process ID, the parent process ID, and the command running in thisprocess. ps Output Formats With the -o option to the ps command, you specify the information about processes that you want in the output as a comma-separated list. For example, ps -o pid,user,start_time,command displays the process ID, the name of the user owning the process, the wall clock time at which the process started, and the command running in the process. See the man page for ps for the full list of field codes. You can use the -f (full listing), -l (long listing), or -j (jobs listing) options instead to get three differ- ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin thủ thuật máy tính tin học quản trị mạng computer networkGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
24 trang 353 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 312 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 299 0 0 -
74 trang 295 0 0
-
96 trang 291 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 279 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 274 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0