C_and_CPP 板


LINE

写完上面那篇之後,仔细想想 #1IGT3gmB (C_and_CPP) 好歹也是我写的, 用那种旧的 alarm() 好像有点掉漆,用新的 timer_ 系列和 nanosleep() 感觉起来就高级很多,而且我把速度调到十倍快,「体感」效能有增加。 $ cat alarm2.c /* https://ideone.com/9tXyDU */ #include <stdio.h> #include <signal.h> #include <string.h> #include <time.h> int n=0; timer_t timerid; void beep(sigval_t sig) { struct itimerspec timeval; printf("%d x %d = %d\n", n/8+2, n%8+2, (n/8+2)*(n%8+2)); n++; if (n>=64) { memset(&timeval, 0, sizeof(timeval)); timer_settime(timerid, 0, &timeval, NULL); } } int main(void) { struct sigevent sigev; struct itimerspec timeval; struct timespec ts; memset(&sigev, 0, sizeof(sigev)); sigev.sigev_notify = SIGEV_THREAD; sigev.sigev_notify_function = beep; timeval.it_interval.tv_sec = 0; timeval.it_interval.tv_nsec = 100000000; timeval.it_value.tv_sec = 0; timeval.it_value.tv_nsec = 100000000; timer_create(CLOCK_MONOTONIC, &sigev, &timerid); timer_settime(timerid, 0, &timeval, NULL); ts.tv_sec = 7; ts.tv_nsec = 0; nanosleep(&ts, NULL); timer_delete(timerid); return 0; } $ gcc -lrt alarm2.c $ ./a.out 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 8 x 9 = 72 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 -- 桃乐丝: 可是, 如果你没有头脑, 为什麽会说话? 稻草人: ㄝ, 我也不知... 但是有些人没有头脑也能说超~多话呢。 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.137.120.19
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1533235880.A.534.html
1F:→ Schottky: 其实最大的改进是 timer 和 nanosleep 不会冲突 08/03 03:16
2F:→ sarafciel: true 08/05 18:14
3F:→ sarafciel: 修个错字还推错orz 08/05 18:14







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP