Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is set by using stime() and clock_settime()?

Tags:

c++

c

clock

Which clock is set by using stime() and clock_settime()? As I can read in man both can set a Linux time. Function stime() sets "the idea of time" and clock_settime() with parameter CLOCK_REALTIME can set a "system wide RTC".

1) What are these clocks?
2) Are there any differences?
3) Do they also set the HW clock?
4) If not: Does anybody know a smart way to set the hwclock in C/C++ without using ioctl()?

[Update 1]
More concrete point 4: What is the C equivalent for std::system("/sbin/hwclock -w")?

like image 870
kirsche40 Avatar asked Nov 03 '22 14:11

kirsche40


2 Answers

Sorry for this short answer i actually just wanted to do a comment, but it wont allow me that.

Well to help you about your question 4. Some quick research on Google sent me to https://github.com/karelzak/util-linux/blob/master/sys-utils/hwclock.c I havent studied this code more than 2 minutes but a quick search showed line 468:

static void set_hardware_clock(const time_t newtime, const bool universal, const bool testing)

edit:

BTW Check out the wikipedia for it http://en.wikipedia.org/wiki/Util-linux and man rtc

like image 144
MrSykkox Avatar answered Nov 15 '22 05:11

MrSykkox


I answer my question with the help provided by MrSykkox.

I read http://linux.die.net/man/4/rtc and had a look into the given sources of sys-utils. Both show me that there are a lot of different HW-clocks types like CMOS, RTC, KD and so on. In a part of sys-utils code I found some descriptors are set by opening some specific devices like /dev/rtc, /dev/rtc0, /dev/misc/rtc, /dev/port exclusive for reading. And of course I found the ioctl() stuff for setting the RTC.

Conclusion

  1. What are these clocks?
    • Linux knows not only 2 clocks. There are two classes: software and hardware clocks.
  2. Are there any differences?
    • system clock is a software clock maintained by kernel
    • RTCs can run even when the system is in a low power state (including "off"), and the system clock can't.
  3. Do they also set the HW clock?
    • system clocks set only the kernel maintained clocks
  4. Does anybody know a smart way to set the hwclock in C/C++ without using ioctl()? What is the C equivalent for std::system("/sbin/hwclock -w")?
    • RTC can only be set by most incompatible ioctl() command.
like image 35
kirsche40 Avatar answered Nov 15 '22 05:11

kirsche40