关于/usr/include/下各种unistd的区别

unistd是unix standard的简称,主要封装了一些glibc函数和系统调用相关的定义(个人理解)。具体参考这里:https://zh.wikipedia.org/wiki/Unistd.h

我在linux的/usr/include下发现了有很多unistd文件,具体说来:

/usr/include/unistd.h  1

/usr/include/linux/unistd.h 2 

/usr/include/asm/unistd.h  3

/usr/include/asm/unistd_32.h  4

/usr/include/asm/unistd_64.h 5

/usr/include/bits/unistd.h  6

1中include了6,如果编译时开了FORTIFY,那么会用6中的函数,对危险函数做一个wrapper。1 6 这些是和glibc有关的。

2中仅仅是include了3。3中根据OS的bits选择是4还是5。2 3 4 5 这些都是从kernel来的。

其中4和5中是有一对关于系统调用的define的,比如#define __NR_exit 1。如果需要用__NR_execve来代表系统调用号的话,需要 #include <linux/unistd.h>。

stackoverflow的介绍言简意赅:

linux/unistd.h actually points to asm/unistd.h, which in turn points to either asm/unistd_32.h or asm/unistd_64.h, which is where system call numbers are defined and presented to user space depending on the system's architecture. These come from the kernel.

bits/unistd.h is a collection of macros that augment unistd.h (mostly stuff to help prevent buffer overflows), which is conditionally included via:

/* Define some macros helping to catch buffer overflows.  */
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/unistd.h>
#endif
In essence, the only POSIX required header is in fact, just unistd.h, the rest are either extensions, or definitions from the kernel. So, just including unistd.h is all you have to worry about doing, everything you need will be pulled in depending on your architecture and whatever build options you've selected.

参考:

https://stackoverflow.com/questions/2948696/what-is-the-difference-between-the-various-unistd-h-under-usr-include-in-linux/2949262#2949262

kernel中的unistd https://elixir.bootlin.com/linux/v2.6.32/source/arch/arm/include/asm/unistd.h

以下是一个使用C语言编写的示例程序,利用inotify机制监控文件变化: ```c #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/inotify.h> #define EVENT_SIZE (sizeof(struct inotify_event)) #define BUF_LEN (1024 * (EVENT_SIZE + 16)) int main(int argc, char* argv[]) { int length, i = 0; int fd; int wd; char buffer[BUF_LEN]; fd = inotify_init(); if (fd < 0) { perror("inotify_init"); } wd = inotify_add_watch(fd, "/home/usr/a.txt", IN_MODIFY); while (1) { i = 0; length = read(fd, buffer, BUF_LEN); if (length < 0) { perror("read"); } while (i < length) { struct inotify_event* event = (struct inotify_event*)&buffer[i]; if (event->mask & IN_MODIFY) { printf("File /home/usr/a.txt has been modified!\n"); } i += EVENT_SIZE + event->len; } } (void)inotify_rm_watch(fd, wd); (void)close(fd); return 0; } ``` 程序中使用inotify_init()函数初始化一个inotify实例,然后使用inotify_add_watch()函数添加要监控的文件或目录,并指定要监控的事件类型(如IN_MODIFY表示文件被修改)。程序进入无限循环,使用read()函数读取inotify实例的事件信息,然后遍历每个事件,检查是否是文件修改事件,如果是则输出消息。 请注意,如果你要运行此程序,需要在编译时链接inotify库,例如: ```bash gcc -o inotify_example inotify_example.c -linotify ``` 同时,你需要在程序中指定要监控的文件路径,例如本例中的"/home/usr/a.txt"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值