Thread: Shared memory IPC Help!!!

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    Shared memory IPC Help!!!

    I was working on a simple example of IPC using the approach of Shared memory in C. And I have test it in the linux environment. It turned out that there could be some syntax problems with the code. I am not familiar with C programming language, and could not pinpoint the error of the code. Please help me. Any help would be appreciated. Thanks a lot. By the way, the error I got is near at the line containing main(). At least, that's what the compiler told me.


    Server.c
    Code:
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include <data.h>
    int main() {
        //shared memroy segment ID
        int segment_id;
        //shared memory buffer
        Message*  buffer;
        //Allocate a shared memory segment
        segment_id = shmget(IPC_PRIVATE, sizeof(*buffer), S_IRUSR | S_IWUSR);
        //Attach the shared memory segment
        buffer = (Message*) shmat(segment_id, NULL, 0);
        //print the shared memory segment id on the screen
        print("Shared memory segment ID is %d\n", segment_id);
        
        while (true) {
            for(; ; ) {
                sleep(100);
                if(buffer->ID==0) break;
            }    
            //process the message sent by the client
            //Reverse the message string sent by the client
            char* strpt = strrev(buffer->str);
            //sent back the result to the client
            buffer->str == strpt;
            buffer->ID = 1;
        
            if(buffer->ID==-1) {
                //Detach the shared memory segment
                shmdt(buffer);
                //Remove the shared memory segment
                shmctl(segment_id, IPC_RMID, NULL);
                break;
            }
        }
        
        return 0;
    }
    
    //Function to reverse a string 
    char* strrev(char* str) {
    
    
      int length = strlen(str);
      char* result = malloc(length + 1);
      if( result != NULL ) {
        result[length] = '\0';
        for ( int i = length - 1, int j=0;   i >= 0;   i--, j++ )  
            result[j] = string[i];
      }
      return result;
    }



    client
    Code:
    #include <sys/types.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <curses.h>
    #include <string.h>
    #include <stdlib.h>
    #include <data.h>
    
    
    int main( ){
    
    
        //shared memory segment ID
        int segment_id;
        //shared memory buffer
         Message * buffer;
        //Manual selection of segment ID
        printf("Enter the shared memroy segment ID: ");
        scanf("%d", &segment_id);
        //Attach the shared memory segment
        buffer = ( Message* ) shmat(segment_id, NULL, 0);
        
        //Enter the Message to be sent to the client
        while(true) {
            printf("Enter the message you want to swap: ");
            gets(buffer->str);
            buffer->ID = 0;
            while(true) {
                //wait for the server to process the messa 
                    sleep(100);
                    if(buffer->ID==1) break;
            }
            //display the message replied by the server
            printf("The server reply: %s\n", buffer->str);
    
    
            //Manual selection of quit the connection and exit
            printf("Do you want to continue? Press Y to continue, N to exit.");
            char* command;
            gets(command);
            if(command=="N"|| command=="n") break;
        }
        
        //Notify the server to close the connection
        buffer->ID = -1;
        //Detach the shared memory segment
        shmdt(buffer);
        
        return 0;
    }


    The Message is simply a struct
    Code:
    struct Mystruct {
       /*Message status: 1-processed by the server. 
                         0-not processed by the server.
                         -1-notify the server to close the connection. */
       int ID;
       //string to store the message string
       char str[255];
    }Message;
    Attached Files Attached Files
    Last edited by liudaisuda; 09-20-2011 at 08:43 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Not many people are going to download files posted by people they do not know. Posting Code?--Read First
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    I have fixed it. Thanks for your suggestion

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Note, this thread is duplicated here: syntax error Help!!.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL in Shared Memory
    By AlenDev in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2010, 04:04 AM
  2. ICP shared memory
    By cavemandave in forum C Programming
    Replies: 1
    Last Post: 11-20-2007, 06:08 AM
  3. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 02:29 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM
  5. using shared memory in c
    By flawildcat in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 12:25 PM

Tags for this Thread