新聞中心
隨著信息技術(shù)的快速發(fā)展和各種新型互聯(lián)網(wǎng)應(yīng)用的不斷涌現(xiàn),使用類Unix操作系統(tǒng)的計(jì)算機(jī)越來(lái)越普遍。作為類Unix系統(tǒng)中的佼佼者,Linux操作系統(tǒng)的普及范圍也越來(lái)越廣泛。Linux系統(tǒng)內(nèi)核是一個(gè)高度模塊化的系統(tǒng),其中重要的模塊之一就是線程。 Linux系統(tǒng)在層次結(jié)構(gòu)上,將進(jìn)程和線程分別看作獨(dú)立的單位,進(jìn)程是擁有資源的最小單位,線程則是CPU調(diào)度的最小單位。線程是輕量級(jí)的進(jìn)程,和進(jìn)程一樣也有自己的代碼、數(shù)據(jù)和堆棧。在Linux系統(tǒng)中,線程是通過(guò)輕量級(jí)進(jìn)程(LWP)來(lái)實(shí)現(xiàn),并且每個(gè)進(jìn)程可以擁有多個(gè)線程。在本文中,我們將介紹一種基于Linux系統(tǒng)中線程的簡(jiǎn)易方法,實(shí)現(xiàn)定時(shí)輸出的功能。

定時(shí)輸出是一個(gè)常見(jiàn)的操作,可以用來(lái)實(shí)現(xiàn)周期性地輸出一些信息,比如每隔一段時(shí)間輸出一次CPU利用率、內(nèi)存使用情況等等。在Linux系統(tǒng)中,使用線程可以非常方便地實(shí)現(xiàn)定時(shí)輸出的功能。Linux系統(tǒng)提供了一個(gè)適用于定時(shí)任務(wù)的函數(shù),即定時(shí)器函數(shù)timer_create()。這個(gè)函數(shù)可以創(chuàng)建一個(gè)單獨(dú)的定時(shí)器,并將它綁定到指定的線程上。在定時(shí)器到期時(shí),系統(tǒng)會(huì)自動(dòng)觸發(fā)定時(shí)器事件,并在指定的線程中執(zhí)行相關(guān)的回調(diào)函數(shù)。
下面是一個(gè)基于Linux系統(tǒng)線程和定時(shí)器函數(shù)的簡(jiǎn)易程序,用于實(shí)現(xiàn)定時(shí)輸出的功能。在這個(gè)程序中,我們使用了pthread庫(kù)中的pthread_create()函數(shù)來(lái)創(chuàng)建一個(gè)新的線程,并將定時(shí)器函數(shù)timer_create()返回的定時(shí)器與其關(guān)聯(lián)。在定時(shí)器到期時(shí),系統(tǒng)會(huì)調(diào)用定時(shí)器回調(diào)函數(shù)timer_handler(),定時(shí)器回調(diào)函數(shù)timer_handler()中會(huì)輸出當(dāng)前的系統(tǒng)時(shí)間。
“`
#include
#include
#include
#include
#include
#include
timer_t timerid;
void timer_handler(int sig)
{
time_t curtime;
struct tm *loc_time;
curtime = time (NULL);
loc_time = localtime (&curtime);
printf(“\nCurrent time is %d:%d:%d\n”, loc_time->tm_hour, loc_time->tm_min, loc_time->tm_sec);
fflush(stdout);
}
void *thread_func(void *arg)
{
int ret;
struct sigevent sev;
struct itimerspec its;
/* Create a timer */
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = timer_handler;
sev.sigev_value.sival_ptr = &timerid;
sev.sigev_notify_attributes = NULL;
ret = timer_create(CLOCK_REALTIME, &sev, &timerid);
if (ret == -1) {
printf(“Error: timer_create fled\n”);
exit(EXIT_FLURE);
}
/* Start the timer */
its.it_value.tv_sec = 1;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 1;
its.it_interval.tv_nsec = 0;
ret = timer_settime(timerid, 0, &its, NULL);
if (ret == -1) {
printf(“Error: timer_settime fled\n”);
exit(EXIT_FLURE);
}
/* Wt forever for the timer to fire */
while(1);
return NULL;
}
int mn(int argc, char *argv[])
{
pthread_t tid;
int ret;
/* Create a new thread */
ret = pthread_create(&tid, NULL, &thread_func, NULL);
if (ret != 0) {
printf(“Error: pthread_create fled\n”);
exit(EXIT_FLURE);
}
/* Wt for the thread to exit */
ret = pthread_join(tid, NULL);
if (ret != 0) {
printf(“Error: pthread_join fled\n”);
exit(EXIT_FLURE);
}
return 0;
}
“`
在這個(gè)程序中,定時(shí)器的時(shí)間間隔是1秒鐘,可以根據(jù)實(shí)際需要進(jìn)行調(diào)整。定時(shí)器函數(shù)timer_create()一共有三個(gè)參數(shù),之一個(gè)參數(shù)是指定計(jì)時(shí)器使用的時(shí)鐘,第二個(gè)參數(shù)是指定計(jì)時(shí)器的行為,第三個(gè)參數(shù)是一個(gè)指向timer_t類型變量的指針,用于返回計(jì)時(shí)器的唯一標(biāo)識(shí)符timerid。定時(shí)器函數(shù)timer_settime()的參數(shù)包括定時(shí)器標(biāo)識(shí)符、與定時(shí)器相關(guān)的參數(shù)結(jié)構(gòu)體itimerspec、以及用于保存計(jì)時(shí)器舊參數(shù)的itimerspec結(jié)構(gòu)體。在調(diào)用定時(shí)器函數(shù)后,程序會(huì)進(jìn)入一個(gè)循環(huán),不斷等待定時(shí)器到期。
相關(guān)問(wèn)題拓展閱讀:
- 在linux C編程中,定時(shí)器函數(shù)選擇與設(shè)置問(wèn)題
在linux C編程中,定時(shí)器函數(shù)選擇與設(shè)置問(wèn)題
試試alarm()與signal(),例子鉛神可以槐坦虧網(wǎng)上搜搜
NAME
alarm – set an alarm clock for delivery of a signal
SYNOPSIS
#include 信宴
unsigned int alarm(unsigned int seconds);
DESCRIPTION
alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds.
If seconds is zero, no new alarm() is scheduled.
In any event any previously set alarm() is canceled.
估計(jì)得自己實(shí)現(xiàn)
關(guān)于linux線程定時(shí)輸出的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開(kāi)通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過(guò)10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開(kāi)發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊(cè)、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
當(dāng)前名稱:Linux線程實(shí)現(xiàn)定時(shí)輸出的簡(jiǎn)易方法(linux線程定時(shí)輸出)
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/cohigcs.html


咨詢
建站咨詢
