Page 1 of 1

Messages : what are used for

Posted: Tue Dec 21, 2004 7:10 pm
by aladdin
hello all, i read some docs about message passing, but i did not find a real using example.

are messages used to help sharing ressources between processes ?

if i develop a monolitic kernel, do i need messages ?

can u give me some real examples of message passing ?


thank's all

Re:Messages : what are used for

Posted: Tue Dec 21, 2004 8:19 pm
by Brendan
Hi,
aladdin wrote: are messages used to help sharing ressources between processes ?
Messaging (or IPC in general) is used for communication between processes/threads, and sometimes the kernel. Here "communication" might include controlling resource sharing, keypresses, video data, syncronization, notifications (something started, something changed, something ended), etc - basically anything the software wants to use it for.
aladdin wrote: if i develop a monolitic kernel, do i need messages ?
No, but you probably do need some form of IPC (Inter-Process Communication). On Unix this normally means "pipes", but modern versions of Unix support messaging too (although they might call it "Datagram Socket Operations"). Alternatively, software could use shared files or shared memory to communicate.
aladdin wrote: can u give me some real examples of message passing ?
Look for any source code designed for Windows (or any other OS that uses messages) :)....

Here's an example from the GCC library documentation:

Code: Select all

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SERVER  "/tmp/serversocket"
#define MAXMSG  512

int
main (void)
{
  int sock;
  char message[MAXMSG];
  struct sockaddr_un name;
  size_t size;
  int nbytes;

  /* Remove the filename first, it's ok if the call fails */
  unlink (SERVER);

  /* Make the socket, then loop endlessly. */
  sock = make_named_socket (SERVER);
  while (1)
    {
      /* Wait for a datagram. */
      size = sizeof (name);
      nbytes = recvfrom (sock, message, MAXMSG, 0,
                         (struct sockaddr *) & name, &size);
      if (nbytes < 0)
        {
          perror ("recfrom (server)");
          exit (EXIT_FAILURE);
        }

      /* Give a diagnostic message. */
      fprintf (stderr, "Server: got message: %s\n", message);

      /* Bounce the message back to the sender. */
      nbytes = sendto (sock, message, nbytes, 0,
                       (struct sockaddr *) & name, size);
      if (nbytes < 0)
        {
          perror ("sendto (server)");
          exit (EXIT_FAILURE);
        }
    }
}
Please note that there's hundreds of different ways messaging could be implemented, and there'd be (mostly minor) differences with how each OS's messaging is used.


Cheers,

Brendan

Re:Messages : what are used for

Posted: Wed Dec 22, 2004 6:07 am
by aladdin
ok, thank you for these information, but, what is the content of messages, is there some protocols? or it is up to the OS to decide ?

Re:Messages : what are used for

Posted: Wed Dec 22, 2004 6:36 am
by Pype.Clicker
Everything will depend up to the OS designer. Most of the time, you desing an interface and a way to map interface methods (code & arguments) into a datastructure...

Re:Messages : what are used for

Posted: Wed Dec 22, 2004 7:39 am
by Brendan
Hi,
aladdin wrote: ok, thank you for these information, but, what is the content of messages, is there some protocols? or it is up to the OS to decide ?
There are no protocols (unless you want a clone of some other OS). In general the software that receives the message determines the format of the message, as it must be able to understand it.

For the OS's services (file systems, device drivers, etc) the messaging protocols used should be the same for all software of the same type, and these would be determined by the OS.

For communication between third party software there is no need for an OS defined protocol (whoever writes it can decide), however there's often a few "OS reserved messages" that the OS uses for special events (e.g. for telling the thread/process to terminate, alarms/timeouts, etc).


Cheers,

Brendan

Re:Messages : what are used for

Posted: Wed Dec 22, 2004 8:22 am
by Pype.Clicker
You may find guidelines to structure your messages in something like SunRPC, DCE, etc. Those are middleware for procedure/method call across a channel (the message abstraction offered by the system being a possible channel).

Another interresting reference is python's b-encoding which is easy enough to decode and will provide a good substrate for passing variable-length strings, list of elements, or sets of elements.

Now, much will depend on the underlying limitations of your own scheme. If you wish transparent confirmation of message reception with an asynchronous messaging system, you need to code a protocol with acknowledgments. If you wish to overcome the limited size of messages, you may need to split large message and thus require a protocol that tells the receiver how many messages are left, etc.

In a general way, you write an _interface definition_ of your service, telling what message are valid and how they're encoded and then you write client & server code according to that definition.

Re:Messages : what are used for

Posted: Sat Dec 25, 2004 1:07 pm
by aladdin
ok, i ll give an example and correct me if i'm wrong

suppose we have a microkernel a memory allocator and another process (called P)

P needs to allocate some memory so it use a function which send a message to the kernel telling it it wants specified size of memory

kernel will redirect the message to mem allocator which will allocate memory and send a reply containing the address of allocated area

Re:Messages : what are used for

Posted: Sat Dec 25, 2004 4:24 pm
by Schol-R-LEA
That would be one possible use, yes.

Another would be synchronization. Say, for example, you have wo messaging primitives, send() and recv(), where send() will wait until the recipient recv()s the message, and recv() will wait until a message is recieved. If thread A send()s to process B, which recv()s it, the two process will have effectively performed a rendevous.

Assuming two asynchronous primitives, pass() and check(), you can still synchronize by having process A pass() a message, then loop on a series of short sleep()s followed by a check() to see when process B replies. If you want to contiue processing in A, you simply omit the sleep() and perform your processing within the loop instead. In practice, it would be useful to support both synchronous and asynchronous message-passing, and allow the programmer of each thread to decide which to use when.

Re:Messages : what are used for

Posted: Sat Dec 25, 2004 7:00 pm
by Balroj
What's the difference between sincron and asincron msg pass? if you don't want to explain, can you tellme some web where i found some info about?

Thanks.

Re:Messages : what are used for

Posted: Sat Dec 25, 2004 8:14 pm
by aladdin
sync messages are those who require a reply, in this case, the sender (process/task ...) is blocked waiting for the ACK (reply).

async messages are those who wait for reply, the message is sent, and the process continue its job.

you can compare it to some network protocols :
UDP = async
TCP = sync

Re:Messages : what are used for

Posted: Tue Dec 28, 2004 8:09 am
by Pype.Clicker
aladdin wrote: you can compare it to some network protocols :
UDP = async
TCP = sync
Wrong. Asynchronous message passing means that you have no idea whether the message has been receive or not when the call terminates. But if your system is correctly designed, the target will eventually receive and process the message, while UDP may lose messages without any possible way for you to detect the loss.

For instance, a messaging mechanism could keep the message in a queue if space remains and block the caller if no room is left in the queue. If the queue size >1, this makes the mechanism asynchronous but still not lossy.

Now, TCP vs synchronized ... TCP is connection-oriented and it is a bytestream. It's thus completely different from a messaging system since it cannot preserve messages bounds.

Re:Messages : what are used for

Posted: Tue Dec 28, 2004 1:42 pm
by Candy
Asynchronous is like placing a memo on somebody's monitor. Synchronous is like waiting at the monitor to tell him something.

With asynchronous you can either check for memo's or place a memo somewhere, but you can't be sure that either a memo will be there or that somebody else will react to the memo and send you a reply back.

Synchronous is like having a conversation, one says something, then the other, and so forth. It also means that you need to have conversational matter and that both parties may not quit without telling the other (for computers never go bored waiting). On the other hand, with asynchronous the other "person" needs to keep his own context and he can't be sure that you'll keep sending him memo's.