Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 交叉内存连接。无法使用管道发送远程地址_C_Unix_Ipc - Fatal编程技术网

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

C 交叉内存连接。无法使用管道发送远程地址,c,unix,ipc,C,Unix,Ipc,我问了我关于跨内存连接的问题() 我正在使用管道将地址从子进程传输到父进程。 代码: #包括 #包括 #包括 #包括 #包括 int main() { int i,nbytes;//nbytes用于记录接收的字节数 int fd[2];//文件描述符 int-pid1,ppid; 焦炭温度; 字符字符串[]=“hello world\n”; char*readbuffer=NULL; int地址; ppid=getpid(); printf(“我是父进程pid:%d\n”,ppid); 管道(f

我问了我关于跨内存连接的问题()

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

#包括
#包括
#包括
#包括
#包括
int main()
{
int i,nbytes;//nbytes用于记录接收的字节数
int fd[2];//文件描述符
int-pid1,ppid;
焦炭温度;
字符字符串[]=“hello world\n”;
char*readbuffer=NULL;
int地址;
ppid=getpid();
printf(“我是父进程pid:%d\n”,ppid);
管道(fd);
pid1=fork();//子对象A
如果(pid1==-1){
佩罗尔(“福克”);
返回1;
}
if(pid1==0){//子进程的主体
readbuffer=string;
printf(“pid为%d\n的子进程A”,getpid());
printf(“字符串地址:%p\n”,readbuffer);
//snprintf(字符串,80,“%p\n”,地址);
关闭(fd[0]);
写入(fd[1],readbuffer,sizeof(readbuffer));
}否则{
printf(“我是父项:%d\n”,getpid());
关闭(fd[1]);
nbytes=读取(fd[0],读取缓冲区,sizeof(读取缓冲区));
printf(“rcvd缓冲区的地址:%s\n”,readbuffer);
}
返回0;
}

父对象从“readbuffer”指针中的子对象接收字符串的地址。但我看不到孩子发送的“字符串”的地址。请让我知道我是否正确使用管道。

在父级中,它应该是:

printf("The address of the rcvd buffer : %p\n", (void *) readbuffer);
作为一个指针值(一个地址),应该打印出来,而不是它所指向的“字符串”


此外,代码也不能确保收到
sizeof(readbuffer)
字节,甚至对
read()
的调用也没有失败


更新:

Peter在回答中也提到,要读取/写入
readbuffer
的值,必须将其地址传递到
write()
/
read()


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


您可能需要
write(fd[1]、&readbuffer、sizeof(readbuffer))
和类似的
read()
,然后确保将readbuffer打印为指针,如@alk所述。如果这样做,请将变量readbuffer重命名为更好地反映其用法的名称。

看不到地址是什么意思?如果你把它打印出来,它是一个数字吗?我在阅读时没有使用符号。这很有帮助。谢谢。
printf("The address of the rcvd buffer : %p\n", (void *) readbuffer);
write(fd[1], &readbuffer, sizeof(readbuffer));

...

nbytes = read(fd[0], &readbuffer, sizeof(readbuffer));