0

I'm trying to figure out which codes of c++ can cause the mode switch (user mode to kernel mode) on Linux. And I've known two things:

  1. A system call cause the mode switch: Is mode switch occur switching from user thread to kernel thread?
  2. The command strace can list all of the system calls.

Now I write such a code as below to do a test:

#include <iostream>
#include <chrono>
#include <thread>

int main()
{
    int i = 0;
    while (true) {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        i++;
    }
    return 0;
}

I compile it with the command g++ -std=c++11 test.cpp -lpthread and then I run strace -ttT ./a.out to list its system calls. Here is the output:

22:08:52.424382 prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0 <0.000009>
22:08:52.424611 brk(NULL)               = 0x55ca3968b000 <0.000009>
22:08:52.424725 brk(0x55ca396ac000)     = 0x55ca396ac000 <0.000011>
22:08:52.424887 futex(0x7f0aad83909c, FUTEX_WAKE_PRIVATE, 2147483647) = 0 <0.000010>
22:08:52.425070 futex(0x7f0aad8390a8, FUTEX_WAKE_PRIVATE, 2147483647) = 0 <0.000010>
22:08:52.425246 nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcae110cb0) = 0 <1.000142>
22:08:53.425553 nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcae110cb0) = 0 <1.000253>
22:08:54.425924 nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcae110cb0) = 0 <1.000138>
22:08:55.426177 nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcae110cb0) = 0 <1.000145>
22:08:56.426455 nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcae110cb0) = 0 <1.000141>
22:08:57.426710 nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcae110cb0) = 0 <1.000146>
22:08:58.426967 nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcae110cb0) = 0 <1.000146>

Now I have some questions:

  1. Why is there futex? As far as I know, futex is the lower mechanism to implement to thread mutex in c++11, but I haven't used any mutex here so why is there futex?
  2. We can see that there are infinite nanosleep, so does this mean that the c++11 code std::this_thread::sleep_for(std::chrono::milliseconds(1000)); will cause the mode switch from user mode to kernel mode?
  3. Is there a better way to check which c++ codes/functions will cause the mode switch or I have to use the command strace?
Yves
  • 11,597
  • 17
  • 83
  • 180
  • 2
    You're conflating your programming language with operating system features. C++ prescribes nothing about mode switches. I'm not sure what you're really after here. For example, your computer might mode switch due to hardware clock interrupts for pre-emptive multi-tasking. But this has nothing to do with the programming language that was used to generate the machine code that is executing. Help me understand what you really want to know? This is obviously platform specific so this really needs a platform tag (e.g., win32) – Wyck Jan 18 '21 at 14:24
  • 1
    I'm not sure any user-side code can deterministically cause a context switch to happen (in a future-proof way) as the kernel/libc can always be changed in such a way that it adds or removes possibilities as optimizations or necessary bugfixes. On a userspace level, you shouldn't care all too much about these context switches, so I wonder: why do you want to know? – rubenvb Jan 18 '21 at 14:25
  • Perhaps the `futex` is generated by the Runtime starting up - initialising streams etc, – Richard Critten Jan 18 '21 at 14:25
  • @Wyck I'm learning about how to profile a c++ project. And many docs and articles told me that the mode switch matters because it spends some OS resource. For example, IO can cause the mode switch. As my understanding, IO at C++ level is simply stdin, stdout, stderr and some other interfaces such as a network IO library. Then, I'm thinking if I can list all of c++ functions which can cause the mode switch... – Yves Jan 18 '21 at 14:33
  • @rubenvb Please read my last reply. – Yves Jan 18 '21 at 14:35
  • 1
    Are you sure you're thinking _mode_ switch and not _context_ switch? _mode_ switch isn't a big deal in my experience whereas a context switch involves saving and restoring thread and process state. – Wyck Jan 18 '21 at 14:35
  • @Wyck I'm sure I'm talking about mode switch. Context switch is about the execution of processes/threads right? Well, if mode switch is not a big deal, I'll leave it alone. :) – Yves Jan 18 '21 at 14:39

0 Answers0