首页 > 系统相关 >《Unix/Linux系统编程》学习笔记8

《Unix/Linux系统编程》学习笔记8

时间:2022-10-22 16:44:26浏览次数:58  
标签:struct tv 编程 timer Unix sec time Linux include

第五章 定时器及时钟服务

一、知识点归纳

(一)硬件定时器

  定时器是由时钟源和可编 程计数器组成的硬件设备。时钟源 通常是一个晶体振荡器,会产生周期性电信号,以料青确的频率驱动计数器。使用一个倒计时值对计数器进行编程,每个时钟信号减1。当计 改减为0时,计数器向CPU生成一个定时器中断,将计数值重新加载到计数器中,并重复复倒计时。计数器周期称为定时器刻店度,是系统的基本计时单元。

(二)个人计算机定时器

(三)CPU操作

(四)中断处理

(五)时钟服务函数

1.gettimeofday-settimeofday

#include<sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);

这些是对 Linux 内核的系统调用。第一个参数 tv 指向一个 timeval 结构体。

struct timeval{
    time_t         tv_sec;    /* seconds */
    suseconds_t    tv_usec;   /* microseconds */
};

第二个参数 timezone 已过期,应设置为 NULL。gettimeofday() 函数用于返回当前时间(当前秒的秒和微秒)。settimeofday() 函数用于设置当前时间。在 Unix/Linux 中,时间表示自 1970年1月1日 00:00:00 起经过的秒数。它可以通过库函数 ctime(&time) 转换为日历形式。下面给出了 gettimeofday() 函数和settimeofday() 函数的示例。

(1)gettimeofday 系统调用

(2)settimeofday 系统调用

2.time 系统调用

3.times 系统调用

times 系统调用

clock_t times(struct tms *bus);

可用于获取某进程的具体执行时间。它将进程时间存储在 struct tms buf 中,即:

struct tms{
    clock_t tms_utime;    // user mode time
    clock_t tms_stime;    // system mode time
    clock_t tms_cutime;   // user time of children
    clock_t tms_cstime;   // system time of children
}

以时钟计时单元报告所有时间。这可以为分析某个正在执行的进程提供信息,包括其子进程的时间(如有)。

4.time 和 date 命令

  • date:打印或设置系统日期和时间。
  • time:报告进程在用户模式和系统模式下的执行时间和总时间。
  • hwclock:查询并设置硬件时钟(RTC),也可以通过 BIOS 来完成。

(六)间隔定时器

  • setitimeer 程序代码

(七)REAL 模式间隔定时器

(八)编程项目

1.系统基本代码

2.定时器中断

3.定时器队列

4.临界区

5.高级主题


二、问题与解决思路
三、实践内容与截图

1.gettimeofday 系统调用

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<time.h>

struct timeval t;

int main()
{
 gettimeofday(&t,NULL);
 printf("sec=%ld usec=%ld\n",t.tv_sec,t.tv_usec);
 printf((char *)ctime(&t.tv_sec));
}

2.settimeofday 系统调用

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<time.h>

struct timeval t;

int main()
{
 int r;
 t.tv_sec=123456789;
 t.tv_usec=0;
 r=settimeofday(&t,NULL);
 if(!r){
  printf("settimeofday() failed\n");
  exit(1);
 }
 gettimeofday(&t,NULL);
 printf("sec=%ld usec=%ld\n",t.tv_sec,t.tv_usec);
 printf("%s",ctime(&t.tv_sec));
}

3.time 系统调用

#include<stdio.h>
#include<time.h>

time_t start,end;

int main()
{
 int i;
 start=time(NULL);
 printf("start=%ld\n",start);
 for(i=0;i<123456789;i++);
 end=time(NULL);
 printf("end =%ld time=%ld\n",end,end-start);
}

4.setitimeer 程序代码

#include<signal.h>
#include<stdio.h>
#include<sys/time.h>
#include<time.h>
int count=0;
struct itimerval t;

void timer_handler(int sig)
{
 printf("timer_hander: signal=%d count=%d\n",sig,++count);
 if(count>=8){
  printf("cancel timer\n");
  t.it_value.tv_sec=0;
  t.it_value.tv_usec=0;
  setitimer(ITIMER_VIRTUAL,&t,NULL);
 }
}

int main()
{
 struct itimerval timer;
 signal(SIGVTALRM,timer_handler);
 timer.it_value.tv_sec=0;
 timer.it_value.tv_usec=100000;
 timer.it_interval.tv_sec=1;
 timer.it_interval.tv_usec=0;
 setitimer(ITIMER_VIRTUAL,&timer,NULL);
 printf("looping: enter Control-C to terminate\n");
 while(1);
}

标签:struct,tv,编程,timer,Unix,sec,time,Linux,include
From: https://www.cnblogs.com/20201212ycy/p/16810299.html

相关文章

  • ffmpeg linux上的安装
    一、安装相关依赖库sudoapt-get-yinstallautoconfautomakebuild-essentiallibass-devlibfreetype6-devlibsdl2-devlibtheora-devlibtoollibva-devlibvdpau-d......
  • Linux网络服务之NFS(文件共享服务)
    一、NFS概述1.1NFS(NetworkFileSystem网络文件服务)1.NFS(网络文件服务):NFS是一种基于TCP/IP传输的网络文件系统协议,最初由Sun公司开发。通过使用NFS协议,客户机可......
  • Linux网络服务之部署YUM仓库
    1YUM简介1.1YUM简介CentOS使用yum和dnf解决rpm的包依赖关系。YUM:rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,up2date的替代工具,CentOS8使用dnf......
  • Lab: Xv6 and Unix utilities
    BootXv6(easy)实验环境:Windows11+WSL2+Ubuntu22.04.1LTSsleep(easy)任务:实现sleep命令,暂停固定的ticks。可以使用系统调用sleep,所以并不困难,本质上解决的只有如何......
  • Linux学什么
    0.C/C++编程语言需要学习基本数据类型、数组、指针、结构体、链表、文件操作、队列、栈等知识,还需要通过大量的代码练习理解其知识。1.熟悉Linux系统基本命令安装......
  • 05.Linux配置静态IP地址
    [root@localhostnetwork-scripts]#catifcfg-enp0s3TYPE="Ethernet"BOOTPROTO="static"IPADDR="192.168.43.202"NETMASK=255.255.255.0GATEWAY=192.168.43.1DNS1=1......
  • Linux常用命令(针对我个人)
    pwd查看当前目录位置cd-返回上一次所在目录cat(文件名)查看文件more(文件名)......
  • Linux设置开机自启动的三种方法
    一、rc.local文件中添加自启动命令1、执行命令:编辑"/etc/rc.local"vi /ect/rc.local2、然后在文件最后一行添加要执行程序的全路径。例如,每次开机时要执行一个hell......
  • 熟悉编程语言
    语言前50排名:分类:命令式面向过程C语言、COBOL语言面向对象Python、C、C++、Java、Per、Fortran、PHP等 声明式函数式lisp、hashshell、erlang、Scal......
  • linux 网络命令
    博主描述:https://www.cnblogs.com/feizirui/p/16800006.html 一、网络配置命令1.1ifconfig查看当前活着的网络接口信息1.2hostname查看或设置主机名1.3route查看或......