当前位置: 首页 > news >正文

网站制作需要哪些软件seo咨询推广找推推蛙

网站制作需要哪些软件,seo咨询推广找推推蛙,人像写真短视频,最近一周新闻用户空间定时器与内核定时器的关系 虽然用户空间定时器和内核定时器在实现上各自独立,但用户空间定时器通常依赖于内核定时器提供的基础设施。以下是具体关系: 依赖性 用户空间定时器的实现基于内核定时器。 例如,POSIX 定时器使用内核的 h…

用户空间定时器与内核定时器的关系

虽然用户空间定时器和内核定时器在实现上各自独立,但用户空间定时器通常依赖于内核定时器提供的基础设施。以下是具体关系:

  1. 依赖性
    • 用户空间定时器的实现基于内核定时器。
      • 例如,POSIX 定时器使用内核的 hrtimer 机制。
      • alarm()setitimer() 使用内核低精度定时器。
    • 内核通过用户态 API(如 timerfdclock_nanosleep())将定时器功能暴露给用户空间。
  2. 精度
    • 用户空间定时器的精度取决于内核定时器的支持:
      • 在启用高分辨率定时器(CONFIG_HIGH_RES_TIMERS=y)时,用户空间定时器可以实现亚毫秒级精度。
      • 如果系统只支持低分辨率定时器,用户空间定时器的精度会受到限制(分辨率基于HZjiffies)。
  3. 隔离性
    • 内核定时器运行在内核态,直接管理硬件资源,对用户态透明。
    • 用户空间定时器运行在用户态,通过系统调用访问内核资源。
  4. 性能
    • 内核定时器的执行效率更高,因为它直接操作硬件并在内核上下文中运行。
    • 用户空间定时器的执行效率相对较低,因其需要从用户态切换到内核态并依赖系统调用。

用户空间定时器

在 Linux 中,用户空间有多种方式可以使用定时器来执行定时任务或管理延时操作。以下是常见的方法及其详细说明:  

1. 使用 **alarm()** 函数

  • 功能:
    • alarm() 用于设置一个定时器,定时结束时会向当前进程发送 SIGALRM 信号。
  • 用法:
#include <unistd.h>
#include <signal.h>
#include <stdio.h>void alarm_handler(int sig) {printf("Alarm triggered!\n");
}int main() {signal(SIGALRM, alarm_handler); // 设置信号处理函数alarm(5); // 设置定时器,5秒后触发pause();  // 等待信号return 0;
}
  • 特点:
    • 简单易用。
    • 只能设置秒级的定时器。
    • 只能设置一个定时器。
    • 定时器到期后,如果是多线程,会选择一个当前非阻塞的线程来处理信号,处理线程并不确定
  • 依赖的内核机制
    • 依赖于**内核 jiffies,**即低分辨率定时器。
    • 内核定时器队列(timer_list)会管理这些定时器,触发时发送信号。

2. 使用 **setitimer()** 函数

  • 功能:
    • setitimer() 可以设置定时器,以精确控制一次性或周期性的定时任务。
    • 当定时时间到达时,会向调用进程发送信号(通常是 SIGALRM)。
  • 用法:
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>void timer_handler(int sig) {printf("Timer triggered!\n");
}int main() {struct itimerval timer;signal(SIGALRM, timer_handler); // 设置信号处理函数// 定时器间隔为1秒timer.it_value.tv_sec = 1;timer.it_value.tv_usec = 0;timer.it_interval.tv_sec = 1;timer.it_interval.tv_usec = 0;setitimer(ITIMER_REAL, &timer, NULL); // 设置定时器while (1); // 无限循环等待定时器触发return 0;
}
  • 特点:
    • 支持周期性定时器。
    • 提供微秒级别的精度。
  • 依赖的内核机制
    • 本身最初是设计为基于低分辨率定时器,但在现代内核中,如果高分辨率定时器启用,它可以利用高分辨率定时器来提高精度
    • 内核定时器队列(timer_list)会管理这些定时器,触发时发送信号。

3. 使用 POSIX 定时器 (**timer_create()**** 和 **timer_settime()**)**

  • 功能:
    • POSIX 定时器提供了灵活的定时功能,可以通过多种方式通知(如信号或回调)。
    • 可以创建多个定时器。
    • 使用 timer_create() 创建一个定时器,时间到达时触发信号或调用特定的回调函数。
  • 用法:
    • **timer_create**:
      • 用于创建一个 POSIX 定时器。
      • 可以选择通知方式,包括发送信号(SIGEV_SIGNAL)和使用线程回调(SIGEV_THREAD)。
    • **timer_settime**:
      • 用于设置定时器的到期时间 (it_value) 和周期性时间 (it_interval)。
    • 定时器的通知方式:
      • SIGEV_SIGNAL: 定时器超时时发送指定信号(如 SIGALRM)。
      • SIGEV_THREAD: 定时器超时时调用用户定义的回调函数。
// gcc -o posix_timer posix_timer.c -lrt -pthread#include <time.h>
#include <signal.h>
#include <stdio.h>void timer_handler(union sigval sv) {printf("POSIX timer triggered![%ld]\n", pthread_self());
}int main() {timer_t timerid;struct sigevent sev;struct itimerspec ts;sev.sigev_notify = SIGEV_THREAD; // 使用线程通知方式sev.sigev_value.sival_ptr = &timerid;sev.sigev_notify_function = timer_handler;sev.sigev_notify_attributes = NULL;timer_create(CLOCK_REALTIME, &sev, &timerid);ts.it_value.tv_sec = 2;  // 初始触发时间ts.it_value.tv_nsec = 0;ts.it_interval.tv_sec = 2;  // 周期触发时间ts.it_interval.tv_nsec = 0;timer_settime(timerid, 0, &ts, NULL);while (1); // 等待定时器触发return 0;
}

可以看到有两个线程,一个main线程,一个回调线程

$ #ps -eLf | grep posix_timer
congchp  1965725 1138860 1965725 99    2 11:11 pts/4    00:02:27 ./posix_timer
congchp  1965725 1138860 1965726  0    2 11:11 pts/4    00:00:00 ./posix_timer
congchp  1966341  939085 1966341  0    1 11:13 pts/2    00:00:00 grep --color=auto posix_timer
  • 特点:
    • 支持多个定时器。
    • 支持多种通知方式(如线程回调或信号)。
    • 通知方式如果使用SIGEV_THREAD,则自动创建一个回调线程。
  • 依赖的内核机制
    • 内核使用 hrtimer(高精度定时器)实现。
    • hrtimer 基于内核的高精度时间子系统,支持纳秒级别的精度。
    • 事件触发通过 softirq 或中断机制实现。

4. 使用 **sleep()** **nanosleep()**``**clock_nanosleep()** 函数

  • 功能
    • 这些函数用于让进程挂起一定时间。
    • sleep() 提供秒级精度,而 nanosleep()clock_nanosleep() 提供纳秒级精度。
    • 可以被信号(如Ctrl+C)打断

※Ctrl+C后,先发送SIGINT,如果SIGINT没有被处理,则发送SIGKILL强制终止进程。

  • 用法:
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <stdlib.h>void handle_sigint(int sig) {printf("Received SIGINT (Ctrl+C). Exiting...\n");
}int main() {struct timespec req, rem;req.tv_sec = 2;  // Sleep for 2 secondsreq.tv_nsec = 500000000;  // Plus 500 milliseconds// 设置自定义信号处理函数signal(SIGINT, handle_sigint);printf("Sleeping for 2.5 seconds...\n");if (nanosleep(&req, &rem) == -1) {perror("nanosleep interrupted");printf("%ld.%ld remain\n", rem.tv_sec, rem.tv_nsec);return 1;}printf("Sleep complete!\n");return 0;
}
  • 依赖的内核机制
    • 使用hrtimer(高精度定时器)进行时间跟踪。
    • 内核会将进程标记为睡眠状态,定时时间到后通过唤醒机制让进程继续运行。

5. **使用 ****poll()**/select()/epoll()的超时功能

  • 功能
    • 提供 I/O 多路复用时的超时功能(timeout),用于等待文件描述符的状态变化。
    • 这些函数可以设置超时时间,如果没有检测到IO,让调用线程挂起指定时间。
  • 用法:
#include <stdio.h>
#include <poll.h>
#include <unistd.h>int main() {struct pollfd fds[1];fds[0].fd = STDIN_FILENO;  // Monitor standard inputfds[0].events = POLLIN;int timeout = 5000;  // 5-second timeoutprintf("Waiting for input (5 seconds timeout)...\n");int ret = poll(fds, 1, timeout);if (ret == -1) {perror("poll failed");} else if (ret == 0) {printf("Timeout occurred!\n");} else if (fds[0].revents & POLLIN) {char buffer[100];read(STDIN_FILENO, buffer, sizeof(buffer));printf("Input received: %s", buffer);}return 0;
}
  • 特点:
    • 高效且支持事件驱动(如结合 epoll)。
    • 适用于文件描述符管理的场景。
  • 依赖的内核机制
    • 高分辨率定时器启用的情况下,使用hrtimer;否则使用tick-based定时器,取决于内核配置和硬件支持。
    • 内核会将调用线程加入等待队列,并在超时到期或事件发生时唤醒线程。

6. 使用 **timerfd** 接口

  • 功能
    • **timerfd**: 通过文件描述符(FD)暴露定时器事件,适合与 epoll 一起使用。
    • read()函数读取出来的,自上一次读取以来的所有未处理的超期次数。
  • 用法:
#include <sys/timerfd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>int main() {int tfd = timerfd_create(CLOCK_REALTIME, 0);struct itimerspec ts;ts.it_value.tv_sec = 2; // 初始触发时间ts.it_value.tv_nsec = 0;ts.it_interval.tv_sec = 2; // 周期触发时间ts.it_interval.tv_nsec = 0;timerfd_settime(tfd, 0, &ts, NULL);uint64_t expirations;while (1) {read(tfd, &expirations, sizeof(expirations)); // 阻塞等待printf("Timer expired %lu times\n", expirations); // expirations = 1}close(tfd);return 0;
}
  • 特点:
    • 高效且支持事件驱动(如结合 epoll)。
    • 适用于大量定时器、文件描述符管理的场景。
  • 依赖的内核机制
    • timerfd 基于内核的 hrtimer 实现。
    • 内核将定时器事件封装为文件描述符事件,通过 VFS 层传递给用户空间。

对比各种方法

方法多定时器支持周期性定时器精度内核机制易用性推荐用途
alarm()秒级低分辨率定时器简单单一的短时任务
setitimer()微秒级低分辨率定时器较简单简单的周期性任务
POSIX 定时器纳秒级高分辨率定时器较复杂多任务,灵活的通知方式
sleep()
/nanosleep()
秒级/纳秒低/高分辨率定时器简单简单的延时任务
poll()/select()/epoll()毫秒级高分辨率定时器较复杂高效的文件描述符管理场景
timerfd纳秒级高分辨率定时器较复杂高效的文件描述符管理场景

根据需求选择合适的定时器接口,可以满足不同的用户空间定时任务需求。


timerfd+IO多路复用组合使用

※※※ 推荐的方法,是**timerfd**+**IO多路复用**组合使用的方法。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <mqueue.h>
#include <sched.h>#define MAX_EVENTS 10
#define WORKER_COUNT 2
#define QUEUE_NAME "/task_queue"// 任务结构
typedef struct {int task_id;
} Task;// 线程信息
typedef struct {mqd_t mqd;pthread_t thread;int id;
} WorkerThread;WorkerThread workers[WORKER_COUNT];// 任务回调
void task_callback(int task_id) {printf("Worker processing task %d\n", task_id);sleep(1);  // 模拟任务执行
}// 设定线程优先级
void set_thread_priority(pthread_t thread, int priority) {struct sched_param param;param.sched_priority = priority;pthread_setschedparam(thread, SCHED_FIFO, &param);
}// 工作线程函数
void *worker_thread(void *arg) {WorkerThread *worker = (WorkerThread *)arg;Task task;while (1) {if (mq_receive(worker->mqd, (char *)&task, sizeof(Task), NULL) > 0) {task_callback(task.task_id);}}
}// 选择负载最轻的工作线程
WorkerThread *get_least_busy_worker() {struct mq_attr attr;WorkerThread *best_worker = &workers[0];for (int i = 1; i < WORKER_COUNT; i++) {mq_getattr(workers[i].mqd, &attr);struct mq_attr best_attr;mq_getattr(best_worker->mqd, &best_attr);if (attr.mq_curmsgs < best_attr.mq_curmsgs) {best_worker = &workers[i];}}return best_worker;
}// 定时器检测线程
void *timer_thread(void *arg) {set_thread_priority(pthread_self(), 90);int epfd = epoll_create1(0);int timerfd = timerfd_create(CLOCK_REALTIME, 0);struct itimerspec timer_value = {.it_value = {2, 0},  .it_interval = {3, 0} };timerfd_settime(timerfd, 0, &timer_value, NULL);struct epoll_event ev;ev.events = EPOLLIN;ev.data.fd = timerfd;epoll_ctl(epfd, EPOLL_CTL_ADD, timerfd, &ev);while (1) {struct epoll_event events[MAX_EVENTS];int nfds = epoll_wait(epfd, events, MAX_EVENTS, -1);for (int i = 0; i < nfds; i++) {if (events[i].data.fd == timerfd) {uint64_t exp;read(timerfd, &exp, sizeof(uint64_t));printf("Timer expired! Dispatching task...\n");static int task_id = 0;Task task = {task_id++};WorkerThread *worker = get_least_busy_worker();mq_send(worker->mqd, (char *)&task, sizeof(Task), 0);}}}
}// 主程序
int main() {struct mq_attr attr = {.mq_flags = 0,.mq_maxmsg = 10,.mq_msgsize = sizeof(Task),.mq_curmsgs = 0};// 初始化工作线程for (int i = 0; i < WORKER_COUNT; i++) {workers[i].id = i;workers[i].mqd = mq_open(QUEUE_NAME, O_CREAT | O_RDWR | O_NONBLOCK, 0644, &attr);pthread_create(&workers[i].thread, NULL, worker_thread, &workers[i]);set_thread_priority(workers[i].thread, 80);}// 启动定时器检测线程pthread_t timer_tid;pthread_create(&timer_tid, NULL, timer_thread, NULL);pthread_join(timer_tid, NULL);for (int i = 0; i < WORKER_COUNT; i++) {pthread_join(workers[i].thread, NULL);mq_close(workers[i].mqd);}mq_unlink(QUEUE_NAME);return 0;
}


还有一些定时器数据结构设计方法,参考之前另一篇定时器文章:《定时器实现》


文章转载自:
http://incursion.tzmc.cn
http://pyranometer.tzmc.cn
http://wuzzy.tzmc.cn
http://husky.tzmc.cn
http://fricandeau.tzmc.cn
http://pinkey.tzmc.cn
http://gunfignt.tzmc.cn
http://bargeboard.tzmc.cn
http://bose.tzmc.cn
http://coulomb.tzmc.cn
http://earthling.tzmc.cn
http://ware.tzmc.cn
http://weatherman.tzmc.cn
http://ionophoresis.tzmc.cn
http://rail.tzmc.cn
http://quatro.tzmc.cn
http://cerebrum.tzmc.cn
http://condemned.tzmc.cn
http://penult.tzmc.cn
http://cryptorchid.tzmc.cn
http://chromophore.tzmc.cn
http://nox.tzmc.cn
http://special.tzmc.cn
http://hexameral.tzmc.cn
http://calipers.tzmc.cn
http://noticeably.tzmc.cn
http://cysticercus.tzmc.cn
http://otology.tzmc.cn
http://proteus.tzmc.cn
http://astrogeology.tzmc.cn
http://ballonet.tzmc.cn
http://evaluating.tzmc.cn
http://protestatory.tzmc.cn
http://declivous.tzmc.cn
http://dismal.tzmc.cn
http://exude.tzmc.cn
http://entoptoscope.tzmc.cn
http://itching.tzmc.cn
http://funest.tzmc.cn
http://divisa.tzmc.cn
http://lidar.tzmc.cn
http://fistuliform.tzmc.cn
http://colluvial.tzmc.cn
http://hogtie.tzmc.cn
http://jameson.tzmc.cn
http://stadle.tzmc.cn
http://satcoma.tzmc.cn
http://virtually.tzmc.cn
http://unbuilt.tzmc.cn
http://blendo.tzmc.cn
http://tripmeter.tzmc.cn
http://airmanship.tzmc.cn
http://telespectroscope.tzmc.cn
http://lymphadenoma.tzmc.cn
http://malingerer.tzmc.cn
http://carom.tzmc.cn
http://glucinum.tzmc.cn
http://minux.tzmc.cn
http://packplane.tzmc.cn
http://harmonium.tzmc.cn
http://oxytone.tzmc.cn
http://slowpoke.tzmc.cn
http://pri.tzmc.cn
http://labiovelarize.tzmc.cn
http://cordierite.tzmc.cn
http://breslau.tzmc.cn
http://autofilter.tzmc.cn
http://umbiliform.tzmc.cn
http://clumsy.tzmc.cn
http://pectin.tzmc.cn
http://goldie.tzmc.cn
http://oaken.tzmc.cn
http://minority.tzmc.cn
http://perjure.tzmc.cn
http://mourner.tzmc.cn
http://milling.tzmc.cn
http://patriot.tzmc.cn
http://chid.tzmc.cn
http://etymologic.tzmc.cn
http://thundering.tzmc.cn
http://sternal.tzmc.cn
http://sweetmeat.tzmc.cn
http://jimp.tzmc.cn
http://itcz.tzmc.cn
http://escabeche.tzmc.cn
http://quicksand.tzmc.cn
http://califate.tzmc.cn
http://iec.tzmc.cn
http://jarful.tzmc.cn
http://zaire.tzmc.cn
http://overtask.tzmc.cn
http://multivolume.tzmc.cn
http://quap.tzmc.cn
http://php.tzmc.cn
http://farmworker.tzmc.cn
http://bewitching.tzmc.cn
http://streakily.tzmc.cn
http://zaffre.tzmc.cn
http://kingship.tzmc.cn
http://hokum.tzmc.cn
http://www.dt0577.cn/news/110861.html

相关文章:

  • 医院网站建设情况汇报沈阳网站关键字优化
  • 做影视网站用的封面网站优化seo教程
  • 外包人力资源公司廊坊关键词排名优化
  • 广州网站建设新际网站网页的优化方法
  • python和php哪个做网站深圳推广公司哪家正规
  • 个人网站能否备案响应式网站建设
  • 赣州网上商城入驻方案aso优化app推广
  • 织梦网站防黑怎么做2022搜索引擎
  • 武汉做网站云优化科技百度关键词优化多少钱
  • 金华关键词优化平台合肥seo外包平台
  • 网站正在建设中视频黄山网络推广公司
  • 多多返利网站建设bt磁力搜索
  • 心理测试网站开发报价关键词首页排名优化价格
  • 湖南建设厅网站天津疫情最新情况
  • 深圳夫博网站建设有限公司百度400电话
  • 做网站申请完空间后下一步干啥短网址在线生成
  • 网站建设公司 电话销售没什么效果如何设置友情链接
  • 网站怎么做支付接口东莞seo托管
  • 学院网站建设项目的活动分解百度怎么发广告
  • 网站logo图怎么做的绍兴seo网站推广
  • 一键建站模板简述网站推广的意义和方法
  • 做四六级模拟题的网站广告联盟骗局
  • 网站邮件推送反向链接查询
  • 建设b2b网站需要多少钱网站建设方案书
  • 小型营销企业网站建设策划厦门头条今日新闻
  • 设计网站的收费图是怎么做的企业培训课程分类
  • 青州做网站的公司seo费用
  • php网站开发工程师招聘网线上营销的优势和劣势
  • 私服网站建设windows优化大师手机版
  • 网站方案怎么写谷歌推广怎么操作