Unix - Fifo

Posted on 2005-04-21
Last Modified: 2012-06-27
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)
Question by:KouKi
    LVL 22

    Accepted 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.


    Author Comment

    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 ?
    LVL 22

    Expert Comment

    > 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.

    Author Comment

    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 :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now