我曾问过关于跨内存附加的问题(跨内存附加。如何将远程地址从子进程获取到父进程)
我正在使用管道将地址从子进程传输到父进程。代码:
#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”指针中的子级接收字符串的地址。但是我看不到孩子发送的“字符串”的地址。如果我正确使用管道,请告诉我。