linux小知识点
一、linux线程
(1)线程库
参考文献 https://blog.youkuaiyun.com/weixin_37787043/article/details/78745836
linux中线程属于第三方库开发
安装线程库man手册:
sudo apt-get install manpages manpages-dev
sudo apt-get install manpages-posix-dev
编译:
gcc -o bin src.c -lpthread //指定线程库链接
如果不安装这个,man pthread_mutex_init 就没有条目。
(2)使用select实现精确定时器
man select
微秒定时器 参考文献 https://www.yisu.com/zixun/409667.html
void microseconds_sleep(unsigned long uSec){
struct timeval tv;
tv.tv_sec=uSec/1000000;
tv.tv_usec=uSec%1000000;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}
#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
int main()
{
int i;
for(i=0;i<5;++i){
printf("%d\n",i);
//seconds_sleep(1);
//milliseconds_sleep(1500);
microseconds_sleep(1900000);
}
}
二、C语言
(1)ifndef
标识的命名规则一般是头文件名全大写,前后加下划线,并把文件名中的“.”也变成下划线,如:stdio.h
#ifndef _STDIO_H_
#define _STDIO_H_…
#endif
(2)short unsigned int 的scanf及printf格式
For scanf, you need to use %hu since you’re passing a pointer to an
unsigned short. For printf, it’s impossible to pass an unsigned short
due to default promotions (it will be promoted to int or unsigned int
depending on whether int has at least as many value bits as unsigned
short or not) so %d or %u is fine. You’re free to use %hu if you
prefer, though.
参考文献 http://www.cplusplus.com/reference/cstdio/printf/
对于printf
A format specifier follows this prototype:
%[flags][width][.precision][length]specifier
对于scanf
A format specifier for scanf follows this prototype:
%[*][width][length]specifier
(3)strlen() VS. sizeof()
strlen(str)只算字符串字符数,sizeof(str)还计算了‘\0’截至符号。