Road C ++ learning --9

IPC: Inter-process communication interprocess Communication, data exchange through a mechanism provided by the kernel buffer.
There are several communication mode IPC:

  • pipe pipeline - the easiest
  • fifo named pipe
  • mmap mapping file sharing IO-- fastest
  • The most stable local socket
  • Signal carrying the minimum amount of information
  • Shared memory
  • message queue

1, pipe conduit half duplex communication
pipe function: int pipe (int pipefd [2 ])

  • pipefd read and write file descriptors 0 for read 1 for write
  • Return Value 0 successful return, else return -1
    inter-process communication Sons
    dup (fd [1], STDOUT_FILENO ); // standard output to the write end of the pipe, the redirection.
    Reading the pipeline:
  • Write end all closed, read read 0, equivalent to the end of the file read
  • Not all the write end is closed, the data - read read data, no data -read blocked, fcnt non-blocking function can be changed.
    Write pipeline:
  • Read all closed end - to generate a signal SIGPIPE, abnormal program termination
  • Not all of the read end of the pipe is closed -write full blockage of tubing under normal write -write
    calculating pipe size. 8 Long the fpathconf * 512 (FD int, int name)
    the ulimit A
    inter-process communication blood only. Parent and child one-way communication

FIFO communication

Realize unrelated interprocess communication
to create a pipeline of a pseudo-file mkfifo

Share mmap mapped area

Create a mapping area
void * mmap (void * addr, size_t length, int prot, int flags, int fd, off_t offset);

  • addr pass NULL
  • length
  • prot prot_read readable and writable prot_write
  • fd file descriptor, open a file open
  • at offset
  • The return value success the first available memory address, failure map_failed
  • flags map_shared shared map_privated private
    release mapping area
    int munmap (vod * adr, size_t length);
  • addr pass return values ​​mmap
  • length
    Here Insert Picture Description
    the MAP_ANON anonymous map

signal

Mechanism: Process B to process A, sending and processing ----- soft interrupt the kernel is responsible, there may be a delay
produce: a key generating, kill, timer alarm, error
status: production, delivery of pending
default Disposition: ignore, perform a default action, capturing
elements: number, event name, the default processing action (termination, ignore, terminate + generate core, pause, continue) man 7 signal
19 and No. 9 signal can not be captured, ignored, and blocking .
blocking signal set signal and the pending
kill function
int kill (pid_t, pid, int sig); pid> 0, the process pid = 0 to be sent on behalf of all the currently loaded process pid = -1 for the process group has permission to send all Therefore, process in the process pid <0 representative of a corresponding group -pid

and abort function raise
raise function sends the specified signal (for ourselves) raise (signo) == kill ( getpid (), signo) to the current process;
clock signal
alarm to the timing of their transmission signal
setitimer function, a periodic transmission signal Here Insert Picture Description
cate and use as tail
signal set function
int sigemptyset (sigset_t * set); clear signal sets
int sigfillset (sigset_t * set); set fill signal
int sigadset (sigset_t * set); adding a set signal to the signal
int sigdelset (sigset_t * set) ; remove a signal from a collection
int sigismember (sigset_t * set); returns 1 if signum collection is represented in the collections of members
int sigpromack (int how, const sigset_t * set, sigset * oldset); settings to block or unblock signals set
how sig_block set blocking sig_unblock unblock sig_setmask set new blocking
set the incoming signal set

int sigpending () Gets the pending signal set
signal capture
sighandler_t signal (int signum, sighandler_t handler ); to prevent the accidental death process
int sigaction (int signum, const struct sigaction * act, strtct sigaction * oldact); registration capture function Here Insert Picture DescriptionHere Insert Picture Description
sigchld signal processing
sub sigchld process will send a signal to pause and exit, we can recover the child process by capturing sigchld signal.

Guess you like

Origin blog.csdn.net/weixin_43615373/article/details/90898619