LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2015, 07:49 PM   #1
Jono_Buzz
LQ Newbie
 
Registered: Sep 2015
Posts: 1

Rep: Reputation: Disabled
Understanding monitor and process lock


Hi, Im new to programming and trying to get my head around the locking process with mesa variables. I have the problem below.

SensorMonitor M;
int main() {
...
M.LockSensorAccess();
/* Access the sensor, reconfigure the sensor, ... */
M.FreeSensorAccess();
...
}
Monitor SensorMonitor {
/* monitor variables here */
void LockSensorAccess() { /* Code here */ }
void FreeSensorAccess() { /* Code here */ }
}

An externalsensor is connected to a computer by a serialline.
Only one process at a time can communicate with the The sensor. controller of the serial line can maintain access to the sensor from
at most 7 processes. Im trying to write in pseudo-code monitor to coordinate process access to the
external sensor. The monitor pseudo-code needs to define monitor variables and
the monitor functions so that at most only 7 processes are actually
communicating with the sensor. Can anyone help me regarding this and help me understand it.

Regards

Jon
 
Old 09-22-2015, 04:22 PM   #2
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
Jono_Buzz,

I may be able to help in a small way. I fairly sure Mesa variables are like any other variable, and synchronization of processes is very much like synchronization of threads in a multi-threaded program.

This link might help shed a light on synchronization: Semaphore Examples or Basic Multithread Programming and Synchronization (Futex, Semaphores, Mutex, etc)

What's your ideal of the interaction between active processes, what are your thoughts on how they should signal each other related to the shared serial line? Thinking this through for a moment, allowing processes to talk directly to the serial could be problematic, beyond trying to constrain the number of processes to seven. In short this approach burdens all involved processes with needing the knowledge of inter-process signaling and the operational details of the serial ;line, along with the required knowledge of how to operate the device to extract valid information. This is exactly why device drivers were created!

I would entertain one program (controller) owning the serial line resource and containing all the functionality required to extract information from the attached device. This controller would then establish a request/response messaging protocol that would allow any number of processes to make requests. Maybe using Message Queues as the IPC between processes.

Hope this helps establishes a design model for What can be done. How do you want to proceed; peer processes sharing a serial line; or the controller IPC model?
 
Old 09-22-2015, 04:32 PM   #3
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
Jono_Buzz,

Quote:
The monitor pseudo-code needs to define monitor variables and
the monitor functions so that at most only 7 processes are actually
communicating with the sensor.
This kind of synchronization is all voluntary, you hope that all process play by the same rules. Assume the interprocess flag is a regular file named 'locked.ser'. If the file exists then the serial line has been locked by a process. If the file does not exist then the serial line is available to be locked. Also, we all agree that we will not access the serial line unless we created the lock, and we also agree to delete the lock as soon as we are done with the serial line.

Whether you use a file that all processes can see, semaphores, or something else. you must synchronized access to serial line, which can only have one user at a time.
 
Old 09-23-2015, 10:20 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
I'm not going to try to second-guess the code that you have. But here are a few ideas (assuming POSIX threads).

(1) If you want to ensure that no more than 7 processes can do something, use a semaphore that is initialized to the value "7." Each process that wants to use the monitor (at all) must first sem_wait() on this semaphore, then must continue to hold the semaphore until it no longer needs to use the monitor. (It could hold this semaphore for quite some time.) Then, sem_post() to give-up its right to use the monitor. You aren't allowed to touch the monitor unless you hold this semaphore. The only purpose of this semaphore is to "wait in line."

(2) The monitor itself is secured by a second semaphore with a value of 1 (a "mutex"), so that only one process can hold it at any time. Exception-handling (try..finally) is used to ensure that this semaphore is always unlocked by any process that has locked it.

If the process of "waiting in line until it's my turn" is immediately followed by doing something with the monitor and then immediately relinquishing semaphore #1, then that's how you write it. First, the process waits in line. Then, it locks the mutex, does its business, releases it. Finally, it releases the wait-in-line semaphore. Exception handling is used to guarantee that this will always be done even if the process has encountered a runtime error along the way.

Last edited by sundialsvcs; 09-23-2015 at 10:24 AM.
 
  


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
Make Process Understanding priyadarshan Linux - Newbie 3 08-05-2010 02:41 AM
understanding linux boot process from source pankaj_909 Linux - General 1 07-12-2007 03:58 AM
Understanding Debian Boot process kushalkoolwal Debian 1 05-11-2007 06:12 PM
Need help understanding upgrade process and procedures; also working on a how-to Geocritter *BSD 2 02-02-2006 08:41 AM
Understanding the boot process? rene.dd Programming 1 06-09-2004 10:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 PM.

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