LinuxC process among several ways to communicate

A conduit, pipe () function 
  to achieve the most simple and practical for the loop kernel buffer queue.
  For interprocess father and son, brother and other blood communications.   Unidirectional flow, can only be read from the read end of the pipe, writes writing end.
  FDS int [2];
  pipe (FDS); // outgoing parameters, fd [0] is the read end descriptor, similar stdin; fd [1] to the write end descriptor, similar to stdout
  
2 named pipe, fifo. () function
  Linux underlying file type.
  It can be used for inter-process communication unrelated.
  You can read multiple end, multiple write end.
  mkfifo ( "test", 0777) ; // create a named pipe
  int fd1 = open ( "test" , O_WRONLY); write (fd1, buf, strlen (buf)); // a process writes
  int fd2 = open ( "test", O_RDONLY) read ( fd2, buf, sizeof (buf)); // read another process
3 files, open function ().
  child process fork to create and share open file descriptors.
  Multiple processes open the same file.
4 . Shared memory mapping, mmap () function
  by means of the memory mapped file is created.
  Interprocess unrelated requirements   
* the mmap void (void * addr, size_t len, int Prot, the flags int, int FD, of off_t offset);
    addr: mapping the first address region, pass NULL, the kernel automatically assigned
    len: mapping area size
    prot: read and write access mapping area, optional PROT_READ, PROT_WRITE, PROT_READ | PROT_WRITE
    flags: flags, MAP_SHARED indicate to synchronize files, MAP_PRIVATE not synchronized to the file, MAP_ANONYMOUS / MAP_ANON anonymous mapping area
    fd: file descriptor
    offset: offset, the offset from the beginning of the file how much start mapping. offset size must be an integer multiple of 4K
  int munmap (void * addr, size_t len); // release the mapping area
    addr: first address mapping area, it is not for the first address mapping zone ++ or - operations
    len: mapping area the size of the
  note point:
    create a file after the mapping area can be turned off immediately.
    Used to create the mapping area file size can not be zero.
    Create a mapping process zone contains a read operation on a file, the file must have at least read access to it.
    Offset offset must be a multiple of 4K, as the page size is 4K Linux
    munmap mmap the address has to be returned, not address mapping area ++ or - Operation
    MAP_SHARED create anonymous mapping area using the linux, MAP_ANONYMOUS MAP_ANON or fd = -1 and parameters can, under Unix requires fd = open ( "/ dev / zero", O_RDWR).

5
semaphore, signal () function
  of the lowest cost
6 . Local socket, socket () function
  best stability

 

Guess you like

Origin www.cnblogs.com/yongfengnice/p/11886480.html