Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading…
Transcript

POSIX IPC

Operating Systems

Micael Guerra Rodríguez

IPC

POSIX Semaphores

Base Definitions

Interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data.

Typically, applications can use IPC, categorized as clients and servers, where the client requests data and the server responds to client requests.

It includes general terms, concepts, and interfaces common to all volumes of POSIX.1-2017, including utility conventions and C-language header definitions.

  • Integer maintained inside kernel
  • Kernel blocks attempt to decrease value below zero
  • Two fundamental operations:
  • sem_post(): increment by 1
  • sem_wait(): decrement by 1
  • Two types of POSIX semaphore:
  • Unnamed
  • Embedded in shared memory
  • Named
  • Independent, named objects

I/O

POSIX MQ notifications

  • mq_send()
  • mq_notify(mqd_t mqd, const struct sigevent *notification)
  • One process can register to receive notification
  • Notified when new message arrives on empty queue, only if another process is not doing mq_receive().
  • Notification says how caller should be notified
  • Send me a signal
  • Start a new thread

#include <mqueue.h>

int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned int msg_prio);

POSIX MQ attributes

struct mq_attr {

long mq_flags; /* Flags (ignored for mq_open()) */

long mq_maxmsg; /* Max. # of messages on queue */

long mq_msgsize; /* Max. message size (bytes) */

long mq_curmsgs; /* # of messages currently in queue

(ignored for mq_open()) */

};

Named semaphores

  • mqdes - MQ descriptor
  • msg_ptr - pointer to bytes forming message
  • msg_len - size of message
  • msg_prio - priority
  • non-negative integer
  • 0 is lowest priority

Example

Message Queues

  • mq_receive()

#include <mqueue.h>

ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio);

  • sem_open(): open/create semaphore

sem_t *sem_open(const char *name, int oflag,

mode_t mode, unsigned int value);

  • Flags
  • O_CREAT - create SHM if it does not exist
  • O_EXCL - create SHM exclusively
  • If creating new semaphore:
  • mode sets premissions
  • value initializes semaphore

  • sem_unlink(): remove semaphore pathname

Unnamed semaphores API

  • mqdes - MQ descriptor
  • msg_ptr - points to buffer that receives message
  • msg_len - size of buffer
  • &msg_prio - receives priority
  • Message-oriented communication
  • Receiver reads messages one at a time.
  • No partial or multiple message reads.
  • Unlike pipes, multiple readers/writers can be useful
  • Messages have priorities
  • Delivered in priority order
  • Message notification feature
  • sem_init(semp, pshared, value): initialize semaphore pointed to by semp to value
  • sem_t *semp
  • pshared: 0, thread sharing; !=0, process sharing
  • sem_post(semp): add 1 to value
  • sem_wait(semp): subtract 1 from value
  • sem_destroy(semp): free semaphore, release resources back to system (must be no waiters)

Queue management

  • mq_open()

creates a new POSIX message queue or opens an existing queue. The queue is identified by name.

Queue management

  • mq_close() closes the message queue descriptor mqdes.

Functions

#include <mqueue.h>

int mq_close(mqd_t mqdes);

#include <fcntl.h> /* For O_* constants */

#include <sys/stat.h> /* For mode constants */

#include <mqueue.h>

mqd_t mq_open(const char *name, int oflag);

mqd_t mq_open(const char *name, int oflag, mode_t mode, struct mq_attr *attr);

  • mq_unlink() removes the specified message queue name. MQ removed only after all users have closed.

#include <mqueue.h>

int mq_unlink(const char *name);

#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);

int sem_post(sem_t *sem);

int sem_wait(sem_t *sem);

int sem_destroy(sem_t *sem);

Flags:

  • O_CREAT create MQ if it does not exist
  • O_EXCL create MQ exclusively
  • O_RDONLY, O_WRONLY, O_RDWR
  • O_NONBLOCK non-blocking I/O

&attr:

Attributes for new MQ

Index

What is POSIX?

System Interfaces

POSIX Shared memory

  • What is POSIX?
  • IPC
  • Message queues
  • Shared Memory
  • Semaphores

POSIX (Portable Operating System Interface) is a set of standard operating system interfaces based on the Unix operating system. The need for standardization arose because enterprises using computers wanted to be able to develop programs that could be moved among different manufacturer's computer systems without having to be recoded. Unix was selected as the basis for a standard system interface partly because it was "manufacturer-neutral." However, several major versions of Unix existed so there was a need to develop a common denominator system.

POSIX has four major components (each in an associated volume):

  • Base Definitions
  • System Interfaces
  • Shell and Utilites
  • Rationale

Definition

The POSIX interfaces were developed by the Institute of Electrical and Electronics Engineers (IEEE).

Currently it is used POSIX.1-2017, which is simultaneously IEEE Std 1003.1-2017

  • Share memory between unrelated process, without creating file in (traditional) filesystem
  • Do not need to create a file
  • Avoid file I/O overhead

The System Interfaces volume of POSIX.1-2017 describes the interfaces offered to application programs by POSIX-conformant systems.

It includes:

  • Error Numbers
  • Signals
  • Standard I/O Streams
  • XSI IPC
  • Threads
  • Sockets
  • Realtime
  • Etc

API

Object managment

  • smh_open(): open/create SHM object.
  • mmap(): map SHM object
  • shm_unlink(): remove SHM object pathname

Operations on SHM object via fd returned by smh_open():

  • fstat(): retrieve information (size, ownership, permissions)
  • ftruncate(): change size
  • fchown(), fchmod(): change ownership, permissions

Functions

Examples

  • Create and map a new SHM object of size bytes:
  • Map an existing SHM object of unknown size:

#include <sys/mman.h>

#include <sys/stat.h> /* For mode constants */

#include <fcntl.h> /* For O_* constants */

int shm_open(const char *name, int oflag, mode_t mode);

int shm_unlink(const char *name);

#include <sys/mman.h>

void *mmap(void *addr, size_t length, int prot, int flags,

int fd, off_t offset);

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

int fstat(int fd, struct stat *buf);

int ftruncate(int fd, off_t length);

int chown(const char *path, uid_t owner, gid_t group);

int fchown(int fd, uid_t owner, gid_t group);

Learn more about creating dynamic, engaging presentations with Prezi