📜 ⬆️ ⬇️

Linux programming interface

Hello, dear readers! Happy holidays to you.

In the last April publication, we would like to tell you about Michael Kerrisk's wonderful book “Linux Programming Interface”, which once again returned to our field of vision due to the excellent sales of other Linux literature:


')
Of course, a complex book on system programming of 1500+ pages is a literature, frankly, an amateur. But, since the reviews about it are still enthusiastic, and we are always sorry to invest in Linux , we suggest reading its review published back in 2011.


Michael Linux’s book The Linux Programming Interface (TLPI) is primarily aimed at system programmers working with Linux, but the target audience is far from limited. Although it is a voluminous volume (they say, “you can knock a bull like this”), the book is read surprisingly easy - and if you just flip through it, and if you study every paragraph. Here you will find encyclopedic information about the interface of the Linux kernel system calls, but all the material is presented in a very accessible style. In general, this is an excellent reference that will take a worthy place in the library of any Linux specialist who works with both the kernel and user space, and will also interest some of those who are not involved in Linux professionally.

Kerrisk has been supporting Linux man-help since 2004 , so he is well aware of the Linux API. As he mentions in the preface, you may already be familiar with some of his work, namely sections 2, 3, 4, and 5 of this reference. But the book is not just a collection of articles from the reference, although thematically it is close to them in many respects. Her material is organized differently and set out much more lively, popular style.

The volume of the book is about 1500 pages, so it was not so easy to write a review on it. However, having started reading, I soon became interested in it. Kerrisk clearly describes the Linux system calls and other API elements. I decided to read only the chapters that most interested me, and skip the rest, but in the end I read much more than I was going to.

The book consists of 64 chapters, each of which has about 20 pages. Therefore, the material is easily absorbed by small fragments, you can read a book and simultaneously engage in other tasks. Kerrisk focuses on Linux, but does not forget about other types of UNIX, notes how other UNIX-like systems differ from Linux. The author carefully examines the various standards that describe Linux behavior - in particular, POSIX and the Single UNIX Specification (SUS) - indicating where Linux is observed and where these standards are not followed.

The book begins with a historical insight: from such classics as Thompson and Richie to the recent past, with the consideration of the various branches of the UNIX tree. Then the author tells what the operating system is, what role the kernel plays in it, and also presents some general concepts that are the essence of Unix (and Linux). Although for most Linux luminaries there are no secrets here, this information is useful to those who have previously worked with other operating systems. The ideas “everything is a file” and “a file is just a stream of bytes” are described so accessible that any system programmer, having become acquainted with the book, can quickly enter the “Unix way”.

After this introduction, Kerrisk proceeds to chapters, each of which describes certain aspects of the system call interface. This material takes up most of the book, with each chapter essentially self-contained. Subsequent chapters are based on previous ones, and their text is replete with references to other sections. In the preface, Kerris mentions that he tried to minimize the number of advanced links, but he clearly did not succeed, since there were no fewer advanced links than normal ones.

Each chapter provides clear code samples that are easy to read. It is important that these examples help a lot in practicing the topic, and some of the listings can easily be finished with good utilities. All source code is available on man7.org/tlpi and is free, released under the Affero GPLv3 license. In addition, in each chapter there are exercises for the reader, the decisions of some of them are given in the appendix.

So what is the book about? It is easy to say “about everything at once”, but it will be a kind of excuse, and inaccurate. In the book you will find many chapters on files, file I / O, extended attributes, access control lists (ACL). There is a chapter on directories and links, and another chapter deals with the inotify call, which allows you to receive notifications about events occurring in files.

Here you will find many chapters on processes, threads, signals, whole chapters that cover process groups, sessions, process priority and planning. I found the chapter on creating protected privileged programs especially interesting. There are two chapters on shared libraries, with the first of these chapters focusing on the basic ideas behind these libraries, as well as on the creation of shared libraries in principle; in turn, the second of these chapters is devoted to the dlopen() system call and similar ones.

Perhaps there are too many chapters on interprocess communication (IPC) in the book, one chapter is devoted to each IPC mechanism in System V (shared memory, message queues and semaphores). There is also a chapter on each of the three POSIX variants of these IPC types. Both POSIX and System V IPC have their own introductory chapter, except for the chapters that provide details on each type. Between the two sections on the mechanisms of System V and POSIX IPC, two chapters on memory mapping and operations on virtual memory are crammed in, which it might be better to put in another part of the book. There is also a chapter that introduces IPC and a chapter on more traditional Unix channels and FIFO queues. There are a total of twelve chapters related to IPC, after which the author begins to examine the socket API.

After IPC, there is a chapter on locking files, and then six chapters on sockets. These chapters cover Unix sockets and Internet sockets, as well as server design, and highlight sockets related topics. The book ends with chapters on terminals and pseudo-terminals, between which the chapter “Alternative I / O Models” somehow got mixed up. This is an interesting chapter that deals with select() , poll() , epoll() , signal-driven I / O, and some other topics. But it is still located a bit strange.

Of course, the content of the book is not limited to this. After reading it, it is especially impressive how great the Linux / Unix API is. The book also discusses some of the flaws and obsolete parts that are preserved in the API. Kerrisk does not hesitate to make such comments in the right places: "In general, it is usually better not to use System V message queues."

There are two topics that I really wanted to read about, but which are poorly disclosed in the book. First, these are containers and namespaces, which are only casually mentioned when discussing the clone() system call flags. It seemed more strange that the book hardly mentioned the system call ptrace() . In those few places where he still appears, readers are referred to the man-help ptrace (2).

Of course, in addition to the system call interface, other elements of the Linux API could be considered - sysf, splice() and perf are remembered offhand - but Kerrisk obviously had to choose what to include in the book and what to refuse. In principle, the author did well. Some technical books on Linux quickly become obsolete, but in the case of this work, the problem of obsolescence is not as acute as, for example, the book about the kernel device.

I still have some complaints about the book, but they are all irrelevant. First of all, I would like to say that the numerous “digressions” in the book are lyrical digressions. They contain additional information that is not critical for understanding the topic. In my opinion, many of them would be better woven into the main text.

The book is useful, first of all, to system programmers and application developers, but the target audience is not limited to them. Kernel developers will be able to clarify whether their new features (or fixes) conflict with the API structure. Programmers who are predominantly working on Unix-like systems will be able to make their code more portable using this book. I think the reader will regularly return to this book with benefit. I suppose everyone who is truly interested in Linux programming will agree with me.

Source: https://habr.com/ru/post/282776/


All Articles