tomhoq
tomhoq

Reputation: 57

How to use read() to get the adress of a pointer?

I have 2 programs communicating with each other through a fifo, one's the writer the other's the reader.

The writer sends a pointer to a struct containing information. The reader should receive the pointer and be able to see the information inside the struct.

Header file:

typedef struct req{
    int _code;
    char _client_pipe[PIPENAME];
    char _box_name[BOXNAME];
} request;


/*writes to pipe tx a pointer with information*/
void send_request(int tx, request *r1) {
    ssize_t ret = write(tx, &r1, sizeof(r1)); 
    if (ret < 0) {
        fprintf(stdout, "ERROR: %s\n", ERROR_WRITING_PIPE);
        exit(EXIT_FAILURE);
    }
}

/*Returns a pointer to a struct containing the request*/

request *serialize(int code, char* client_pipe, char* box_name){
    request *r1 = (request*) malloc(sizeof(request));
    r1->_code  = code;
    strcpy(r1->_client_pipe, client_pipe); 
    strcpy(r1->_box_name, box_name); 
    return r1;
}

Program writer:

int main(int argc, char **argv){
    (void *) argc; // in my program i used argc, but for this problem it's not important hence why the //typecast to void

    char register_pipe[PIPENAME]; 
    char personal_pipe[PIPENAME];
    char box_name[BOXNAME];

    strcpy(register_pipe, argv[1]);
    strcpy(personal_pipe, argv[2]);
    strcpy(box_name, argv[3]);

    int reg_pipe = open(register_pipe, O_WRONLY);
    if (reg_pipe == -1) {
        fprintf(stdout, "ERROR: %s\n", UNEXISTENT_PIPE);
        return -1;
    }

    send_request(reg_pipe, serialize(1, personal_pipe, box_name));
}

Program reader:

    char register_pipe[PIPENAME]; 

    strcpy(register_pipe, argv[1]);

    if(mkfifo(register_pipe, 0644) < 0)
        exit(EXIT_FAILURE);
    
    if ((reg_pipe = open(register_pipe, O_RDONLY)) < 0){ 
        exit(EXIT_FAILURE);
    }

    
    if ((reg_pipe = open(register_pipe, O_RDONLY)) < 0){ 
        exit(EXIT_FAILURE);
    }
    
    request* buffer = (request*) malloc(sizeof(request));  //this might be the issue but not sure
    ssize_t broker_read= read(reg_pipe, buffer, 256);    //is not reading correctly
    printf("%d, %s, %s\n", buffer->_code, buffer->_client_pipe, buffer->_box_name);

So if i start program reader and set register pipe as "reg", this will create the register pipe and wait for someone to join it. Then if i start the program writer like ./writer reg personal box this will open the reg pipe correctly, create a struct of type request and then sent it to the reader.

The reader should receive a pointer to a struct req set like:

   _code = 1;
   _client_pipe[PIPENAME] = "personal";
   _box_name[BOXNAME] = "box";

The reader is in fact receiving but for some reason it's not receiving correctly. If i try to print like in the last line, it will output some random numbers and letters.

How can i fix this?

Upvotes: 0

Views: 13

Answers (1)

mevets
mevets

Reputation: 10445

You would need to have that structure exist inside a shared memory region that you have arranged to be mapped into both processes at the same address.

Without some such arrangement, each process has a private address space, so an address known to process A is meaningless to process B.

How to make such an arrangement is very much dependent upon you operating system, and perhaps even variant of said operating system.

You will likely find it easier to just copy the structure, as opposed to its address, via the fifo.

Upvotes: 1

Related Questions