Linux Programming Guide 2 -- Linux_Programming_posix_ipc.ppt

为给组内兄弟们扫盲,从网上整理的一些东东,与大家一起交流学习。

 

1. Linux_Programming_posix_ipc.ppt

http://download.csdn.net/source/1360609

 

Linux Programming
 
POSIX Inter-Process Communication

Topics

±    Shared Memory

±    Message Queues

±    Semaphores

±    Real-Time Signals

±    Clocks and Timers

±    Asynchronous I/O

Linux Programming
POSIX Shared Memory

POSIX shared memory (1)þ

A great way to communicate between processes
without going through expensive system calls.

±Open a shared memory object:
shm_fd = shm_open(acme
, O_CREAT | O_RDWR, 0666);
A zero size /dev/shm/acme file appears.

±Set the shared memory object size
ftruncate(shm_fd, SHM_SIZE);
/dev/shm/acme
is now listed with the specified size.

±If the object has already been sized by another process,
you can get its size with the
fstat function.

 

POSIX shared memory (2)þ

±Map the shared memory in process address space:
addr = mmap (0, SHM_SIZE, PROT_WRITE,
                             MAP_SHARED, shm_fd, 0);

Now we have a memory area we can use!

±Lock the shared memory in RAM (best for real-time tasks):
mlock(addr, SHM_SIZE);

±Use the shared memory object!
Other processes can use it too.

 

POSIX shared memory (3)þ

Exiting

±Unmap the shared memory object:
munmap (addr, SHM_SIZE);
This automatically unlocks it too.

±Close it:
close (shm_fd);

±Remove the shared memory object:
shm_unlink (acme);

The object is effectively deleted after the last call to shm_unlink.

More details in man shm_open.

Linux Programming
 
POSIX message queues

POSIX message queues

Deterministic and efficient IPC. See man mqueue.h.
Advantages for real-time applications:

±Preallocated message buffers

±Messages with priority.
A message with a higher priority is always received first.

±Send and receive functions are synchronous by default.
Possibility to set a wait timeout to avoid non-determinism.

±Support asynchronous delivery notifications.

Creating and opening a message queue

±Declare queue attributes:
queue_attr.mq_maxmsg = 16;
/* max number of messages in queue */

queue_attr.mq_msgsize = 128;
/* max message size */

±Open a queue:
qd = mq_open(
              
/msg_queue,              /* queue name  */

               OCREAT | O_RDWR,       /* opening mode*/
               0600,                           /* permissions       */
               &queue_attr);

Posting a message

± Posting a message:
#define PRIORITY 3
char msg[] =
Goodbye Bill;

mqsend(qd, msg, strlen(msg), PRIORITY);

± Closing the queue:
mq_close(qd);

Caution: simplistic example code. Should check return values.

Receiving a message

From another application:

±  Opening the shared message queue:
qd = mq_open(/msg_queue
, O_RDWR,
                             0600, NULL);

±  Waiting for a message:
mq_receive(qd, text, buf, buf_size, &prio);

±  Close the queue:
mq_close(qd);

±  Destroy the queue:
mq_unlink(/msg_queue);

Linux Programming
POSIX Semaphores

POSIX semaphores (1)þ

Resources for sharing resources between threads or processes.
See
man semaphore.h.

±Named semaphores:
can be used between unrelated processes.

±Unnamed semaphores: can be used between threads from the same process, or by related processes (parent / child).

POSIX semaphores (2)þ

±sem_open
Open and / or create
a named semaphore.

±sem_close
Close a named semaphore

±sem_unlink
Destroy a named semaphore

±sem_init
Initialize an unnamed semaphore

±sem_destroy
Destroy an unnamed semaphore

±sem_getvalue
Get current semaphore count

±sem_wait
Try to lock the semaphore.
Wait otherwise.

±sem_trywait
Just tries to lock the semaphore,
but gives up if the semaphore is
already locked.

±sem_post
Release the semaphore.

Linux Programming
 
POSIX Real-time signals

POSIX real-time signals (1)þ

Regular signals

±Just 2 applications-specific signals:
SIGUSR1 and SIGUSR2

±No signal priorities

±Signals can't carry any extra information.

±Signals can be lost. When a signal is sent multiple times, the receiver will just process one instance.

POSIX signals

±Whole range of application specific signals: SIGRTMIN to SIGRTMAX

±Priorities available.
Top priority signals delivered first.

±Possible to carry extra information in a signal.

±Signals are queued. All pending signals are processed: no signal is lost.

POSIX real-time signals (2)þ

POSIX signal API available through signal.h
2 ways of processing signals:

±sigwaitinfo() and sigtimedwait() to wait for blocked signals (signals which remain pending until they are processed by a thread waiting for them.)þ

±sigsuspend() to register a signal handler and suspend the thread until the delivery of an unblocked signal (which are delivered without waiting for a thread to wait for them).

These signals are key in real-time applications, to notify processes of asynchronous events: high-resolution timer expiration, asynchronous I/O completion, new message in an empty message queue...

Linux Programming
 
POSIX clocks and timers

POSIX clocks and timers

Compared to standard (BSD) timers in Linux

± Possibility to have more than 1 timer per process.

± Increased precision, up to nanosecond accuracy

± Timer expiration can be notified
either with a signal or with a thread.

± Several clocks available.

Available POSIX clocks (1)þ

Defined in /usr/include/linux/time.h

±   CLOCK_REALTIME
System-wide clock measuring the time in seconds and nanoseconds since Jan 1, 1970, 00:00. Can be modified.
Accuracy: 1/HZ (1 to 10 ms)
þ

±   CLOCK_MONOTONIC
System-wide clock measuring the time in seconds and nanoseconds since system boot. Cannot be modified,
so can be used for accurate time measurement.
Accuracy: 1/HZ

 

Available POSIX clocks (2)þ

± CLOCK_PROCESS_CPUTIME_ID
Measures process uptime. 1/HZ accuracy. Can be changed.

± CLOCK_THREAS_CPUTIME_ID
Same, but only for the current thread.

Time management

Functions defined in time.h

±  clock_settime
Set the specified clock to a value

±  clock_gettime
Read the value of a given clock

±  clock_getres
Get the resolution of a given clock.

See man time.h and the manual of each of these functions.

Using timers (1)þ

Functions also defined in time.h

± clock_nanosleep
Suspend the current thread for the specified time,
using a specified clock.

± nanosleep
Same as clock_nanosleep,
using the
CLOCK_REALTIME clock.

Using timers (2)þ

± timer_create
Create a timer based on a given clock.

± timer_delete
Delete a timer

± timer_settime
Arm a timer.

± timer_gettime
Access the current value of a timer.

Using high resolution timers

Only added in recent kernels (2.6.21)þ

± 2 new clocks added to POSIX clocks:
CLOCK_REALTIME_HR

CLOCK_MONOTONIC_HR

± Depending on the hardware capabilities,
can give microsecond or nanosecond accuracy.

± Use them with regular clock and timer functions.

Linux Programming
 
Asynchronous I/O

Asynchronous I/O

±Helpful to implement non-blocking I/O.

±Allows to overlap compute tasks with I/O processing,
to increase determinism.

±Supported functionality:

®  Send multiple I/O requests at once from different sources

®  Cancel ongoing I/O requests

®  Wait for request completion

®  Inquire the status of a request: completed, failed, or in progress.

±API available in aio.h (man aio.h for details)þ

Compiling instructions

± Includes: nothing special to do.
Available in the standard path.

± Libraries: link with librt

± Example:
gcc -lrt -o rttest rttest.c

More information on the POSIX interface

±The POSIX manual pages

±Online:

http://www.opengroup.org/onlinepubs/009695399/idx/realtime.html

Embedded Linux System Design and Development
P. Raghavan, A. Lad, S. Neelakandan, Auerbach, Dec. 2005.
http://free-electrons.com/redirect/elsdd-book.html
Very nice and clear coverage on real-time programming
with the POSIX interface. Nice and useful examples.

±Guide to real-time programming
http://mia.ece.uic.edu/~papers/WWW/books/posix4/TOC.HTM

A 11-year old document, with some Digital Unix specifics,
but still up to date (thanks to standards).

 THANKS

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值