C++ Library, mos@ua  0.9
cpplib-mos
process

Process, System-V, and POSIX libraries wrapper module. More...

Files

file  process.h
 Process, System-V, and POSIX libraries wrapper module.
 

Process handling

pid_t pfork (void)
 fork wrapper function. More...
 
pid_t pwait (int *status)
 wait wrapper function. More...
 
pid_t pwaitpid (pid_t pid, int *status, int options)
 waitpid wrapper function. More...
 

System V - shared memory

Example
#include <sys/shm.h>
...
int shmid;
// creation:
shmid = pshmget(key, size, 0600 | IPC_CREAT | IPC_EXCL);
// or, use existing:
shmid = pshmget(key, 0, 0);
...
// attach shm to pointer address:
void* p = pshmat(shmid, NULL, 0);
...
// detach shm from pointer address:
pshmdt(p);
...
// destroy shm:
pshmctl(shmid, IPC_RMID, NULL);
int pshmget (key_t key, size_t size, int shmflg)
 shmget wrapper function. More...
 
int pshmctl (int shmid, int cmd, struct shmid_ds *buf)
 shmctl wrapper function. More...
 
void * pshmat (int shmid, const void *shmaddr, int shmflg)
 shmat wrapper function. More...
 
void pshmdt (const void *shmaddr)
 shmdt wrapper function. More...
 

System V - semaphores

Example
#include <sys/sem.h>
...
int semid;
// creation:
semid = psemget(key, 1, 0600 | IPC_CREAT | IPC_EXCL); // 1 semaphore!
// or, use existing:
semid = psemget(key, 0, 0);
...
// lock:
struct sembuf down = {0, -1, 0};
psemop(semid, &down, 1);
// or, simply use provided function:
psem_down(semid, 0);
...
//unlock:
struct sembuf up = {0, 1, 0};
psemop(semid, &up, 1);
// or, simply use provided function:
psem_up(semid, 0);
...
// destroy sem 0:
psemctl(semid, 0, IPC_RMID, NULL);
int psemget (key_t key, int nsems, int semflg)
 semget wrapper function. More...
 
int psemctl (int semid, int semnum, int cmd)
 semctl wrapper function. More...
 
int psemctl (int semid, int semnum, int cmd, void *u)
 
void psemop (int semid, struct sembuf *sops, size_t nsops)
 semop wrapper function. More...
 
void psem_up (int semid, short unsigned int index)
 Increment a semaphore (uses psemop()).
 
void psem_down (int semid, short unsigned int index)
 Decrements a semaphore (uses psemop()).
 

System V - message queues

Example
#include <sys/msg.h>
...
typedef struct Item
{
...
} Item;
typedef struct Message
{
long type;
Item item;
} Message;
...
int msgid;
// creation:
msgid = pmsgget(key, 0600 | IPC_CREAT | IPC_EXCL);
// or, use existing:
msgid = pmsgget(key, 0);
...
Message msg;
// send msg:
msg = ...;
pmsgsnd(msgid, &msg, sizeof(Item), 0);
...
// receive msg:
pmsgrcv(msgid, &msg, sizeof(Item), type, 0);
...
// destroy msg:
pmsgctl(msgid, IPC_RMID, NULL);
int pmsgget (key_t key, int msgflg)
 msgget wrapper function. More...
 
int pmsgctl (int msqid, int cmd, struct msqid_ds *buf)
 msgctl wrapper function. More...
 
void pmsgsnd (int msqid, const void *msgp, size_t msgsz, int msgflg)
 msgsnd wrapper function. More...
 
size_t pmsgrcv (int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
 msgrcv wrapper function. More...
 

POSIX semaphores

sem_t * psem_open (const char *name, int oflag)
 sem_open wrapper function. More...
 
sem_t * psem_open (const char *name, int oflag, mode_t mode, unsigned int value)
 
void psem_close (sem_t *sem)
 sem_close wrapper function. More...
 
void psem_unlink (const char *name)
 sem_unlink wrapper function. More...
 
void psem_init (sem_t *sem, int pshared, unsigned int value)
 sem_init wrapper function. More...
 
void psem_destroy (sem_t *sem)
 sem_destroy wrapper function. More...
 
void psem_wait (sem_t *sem)
 sem_wait wrapper function. More...
 
int psem_trywait (sem_t *sem)
 sem_trywait wrapper function. More...
 
int psem_timedwait (sem_t *sem, const struct timespec *abs_timeout)
 sem_timedwait wrapper function. More...
 
void psem_post (sem_t *sem)
 sem_post wrapper function. More...
 

Detailed Description

Process, System-V, and POSIX libraries wrapper module.

This module removes defensive programming approach of native libraries.

All implemented functions, have exactly the same arguments and/or result of the original function, with the exception of returning an error indication.

Errors are handled by the implementation of two policies:

  1. EXIT_POLICY (default): describes the failed call in stderr (with the identification of the errno error, and the precise location the call), generates a segmentation fault (enabling a stack trace within a debugger like gdb), and exits program execution;
  2. EXCEPTION_POLICY: throws a int exception with the (errno) status error returned by the original function.

System V group of IPC mechanisms

There are three resources for IPC: shared memory, semaphores, and message queues.

Common to all, is resource handling through a non-negative integer identifier. After proper creation and initialization, different processes can establish communication using the same integer identifier.

A key (key_t) is used to establish this common identifier. There are three possibilities to define a key:

  1. IPC_PRIVATE: In this case an alternative channel to communicate the identifier between processes is necessary (parent/child fork, file system, ...).
  2. A fixed predetermined key number (may collide with other existing keys).
  3. ftok function to generate a key from a path and a byte integer.

Usage:

Author
Miguel Oliveira e Silva, 2017-2018

Function Documentation

◆ pfork()

pid_t pfork ( void  )

fork wrapper function.

Documentation in

man 2 fork 
See also
https://man.cx/fork(2)

◆ pwait()

pid_t pwait ( int *  status)

wait wrapper function.

Documentation in

man 2 wait 
See also
https://man.cx/wait(2)

◆ pwaitpid()

pid_t pwaitpid ( pid_t  pid,
int *  status,
int  options 
)

waitpid wrapper function.

Documentation in

man 2 waitpid 
See also
https://man.cx/waitpid(2)

◆ pshmget()

int pshmget ( key_t  key,
size_t  size,
int  shmflg 
)

shmget wrapper function.

Precondition:
(size > 0) || !(shmflg & IPC_CREAT)

Documentation in

man 2 shmget 
See also
https://man.cx/shmget(2)

◆ pshmctl()

int pshmctl ( int  shmid,
int  cmd,
struct shmid_ds *  buf 
)

shmctl wrapper function.

Documentation in

man 2 shmctl 
See also
https://man.cx/shmctl(2)

◆ pshmat()

void* pshmat ( int  shmid,
const void *  shmaddr,
int  shmflg 
)

shmat wrapper function.

Documentation in

man 2 shmat 
See also
https://man.cx/shmat(2)

◆ pshmdt()

void pshmdt ( const void *  shmaddr)

shmdt wrapper function.

Documentation in

man 2 shmdt 
See also
https://man.cx/shmdt(2)

◆ psemget()

int psemget ( key_t  key,
int  nsems,
int  semflg 
)

semget wrapper function.

Precondition:
nsems > 0

Documentation in

man 2 semget 
See also
https://man.cx/semget(2)

◆ psemctl()

int psemctl ( int  semid,
int  semnum,
int  cmd 
)

semctl wrapper function.

Documentation in

man 2 semctl 
See also
https://man.cx/semctl(2)

◆ psemop()

void psemop ( int  semid,
struct sembuf *  sops,
size_t  nsops 
)

semop wrapper function.

Documentation in

man 2 semop 
See also
https://man.cx/semop(2)

◆ pmsgget()

int pmsgget ( key_t  key,
int  msgflg 
)

msgget wrapper function.

Documentation in

man 2 msgget 
See also
https://man.cx/msgget(2)

◆ pmsgctl()

int pmsgctl ( int  msqid,
int  cmd,
struct msqid_ds *  buf 
)

msgctl wrapper function.

Documentation in

man 2 msgctl 
See also
https://man.cx/msgctl(2)

◆ pmsgsnd()

void pmsgsnd ( int  msqid,
const void *  msgp,
size_t  msgsz,
int  msgflg 
)

msgsnd wrapper function.

Documentation in

man 2 msgsnd 
See also
https://man.cx/msgsnd(2)

◆ pmsgrcv()

size_t pmsgrcv ( int  msqid,
void *  msgp,
size_t  msgsz,
long  msgtyp,
int  msgflg 
)

msgrcv wrapper function.

Documentation in

man 2 msgrcv 
See also
https://man.cx/msgrcv(2)

◆ psem_open()

sem_t* psem_open ( const char *  name,
int  oflag 
)

sem_open wrapper function.

Documentation in

man 3 sem_open 
See also
https://man.cx/sem_open(3)

◆ psem_close()

void psem_close ( sem_t *  sem)

sem_close wrapper function.

Precondition:
sem != NULL

Documentation in

man 3 sem_close 
See also
https://man.cx/sem_close(3)

◆ psem_unlink()

void psem_unlink ( const char *  name)

sem_unlink wrapper function.

Documentation in

man 3 sem_unlink 
See also
https://man.cx/sem_unlink(3)

◆ psem_init()

void psem_init ( sem_t *  sem,
int  pshared,
unsigned int  value 
)

sem_init wrapper function.

Precondition:
sem != NULL

Documentation in

man 3 sem_init 
See also
https://man.cx/sem_init(3)

◆ psem_destroy()

void psem_destroy ( sem_t *  sem)

sem_destroy wrapper function.

Precondition:
sem != NULL

Documentation in

man 3 sem_destroy 
See also
https://man.cx/sem_destroy(3)

◆ psem_wait()

void psem_wait ( sem_t *  sem)

sem_wait wrapper function.

Precondition:
sem != NULL

Documentation in

man 3 sem_wait 
See also
https://man.cx/sem_wait(3)

◆ psem_trywait()

int psem_trywait ( sem_t *  sem)

sem_trywait wrapper function.

Precondition:
sem != NULL

Documentation in

man 3 sem_trywait 
See also
https://man.cx/sem_trywait(3)
Returns
true (!=0) on success, false (0) if semaphore is zero

◆ psem_timedwait()

int psem_timedwait ( sem_t *  sem,
const struct timespec *  abs_timeout 
)

sem_timedwait wrapper function.

Precondition:
sem != NULL

Documentation in

man 3 sem_timedwait 
See also
https://man.cx/sem_timedwait(3)
Returns
true (!=0) on success, false (0) if timeout has expired before being able to decrement the semaphore

◆ psem_post()

void psem_post ( sem_t *  sem)

sem_post wrapper function.

Precondition:
sem != NULL

Documentation in

man 3 sem_post 
See also
https://man.cx/sem_post(3)