Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

IPC of LINUX system programming

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

IPC (Inter Processes Communication) for LINUX system programming

I. signal

1. Signal generation

Software interrupts, asynchronous communications, ctrl+c,kill functions, kill commands, hardware exceptions (segment errors), software exceptions

two。 When a process receives a signal, it can be processed in the following ways:

Perform the system default action (termination), ignore this signal, and execute a custom signal processing function.

3. Signal operation function

Kill (), alarm (), raise (), abort (), pause ()

Typedef void (* sighandler_t) (int); signal ()

Sigemptyset (), sigfillset (), sigismember (), sigdelset (), sigaddset ()....

Signal blocking set: receive sigprocmask of blocking signal ()

II. Pipes / named pipes

1. Unnamed pipe: similar to a file, with 2 file descriptors that exist only in memory

Half-duplex, data in from write, read out, first in, first out, fd [0] output, fd [1] write

Data has no format requirements, only fixed size, no name, using fork and vfork will inherit its descriptor

Only common ancestor processes, such as pipes created by the parent process, can only be accessed by the parent process and its child processes

Int fd [2]

Pipe (fd)

Write (); read ()

Close (fd)

Read will block when reading data from the pipeline. Some data will be read to return, and if there is no data, it will wait.

Write writes data from the pipeline and is full of blocking until the data is read out.

If the read process exits, the write process also exits the available fcntl () setting blocking feature when the write process writes data

Dup (old) copies the old file descriptor and assigns a new descriptor, and the read-write location is also copied.

Dup2 (old, new) copies the file descriptor old, assigns a new file descriptor new, and new also identifies the file identified by old

Commonly used descriptors such as redirect 01pm 2 descriptors

Int fd_stdout=dup (1); copy stdout descriptor 1

Dup2 (fd_stdout, 1); reassign descriptor 1 to stdout

two。 Named pipe FIFO

It has a name, exists in the file system, and the content exists only in memory.

Unrelated processes can also be used

Named pipes become normal files after they are copied

Mkfifo (". / cmd_fifo", 0777)

Open (". / cmd_fifo", O_RDWR)

Write (); read ()

Close ()

3. Message queue

A linked list maintained by the kernel. Messages have a format (structure) and messages have types, which can be randomly queried by type.

There is an identifier, which can only be deleted by kernel restart or manual deletion.

The first member of the structure represents that the message type must be a long variable, and the others are self-defined.

Send message msgsent (msqid, & msg, sizeof (msg)-4,0)

Receive message msgrcv (msqid, & msg, sizeof (msg)-4, type, 0); return length

Control msgctl (msgqid, IPC_RMID, NULL)

Typedef struct _ msg

{

Long mtype

Char mtext [50]

} MSG

MSG msg

Key=ftok (., 2012)

Msgqid = msgget (key, IPC_CREAT | 0666)

If (msgqid==-1)

Perror ("msgget")

Msgrcv (msgqid, & msg, sizeof (msg.mtext), 10,0)

Msgctl (msgqid, IPC_RMID, NULL)

View message queuing ipcs-Q

Delete message queuing ipcrm-Q msgqid

4. Shared memory

Multiple processes share a given storage space

The fastest way to communicate, access mutual exclusion

View ipcs-m

Delete ipcrm-m shmid

Create or open int shmid = shmget (key, size, flag)

Memory mapped shmat ()

Unmap shmdt ()

Control shmctl ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report