Ipc
Why
1. Data transfer
2. Resource Sharing
Objective:
3. Notification events
4. Process Control
Development:
1.UNIX interprocess Communication
2. Based on system V
3.POSIX
Way classification:
1.pipe (pipe) FIFO (named pipe)
2.signal
3. Message Queuing
4. Shared Memory
5. Signal Volume
6. Socket (socket)
Pipeline communication: Unidirectional, FIFO
Create
Pipe: Communication between father and son
FIFO: Any
Create:
int pipe (int filedis[2]);
When a pipeline is created, two file descriptors are generated: filedis[0] read pipeline, Filedid[1] write pipeline.
Shut down:
Close (filedis[1,2])
Close two descriptors
Pipe Reading and writing:
Parent process Write filedis[1], child process read Filedis[0]
Fork must be created again before pipe.
Famous pipeline: FIFO
Create
#include <sys/types.h>
#include <sys/stat.h>
int Mkfifo (const char*pathname,mode_t mode);
Pathname:fifo file name
Mode: Properties
Operation
When FIFO is turned on, flag (O_nonblock)
1. When not in use: The process is blocked when access is not met
2. Use: Access cannot be met without blocking, immediately error return, Error=enxio
Signal communication:
Signal type: (30 kinds)
E:
Sigint:crtl+c
SIGKILL:
Processing
1. Ignore
Sigkill,sigstop can't ignore
2. Perform the actions desired by the user
3. Perform default actions (mostly ignored)
Signal
#include <signal.h>
void (*signal (int signo,void (*fun) (int))) (int)//How to understand??
Func may take value:
1.SIG_LGN: Ignore this signal
2.SIG_DFL: by system default mode
3. Signal Processing function Name: Use this function to handle
Shared Memory:
Shared part of physical memory by multiple processes
Fast, no additional access functions required
Realize:
1. Create Shmget ()
2. Mapping to a specific process Shmat ()
int Shmget (key_t key,int size,int SHMFLG)
Key: Shared Memory key value
0/ipc_private created new memory, successfully returned the shared memory identifier, failed loopback-1.
int Shmat (int shmid,char*shmaddr,int flag)
Shmid: Shared Memory identifier
Flag: Determines in what way to determine the mapping address, usually 0
The mapped address is returned successfully, and the failure returns-1.
Out:
int Shmdt (CHAR*AHMADDR)
Send:
Kill: Send to itself or to another process
Rasie: Self
#include <sys/types.h>
#include <signal.h>
int Kill (Pid_t,int Signo)
int Rasie (int signo)
Pid>0:
Pid==0: Same group
Pid<0: A process sent to its process group ID equals |pid|
Pid==-1: All Processes
Alarm
unsigned int alarm (unsigned int seconds);
After a predetermined time, the SIGALRM signal is generated, and if not captured, the process is terminated.
Pause
Causes the process call to hang until a signal is received
int pause (void)
Only after a signal processing function has been executed does the hang end.
Signal Processing
Linux application Development-process communication (IPC)