[root@FC5 tmp]# cat fcntl.c #include #include #include 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 #include #include 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; } |