如何使文件描述符阻塞? [英] How to make a file descriptor blocking?

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

问题描述

给出一个任意的文件描述符,如果它是非阻塞的,我可以使其阻塞吗?如果可以,怎么办?

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

推荐答案

自从我玩C以来已经有一段时间了,但是您可以使用

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);

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

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