![这里写图片描述](https://img-blog.csdn.net/20150820123503872)
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int flags = -1;
int accmode = -1;
flags = fcntl(0, F_GETFL, 0);
if( flags < 0 ){
printf("failure to use fcntl\n");
return -1;
}
accmode = flags & O_ACCMODE;
if(accmode == O_RDONLY)
printf("STDIN READ ONLY\n");
else if(accmode == O_WRONLY)
printf("STDIN WRITE ONLY\n");
else if(accmode ==O_RDWR)
printf("STDIN READ WRITE\n");
else
printf("STDIN UNKNOWN MODE");
if( flags & O_APPEND )
printf("STDIN APPEND\n");
if( flags & O_NONBLOCK )
printf("STDIN NONBLOCK\n");
return 0;
}
![这里写图片描述](https://img-blog.csdn.net/20150820123517965)
例子一:使用函数int fcntl(int fd,int cmd);返回值为新的文件描述符
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>/*strlen函数*/
#define NEWFD 8
int main(void)
{
char buf[] = "FCNTL";
int fd = open("test.txt", O_RDWR);
printf("the file test.txt ID is %d\n",fd);
fd = fcntl(fd, F_GETFD);
printf("the file test.txt ID is %d\n",fd);
fd = NEWFD;
fcntl(NEWFD, F_SETFL, &fd);
write(NEWFD, buf, strlen(buf));
close(NEWFD);
return 0;
}
例子二:使用函数int fcntl(int fd,int cmd,long arg);返回值为获得的响应标志位
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>/*strlen函数*/
int main(void)
{
int flags = -1;
char buf[] = "FCNTL";
int fd = open("test.txt", O_RDWR);
flags = fcntl(fd, F_GETFL, 0);
flags |= O_APPEND;
flags = fcntl(fd, F_SETFL, &flags);
if( flags < 0 ){
printf("failure to use fcntl\n");
return -1;
}
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
![](https://img-blog.csdn.net/20150820123527713)
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#define NEWFD 8
int main(void)
{
int uid;
int fd = open("test.txt", O_RDWR);
uid = fcntl(fd, F_GETOWN);
printf("the SIG recv ID is %d\n",uid);
close(fd);
return 0;
}
![这里写图片描述](https://img-blog.csdn.net/20150820123608249)
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#define NEWFD 8
int main(void)
{
int uid;
int fd = open("test.txt", O_RDWR);
uid = fcntl(fd, F_SETOWN,1000);
close(fd);
return 0;
}