I was working on a very simple program which implements IPC by using shared memory in C. But it didn't work. I have been thinking for two days. Please help me with it.
The program includes both the client and server side.The client sends a string to server, then the server reverses the string and sends it back to the client. But when I ran the program, it seemed that client and server was waiting for each other(self-blocking?) Please take a look at it. Thanks a lot. Here's the code.
First, the Message structure --- data.h
Code:
/*Message struct to store the message and a flag whether
/the message has been processed by the server */
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];
};
typedef struct Mystruct Message;
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 <unistd.h>
#include "data.h"
//Function to reverse a string
void strrev(char *str) {
int i, N = strlen(str);
char ch;
for ( i = 0; i < N/2; i++) {
ch = str[i];
str[i] = str[N-1-i];
str[N-1-i] = ch;
}
}
int main() {
//shared memroy segment ID
int segment_id;
//shared memory buffer
Message *buffer = 0;
//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
printf("Shared memory segment ID is %d\n", segment_id);
//Initialize the Message structure and send a hello message to client
strcpy(buffer->str, "How are you?");
buffer->ID = 1;
while (true) {
//waiting for the client message
for(; ;) {
sleep(1);
if( buffer->ID == 0 || buffer->ID == 99 )
break;
}
if( buffer->ID == 0 ) {
strrev( buffer->str );
buffer->ID == 1;
}
//detach the memory segment signaled by the client
//99 is a signal of detaching memory
if( buffer->ID == 99 ) {
shmdt(buffer);
sleep(1);
shmctl(segment_id, IPC_RMID, NULL);
break;
}
}
return 0;
}
The client code --client.c
Code:
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <unistd.h>
#include "data.h"
int main( ){
//shared memory segment ID
int segment_id;
//shared memory buffer
Message *buffer = 0;
//Manual selection of segment ID
printf("Enter the shared memroy segment ID: ");
scanf("%d", &segment_id);
getchar();
//Attach the shared memory segment
buffer = (Message*) shmat(segment_id, NULL, 0);
while(true) {
//get message from user input in the client side
printf("Enter the message you want to swap: ");
gets(buffer->str);
buffer->ID = 0;
//wait for the server to process the message
while(true) {
sleep(1);
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.\n");
char command;
command = getchar();
getchar();
if(command == 'N'|| command == 'n')
break;
}
//Notify the server to close the connection
buffer->ID = 99;
//Detach the shared memory segment
shmdt(buffer);
return 0;
}