Linux的进程编程-之二-进程间通信(定时器)

1.1         定时器-Timer

1.1.1       Timer的创建和删除

1.1.1.1     timer_create( )

#include<time.h>

#include<signal.h>

inttimer_create( clockid_t clockid, struct sigevent *event, timer_t *timerid );

timer不能够被子进程从父进程中继承,而且一旦当前进程调用了exec( ),之前所有的timer都会失效,并被删除。

成功返回0,失败返回-1。

各个系统会支持不同的clockid,但是都必须支持CLOCK_REALTIME。

参数timerid用来存放创建成功的timerID。

struct sigevent

{

int   sigev_notify;

int   sigev_signo;  // signalnumber sent to current process when the timer expires

union sigval         sigev_value;  // info carried with signal

NotifyFUN         sigev_notify_function;      //typedef  void (*NotifyFUN)( union sigval)

pthread_attr_t*   sigev_notify_attributes;

}

sigev_notify的取值:

SIGEV_NONE        :No notification will be delivered when the event ofinterest occurs.

SIGEV_SIGNAL     :A queued signal will be generated when theevent of interest occurs.

SIGEV_THREAD   :A notification function will be called toperform notification.

如果参数event是NULL,相当于:

sigev_notify       = SIGEV_SIGNAL

sigev_signo =default signal number

sigev_value =timerid

1.1.1.2      timer_delete( )

#include <time.h>

int timer_delete( timer_t timerid );

成功返回0,失败返回-1。

1.1.2       Timer的运用

1.1.2.1     timer_settime()

#include<time.h>

int timer_settime

(timer_t timerid, int flag, const struct itimerspec *newValue, struct itimerspec*oldValue );

成功返回0,失败返回-1。

参数flag:TIMER_ABSTIME

如果设定,it_value指定的是绝对时间,也就是说timer将在it_value指定的时刻expires,如果it_value指定的时刻已经过了,立即成功返回,并且相当于发生了一次expires。

如果未设定,it_value指定的是相对时间,也就是说timer将在timer_settim( )调用之后,再经过it_value指定的时间之后expires。

参数newValue:

如果it_value设定为0,timer失效。

如果it_ interval设定为0,timer不重复。

参数oldvalue:

it_value返回timer离之前设定的expire还有多少时间,如果timer是失效的,返回0。

it_interval返回timer之前的reload value。

struct itimerspec

{

struct timespec  it_interval;    //reload value of timer

struct timespec  it_value;

}

struct timespec

{

time_t    tv_sec;   // seconds

long       tv_nsec; // nanoseconds

}

1.1.2.2     timer_gettime( )

#include<time.h>

inttimer_gettime( timer_t timerid, struct itimerspec *value );

it_value返回timer离expire还有多少时间,如果timer是失效的,返回0。

it_interval返回timer的reload value

成功返回0,失败返回-1。

1.1.2.3      timer_getoverrun( )

#include<time.h>

int timer_getoverrun( timer_t timerid );

Only one single signal will be queued tothe process for a given timer at any point in time. When a timer for which asignal is still pending expires, no signal will be queued, and a timer overrunoccurs.

成功返回timer expiration overrun count,返回的数目包括了extra timer expirations that occurred between the timesignal was generated (queued) and when it was delivered or accepted.

失败返回-1。

1.1.3       简化版的Timer

1.1.3.1     getitimer( )

#include<sys/time.h>

intgetitimer( int which, struct itimerval *value )

返回由which指定的timer的设定值。

成功返回0,失败返回-1。

1.1.3.2     setitimer( )

#include<sys/time.h>

intsetitimer( int which, const struct itimerval *newValue, struct itimerval *oldValue)

成功返回0,失败返回-1。

参数which指定定时器类型:

ITIMER_REAL:绝对时间计时,发送SIGALRM给本进程

ITIMER_VIRTUAL:进程执行时间计时,发送SIGVTALRM给本进程

ITIMER_PROF:进程执行+内核因本进程而消耗的时间,发送SIGPROF给本进程

it_value设定的是相对时间,也就是说timer将在setitimer( )调用之后,再经过it_value指定的时间之后expires。

参数newValue:

如果it_value设定为0,timer失效。

如果it_ interval设定为0,timer不重复。

参数oldvalue返回timer上次的设定值。

struct itimerval

{

struct timeval it_interval;

struct timeval it_value;

};

struct timeval

{

long tv_sec;         // seconds

long tv_usec;       // microseconds

};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux内核 前言 第1章 硬件基础与软件基础 6 1.1 硬件基础 6 1.1.1 CPU 7 1.1.2 存储器 8 1.1.3 总线 8 1.1.4 控制器和外设 8 1.1.5 地址空 9 1.1.6 时钟 9 1.2 软件基础 9 1.2.1 计算机语言 9 1.2.2 什么是操作系统 11 1.2.3 内核数据结构 13 第2章 内存管理 15 2.1 虚拟内存抽象模型 15 2.1.1 请求调页 17 2.1.2 交换 17 2.1.3 共享虚拟内存 18 2.1.4 物理寻址模式和虚拟寻址模式 18 2.1.5 访问控制 18 2.2 高速缓存 19 2.3 Linux页表 20 2.4 页分配和回收 21 2.4.1 页分配 22 2.4.2 页回收 22 2.5 内存映射 22 2.6 请求调页 23 2.7 Linux页缓存 24 2.8 页换出和淘汰 25 2.8.1 减少缓冲区和页缓存大小 25 2.8.2 换出System V共享内存页 26 2.8.3 换出和淘汰页 27 2.9 交换缓存 27 2.10 页换入 28 第3章 进程 29 3.1 Linux进程 29 3.2 标识符 31 3.3 调度 32 3.4 文件 34 3.5 虚拟内存 35 3.6 创建进程 36 3.7 时定时器 37 3.8 执行程序 38 3.8.1 ELF 39 3.8.2 脚本文件 40 第4章 进程通信机制 41 4.1 信号机制 41 4.2 管道 42 4.3 套接字 44 4.3.1 System V的进程通信机制 44 4.3.2 消息队列 44 4.3.3 信号量 45 4.3.4 共享存储区 47 第5章 PCI 49 5.1 PCI的地址空 49 5.2 PCI配置头 50 5.3 PCI的I/O和存储地址空 51 5.4 PCI-ISA桥 51 5.5 PCI-PCI 桥 51 5.5.1 PCI-PCI桥:PCI I/O和存储地址 空的窗口 51 5.5.2 PCI-PCI桥:PCI配置周期和PCI 总线编号 52 5.6 Linux PCI初始化 53 5.6.1 Linux内核PCI数据结构 53 5.6.2 PCI设备驱动程序 53 5.6.3 PCI的BIOS函数 56 5.6.4 PCI修正过程 57 第6章 中断处理与设备驱动程序 60 6.1 中断与中断处理 60 6.1.1 可编程中断控制器 61 6.1.2 初始化中断处理数据结构 61 6.1.3 中断处理 62 6.2 设备驱动程序 63 6.2.1 测试与中断 64 6.2.2 直接存储器访问(DMA) 65 6.2.3 存储器 66 6.2.4 设备驱动程序与内核的接口 66 6.2.5 硬盘 69 6.2.6 网络设备 74 第7章 文件系统 77 7.1 第二个扩展文件系统EXT2 78 7.1.1 EXT2系统的inode节点 79 7.1.2 EXT2系统的超级块 80 7.1.3 EXT2系统的组描述符 80 7.1.4 EXT2系统的目录 81 7.1.5 在EXT2文件系统中查找文件 81 7.1.6 在EXT2文件系统中改变文件 的大小 82 7.2 虚拟文件系统 83 7.2.1 VFS文件系统的超级块 84 7.2.2 VFS文件系统的inode节点 84 7.2.3 注册文件系统 85 7.2.4 装配文件系统 85 7.2.5 在虚拟文件系统中查找文件 87 7.2.6 卸载文件系统 87 7.2.7 VFS文件系统的inode缓存 87 7.2.8 目录缓存 88 7.3 缓冲区缓存 88 7.3.1 bdflush内核守护进程 90 7.3.2 update进程 90 7.4 /proc文件系统 91 7.5 特殊设备文件 91 第8章 网络 92 8.1 TCP/IP网络概述 92 8.2 Linux中的TCP/IP网络层次结构 95 8.3 BSD套接字接口 96 8.4 INET的套接字层 97 8.4.1 创建BSD套接字 98 8.4.2 为INET BSD Socket绑定地址 99 8.4.3 建立INET BSD Socket连接 99 8.4.4 INET BSD Socket侦听 100 8.4.5 接受连接请求 100 8.5 IP层 100 8.5.1 套接字缓冲区 100 8.5.2 接收IP报文 101 8.5.3 发送IP报文 102 8.5.4 数据分片 102 8.6 地址解析协议 103 8.7 IP路由 104 第9章 内核机制与模块 107 9.1 内核机制 107 9.1.1 Bottom Half控制 107 9.1.2 任务队列 108 9.1.3 定时器 109 9.1.4 等待队列 110 9.1.5 自旋锁 110 9.1.6 信号量 110 9.2 模块 111 9.2.1 模块载入 112 9.2.2 模块卸载 113 第10章 处理器 115 10.1 X86 115 10.2 ARM 115 10.3 Alpha AXP处理器 115 第11章 Linux内核源代码 117 11.1 怎样得到Linux内核源码 117 11.2 内核源码的编排 117 11.3 从何处看起 118 第12章 Linux数据结构 120 附录A 有用的Web和FTP站点 138 附录B 词汇表 139

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值