LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-23-2011, 10:04 AM   #1
liudaisuda
LQ Newbie
 
Registered: Sep 2011
Posts: 1

Rep: Reputation: Disabled
IPC shared Memory


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;
}
 
Old 09-24-2011, 11:00 PM   #2
gary185
Member
 
Registered: Jul 2011
Posts: 113

Rep: Reputation: Disabled
Hi,
I'm not that good at this stuff but i will give it a shot without running it
i love questions like this.

something strikes me as not correct about the shmget

i think you didn't "create" it
might be wrong
Code:
//Allocate a shared memory segment
	segment_id = shmget(IPC_PRIVATE, sizeof(*buffer),IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
the rest of it looks like a race condition weighting to happen
i see what you are trying to do with the flag but i think the proper way to do that is to use
shmget and semget together
semget is a binary semaphore with a key just like the shm that has wait and post.

http://publib.boulder.ibm.com/infoce...apiexusmem.htm
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to share a list of structure using in shared memory in linux IPC Guptarulz Programming 11 10-09-2009 11:31 PM
Doubt with shared memory (IPC). webquinty Linux - Newbie 0 11-05-2008 10:51 AM
Shared Memory vs. Pipes for IPC ta0kira Programming 11 12-23-2007 03:56 PM
IPC-shared memory hegdeshashi Linux - Server 5 02-27-2007 03:46 AM
IPC Shared Memory support in kernel? stevho Linux - General 1 01-17-2002 07:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration