Linux System Programming Part 6 - IPC (Synchronization) : IBA Bulgaria 2018

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Linux System Programming

Part 6 - IPC (synchronization)

IBA Bulgaria
2018
Message queue
● Message queues can be described as an
internal linked list within the kernel's
addressing space.
● Messages can be sent to the queue in order
and retrieved from the queue in several
different ways.
● Each message queue (of course) is uniquely
identified by an IPC identifier.
Messaging in C

The msgget() system call returns the System V message queue identifier associated with the value of
the key argument.
The msgsnd() and msgrcv() system calls are used, respectively, to send messages to, and receive
messages from, a System V message queue. The calling process must have write permission on
the message queue in order to send a message, and read permission to receive a message.
msgctl() and cmd = IPC_RMID removes a message queue.
msgtypes.h

Define common data


Define KEY - the queue unique ID

Define MAXLEN - max length of message

Define msg_1_t for the client message

Define msg_1_2 for the server response


msgsrv.c

Message Server
Create a message queue with a given KEY

Wait for a message of type msg_1_t from


the queue

Print the received message data

Prepare and send answer of type msg_2_t

Wait for confirmation msg_1_t

Remove the message queue


msgcli.c

Message Client
Open a message queue with a given KEY
and notify and exit if failed

Read a message body from the keyboard


and set the other parameters of msg_1_t

Send msg_1_t message to the queue

Wait for msg_2_t and print it

Send back a confirmation msg_1_t


Shared Memory
● Shared memory can be described as the
mapping of an area (segment) of memory
that will be mapped and shared by more
than one process.
● This is by far the fastest form of IPC,
because there is no intermediation -
information is mapped directly from a
memory segment into the addressing space
of the calling process.
● A segment can be created by one process,
and subsequently written to and read from
by any number of processes.
Shared memory in C
ftok() - convert a pathname and a project
identifier to a System V IPC key.
shmget() - allocates a System V shared memory
segment.
shmat() - attaches the System V shared memory
segment identified by shmid to the address space
of the calling process.
shmdt() - detaches the shared memory segment
located at the address specified by shmaddr from
the address space of the calling process.
shmctl() with cmd = IPC_RMID - marks the
segment to be destroyed.
shmemtypes.h

Define common data


Define FTOK_FILE - the queue unique ID

Define the shared memory structure memory_block:


server_lock is 1 when server uses the memory

client_lock is 1 when client uses the memory


turn is 0 when waiting for client message
turn is 1 when waiting for server message
readlast is 0 when client received last message
readlast is 1 when server received last message
string holds the current message
shmemserv.c

Memory Server
Create token ‘./shmemserv’, exit on error

Allocate and attach shared memory

Configure the memory for client turn and


write ‘Hello!’ message in it

While current message is not ‘q’:

Lock memory for server, set client’s turn

Wait if client is using the memory


If client processed last message -
indicate server processed the current
one, print the client’s message, return
‘Ok!’, and release the server lock

Detach and remove the shared memory


shmemcli.c

Memory Client
Create token ‘./shmemserv’, exit on error

Attach the shared memory

While current message is not ‘q’:

Lock memory for client, set server’s turn

Wait if server is using the memory


If server processed last message -
indicate client processed the current
one, print the server’s message, read a
line from keyboard, and release the
client lock

Detach the shared memory


Semaphore
● Semaphores are counters used to control
access to shared resources by multiple
processes (resource counters).
● They are used as a locking mechanism to
prevent processes from accessing a
particular resource while another process
is performing operations on it.
● A resource counter is decreased when a
process starts using a resource and
increased back, when the resource is
released.
● When the counter = 0, the resource is
unavailable at the moment.
Using semaphores semget() - returns the System V semaphore set
identifier associated with the argument key.

semctl() with cmd = SETVAL - Sets the value of


semval to arg.val for the semnum-th semaphore
of the set.

semctl() with cmd = IPC_RMID - removes the


semaphore set.

semop() performs operations on selected


semaphores in the set indicated by semid.
semtypes.h

Define common data


Define FTOK_FILE = ‘./semserv’

Define the shared memory structure memory_block:


string holds the current message
semserv.c

Semaphore Server
Set 2 semaphores - first indicating that the
server has to read, second for the client

Create token ‘./shmemserv’, exit on error

Allocate and attach shared memory and


write ‘Hello!’

Release the client semaphore

While current message is not ‘q’:

Request server semaphore resource


If current message is not ‘q’, write ‘Ok!’
in memory and release the client sem.

Detach and remove the shared memory and


the semaphores
semcli.c

Semaphore Client
Set 2 semaphores - first indicating that the
server has to read, second for the client

Create token ‘./shmemserv’, exit on error

Attach the shared memory

While current message is not ‘q’:


Request client semaphore resource and
print the next message
Read a new line from the keyboard to
the shared memory
Release the server semaphore

Detach the shared memory


Exercise
Project DoubleSharedMemory:

Write a server/client pair of programs (‘dblmemsrv.c’ and ‘dblmemcli.c’), which share blocks of memory between them.
The server supports up to 2 client instances in parallel execution, which should not interfere with each other. The server
initializes the shared memory with welcoming messages for the clients and waits for them to modify the memory with
their messages. When a new message from the client is written, the server answers with the same message, but with
‘Confirmed!” at the end.

The client is started with a single argument: client_number, which identifies the client (1 or 2) for the server. The client
should continuously print the message from the shared memory, read a line from the keyboard and write it to the shared
memory. The program should quit when ‘q’ message is entered.

Program DoubleDumper:

Write a program (‘dbldump.c’), which in real time shows the changes in the shared memory from the
DoubleSharedMemory project. You may need to modify the server and/or client programs to support this functionality.

You might also like