通过文件描述符 - 的execve(类型转换) [英] Pass File Descriptor - Execve (typecast)

查看:144
本文介绍了通过文件描述符 - 的execve(类型转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎么能传递一个文件描述符直通的execve的命令,然后访问它的另一面。我知道,我可以使用dup2重定向文件描述符,但我不能这样做。我需要真正的文件描述符传递给孩子,在孩子使用它。

我到目前为止有:

母公司使得管+ ARGS像这样:

  INT的pfd [2];
如果(管道(PFD)== - 1)
    exitWithError(管子失败,1);
字符* args_1 [] = {读者,ARGV [1],(字符*)PFD(字符*)0};

那么孩子呼吁,像这样fork之后execve的:

 关闭(PFD [1]);
的execve(./读者,args_1,NULL);

那么,在阅读器程序我试图访问传递管描述:

  INT主(INT ARGC,CHAR *的argv []){
    ...
    写(的argv [2] [1],BUF,read_test);

^^的argv [2]应该引用管描述符,则在[1]应该去管道的写入端。我几乎可以肯定,我需要做一些类型强制转换不同位置,但一切我尝试并不完全工作了。

- 注意::我有使用dup2重定向到stdin和stdout为孩子们这个程序的工作版本,但我实际上通过管道描述了孩子根据项目的说明。

任何帮助是AP preciated。


解决方案

只要铸造文件描述符到的char * 是不是一个好主意。它可能会导致数据丢失,如果操作系统选择复制串,并在0字节停止,所以数据不完全复制。这将是更安全创建包含文件描述符的字符串。

  INT的pfd [2];
如果(管道(PFD)== - 1)
    exitWithError(管子失败,1);
字符字符串1 [12]; //一个32位的数字不能拿超过11个字符,一个+ 0终止
的snprintf(string1,12,%I,PFD [1]); //文件描述符复制到一个字符串
字符* args_1 [] = {读者,ARGV [1],和放大器;字符串1 [0],(的char *)0};

阅读器程序,然后使用的atoi 转换这一回一个int。

  INT FD =的atoi(argv的[2]);
写(FD,BUF,read_test);


如果你真的想用一个演员,那么你需要投的argv [2] 为int * 在你的读者。

 写(((INT *)的argv [2])[1],BUF,read_test);

I am wondering how I can pass a file descriptor thru the execve command and then access it on the other side. I know that I can use dup2 to redirect the filedescriptor but I CANNOT do that. I am required to actually pass the file descriptor to the child and use it in the child.

What I have so far:

Parent makes pipe + args like so:

int pfd[2];
if(pipe(pfd) == -1)
    exitWithError("PIPE FAILED", 1);
char *args_1[] = {"reader", argv[1], (char*) pfd, (char *) 0};

Then the child calls execve after fork like so:

close(pfd[1]);
execve("./reader", args_1, NULL);

Then, in the reader program I try to access pipe descriptor that was passed:

int main(int argc, char* argv[]){
    ...
    write(argv[2][1], buf, read_test);

^^argv[2] should be referencing the pipe descriptor, then the [1] should go to the write end of the pipe. I am almost positive that I need to do some type-casting differently here, but everything I try doesn't quite work out.

--NOTE::I have a working version of this program using dup2 to redirect to stdin and stdout for the children, but I have to actually pass the pipe descriptor to a child as per instructions of the project.

Any help is appreciated.

解决方案

Simply casting the file descriptor to a char* isn't a good idea. It could cause data loss if the OS chooses to copy the strings and stops at the 0 bytes, so the data isn't entirely copied. It would be safer to create a string containing the file descriptor.

int pfd[2];
if(pipe(pfd) == -1)
    exitWithError("PIPE FAILED", 1);
char string1[12]; // A 32 bit number can't take more than 11 characters, + a terminating 0
snprintf(string1,12,"%i",pfd[1]); // Copy the file descriptor into a string
char *args_1[] = {"reader", argv[1], &string1[0], (char *) 0};

The reader program then uses atoi to convert this back into an int.

int fd = atoi(argv[2]);
write(fd,buf,read_test);


If you really want to use a cast, then you need to cast argv[2] as int* in your reader.

write(((int*)argv[2])[1],buf,read_test);

这篇关于通过文件描述符 - 的execve(类型转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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