open("/dev/null",o_rdwr)作用

 open("/dev/null",..)

很多程序在一开始的时候要做这样的处理
/* 
* make sure we have stdin/stdout/stderr reserved
*/

while((t = open("/dev/null",O_RDWR)) < 3 && T >= 0)
if(t >= 3) close(t)

写入/dev/null的东西会被系统丢掉
就像注释写的那样,对stdin/stdout/stderr进行保护
代码中利用while先把文件描述符0,1,2分配出去,以后再分配
的时候就不会将stdin/stdout/stderr打开,以达到保护目的。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
打开/dev/fb0文件通常用于在Linux系统中访问帧缓冲设备。如果要在多个线程中访问此设备,则可以使用线程安全的方法打开该设备。下面是一个示例代码: ```c #include <fcntl.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <linux/fb.h> #include <pthread.h> void *fb_thread(void *arg) { int fbfd = open("/dev/fb0", O_RDWR); if (fbfd == -1) { perror("Error: cannot open framebuffer device"); exit(1); } struct fb_fix_screeninfo finfo; if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) { perror("Error reading fixed information"); exit(2); } struct fb_var_screeninfo vinfo; if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) { perror("Error reading variable information"); exit(3); } long screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; void *fbp = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); if ((intptr_t)fbp == -1) { perror("Error: failed to map framebuffer device to memory"); exit(4); } // 进行帧缓冲操作... munmap(fbp, screensize); close(fbfd); return NULL; } int main() { pthread_t t1, t2; pthread_create(&t1, NULL, fb_thread, NULL); pthread_create(&t2, NULL, fb_thread, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; } ``` 上述代码使用了 pthread 库来创建两个线程,每个线程都打开了/dev/fb0设备,并进行了一些帧缓冲操作。注意,这里使用了 mmap() 函数将帧缓冲设备映射到内存中,以便进行读写操作。在程序结束时,使用 munmap() 函数将内存映射释放。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值