Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
C++ Programming for Linux Systems

You're reading from  C++ Programming for Linux Systems

Product type Book
Published in Sep 2023
Publisher Packt
ISBN-13 9781805129004
Pages 288 pages
Edition 1st Edition
Languages
Authors (2):
Desislav Andreev Desislav Andreev
Profile icon Desislav Andreev
Stanimir Lukanov Stanimir Lukanov
Profile icon Stanimir Lukanov
View More author details

Table of Contents (15) Chapters

Preface 1. Part 1:Securing the Fundamentals
2. Chapter 1: Getting Started with Linux Systems and the POSIX Standard 3. Chapter 2: Learning More about Process Management 4. Chapter 3: Navigating through the Filesystems 5. Chapter 4: Diving Deep into the C++ Object 6. Chapter 5: Handling Errors with C++ 7. Part 2:Advanced Techniques for System Programming
8. Chapter 6: Concurrent System Programming with C++ 9. Chapter 7: Proceeding with Inter-Process Communication 10. Chapter 8: Using Clocks, Timers, and Signals in Linux 11. Chapter 9: Understanding the C++ Memory Model 12. Chapter 10: Using Coroutines in C++ for System Programming 13. Index 14. Other Books You May Enjoy

Using Clocks, Timers, and Signals in Linux

In this chapter, we will commence by exploring the various timers available in the Linux environment. Subsequently, we will delve into the significance of the clock epoch and delve into the concept of UNIX time. Following this, we will unveil the methodology for employing POSIX in Linux to precisely measure time intervals. Transitioning further, we will uncover the realm of std::chrono and examine the capabilities that C++ offers for effective time-related operations. Our journey then progresses to a comprehensive examination of duration, timepoints, and clocks as delineated within the std::chrono framework. Venturing onward, we will acquaint ourselves with the diverse array of clocks at our disposal within std::chrono. As we navigate our path, we will take our initial steps into harnessing the calendar functionalities provided by std::chrono. In the final leg of our exploration, we will become familiar with time zones and refine our expertise...

Technical requirements

All examples in this chapter have been tested in an environment with the following configuration:

Handling time in Linux

Timing is an essential aspect of any computer system, and Linux is no exception. In Linux, there are different types of timers available, each designed to handle specific tasks and requirements.

These timers can be used to measure the execution time of programs, schedule tasks, trigger events, and more. In this section, we’ll explore the different types of timers available in Linux and how to use them effectively.

Here are the different kinds of timers used in the Linux system:

  • System timers: The Linux kernel uses system timers to keep track of the time and schedule various tasks. System timers are used to measure the system uptime, delay, and timeouts. The most important system timer in Linux is the Jiffies timer, which increments by 1 with every tick of the system clock. The Jiffies timer is used to track the time elapsed since the system booted up, and it is frequently used by various kernel modules and drivers.
  • Real-Time Clock (RTC...

Handling time in C++

While POSIX timers have their own merits, in C++ there are libraries that provide higher-level and more portable solutions for timing and time-related operations.

One good example of such a library is std::chrono. This is a C++ library that provides a set of utilities for working with time-related operations and measurements. It is part of the Standard Library and is included in the <chrono> header. The std::chrono library provides a flexible and type-safe mechanism for representing and manipulating time durations, time points, clocks, and time-related operations. By using std::chrono, you will benefit from the standardization, type safety, flexibility, and integration that comes with the C++ Standard Library. Some of the advantages of std::chrono compared to the traditional POSIX approach are as follows:

  • Standardization: std::chrono is part of the C++ Standard Library, making it a cross-platform solution that works consistently across different...

Using clocks, timers, and ratios

Before getting into more examples with clocks and timers, we first have to get a better understanding of how the chrono library defines a duration.

As we saw in the previous example, a duration is the distance between two points of time, called timepoints. In our previous example, these were the start and end timepoints.

Figure 8.1 – Timepoint and duration

Figure 8.1 – Timepoint and duration

The duration itself is a combination of the count of ticks and a fraction that represents the time in seconds from one tick to the next. The fraction is represented by the std::ratio class. Here are some examples:

using namespace std::chrono;
constexpr std::chrono::duration<int, std::ratio<1>>
  six_minutes_1{360};
constexpr std::chrono::duration<double, std::ratio<3600>>
  six_minutes_2{0.1};
constexpr std::chrono::minutes six_minutes_3{6};
constexpr auto six_minutes_4{6min};
std::cout << six_minutes_1 <...

Using calendar and time zone capabilities

C++20 introduces brand-new support for calendar and time zone operations to the standard. When we talk about calendar operations, this means operations in days, months, and years. They, together with the time zone notion, allow conversions of time between different time zones taking into account time zone adjustments such as daylight saving time.

Let’s define a date and print it with the help of the chrono library:

using namespace std::chrono;
year theYear{2023};
month theMonth{8};
day theDay{4};
std::cout << "Year: " << theYear;
std::cout << ", Month: " << theMonth;
std::cout << ", Day: " << theDay << '\n';

As you can see, the std::chrono namespace provides year, month, and day classes, which make it easy to work with dates. The benefit of these classes is that they provide strict type and boundary checks, some operators for summation and subtraction...

Summary

In this chapter, we explored the different timers available within the Linux environment. Subsequently, we gained an understanding of the significance behind the clock epoch and the concept of UNIX time. Following this, we delved into the practical implementation of POSIX in Linux for accurate time measurement. Additionally, we investigated the realm of std::chrono and examined the array of capabilities that C++ affords for effective time-related operations. Our exploration then took us on a detailed journey through duration, timepoints, and clocks as they are defined within the std::chrono framework. Moving forward, we acquainted ourselves with the various clock types at our disposal within std::chrono. As our journey continued, we initiated our exploration into the calendar capabilities presented by std::chrono. Finally, we developed familiarity with time zones and honed our proficiency in executing seamless time conversions utilizing the tools offered by std::chrono. Now...

lock icon The rest of the chapter is locked
You have been reading a chapter from
C++ Programming for Linux Systems
Published in: Sep 2023 Publisher: Packt ISBN-13: 9781805129004
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}