• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1696
  • Last Modified:

Unix - Fifo

I have to make two process communicate via a fifo.
the first program is like a mobile and can send different messages to the second (the server).
the mobile write in the fifo and the server read.

the mobile can send different kinds of messages :
1) a message when it is online :
        - type of message
        - process id
        - phone number
2) a message when it is offline
        - type of message
        - phone number
3) a message to a specific user
        - type of message
        - phone number of the sender
        - phone number of the reciever
        - message

My teacher wants that the mobile send into the fifo a union of 3 structures,
but I don't know how to do this, if someone can help ?
(I heard that for example that in the union he wants the 'type of message' and then in the 3 structures the 'type' need to occur again because they all have a 'type' ... hum ... but i didn't understand very good what he was saying)
thank you (srry need to improve my english as well)
  • 2
  • 2
1 Solution
Unions in C are a way of having one data structure with a number of different possible data structures in it.  You usually have a leading field that indicates which type of struct actually follows.  Something like:

typedef struct OnLineType  {  int type, pid, phonenumber; };
typedef struct OffLineType  {  int type,  phonenumber; };
typedef struct OnLineType {  int type, pid, phonenumbersender, phonenumbereceiver, char Message[5000]; };

typedef struct ThreeMsgFormats {
    char Format;
    union {
       OnLineType olt;
       OffLineType oft;
       MsgToUserType  mtu;

When you store values into this union, first set the Format field to something distinctive for each possible sub-structure, say 'n' for on line, 'f' for off line, or 'm' for message.   Then go ahead and set the corresponding data fields.

In this particular case, each type is a superset of the previous type, so you could even declare the fields in the same order to make things a bit easier.

KouKiAuthor Commented:
Thanks I tried it today at school it is working,
I understood the type to select the right structure,
but I don't understand why in each structure I have to put the type one more time ?
> I don't understand why in each structure I have to put the type one more time ?

Because that's what your instructions said!

Actually, now you don't need that type field anymore.
KouKiAuthor Commented:
I know the field type in each structure is still needed because
there would be fields stored at the same adress otherwise.
Anyway thanks for your help :)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now