使用settimeofday设置时间后, 重启系统时间并没有设置成功是因为settimeofday,紧紧能设置系统时间并不能设置硬件时间,
开机时linux会从硬件的rtc的时钟芯片中获取一次硬件时间,然后以此为基础来运行系统时间。
settimeofday 相当于date shell命令,紧紧能设置系统时间。
如果想设置硬件时间,需要一个hwclock -w shell原理的函数来写硬件的rtc。
扒一扒 busybox的 hwclock.c源码(http://www.codeforge.cn/read/97203/hwclock.c__html)
可以知道这么抄写功能源码:
static void write_rtc(time_t t, int utc)
{
int rtc;
struct tm tm;
if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 )
{
if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
bb_perror_msg_and_die ( "Could not access RTC" );
}
tm = *( utc ? gmtime ( &t ) : localtime ( &t ));
tm. tm_isdst = 0;
if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )
bb_perror_msg_and_die ( "Could not set the RTC time" );
close ( rtc );
}
也可执行system系统调用。
最后
以上就是完美棒棒糖最近收集整理的关于嵌入式linux C语言 如何同步系统时钟到硬件时钟的全部内容,更多相关嵌入式linux内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复