Home > database >  Is correct this pointer value? (Sending a pointer through a pipe in C)
Is correct this pointer value? (Sending a pointer through a pipe in C)

Time:11-23

It is not a hard doubt, but I'm not sure.

  1. My goal: I need a C program that writes an only variable at the same time by 2 instances of software.
  2. What I Expect: write the variable using a parent and a child process, and print the value of that variable at the time each process has his time of processor use.
  3. My actual results: I have a program, with a parent that sends a number and a child that reads.
  4. Errors/warnings: None (at now).
  5. What I've tried: Here come my questions. I'm trying to send a pointer using the pipe. when I read it from the parent process it shows a value for that pointer, but when I read it from the child, it reads (partialy) the same value for the pointer but "truncated".

My questions:

  • Is this the same pointer just printed different?
  • If it isn't, how can I fix the pointer value for being the same between the parent and child?

The code is as follows:

#include <sys/types.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#define DIM 100000

int main(void){
    printf("\n Buffer del sistema = %d\n ",BUFSIZ);
    int bytes_sent = 0, bytes_read = 0, i = 0, *FD=NULL;
    FD = (int*)calloc(2,sizeof(int));
    int *buffer = (int*)calloc(1,sizeof(int));
    int pid = 0;
    if(!pipe(FD)){
        pid = fork();
        if (pid < 0){
            perror("Error al crear bifurcación");
            exit(pid);
        }
        if (pid == 0){
            for(i=0;i<=27;i  ){
                bytes_read = read(*FD,buffer,8);
                printf("\nEl proceso hijo ha leído %d bytes.\nEl contenido es: %p\n", bytes_read,*(buffer));
            }
        exit(0);
        }
        else{
            int a = 10, *z = &a;
            int **pp = &z;
            printf("\nApuntador: %p\n", z);
            for(i=DIM;i<DIM 30;i  ){
                bytes_sent = write(*(FD 1),pp,sizeof(int*));
                printf("\nEl proceso padre ha enviado %d bytes\n",bytes_sent);
                usleep(DIM);
            }
        }
    }
    return 0;
}

And the output at execution is this:


 Buffer del sistema = 8192
 
Apuntador: 0x7ffedc1370ec

El proceso padre ha enviado 8 bytes
 
El proceso hijo ha leído 8 bytes.
El contenido es: 0xdc1370ec

... some repeated output here ...

El proceso hijo ha leído 8 bytes.
El contenido es: 0xdc1370ec

El proceso padre ha enviado 8 bytes

El proceso padre ha enviado 8 bytes


------------------
(program exited with code: 0)
Press return to continue

Thanks in advance!

CodePudding user response:

The problem is that buffer is declared as a pointer to int, but it should be a pointer to pointer to int. Thus the value that the child process is printing, *(buffer), is not passed as a pointer, but an int which apparently is 4 bytes on your system while a pointer is 8 bytes.

To fix this, change the line

    int *buffer = (int*)calloc(1,sizeof(int));

into

    int **buffer = (int**)calloc(1,sizeof(int *));
  • Related