UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-12-10 09:38 AM

rahulnitk2004
Member
Registered: 2008-12-08
Posts: 2

Re: Inter processor communication

Inter processor communication (Dont get confused with inter process comm.).

  Hi,
      I'm working on project to create libraries for inter processor communication
(Simulation using threads: each thread represents a processor)
.
The scenario is like this : I have 2 processors. i want to craete API's like

SendMessage(Source processor,dest. processor,message,messagelength)

What i am planning to do is to create a shared memeory and write the message to the shared memory using memcopy(). Signal the dest processor after writing int to the memory so that the dest. processor can read it.

But the problem here is, the shared memory size is limted. So when shared memory overflows, we have to find an alternative mechanism to directly
send the message to the processor. Is there any mechanism for this??

Please help on this topic....

Thanks

Offline

#2 2008-12-10 06:13 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Inter processor communication

Well, the easiest way to do it would be to use pipes (or unix domain sockets, or etc) and avoid using shared memory at all.  If your messages are small (e.g. single integers) this will be much simpler than shared memory and no less efficient.

That said, if you are absolutely set on using shared memory (e.g. because your messages may be quite large, or for some other reason), then I would set up a double-ended queue (or linked list, or some other FIFO data structure that you like) in the shared memory region, and serialize access to the queue with a Mutex.  Then SendMessage() function might something like this (pseudocode):

status_t SendMessage(const Message & msg)
{
   status_t ret = RESULT_ERROR; 

   if (_mutex.Lock() == RESULT_OK)
   {
      ret = _dequeue.AppendToTail(msg);
      if ((ret == RESULT_OK)&&(_dequeue.GetNumItems() == 1)) 
      {
         // When the queue goes from empty to not-empty, tell the
         // consumer thread to wake up and check the queue ASAP
         SignalConsumerThreadThatMessagesAreAvailableInDequeue();
      }
      _mutex.Unlock();
   }
   return ret;
}

... and ReceiveMessage() might look like this:

status_t ReceiveMessage(Message & msg)
{
   status_t ret = RESULT_ERROR; 

   if (_mutex.Lock() == RESULT_OK)
   {
      ret = _dequeue.RemoveFromHead(msg);
      if ((ret == RESULT_OK)&&(_dequeue.GetNumItems()+1 == _dequeue.GetCapacity())) 
      {
         // When the dequeue goes from full to not-full, signal
         // the producer thread that now is a good time to try to add
         // any additional Messages it may have to the dequeue
         SignalProducerThreadThatDequeueIsNoLongerFull();
      }
      _mutex.Unlock();
   }
   return ret;
}

... The Signal*() functions shows above could be implemented in various ways; one way would be to simply send a dummy byte on a socket whose remote end is held by the other thread. 

The consumer thread should wait for a signal from the producer thread, and when it gets one, it should call ReceiveMessage() in a loop (and handle the resulting Message objects it gets from ReceiveMessage()) until ReceiveMessage returns RESULT_ERROR, indicating that the shared dequeue is now empty.  At that point the receive thread can go back to sleep until it receives the next signal from the producer thread.

The only remaining issue is what the producer thread should do if SendMessage() returns RESULT_ERROR (presumably because the dequeue was full).  In that case, I would have SendMessage() append its unsent Message to the end an "overflow dequeue" that it keeps in local (non-shared) memory.  The next time the producer thread receives a "dequeue is no longer full" signal from the consumer thread, it would then try to send some more messages from the head of the overflow queue, and so on.  Of course, any time a new Message is generated while there are already Messages in the overflow Queue, that Message should be appended to the end of the overflow Queue instead of being sent directly... that way the Messages will still be sent in FIFO order, which is usually what you want.

Hope that helps,
Jeremy

Offline

#3 2008-12-11 09:38 AM

rahulnitk2004
Member
Registered: 2008-12-08
Posts: 2

Re: Inter processor communication

Many thanks......

I will work on what u mentioned.....hopeful to get a result this time..


Once again thanking you.

Bye.

Offline

#4 2009-04-12 10:45 PM

parisa1984
Member
Registered: 2009-04-12
Posts: 29

Re: Inter processor communication

Hi, I am trying to write a simple server and a simple client program in which client connects to a server and sends a peiece of unimportant message that user specifies such as hi server! or what ever. here I have bot hthe codes for server and client. but the problem is that the server is not able to reciev the message from the client. it seems that the accept does not return a suitable socket id, or may be problem is somewhere else that I will be happy if you can help me. actually I realize there is a problem because on the recv() it returns -1 saying: socket operation on non socket.

client code:

/*
*   this is a cllient program only, who opens a connection to a server and tries to connect to through a TCP socket and send a message to.
*
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>

main(int argc, char ** argv)
{

int sid;
int counter=0;
struct sockaddr_in server_addr;
char message2server[1024];
int bytes_sent=0

if(argc!=3)
{
perror("incompatible use of this function, Try Again!1\n");
exit(1);
}
sid=socket(AF_INET,SOCK_STREAM,0);

server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr(argv[1]);
server_addr.sin_port=htons(atoi(argv[2]));

if(connect(sid,(struct sockaddr *)&server_addr,sizeof(server_addr))<0)
{
perror("connection of client to server failed. \n");
exit(1);
}


//while(1)
{
fprintf(stdout,"enter the message to send to server: ");
gets(message2server);
message2server[strlen(message2server)]='\0';
fprintf(stdout,"%s &&& the size is: %d\n",message2server,strlen(message2server));
bytes_sent=sendto(sid,message2server,strlen(message2server),0,(struct sockaddr*)&server_addr,sizeof(server_addr));
if(bytes_sent<0)
{
fprintf(stdout,"Problem in sending bytes\n");
exit(1);
}
fprintf(stdout,"number of bytes sent:%d \n",bytes_sent);
close(sid);
//break;
}
exit(0);
}

-----------------------------
server code:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<sys/select.h>

main(int argc, char **argv)
{
int sid;
int true=1;
int new_socket_id=9;
unsigned int client_addr_size;
int received_bytes=0;
struct sockaddr_in server_address, client_address;
char messageFROMclient[1024]="";



if(argc!=3)
{
fprintf(stdout,"incompatible use: Try Again!\n");
exit(1);
}
sid=socket(AF_INET,SOCK_STREAM,0);

server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=inet_addr(argv[1]);
server_address.sin_port=htons(atoi(argv[2]));

if(bind(sid,(struct sockaddr* )&server_address, sizeof(server_address))<0)
{
perror("There was a problem in server for addrees binding.\n");
exit(1);
}

if(listen(sid,5)<0)
{
perror("there is no conection coming to the server.\n");
exit(1);
}
client_addr_size=sizeof(client_address);
//while(1)
{


if(new_socket_id=accept(sid,(struct sockaddr*)&client_address,&client_addr_size)<0)

{
perror("acception fault. !\n");
exit(1);
}


while(1)
{
fprintf(stdout," new id is: %d\n and old sid is: %d \n",new_socket_id,sid);

//do
{

received_bytes=recvfrom(new_socket_id,messageFROMclient,1024,0,(struct sockaddr*)&client_address,&client_addr_size);

}
//while(received_bytes==-1);

if(received_bytes<0)
{

perror("Some thing is wrong: We did not receive any thing !\n");
close(sid);
close(new_socket_id);
exit(1);
}
messageFROMclient[received_bytes]='\0';
fprintf(stdout,"This comes from client:number of received charcters: %d  %s\n",received_bytes,messageFROMclient);
close(new_socket_id);
close(sid);
}

}//end of while(1)

}

Offline

Board footer

Powered by FluxBB