PDA

View Full Version : Shared memory IPC Help!!!



liudaisuda
09-20-2011, 06:31 PM
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


#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


#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


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;

AndrewHunter
09-20-2011, 06:40 PM
Not many people are going to download files posted by people they do not know. Posting Code?--Read First (http://cboard.cprogramming.com/linux-programming/announcement-posting-code-read-first.html)

liudaisuda
09-20-2011, 08:44 PM
I have fixed it. Thanks for your suggestion

anduril462
09-21-2011, 04:14 PM
Note, this thread is duplicated here: http://cboard.cprogramming.com/c-programming/141239-syntax-error-help.html.