0

I'm trying to pipe a structure from multiple child processes back to a parent. The structure

typedef struct node* Node;
struct node{
    char *str;
    int num;
    struct node* next;
};

is set up as so. When i read/write with the pipe and try to print the structure fields out, it says my variable is requesting something from a structure but is not a structure.

wordc-mp-main.c:123:44: error: request for member ‘num’ in something   not a structure or union
printf("%s, count %d\n", taken->str, taken->num);
                                        ^

Here is the pipe code from the Child processes:

close(fd[0]);
fclose(inputFile);
Node* pass = wordList->head;
write(fd[1], pass, sizeof(Node));

Here is the code from the Parent:

close(fd[1]);
Node* taken;
read(fd[0], taken, sizeof(Node));
printf("%s, count %d\n", taken->str, taken->num);

Please help and thank you.

update: removed * from the nodes and made the one

Node taken = (Node) malloc(sizeof(Node));

I get no segmentation faults, but my structure did not pass through the pipe correctly.

  • 2
    And this is why you don't hide your indirection through `typedef`s. – EOF Mar 18 '16 at 22:56
  • Note that a structure that contains 2 pointers is not a good candidate for being sent between processes. The pointer in the sending process is, in general, invalid in the receiving process; you've not passed the data that the pointers are pointing at. This will probably cause crashes once you've got beyond the compilation problems. – Jonathan Leffler Mar 18 '16 at 23:31
  • See [Is it a good idea to `typedef` pointers](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) for a detailed discussion of @EOF's point. – Jonathan Leffler Mar 18 '16 at 23:32

1 Answers1

0

Node is typedef struct node* Node (i.e. Node is already a pointer definition). So, you have an extra layer of indirection:

Change:

Node *taken;

Into:

Node taken;
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • When I did that, I got a segmentation fault. – Tyler Hendrick Mar 18 '16 at 23:02
  • You'll get segmentation faults if you pass pointers to memory allocated in a child process and try to dereference them in the parent process. – Crowman Mar 18 '16 at 23:15
  • @PaulGriffiths Yes, that may be possible [and I was aware of such possibilities]. But, OP's question was about a compilation error. He didn't post enough to do deeper analysis. Maybe he will when he sees this comment. – Craig Estey Mar 18 '16 at 23:22
  • I got rid of the segmentation fault by adding a malloc to taken. It executes, but my information is not passing through the pipe. – Tyler Hendrick Mar 18 '16 at 23:23
  • @TylerHendrick: The information is not passing through the pipe because you're not writing the information to the pipe. You pass a pointer over the pipe (two pointers, in fact), but the data that they point at is only in the sending program — not in the receiving program. You need to look at how serialization is done. One way is to use a structure containing an `char str[60];` (and no `struct node *next;`). Other ways include TLV (type, length, value) encoding of what you're sending over the pipe. – Jonathan Leffler Mar 18 '16 at 23:43