rasProject_01 / weSweetHome  R. 77 2025-03-07
process control       /     RasPi software         by   weinert-automation
Loading...
Searching...
No Matches
weShareMem.h File Reference

One chunk of shared memory on a Raspberry Pi. More...

#include "weUtil.h"
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/sem.h>
#include <errno.h>

Data Structures

union  semCtlPar_t
 Parameter type for semctl(). More...
 

Macros

#define ANZ_SEMAS
 Standard semaphore set size. More...
 
#define PERM
 access rights
 
#define SEMAPHORE_KEY
 Semaphore unique key "KÇÏfig25".
 
#define SHARED_MEMORY_KEY
 Shared memory key "Buffer25".
 
#define SHARED_MEMORY_SIZE
 Sweet home control (meterPi) shared memory size. More...
 

Functions

int deleteSemas ()
 Delete the one semaphore set. More...
 
int deleteSharedMem ()
 Delete and detach the shared memory. More...
 
int detachSharedMem ()
 Detach the shared memory.
 
int getSemas ()
 Get the one semaphore set. More...
 
int initialiseSemas ()
 Initialise the one semaphore set. More...
 
void * initialiseSharedMem ()
 Initialise shared memory. More...
 
int semaphoreClt (int const semNum, int const op, semCtlPar_t par)
 Control semaphores of the set. More...
 
int semaphoreLock (int const semNum, int ms)
 Lock one semaphore of the set. More...
 
int semaphoreOperation (int const semNum, int const op, int ms)
 Operation on one semaphore of the set. More...
 
int semaphoreUnlock (int const semNum)
 Unlock one semaphore of the set. More...
 

Variables

int shMemErrno
 Last error number of (some ) semaphore operations. More...
 
int shMemSem
 Semaphore set identifier. More...
 
const semCtlPar_t VAL0
 value 0 for SETVAL
 
const semCtlPar_t VAL1
 value 1 for SETVAL
 
const semCtlPar_t VAL9
 value 9 for SETVAL
 

Detailed Description

One chunk of shared memory on a Raspberry Pi.

Copyright (c) 2018 2025 Albrecht Weinert
weinert-automation.de a-weinert.de
/ / /\
/ /___ / \ |
\ /____\ /____\ | _|__
\ /\ / \ / \| |
\/ \/ \__/ \__/|_

Revision history

Rev. 76 26.02.2025
Rev. 80 11.11.2017 : new
Rev. 175 28.07.2018 : beauty
Rev. 191 15.02.2019 : minor comment text changes
Rev. 270 03.11.2024 : semphore set & shared memory defined here, only!
Rev. 76 24.02.2025 : clarification of 64 bit OS malfunction

Provide one piece of shared memory to be used by multiple programs on one Pi, e.g. one process control program range and few (cgi) programs for (human) observation and control.

Macro Definition Documentation

◆ ANZ_SEMAS

#define ANZ_SEMAS

Standard semaphore set size.

Standard semaphore set of three (3..10)

Usually three (3..10)

◆ SHARED_MEMORY_SIZE

#define SHARED_MEMORY_SIZE

Sweet home control (meterPi) shared memory size.

It is 1K. 512 byte would be enough, but shared memory segments will probably come as multiples of PAGE_SIZE. With meterPi (32bit Raspbian) and growPi (64bit) the values are 4096.
Get the page size by /code meterPi:~ $getconf PAGE_SIZE 4096 /endcode

Function Documentation

◆ getSemas()

int getSemas ( )

Get the one semaphore set.

The one semaphore set, if existing, will be registered and used as is.

Returns
0: OK found existing semaphore set; -1: error (errno set and errorText generated)

◆ initialiseSemas()

int initialiseSemas ( )

Initialise the one semaphore set.

The number of semaphores in the set is ANZ_SEMAS (default three). The one semaphore set, if existing, will be registered and used as is. If this is not possible it will be made and initialised.

Hint: This function has two OK return values!

Returns
1: OK semaphore set made new; 0: OK found existing semaphore set; -1: error (errno set and errorText generated)

◆ semaphoreOperation()

int semaphoreOperation ( int const  semNum,
int const  op,
int  ms 
)

Operation on one semaphore of the set.

Parameters
semNumthe semaphore number in the set (0..ANZ_SEMAS -1)
opthe semaphore operation
msif 2..20000 a timeout in ms
Returns
0: OK; -1: error (errno set and errorText generated)

◆ semaphoreLock()

int semaphoreLock ( int const  semNum,
int  ms 
)

Lock one semaphore of the set.

Parameters
semNumthe semaphore number in the set (0..ANZ_SEMAS -1)
msif 2..20000 a timeout in ms
Returns
0: OK; -1: error (errno set and errorText generated)

◆ semaphoreUnlock()

int semaphoreUnlock ( int const  semNum)

Unlock one semaphore of the set.

Parameters
semNumthe semaphore number in the set (0..ANZ_SEMAS -1)
Returns
0: OK; -1: error (errno set and errorText generated)

◆ semaphoreClt()

int semaphoreClt ( int const  semNum,
int const  op,
semCtlPar_t  par 
)

Control semaphores of the set.

Parameters
semNumthe semaphore number in the set (0..ANZ_SEMAS -1)
opthe semaphore operation, like e.g. SETVAL
parop's parameter if any
Returns
0: OK; -1: error (errno set and errorText generated)

◆ deleteSemas()

int deleteSemas ( )

Delete the one semaphore set.

Server operation only.

Returns
0: OK; -1: error (errno set and errorText generated)

◆ initialiseSharedMem()

void * initialiseSharedMem ( )

Initialise shared memory.

Make or get and attach. return pointer to attached shared memory or (void *)-1

◆ deleteSharedMem()

int deleteSharedMem ( )

Delete and detach the shared memory.

Server operation only.

Variable Documentation

◆ shMemSem

int shMemSem
extern

Semaphore set identifier.

The value returned by e.g. semget() (within getSemas() etc.) On success, semget() returns the semaphore set identifier (a nonnegative integer). On failure, -1 is returned, and errno is set to indicate the error.

◆ shMemErrno

int shMemErrno
extern

Last error number of (some ) semaphore operations.