文件锁
理解 flock 和 fcntl
信号量
sigprocmask的参数意义
参考:man SIGPROCMASK
siglongjmp与sigsetjmp
int sigsetjmp(sigjmp_buf env, int savesigs)
设置跳转点,类似于goto标签,等到siglongjmp会跳转到sigsetjmp之后的代码,同时恢复sigsetjmp前的env,即进程上下文环境
savesigs设为非0时会恢复之前信号掩码,savesigs为0则不恢复。
sigsetjmp的正常的返回值为0,由siglongjmp跳转到的时候返回值等于siglongjmp的val
void siglongjmp(sigjmp_buf env, int val)
恢复到env时的指令地址及环境,同时设置val
信号处理函数的默认原型void sig_handler(int sig_num)
sig_num代表具体的信号编号
#include<setjmp.h> #include<signal.h> #include<stdio.h> #include<unistd.h> static sigjmp_buf jmpbuf; static volatile sig_atomic_t jumpok=0; static void hd(int sign) { printf("sign=%d\n",sign); if(!jumpok)return; siglongjmp(jmpbuf,3); } int main() { printf("SIGINT=%d\n",SIGINT); struct sigaction act; act.sa_flags=0; act.sa_handler=hd; sigemptyset(&act.sa_mask); sigaction(SIGINT,&act,NULL);//仅设置SIGINT的处理函数 int ret; puts("before sigsetjmp"); ret=sigsetjmp(jmpbuf,2333); if(ret)printf("ret=%d,return to main loop\n",ret); puts("after sigsetjmp"); jumpok=1; while(1); return 0; }
获取系统时间
struct timeval tv; gettimeofday(&tv,NULL); long now=tv.tv_sec*1000+tv.tv_usec; //微秒级系统时间
获取线程uid
#include <sys/syscall.h> #define gettidv1() syscall(__NR_gettid) #define gettidv2() syscall(SYS_gettid) printf("The ID of this thread is: %ld\n", (long int)gettidv1());// 最新的方式 printf("The ID of this thread is: %ld\n", (long int)gettidv2());// traditional form