can someone check this?

This is a discussion on can someone check this? within the Linux Programming forums, part of the Platform Specific Boards category; hi here is the problem: and i only have a few days left.. basically i was wandering if anyone could ...

  1. #1
    Unregistered
    Guest

    can someone check this?

    hi here is the problem: and i only have a few days left..

    basically i was wandering if anyone could help me out here please??

    i have parts of my code down below..

    /************************************

    Due date: Friday, 31st May, 2002. (6:00 p.m.)

    A particular distributed service is envisaged for implementation by means of a program which will run on each of the hosts on which the service is to be available.

    This program is divided conceptually into the application and the protocol entity (PE). The task of the PE is to provide a simple locking service invoked by the application side.

    Your task is to develop sufficient of the PE logic to provide for consistent operation of the lock according to the protocol rules given below. There are other issues, such as failure detection and recovery, which you aren't required to address.

    The locking mechanism is based on token passing, i.e. all participating PE's form a logical ring, sending/receiving messages to/from just one other PE in such a way that a message from any one PE can reach any other PE; being forwarded as necessary by any intervening PE's. A distinguished message represents the token, and the PE at present holding the token, and only that PE, can grant the lock to its application.

    To prevent the token from "buzzing" and thereby consuming communications capacity, it will be held at some PE if there are no outstanding lock requests. A PE requested for the lock may send a demand message around the ring to notify the token holding PE that the token is required.

    The PE's observe the following rules:


    on lock request: if holding token unset holding token; grant lock

    on unlock: unset demand flag; forward token

    on lock needed: circulate demand if demand flag is unset

    on receiving demand message: set demand flag; forward token if holding token

    on receiving token: if waiting for lock then grant lock;
    if demand flag set then unset demand flag; forward token {else hold token}

    demand flag, holding token, etc. are boolean local variables for each participating PE.

    Notes:


    It is not necessary to develop the solution on a network: a single host will be all that's required. Any testing which involves more than a single PE should use additional processes on the one host.

    You may make assumptions to complete the above set of protocol rules if you deem it necessary.

    You are advised to structure your solution logic as an event loop, i.e. using the

    case wait-event ( ... ) of

    structure dealt with in lectures.

    You are required to produce a complete program, but note that the above specification is silent on the application side of the program, so you therefore need to supply only sufficient logic to exercise the PE side. However the application side must have an independent thread of execution capable of interacting with its environment asynchronously with the PE. In other words, the application side and the PE must run as separate processes.

    Black box testing will be insufficent for this exercise, so you will need to include trace statements in your development code to verify that the PE responds correctly to events.

    The PE is make function calls to send/receive messages to/from the ring; these functions must necessarily be stubs in your implementation. You may consider using message queues within these stubs to simulate interaction with the ring.

    You are to assume that the message passing on the ring is asynchronous.

    //*************************************************
    well i spoke to my tutor about the Application part and he told me that the application is implemented under the main method by making a procedure call to the PE, if you know what i mean. , and then the PE is generated by using the fork() process.

    Basically the application part is called the Testing between the PE.
    The Testing (App) sends an application_demand to the PE via message queues, which is known as a way of communicating between processes.

    And the token is communicated between the PE to PE by using the message queues.

    So, i did some research and found something on message queues, all i need help with is the implementation of the application to the PE, if someone is willing to help that would be great because i only have a few days left before the due date..

    here is the code for message queues..all that is needed for my project is the sending/recieving of messages, so discard the other functions in this program.

    kind regards
    Atif

    **********************************************
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>

    #define MAX_SEND_SIZE 80

    struct mymsgbuf {
    long mtype;
    char mtext[MAX_SEND_SIZE];
    };

    void send_message(int qid, struct mymsgbuf *qbuf, long type, char
    *text);
    void read_message(int qid, struct mymsgbuf *qbuf, long type);
    void remove_queue(int qid);
    void change_queue_mode(int qid, char *mode);
    void usage(void);


    int main(int argc, char *argv[])
    {
    key_t key;
    int msgqueue_id;
    struct mymsgbuf qbuf;

    if(argc == 1)
    usage();

    /* Create unique key via call to ftok() */
    key = ftok(".", 'm');

    /* Open the queue - create if necessary */
    if((msgqueue_id = msgget(key, IPC_CREAT|0660)) == -1) {
    perror("msgget");
    exit(1);
    }

    switch(tolower(argv[1][0]))
    {
    case 's': send_message(msgqueue_id, (struct mymsgbuf
    *)&qbuf,
    atol(argv[2]), argv[3]);
    break;
    case 'r': read_message(msgqueue_id, &qbuf,
    atol(argv[2]));
    break;
    case 'd': remove_queue(msgqueue_id);
    break;
    case 'm': change_queue_mode(msgqueue_id, argv[2]);
    break;

    default: usage();

    }

    return(0);
    }

    void send_message(int qid, struct mymsgbuf *qbuf, long type, char
    *text)
    {
    /* Send a message to the queue */
    printf("Sending a message ...\n");
    qbuf->mtype = type;
    strcpy(qbuf->mtext, text);

    if((msgsnd(qid, (struct msgbuf *)qbuf,
    strlen(qbuf->mtext)+1, 0)) ==-1)
    {
    perror("msgsnd");
    exit(1);
    }
    }

    void read_message(int qid, struct mymsgbuf *qbuf, long type)
    {
    /* Read a message from the queue */
    printf("Reading a message ...\n");
    qbuf->mtype = type;
    msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);

    printf("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
    }

    void remove_queue(int qid)
    {
    /* Remove the queue */
    msgctl(qid, IPC_RMID, 0);
    }

    void change_queue_mode(int qid, char *mode)
    {
    struct msqid_ds myqueue_ds;

    /* Get current info */
    msgctl(qid, IPC_STAT, &myqueue_ds);

    /* Convert and load the mode */
    sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);

    /* Update the mode */
    msgctl(qid, IPC_SET, &myqueue_ds);
    }

    void usage(void)
    {
    fprintf(stderr, "msgtool - A utility for tinkering with msg
    queues\n");
    fprintf(stderr, "\nUSAGE: msgtool (s)end <type>
    <messagetext>\n");
    fprintf(stderr, " (r)ecv <type>\n");
    fprintf(stderr, " (d)elete\n");
    fprintf(stderr, " (m)ode <octal mode>\n");
    exit(1);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Well it looks like a reasonable start, but you need to do a bit more planning

    For instance, you only create one message queue, where in fact you need several.

    Each PE needs three message queues
    - one to talk (send and receive) with the controlling app
    - one to talk (send) to the next PE in the ring
    - one to listen (receive) from the previous PE in the ring

    > key = ftok(".", 'm');
    According to the manual page, you need a file
    "." is a directory

    So draw a ring of PE's, draw lines to represent the message channels, and write numbers on them to represent the queue identifiers (like you would pass to ftok)

  3. #3
    Unregistered
    Guest
    Thanks..for your help salem, but could you be a bit more specific about the PE, like how does the PE to PE work..
    How can i send and receive messages from 1 PE to another...

    And for the App i just make function calls from the main to the PE right.

    Atif

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > How can i send and receive messages from 1 PE to another
    You use that send_message and read_message you've written

    > And for the App i just make function calls from the main to the PE right
    No, you send and receive messages (as your homework describes).

    Code:
    #define NUM_PE  10
    
    // PE->PE are numbered 0..9
    // PE<->host are numbered 10..19
    void protocol_entity ( int this_pe ) {
        key_t  next_pe_key = ftok("lockfile", (this_pe + 1) % NUM_PE ); 
        key_t  host_key    = ftok("lockfile", this_pe + NUM_PE );
        // now each instance of the PE have two message keys
        // one for the next PE in the ring of PE's
        // one for communicating with the Application
        // receive and send messages as appropriate
    }
    
    int main ( ) {
        int i;
        for ( i = 0 ; i < NUM_PE ; i++ ) {
            int res = fork();
            if ( res == 0 ) {
                protocol_entity( i );
            } else
            if ( res == -1 ) {
                perror( "Fork" );
                exit( 1 );
            }
        }
        // now do your main task here, of choosing a PE
        // to talk to
        // choose a channel ID in the range 10..19 to talk to
        // a PE
        return 0;
    }

    > on lock request: if holding token unset holding token; grant lock
    > on unlock: unset demand flag; forward token
    > on lock needed: circulate demand if demand flag is unset
    > on receiving demand message: set demand flag; forward token if holding token
    > on receiving token: if waiting for lock then grant lock;
    if demand flag set then unset demand flag; forward token {else hold token}

    Make each one of these a separate function, which is called from the protocol_entity() function.

    Good Luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 08:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2002, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21