Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15495268
  • 博文数量: 112
  • 博客积分: 11195
  • 博客等级: 上将
  • 技术积分: 1989
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-20 11:04
文章分类

全部博文(112)

文章存档

2013年(2)

2012年(27)

2011年(6)

2010年(11)

2009年(6)

2007年(7)

2006年(23)

2005年(30)

分类: LINUX

2005-11-30 15:36:09

RTlinux主要的api函数

实时应用程序分为两部分,内核部分和应用部分,应用部分需要和内核部分通过FIFO进行数据交换和控制,除此之外和一般应用程序没有太多区别,内核部分比较复杂,程序以模块方式挂入内核,这部分程序的编写需要对底层的东西有较高的要求,除了掌握RTLinux的API以外还需要对Linux内核编程有较深的了解,以及对硬件部分也有比较熟悉的掌握.

 

没有找到完整的rtlinux的api说明,下面是网上找到的主要的一些函数.

 

POSIX 线程创建函数

一个实时程序是由几个执行的线程组成的。线程是轻量级进程,它们共享共有的地址空间。在RTLinux 中,所有的线程共享Linux 内核地址空间。

int pthread_create (pthread_t *thread, pthread_attr_t * attr, void * (*start_routine)(void *), void *arg)

这是RTLinux 的标准POSIX 线程创建函数。这个线程运行函数指针start_routine 指向的过程,arg 是这个函数的指针的入口参数。线程的属性由attr 对象决定,可以为这个属性设置CPU 号、堆栈大小等属性。设定若为NULL,将会使用默认属性。返回0 表示成功创建线程,线程号放在thread所指向的空间;返回非0 表示创建失败。线程的属性决定在特定的CPU 上创建线程(pthread_attr_setcpu_np),是否使用FPU(pthread_attr_setfp_np)

 

int pthread_attr_init (pthread_attr_t *attr)

初始化线程运行的属性。

 

int pthread_ attr_setschedparam (pthread_attr_t *attr, const structsched_param *param)

int pthread_ attr_setschedparam (constpthread_attr_t *attr, struct sched_param *param)

这两个函数根据程序的需要相应地从attr 中设定/取得线程的运行参数。param 是为调度的SCHED_FIFO SCHED_RR 策略定义的属性。

 

int pthread_attr_setcpu_np (pthread_atte_t *attr, int cpu)

int pthread_attr_getcpu_np (pthread_atte_t *attr, int cpu)

设定/取得线程运行的CPU 号。在SMP 机器上允许线程在一个特定的CPU 上运行。

 

int pthread_cancel (pthread_t thread)

取消一个运行的线程。

 

int pthread_delete_np (pthread_t thread)

删除一个线程,并且释放该线程的所有资源。返回0 表示成功删除,非0 表示删除失败。

 

pthrad_t pthread_self (void)

获得当前正在运行的线程号。

 

clockid_t rtl_getschedclock (void)

获得当前调度方法的时钟。

 

int rtl_setclockmode (clockid_t clock, int mode, hrtime_t mode_param)

设置当前的时钟模式,mode=RTL_CLOCK_MODE_ONESHOT 时是非周期( 一次性) 模式mode_param 参数无用;

mode=RTL_CLOCK_MODE_PERIODIC 时是周期模式,mode_param 参数是周期的长度。

 

int pthread_wait_np (void)

当前周期的线程运行结束,总是返回0

 

 

时间相关函数

RTLinux 提供了一些时钟函数用于计时功能,包括线程调度,获得TSP(timestamps)等。

下面的是一般的计时函数(需要包含rtl_time.h):

 

int clock_gettime(clockid_t clock_id, struct timespec *ts);

读取当前的时间,保存到clock_id 所指的对象中。

 

hrtime_t clock_gethrtime(clockid_t clock);

读取当前时间,但返回一个64 (hrtime_t)的纳秒时间值。

struct timespec {

time_t tv_sec; /* */

long tv_nsec; /* 纳秒 */

};

 

一些时间转换的函数,用于把时间格式转换为另外一种格式。

时间转换函数(需要包含rtl_time.h):

hrtime_t timespec_to_ns(const struct timespec *ts)

timespec 到纳秒数转换

struct timespec timespec_from_ns(hrtime_t t)

纳秒数到timespec 转换

const struct timespec * hrt2ts(hrtime_t value)

下面是一些支持的时钟类型。

时钟类型相关的宏:

CLOCK_MONOTONIC: POSIX 时钟,以恒定速率运行;不会复位和调整

CLOCK_REALTIME: 标准POSIX 实时时钟。目前与

CLOCK_MONOTONIC 时钟相同

CLOCK_RTL_SCHED: 调度器用来任务调度的时钟

以下是机器结构相关的时钟:

CLOCK_8254: x86 单处理器机器上用于调度的时钟

CLOCK_APIC: 用在SMP x86 机器的时钟

 

 

线程调度函数

RTLinux 提供一些调度方式,允许线程代码在特定的时刻运行。

RTLinux 使用单纯优先级驱动的调度器,更搞优先级的线程总是被选择运行。如果两个线程的优先级拥有一样的优先级,选择那一个线程运行是不确定的。

RTLinux 使用下面的调度API

int pthread_setschedparam (pthread_t thread, int policy, const structsched_param *param)

设置一个线程的调度参数,用policy sched_param 两个参数设置thread 的调度参数属性:

policy=SCHED_RR:使用Round-Robin 方法调度

policy=SCHED_FIFO:使用先进先出的方法调度

返回0 表示成功调度,非0 表示失败。

 

int pthread_getschedparam (pthread_t thread, int policy, const structsched_param *param)

获得一个线程的调度参数。将获得的policy sched_param 结构放在入口参数所指向的地址里面。

 

int pthread_make_periodic_np (pthread_t thread, hrtime start_time,hrtime_t period)

这个函数标记thread 线程为可运行。线程将在start_time 时刻开始运行,运行的时间间隔由period 给定。

 

int pthread_wait_np (void)

pthread_wait_np 函数将挂起当前运行发线程直到下一周期。这个线程必须是pthread_make_periodic_np 函数标记为可执行。

 

int sched_get_priority_max (int policy)

int sched_get_priority_min (int policy)

确定sched_priority 可能的值。

 

实时任务与Linux进程之间通信的主要方法是FIFO:

rtf_create()

创建 一个一定大小的FIFO

rtf_put()

将数据送入FIFO,如果FIFO满,则返回一个错误。

rtf_get()

FIFO中取出数据,如果FIFO空,则返回一个错误。

rtf_create_handler()

建立响应RT-FIFO变化的子程序。

rtf_destroy()

销毁一个指定的FIFO

 

编程示例

一个实例来说明RTLinux下的编程方法。这是一以测试RTLinux 下中断延迟的程序。

 

实时部分

init_module 完成对实时部分的初始化。cleanup_module 实现关闭实时

模块的任务。

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "common.h"

 

int ntests=500;

int period=1000000;

int bperiod=3100000;

int mode=0;

int absolute=0;

int fifo_size=4000;

int ad

阅读(125733) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~