在C中,如何让一个文件描述符阻止? [英] In c, how to make a file descriptor blocking?

查看:110
本文介绍了在C中,如何让一个文件描述符阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个任意的文件描述符,我可以把它封锁,如果它是非阻塞的?如果是这样,怎么样?

Given an arbitrary file descriptor, can I make it blocking if it is non-blocking? If so, how?

推荐答案

它已经有一段时间我打C,但你可以使用的的fcntl()函数更改文件描述符的标志:

Its been a while since I played with C, but you could use the fcntl() function to change the flags of a file descriptor:

#include <unistd.h>
#include <fcntl.h>

// Save the existing flags

saved_flags = fcntl(fd, F_GETFL);

// Set the new flags with O_NONBLOCK masked out

fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK);

这篇关于在C中,如何让一个文件描述符阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆