Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

LINUX and interprocess communication

Hi,
Can somebody give me some details how following situation can be handled in Linux (I need to programm it in C)?
Process A needs to inform process B about something that happened in process A. In Windows one can use window message, what in Linux (Is there some sample code?).
How can one retrieve from B the information set due to A?
Thx.
0
jlsjls
Asked:
jlsjls
2 Solutions
 
garbouaCommented:
you can use global memory methods such as mailBoxes, and struct,
you can use signals,
use sockets, client server methods.
0
 
mnashadkaCommented:
You can also use named semaphores to help with that (if your waiting process can go into a wait).  Use the sem_wait to lock the semaphore and sem_post to unlock the semaphore.  Process B can go into a sem_wait, which will wait for Process A (or some other process) to perform a sem_post.  But of course there are many ways to do this (as garboua said).
0
 
ahoffmannCommented:
you can use sockets
you can use shared memory
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
superschlonzCommented:
You could also use named pipes.
Create it with 'mknod p name' where name is a filename. After that you use it like a file.
A opens it for writing an writes a message into it, B opens it for reading and waits for a message.
0
 
johanhzCommented:
I am relatively new to linux programming, and a collegue of jlsjls, so jlsjls, don't accept my comments as an answer.

what we basically want to do is the following :

Program A receives some input (in our case from a serial port)
Program A must signal Program B that some input has been received
If Program B receives the signal, it then gets the data from a Shared library.

Program A receives --> place the data in shared memory of the shared library --> Signal PRogram B that something was received --> Program B gets the data from the shared library.

I think we can do with
Message queue to share memory of the so with program A
use kill() to signal Program B, program B uses Signal() to receive the signal. Program B then call's an exported function of the so to get the data.

It this a correct method of working?

JOHan.
0
 
superschlonzCommented:
if the data is small (up to 1kB) it's easier to use named pipes, you would not need SHM and signals, only open, read and write. With ls you can see if data is in the pipe.

If you plan to to extend the system later (for ex. more processes communicating with each other) then it's probably better to use SHM and protect it with a semaphor (locking it before using the SHM).
I think you need a semaphor even if you use signals because a could overwrite the shared memory when B is reading it.
0
 
garbouaCommented:
well, you can create two threads, one to watch the serial port for stream, and one to program B that will do the processing of data, I think that what U want.
You can use semaphores to control the two, have them wait on each other,  you can create a shared memory buffer, or a specific strucutre and pass its address to both threads, semaphore will guarantee that processes won't mix up data.  
or three, one to scan port and copy it to a waiting QuE, which is your second, and third to process.
0
 
jmcgOwnerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: ahoffmann {http:#6488750} & superschlonz {http:#6490153}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now