传递文件描述符 - Execve(类型转换)
我想知道如何通过 execve() 命令传递文件描述符,然后在另一端访问它。我知道我可以使用 dup2 来重定向文件描述符,但我不能这样做。我需要将文件描述符实际传递给孩子并在孩子中使用它。
到目前为止我所做的:
父进程使 pipe
+ args 如下所示:
int pfd[2];
if(pipe(pfd) == -1)
exitWithError("PIPE FAILED", 1);
char *args_1[] = {"reader", argv[1], (char*) pfd, (char *) 0};
然后子进程在 fork()
之后调用 execve
,如下所示:
close(pfd[1]);
execve("./reader", args_1, NULL);
然后,在读取器程序中,我尝试访问传递的管道描述符:
int main(int argc, char* argv[]){
...
write(argv[2][1], buf, read_test);
argv[2] 应该引用管道描述符,然后 argv[1] 应该转到管道的写入端。我几乎肯定我需要在这里进行一些不同的类型转换,但我尝试的一切都不太成功。
注意:我有一个该程序的工作版本,使用 dup2
为孩子们重定向到 stdin
和 stdout
,但我实际上必须按照项目的说明将管道描述符传递给孩子。
任何帮助表示赞赏。
I am wondering how I can pass a file descriptor through the execve()
command and then access it on the other side. I know that I can use dup2
to redirect the file-descriptor 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 done so far:
Parent makes pipe
+ args like the following:
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 the following:
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 argv[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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地将文件描述符转换为
char*
并不是一个好主意。如果操作系统选择复制字符串并在 0 字节处停止,因此数据未完全复制,则可能会导致数据丢失。创建一个包含文件描述符的字符串会更安全。然后,阅读器程序使用atoi将其转换回int。
如果您确实想使用强制转换,则需要在阅读器中将
argv[2]
强制转换为int*
。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.The reader program then uses
atoi
to convert this back into an int.If you really want to use a cast, then you need to cast
argv[2]
asint*
in your reader.