gpt4 book ai didi

交叉内存连接。无法使用管道发送远程地址

转载 作者:行者123 更新时间:2023-11-30 15:47:33 24 4
gpt4 key购买 nike

我问了有关跨内存附加 (cross memory attach. How do I get the remote address from a child process to a parent process) 的问题

我正在使用管道将地址从子进程传输到父进程。代码:

#include <stdio.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

int main()
{
int i, nbytes; //nbytes to keep a count of the no. of bytes received
int fd[2]; //file descriptor
int pid1, ppid;
char temp;
char string[] = "hello world\n";
char *readbuffer = NULL;

int address;

ppid = getpid();
printf("I am the parent process pid : %d \n", ppid);

pipe(fd);
pid1 = fork(); //child A

if(pid1 == -1){
perror("fork");
return 1 ;
}

if(pid1 == 0){ //body of the child process
readbuffer = string;
printf("child process A with pid : %d\n",getpid());
printf("Address of the string : %p\n", readbuffer);
//snprintf(string, 80,"%p\n",address);
close(fd[0]);
write(fd[1], readbuffer, sizeof(readbuffer));
} else {
printf("I am the parent : %d\n", getpid());
close(fd[1]);
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("The address of the rcvd buffer : %s\n", readbuffer);
}

return 0;

}

父进程从“readbuffer”指针中接收子进程的字符串地址。但我看不到 child 发送的“字符串”的地址。请告诉我我是否正确使用管道。

最佳答案

指针和值之间存在很多混淆。 write(readbuffer)发送readbuffer指向的内存,但是sizeof(readbuffer)是指针本身的大小。在父子句中,您尝试读取 readbuffer 指向的缓冲区,这应该会崩溃,因为 readbuffer 未初始化。

您可能想要write(fd[1], &readbuffer, sizeof(readbuffer))以及同样的read(),然后确保您正在打印readbuffer 作为指针,正如@alk 提到的。如果您这样做,请将变量 readbuffer 重命名为更好地反射(reflect)其用法的名称。

关于交叉内存连接。无法使用管道发送远程地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17325712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com