Linux System Programming: Talking Directly to the Kernel and C Library

Linux System Programming: Talking Directly to the Kernel and C Library

Robert Love

Language: English

Pages: 456

ISBN: 1449339530

Format: PDF / Kindle (mobi) / ePub


Write software that draws directly on services offered by the Linux kernel and core system libraries. With this comprehensive book, Linux kernel contributor Robert Love provides you with a tutorial on Linux system programming, a reference manual on Linux system calls, and an insider’s guide to writing smarter, faster code.

Love clearly distinguishes between POSIX standard functions and special services offered only by Linux. With a new chapter on multithreading, this updated and expanded edition provides an in-depth look at Linux from both a theoretical and applied perspective over a wide range of programming topics, including:

  • A Linux kernel, C library, and C compiler overview
  • Basic I/O operations, such as reading from and writing to files
  • Advanced I/O interfaces, memory mappings, and optimization techniques
  • The family of system calls for basic process management
  • Advanced process management, including real-time processes
  • Thread concepts, multithreaded programming, and Pthreads
  • File and directory management
  • Interfaces for allocating memory and optimizing memory access
  • Basic and advanced signal interfaces, and their role on the system
  • Clock management, including POSIX clocks and high-resolution timers

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

the e, and, finally, the g. When there are no more characters left to read, the device returns end-of-file (EOF). Missing a character, or reading them in any other order, would make little sense. Character devices are accessed via character device files. A block device, in contrast, is accessed as an array of bytes. The device driver maps the bytes over a seekable device, and user space is free to access any valid bytes in the array, in any order—it might read byte 12, then byte 7, and then byte

returns -1 and errno is set as appropriate. 42 | Chapter 2: File I/O For example, to set the file position of fd to 1825: off_t ret; ret = lseek (fd, (off_t) 1825, SEEK_SET); if (ret == (off_t) -1) /* error */ Alternatively, to set the file position of fd to the end of the file: off_t ret; ret = lseek (fd, 0, SEEK_END); if (ret == (off_t) -1) /* error */ As lseek( ) returns the updated file position, it can be used to find the current file position via a SEEK_CUR to zero: int pos; pos =

or more of the parameters addr, len, or off are invalid. ENFILE The system-wide limit on open files has been reached. ENODEV The filesystem on which the file to map resides does not support memory mapping. ENOMEM The process does not have enough memory. EOVERFLOW The result of addr+len exceeds the size of the address space. EPERM PROT_EXEC was given, but the filesystem is mounted noexec. Associated signals Two signals are associated with mapped regions: SIGBUS This signal is generated when

useful data. Thus, it makes sense for a streaming video application to periodically request that streamed data be evicted from the cache. A process that intends to read in an entire file can provide the POSIX_FADV_SEQUENTIAL hint, instructing the kernel to perform aggressive readahead. Conversely, a process that knows it is going to access a file randomly, seeking to and fro, can provide the POSIX_FADV_RANDOM hint, instructing the kernel that readahead will be nothing but worthless overhead.

process group and session, both of which have it as leader. This also ensures that the process has no associated controlling terminal (as the process just created a new session, and will not assign one). 4. Change the working directory to the root directory via chdir( ). This is done because the inherited working directory can be anywhere on the filesystem. Daemons tend to run for the duration of the system’s uptime, and you don’t want to keep some random directory open, and thus prevent an

Download sample

Download