message passing IPC and multithreading

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
bluecode

message passing IPC and multithreading

Post by bluecode »

hi,

I'm a little bit stuck on design & implementing my message passing ipc. Currently I've got
* send(): Send a message to a specific process
* peek(): get one of the messages send to the process. If there is none return with errorcode
* get(): Same as peek, but get() is blocking if there is no message available
* next on the agenda: waitOnMsg(), which waits for a specific message
syscalls. I have to mention, that I want a microkernel and I want it to support multithreading (which it already does). So my e.g. virtual filesystem will be a server. The requests to and the replies from these servers are messages. So e.g. opening a file would go like this (only schematic, C++ code):

Code: Select all

/* Send OPEN_FILE message to the vfs server */
sendMsg(VFS_SERVER, OPEN_FILE);
unsigned long handle;
/* Wait for the reply from the vfs server and get the handle */
waitOnMsg(VFS_SERVER, OPEN_FILE, handle);
This is not problematic in itself, but when I add another thread, which looks like this:

Code: Select all

unsigned long pid;
unsigned long id
unsigned long param1;
/* Waiting for any message */
while(getMsg(pid, id, param1))
{
   switch(id)
   {
       case GUI_DRAW_WINDOW:
       /*Do whatever you do*/
       break;
   }
}
The problem here is, that if the first thread is interrupted after sending the message, but before waiting on the message, then the kernel does not know that a thread (except the second one s waiting for a message) and the kernel will happily invoke the second thread, but the message from the vfs beeing meant for the first thread.
So, what I wanted to ask now: How do/did you design your message passing in a microkernel, so that no thread could take away the messages, which were meant for another thread?
User avatar
kataklinger
Member
Member
Posts: 381
Joined: Fri Nov 04, 2005 12:00 am
Location: Serbia

Re:message passing IPC and multithreading

Post by kataklinger »

Well, the each thread needs to have own 'mailbox'.
bluecode

Re:message passing IPC and multithreading

Post by bluecode »

rofl, ok, the answer is so simple & good, that I didn't come up with it ;D

So you basically have also have a mailslot for every thread? And the server has to decide, wheather it sends it's reply to the whole process or to the thead? Or don't you have a mailslot for the process and the server always send it's messages to the thread?
durand
Member
Member
Posts: 193
Joined: Wed Dec 21, 2005 12:00 am
Location: South Africa
Contact:

Re:message passing IPC and multithreading

Post by durand »

It all depends on how you want your system to work. I've been through the following three situations:
  • Mailbox per process
  • Mailbox per thread
  • Multiple mailboxes which threads can register to handle.
The first two are obvious.

The last one is the one I'm currently using. It's a little like networking and TCP. I've decided on 1024 "ports" for each process. Then any thread can do a system call to request port X (or multiple ports). Any message coming in for port X will wake up the registered thread and it will deal with it.

That way you have the flexibility to work any way you want. One thread can handle all incoming messages or multiple threads can handle messages coming in on multiple ports.
User avatar
kataklinger
Member
Member
Posts: 381
Joined: Fri Nov 04, 2005 12:00 am
Location: Serbia

Re:message passing IPC and multithreading

Post by kataklinger »

Yes, something like third one. Thread can open one or more mailslots(ports, mailboxes...), and then send message to the server which includes ID of mailslot at which it expects answer. Have you tought about adding a new method for requesting something from server (lets call it request() ;)), it'll send message to server and then wait for response. So then you have to make only one call instead of two(send() and wait_for_message()).
bluecode

Re:message passing IPC and multithreading

Post by bluecode »

kataklinger wrote:Have you tought about adding a new method for requesting something from server (lets call it request() ;)), it'll send message to server and then wait for response.
Yes, I have thought about that, but it wouldn't have worked with my previous approach.

And yes, I also thought about the explicit creation of a mailbox, but I discarded that idea, because then the application programmer has to explicitly pass an id (of the mailbox) which reduces the number of parameters again by one. But seeing that this is the only flexible enough design I implemented it last night and this morning.

So thanks for your suggestions.
durand
Member
Member
Posts: 193
Joined: Wed Dec 21, 2005 12:00 am
Location: South Africa
Contact:

Re:message passing IPC and multithreading

Post by durand »

I've also seen implementations which have global mailboxes in addition to local process mailboxes. So, the VFS will always be sitting on global mailbox 5 (for example) but inter-process IPC can still occur using the normal mailboxes.

The only real advantages I see to global mailboxes are that your VFS server (or whatever) doesn't need to have a fixed process name all the time and you could have 1 process handling multiple mailboxes (VFS, networking, etc). You could just send_global( 5, ... ). Otherwise, you'd have to add an extra call to find the process ID of a specific name. Example: pid = get_pid("vfs_server"); send_ipc( pid, ... );

Global mailboxes is something I've considered implementing but I decided that it could be reasonably reproduced by finding the pid. It just limits me to always expecting the VFS to be running as "vfs_server".

However, this is still one of those design decisions which need to be made. If you're happy with a requirement that the VFS process always be called a certain name, then you don't really need global mailboxes. If you want otherwise, you might be interested in them.
durand
Member
Member
Posts: 193
Joined: Wed Dec 21, 2005 12:00 am
Location: South Africa
Contact:

Re:message passing IPC and multithreading

Post by durand »

One more thing... While you're implementing all of this stuff, you should also take into account the possibility of a rogue-process spamming your mailboxes.

For example, you've initiated a specific IPC communication between process A, thread1 (mailbox 1) and process B, thread2 (mailbox 2). There might be the potential for process C, thread x to send a rogue message to mailbox 2 while the two valid processes are communicating. If process B is not expecting this sort of thing and not verifying the sender, then it's obviously going to break the communication between A and B.

I've seen people implement capability-based/token-based IPC so process A will need to request permission to speak to B from the kernel. It's generally given a token and it sends this token in all future IPC to authenticate itself.

Otherwise, I've gone for the simple version of having process B ignore anything not coming from process A. This is a userland check and it's not as good as authenticated IPC.
bluecode

Re:message passing IPC and multithreading

Post by bluecode »

I have already gone the global mailboxes approach, leaving the id's 1-10 for specific servers, like vfs, gui and the like. I dislike searching through the process list, because there might be some other application calling itself vfs_server and then hijacking my messages.
I know too less about capabilities/tokens to really implement them in my message passing. Can you recommend any article, tutorial or os implementing this? Can this be done with reasonable performance?
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:message passing IPC and multithreading

Post by distantvoices »

No, sorry, I for one can't.

But, regarding the name of the services in your microkernel:

I have all the drivers and services register in a list called "services". That's the only list to query for service pid's. It's small, quick and doesn't give too much overhead if the calling library/program is smart enough to save the gathered pids away for future use. *gg*

Stay safe :-)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
bluecode

Re:message passing IPC and multithreading

Post by bluecode »

beyond infinity wrote:is smart enough to save the gathered pids away for future use. *gg*
I just have to change one number in one file to get as much id's reserved as I want ;) It's just that I dislike searching if I can do it without. btw. do you search everytime you send a message or once at program startup?

I have found an article about capabilities and one project implementing capabilities. Currently reading through these...

OT: How is your os coming along? When is the next version coming?
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:message passing IPC and multithreading

Post by distantvoices »

re searching: if possible I do the lookup at first need and save the gathered pid away for further use, but not earlier.

OT: Recently I've started converting the GUI stuff to offscreen buffers per window and removing all the widget stuff from the gui service - that's gonna be in the ui library in the future. Gonna see how that'll work out. My experience with client side stuff is rather good currently.

Well ... development is not going that fast as one might wish for I'm attending a university of applied science currently (summer holidays for gods sake ... got a D in maths and glad I am. Doing a maths colloquium with big head ache IS crap)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:message passing IPC and multithreading

Post by Pype.Clicker »

Imho, the "port" (or mailbox) abstraction is essential to message passing. I have seen no clean solution when we deliver messages to process _directly_ (at least, not after one process needs to communicate with several other process).

Yet, iirc, i do not have the "please reply to port XXX" as part of the message proper. Instead, when a reply is intended the 'container' of the message implies the reply port and the 'recv(xxx)' call will return whether a reply is requested or not and on which port.

I'm equally toying with the idea of 'capabilities' atm. I don't know if they'll look to the ones in "Coyotos" or not (i tend to rename my concepts according to existing SoA rather than following other's concepts :P ). But basically, it stems that you only use "local" handles ... there will be no such thing like a 'global port number' that you can give to another process with a raw number. If you want another process to access a resource, register that resource to the capabilities-naming system or delegate a capability. period.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:message passing IPC and multithreading

Post by Pype.Clicker »

I got a look at "what capabilities are", and i get the feeling that saying "there is one way to get rid of chicken-and-egg: universal persistence of objects" is somehow avoiding the problem rather than actually solving it ...
JoeKayzA

Re:message passing IPC and multithreading

Post by JoeKayzA »

Pype.Clicker wrote: Imho, the "port" (or mailbox) abstraction is essential to message passing. I have seen no clean solution when we deliver messages to process _directly_ (at least, not after one process needs to communicate with several other process).
Is probably a matter of taste as well, but I think the same way.
Pype.Clicker wrote: I'm equally toying with the idea of 'capabilities' atm. I don't know if they'll look to the ones in "Coyotos" or not (i tend to rename my concepts according to existing SoA rather than following other's concepts :P ). But basically, it stems that you only use "local" handles ... there will be no such thing like a 'global port number' that you can give to another process with a raw number. If you want another process to access a resource, register that resource to the capabilities-naming system or delegate a capability. period.
Same here. Using only locally visible handles also has the advantage that you don't need to secure your channels. IMO, the basic concept behind message ports in Mach was pretty good already, a process could only 'see' services which it retrieved access to, so you could completely sandbox every single process with ease.

When you use globally visible endpoints (say, thread ids), you need a seperate security mechanism as well,or delegate security to servers entirely, like L4 does.

cheers Joe
Post Reply