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格式

参考文献 https://stackoverflow.com/questions/8699812/what-is-the-format-specifier-for-unsigned-short-int#:~:text=For%20printf,%20it%27s%20impossible%20to%20pass%20an%20unsigned,You%27re%20free%20to%20use%20%25huif%20you%20prefer,%20though

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’截至符号。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值