文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>续fcntl()之F_SETFD,F_GETFD

续fcntl()之F_SETFD,F_GETFD

时间:2006-08-03  来源:playmud

上文书说到对于这两个参数还没有搞定是如何用的,结果斗胆在CU发了一个帖子, 终于弄明白了~~  

[root@FC5 tmp]# cat fcntl.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int set_fd(int);
int main()
{
        int fd,ret;
        char f[20];
        fd=open("/tmp/pighead.txt",O_RDWR|O_CREAT);
        set_fd(fd);
        sprintf(f,"%d",fd);
        ret=execl("/tmp/test",f,(char *)0);
        printf("ret:%d\n",ret);
        return 0;
}

int set_fd(int fd)
{
        unsigned int flag;
        flag=fcntl(fd,F_GETFD);
        flag|=~FD_CLOEXEC; //去掉'~'看看
        fcntl(fd,F_SETFD,flag);
        return 0;
}
[root@FC5 tmp]# cat test.c
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int main(int argc,char **argv)
{
int fd,ret;
system("echo abc >te.log");
printf("%s:argv[1]:%s\n",argv[0],argv[1]);
fd=atoi(argv[0]);
char str[16];
strcpy(str,"hello,world~\n");

printf("%s\n",str);
ret=write(fd,str,strlen(str));
printf("ret:%d\n",ret);
return 0;
}

  结果

[root@FC5 tmp]# gcc -o test test.c
[root@FC5 tmp]# gcc -o fcnt fcntl.c
[root@FC5 tmp]# ls pighead.txt
ls: pighead.txt: No such file or directory
[root@FC5 tmp]# ./fcnt
3:argv[1]:(null)
hello,world~

ret:13
[root@FC5 tmp]# cat pighead.txt
hello,world~

相关阅读 更多 +
排行榜 更多 +
精英射击求生

精英射击求生

飞行射击 下载
精英射击训练安卓版

精英射击训练安卓版

飞行射击 下载
超级坦克射击

超级坦克射击

飞行射击 下载