Operating system - inter-process communication (a)

Verbatim https://www.dazhuanlan.com/2019/08/26/5d632d2a07f58/


IPC use category

avatar

This article has nothing to do with pictures and purely fun

Relative to do something, we want to sleep late, but relative to sleep, I want to write summary!
Today the blog's gitbranch confused, wasted little time. Then introduce today about inter-process communication ( IPC). Interprocess communication, from the << UNIX2 Network Programming Volume: interprocess communication >> last chapter of Sun RPC talk, then why? Because we have a thing to grasp, because it is certainly valuable, we go to learn if some do not have the technology now, out of, then we do not need to get to the bottom, only to learn their thoughts, you can kick the tires.

Finally, a summary section is introduced, when building an application process, we first make a choice between:

(1) constructing a large single process, all the work is completed;
(2) the entire process applied to a walk in a plurality of processes communicating with each other.
If we choose the latter, the next choice is:

This book is mainly concerned with 2athis situation, that is, use 消息传递, 共享内存and may use some form 同步for inter-process on the same hostIPC . Within the same process between different threads IPCand different processes between the various threads IPCjust a special case of this situation.

The application process required for network communication between different parts of the most use to display network programming ( explicit network programmingwrite) the way we are now popular, is Socketprogrammed.

So now what happens then? Thanks to the Internet fire, leading to inter-process communication is now mainstream popular way to use into a Socketsocket communication. This can also be understood, because the Internet is the traditional C/S(server - client) architecture for client development, the main is the mobile application development, PCend application development, Web browser development, which is the Internet's main traffic entrance applications and browser objects need to communicate is the server, the server and the client need to do is communicate better, so if you need working on the Internet, then the Socketprogram relative to other IPCmeans need to be more familiar with, especially in the server backend development, Fortunately, the client, the client business logic and UIexperience relative importance, as some systems layer technology, development framework have been packaged well, you can just learn how to use, like android, IOSdevelopment. But if you are engaged in system development, then all the IPCway need to know, in particular 消息传递, 共享内存need to understand.

Specific IPC means

signal

Software interrupt notification event handling example: SIGFPE, SIGKILL, SIGUSR1, SIGSTOP , SIGCONT
received signal what will happen:

  • Catch: Specifies the signal handler is called
  • Ignore: relying on the operating system's default
    example: Abort, memory dump, suspend or resume process
  • Mask: a blocking signal does not occur
    may be temporary (the same type of signal processing)

Inadequate: not transmit any data to be exchanged

Features:

  • signals are only delivered between processes when either: only transfer between a user of the same process, or the super-user signaling
    1. the processes involved are run by the same user
    2. the raising process is run by a superuser
  • signal types and type names are defined in signal.h defined in the document signal.h
  • signals are not queued signals do not line up
  • handlers "terminate process" (SIG_DFL) and "ignore signal" (SIG_IGN) are already defined in signal.h commonly used to terminate the process and ignore signals defined in signal.h

unix The main interface information:

1
2
3
4
5
6
7
8
9
10


Functions: void ( int sig_type)
Description: signal sig_type signal.

Functions: void Signal ( int sig_type, void (* sig_handler) ( int signal_type))
Description: sig_type is (or name) signal.h one of the signal values defined. sig_handles is a pointer to a function with a parameter int. When the process is to specify the type of hit signals, this function will run in the process context.

Functions: int the kill ( pid_t dest_pid, int sig_type)
the Description: Send sig_type type of signal to the process using PID dest_pid run. dest_pid particular type 0 and -1 make kill () all all processes, systems and signals are transmitted with the same set of sender executed operation (only for the super user credentials).

Pipes and FIFO

1. pipeline, conduit typically refers to the unknown, it is the UNIXsystem IPCof the oldest forms .
Features:

  • pipes are uni-directional half-duplex
  • with file descriptors, they can only be shared between processes with parent-child relationship can only be used between parent and child
  • atomicity is guaranteed for messages shorter than PIPE_BUF bytes cache size specific atomicity
  • alf-open pipes are told widowed (or broken) pipes. Writing on them causes a write error, reading from them always returns 0 bytes. bad pipeline, the write error occurs, when read, it returns 0 bytes.

unix The main interface information:

1
2
3
4
# The include : unistd.h
Functions: int pipe ( int * fd_couple)
Description: Creates a pipeline and stored in the document descriptor fd_couple [0] (read end) and fd_couple [1] (writing end).
return : the successful return 0 , error return -1

Simulated usage scenarios, implement Linuxa system call popen, the main code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
FILE * vpopen(const char * cmdstring, const char * type)
{
int pfd[2];
FILE * fp;
pid_t pid;

if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0)
{
errno = EINVAL;
return (NULL);
}

if(pipe(pfd) != 0)
{
return NULL;
}

if((pid = vfork()) < 0)
{
return NULL;
}
else if(pid == 0)
{
if(*type == 'r')
{
close(pfd[0]);
if (pfd[1] != STDOUT_FILENO)
{
dup2(pfd[1], STDOUT_FILENO);
close(pfd[1]);
}
}
else
{
close(pfd[1]);
if (pfd[0] != STDIN_FILENO)
{
dup2(pfd[0], STDIN_FILENO);
close(pfd[0]);
}
}

if(execl("/bin/bash", "bash", "-c", cmdstring, (char *) 0) < 0)
{
return NULL;
}
}

the wait ( 0 ); end // and other sub-process, sub-process recycling

if (*type == 'r')
{
close(pfd[1]);
if ((fp = fdopen(pfd[0], type)) == NULL)
{
return (NULL);
}
}
else
{
close(pfd[0]);
if ((fp = fdopen(pfd[1], type)) == NULL)
{
return (NULL);
}
}

return (fp);
}

2. Named pipes, FIFO,
features:

  • Each FIFO has a name associated with the path that allows communication between unrelated processes
  • Has the attributes and characteristics of the standard pipe

unix The main interface information:

1
2
3
4
*  #include: sys/types.h and sys/stat.h
* functions: int mkfifo(char *path, mode_t mode)
* description:path是要创建的FIFO文档的(路径+)名称。 mode是文档权限(请参阅umask(2)和chmod(2))
* return: 成功返回 0,错误返回非 0

模拟使用场景:

1
2
3
4
5
6
7
* 模拟场景
* 1. 在 shell 端使用命令创建有名管道: mkfifo /tmp/named_pipe
* 2. 运行该接口
* 3. 在 shell 端写入数据到管道 :echo "hey, this is a message" > /tmp/named_pipe
* 4. 测试完毕,删除管道: rm /tmp/named_pipe
*
* 当然我们也可以自己启动一个任意进程向FIFO管道里面写数据

具体实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int fifo_read()
{
int pipe;
char ch;
static char TmpCh;

/* we expect a named pipe has been created
* in /tmp/named_pipe . The command
* $ mkfifo /tmp/named_pipe
* is a friend to get to this point
*/

pipe = open("/tmp/named_pipe", O_RDONLY);
if (pipe < 0) {
printf("open fifo error, maybe you should create a fifo first !n");
exit(1);
}

/* preparing to read from the pipe... */
printf("Waiting data from the pipe... n");

/* reading one char a time from the pipe */
while (1) {
if (read(pipe, &ch, 1) < 0) {
printf("Read the pipe failed! n");
exit(2);
}


if(ch != TmpCh)
{
printf("%c", ch);
}

TmpCh = ch;
}

/* leaving the pipe */
close(pipe);

return 0;
}

总结

Above mainly IPCmade of a general introduction, and introduces signal (Signal) and duct two kinds of IPCways, both of which Linuxcomes more functions implemented by the system. As Posixand System Vinter-process communication message queues , signals , and shared memory , since the content more later reintroduced separately.

Guess you like

Origin www.cnblogs.com/petewell/p/11410494.html