Excellent "Linux System Programming ... " Book

Following is an excellent Linux System Programming book that gives excellent C program examples and the book has excellent topics as chapter titles.

https://www.amazon.com/Linux-System-Programming-Techniques-proficient/dp/1789951283/

Chapters are also very well written.

I need a similar chapter topics book, that has many examples developed using latest C++ 20 features.

Is there a such book please?

Last edited on
[Small point re Amazon URL. To make the URL shorter, everything after the first ? can be discarded]
You can even cut out the "ref=" part and the URL will still be accurate, and non-tracking.
A better book to read would be one that informs you on how to interface C++ code with a C API. Armed with this information, you'd be able to cope with any 3rd party library with a C API, not just the Linux kernel.

For example, using C++ creature comforts like RAII - such as the ctor opens a file and the dtor closes the file - so you don't end up with annoying resource leaks.

Half the book you imagine would be describing
- this is a wrapper for files
- this is a wrapper for sockets
- this is a wrapper for processes

Thank you @salem c for writing:
"A better book to read would be one that informs you on how to interface C++ code with a C API."

Is there such a book?


C++ has excellent support for using a C API in C++, insanely easy:

https://forums.codeguru.com/showthread.php?145762-Using-a-C-API-with-C

The reverse ain't so easy, using C++ in a C environment:

https://stackoverflow.com/questions/51699877/how-to-create-c-api-to-c-function

Here's a metasearch that has other helpful links:
https://duckduckgo.com/?q=c%2B%2B+using+a+C+API&t=ffsb&ia=web
"A better book to read would be one that informs you on how to interface C++ code with a C API."

Is there such a book?

I can't name any books, but you need to reimplement the abstract data type.

The methods mentioned in post above just deal with using C from C++ and vice versa and don't give you anything.

Lets take a subset of the ISO C File interface. https://en.cppreference.com/w/c/io

Lets consider:
fopen(), fclose(), fflush(), fread(), fwrite(). They all operate on an opaque type FILE*. It's a pointer so the implementation can remain private.

So, how would we implement this ADT in C++? One possible implementation might be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class File {
    FILE* fp_{};
    void close() {
        if (fp_) {
            ::fclose(fp_);
            fp_ = nullptr;
        }
    }
public:
    File() = default;
    File(const char* filename, const char* mode)
      : fp_(::fopen(filename, mode)) {
    }
    ~File() {
        close();
    }
    File(const File &) = delete;
    File& operator=(const File &) = delete;
    File(File &&n) : fp_(n.fp_) {
        n.fp_ = nullptr;
    }
    File& operator=(File &&n) {
        if (this != &n) {
            close();
            fp_ = n.fp_;
            n.fp_ = null;
        }
    }
    void flush() {
        if (fp_) {
            ::fflush(fp_);
        }
    }
    size_t read(char* buffer, size_t size, size_t count) {
        if (fp_) {
            return ::fread(buffer, size, count, fp_);
        }
        return 0;
    }
    size_t write(const char* buffer, size_t size, size_t count) {
        if (fp_) {
            ::fwrite(buffer, size, count, fp_);
        }
        return 0;
    }
};

Most C APIs are abstract data types, some are object oriented; most notably the sockets library and GUI interfaces. But it doesn't matter, most of these C ADT or OO interfaces are better expressed in C++ using this technique.

I hope this helps.

BTW, I wrote this code into the editor and not compiled it. Consider any syntax errors an exercise for the reader.
Last edited on
Thanks a bunch to all of you for the excellent help and information.
Best regards,
Topic archived. No new replies allowed.