linux rt 应用程序,RTlinux编程
RTlinux主要的api函數(shù)
實時應用程序分為兩部分,內(nèi)核部分和應用部分,應用部分需要和內(nèi)核部分通過FIFO進行數(shù)據(jù)交換和控制,除此之外和一般應用程序沒有太多區(qū)別,內(nèi)核部分比較復雜,程序以模塊方式掛入內(nèi)核,這部分程序的編寫需要對底層的東西有較高的要求,除了掌握RTLinux的API以外還需要對Linux內(nèi)核編程有較深的了解,以及對硬件部分也有比較熟悉的掌握.
沒有找到完整的rtlinux的api說明,下面是網(wǎng)上找到的主要的一些函數(shù).
POSIX 線程創(chuàng)建函數(shù)
一個實時程序是由幾個執(zhí)行的線程組成的。線程是輕量級進程,它們共享共有的地址空間。在RTLinux中,所有的線程共享Linux內(nèi)核地址空間。
int pthread_create (pthread_t *thread, pthread_attr_t * attr, void * (*start_routine)(void *), void *arg)
這是RTLinux的標準POSIX線程創(chuàng)建函數(shù)。這個線程運行函數(shù)指針start_routine指向的過程,arg是這個函數(shù)的指針的入口參數(shù)。線程的屬性由attr對象決定,可以為這個屬性設置CPU號、堆棧大小等屬性。設定若為NULL,將會使用默認屬性。返回0表示成功創(chuàng)建線程,線程號放在thread所指向的空間;返回非0表示創(chuàng)建失敗。線程的屬性決定在特定的CPU上創(chuàng)建線程(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)
這兩個函數(shù)根據(jù)程序的需要相應地從attr中設定/取得線程的運行參數(shù)。param是為調(diào)度的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)
獲得當前調(diào)度方法的時鐘。
int rtl_setclockmode (clockid_t clock, int mode, hrtime_t mode_param)
設置當前的時鐘模式,mode=RTL_CLOCK_MODE_ONESHOT時是非周期( 一次性) 模式mode_param參數(shù)無用;
mode=RTL_CLOCK_MODE_PERIODIC時是周期模式,mode_param參數(shù)是周期的長度。
int pthread_wait_np (void)
當前周期的線程運行結(jié)束,總是返回0。
時間相關函數(shù)
RTLinux提供了一些時鐘函數(shù)用于計時功能,包括線程調(diào)度,獲得TSP(timestamps)等。
下面的是一般的計時函數(shù)(需要包含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; /*納秒*/
};
一些時間轉(zhuǎn)換的函數(shù),用于把時間格式轉(zhuǎn)換為另外一種格式。
時間轉(zhuǎn)換函數(shù)(需要包含rtl_time.h):
hrtime_t timespec_to_ns(const struct timespec *ts)
timespec到納秒數(shù)轉(zhuǎn)換
struct timespec timespec_from_ns(hrtime_t t)
納秒數(shù)到timespec轉(zhuǎn)換
const struct timespec * hrt2ts(hrtime_t value)
下面是一些支持的時鐘類型。
時鐘類型相關的宏:
CLOCK_MONOTONIC: POSIX時鐘,以恒定速率運行;不會復位和調(diào)整
CLOCK_REALTIME:標準POSIX實時時鐘。目前與
CLOCK_MONOTONIC時鐘相同
CLOCK_RTL_SCHED:調(diào)度器用來任務調(diào)度的時鐘
以下是機器結(jié)構相關的時鐘:
CLOCK_8254:在x86單處理器機器上用于調(diào)度的時鐘
CLOCK_APIC:用在SMP x86機器的時鐘
線程調(diào)度函數(shù)
RTLinux提供一些調(diào)度方式,允許線程代碼在特定的時刻運行。
RTLinux使用單純優(yōu)先級驅(qū)動的調(diào)度器,更搞優(yōu)先級的線程總是被選擇運行。如果兩個線程的優(yōu)先級擁有一樣的優(yōu)先級,選擇那一個線程運行是不確定的。
RTLinux使用下面的調(diào)度API:
int pthread_setschedparam (pthread_t thread, int policy, const structsched_param *param)
設置一個線程的調(diào)度參數(shù),用policy和sched_param兩個參數(shù)設置thread的調(diào)度參數(shù)屬性:
policy=SCHED_RR:使用Round-Robin方法調(diào)度
policy=SCHED_FIFO:使用先進先出的方法調(diào)度
返回0表示成功調(diào)度,非0表示失敗。
int pthread_getschedparam (pthread_t thread, int policy, const structsched_param *param)
獲得一個線程的調(diào)度參數(shù)。將獲得的policy和sched_param結(jié)構放在入口參數(shù)所指向的地址里面。
int pthread_make_periodic_np (pthread_t thread, hrtime start_time,hrtime_t period)
這個函數(shù)標記thread線程為可運行。線程將在start_time時刻開始運行,運行的時間間隔由period給定。
int pthread_wait_np (void)
pthread_wait_np函數(shù)將掛起當前運行發(fā)線程直到下一周期。這個線程必須是pthread_make_periodic_np函數(shù)標記為可執(zhí)行。
int sched_get_priority_max (int policy)和
int sched_get_priority_min (int policy)
確定sched_priority可能的值。
實時任務與
總結(jié)
以上是生活随笔為你收集整理的linux rt 应用程序,RTlinux编程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 饿了么超时20分钟_饿了么回应“多等5分
- 下一篇: asp连接oracle6,asp下用Or