Danh mục

Advanced Linux Programming: 11-A Sample GNU/Linux Application

Số trang: 40      Loại file: pdf      Dung lượng: 291.23 KB      Lượt xem: 8      Lượt tải: 0    
tailieu_vip

Xem trước 4 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: 11-a sample gnu/linux application, 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: 11-A Sample GNU/Linux Application 11 A Sample GNU/Linux ApplicationT HIS CHAPTER IS WHERE IT ALL COMES TOGETHER.WE’LL DESCRIBE and implement acomplete GNU/Linux program that incorporates many of the techniques described inthis book.The program provides information about the system it’s running on via aWeb interface. The program is a complete demonstration of some of the methods we’ve describedfor GNU/Linux programming and illustrated in shorter programs.This program iswritten more like “real-world” code, unlike most of the code listings that we presentedin previous chapters. It can serve as a jumping-off point for your own GNU/Linuxprograms.11.1 OverviewThe example program is part of a system for monitoring a running GNU/Linuxsystem. It includes these features: n The program incorporates a minimal Web server. Local or remote clients access system information by requesting Web pages from the server via HTTP. n The program does not serve static HTML pages. Instead, the pages are generated on the fly by modules, each of which provides a page summarizing one aspect of the system’s state.220 Chapter 11 A Sample GNU/Linux Application n Modules are not linked statically into the server executable. Instead, they are loaded dynamically from shared libraries. Modules can be added, removed, or replaced while the server is running. n The server services each connection in a child process.This enables the server to remain responsive even when individual requests take a while to complete, and it shields the server from failures in modules. n The server does not require superuser privilege to run (as long as it is not run on a privileged port). However, this limits the system information that it can collect. We provide four sample modules that demonstrate how modules might be written. They further illustrate some of the techniques for gathering system information pre- sented previously in this book.The time module demonstrates using the gettimeofday system call.The issue module demonstrates low-level I/O and the sendfile system call.The diskfree module demonstrates the use of fork, exec, and dup2 by running a command in a child process.The processes module demonstrates the use of the /proc file system and various system calls. 11.1.1 Caveats This program has many of the features you’d expect in an application program, such as command-line parsing and error checking. At the same time, we’ve made some simpli- fications to improve readability and to focus on the GNU/Linux-specific topics dis- cussed in this book. Bear in mind these caveats as you examine the code. n We don’t attempt to provide a full implementation of HTTP. Instead, we implement just enough for the server to interact with Web clients. A real-world program either would provide a more complete HTTP implementation or would interface with one of the various excellent Web server implementations1 available instead of providing HTTP services directly. n Similarly, we don’t aim for full compliance with HTML specifications (see http://www.w3.org/MarkUp/).We generate simple HTML output that can be handled by popular Web browsers. n The server is not tuned for high performance or minimum resource usage. In particular, we intentionally omit some of the network configuration code that you would expect in a Web server.This topic is outside the scope of this book. See one of the many excellent references on network application development, such as UNIX Network Programming,Volume 1: Networking APIs—Sockets and XTI, by W. Richard Stevens (Prentice Hall, 1997), for more information. 1.The most popular open source Web server for GNU/Linux is the Apache server, available from http://www.apache.org. 11.2 Implementation 221 n We make no attempt to regulate the resources (number of processes, memory use, and so on) consumed by the server or its modules. Many multiprocess Web server implementations service connections using a fixed pool of processes rather than creating a new child process for each connection. n The server loads the shared library for a server module each time it is r ...

Tài liệu được xem nhiều: