Obsah

Mezi-procesová komunikace (IPC)

Sdílená paměť

Zamykaní

Existuje C++ třída mutex

POSIX semafory 1 2

System V Semafor 1

Nepojmenovaná roura

#include <unistd.h>
 
int pipefd[2];
 
pipe(pipefd);
..
close(pipefd[0]);
close(pipefd[1]);

Spojení s procesem jednosměrně

Spojení s procesem obousměrně

Neblokující roura

// set non-blocking mode
if (fcntl(pipe_fd, F_SETFL, fcntl(pipe_fd, F_GETFL) | O_NONBLOCK) == -1) {
  fprintf(stderr,"Cannot set non-blocking mode to pipe\n");
}
// set blocking mode
if (fcntl(pipe_fd, F_SETFL, fcntl(pipe_fd, F_GETFL) & ~O_NONBLOCK) == -1) {
  fprintf(stderr,"Cannot set blocking mode to pipe\n");
}
struct timeval tv;
fd_set rfds;
fd_set wfds;
int nfds;
 
// set highest pipe FD +1 
nfds = (pipefd[0]>pipefd[1])?pipefd[0]+1:pipefd[1]+1;
 
while(...) {
 
  FD_ZERO(&rfds);
  FD_SET(pipefd[0],&rfds);
 
  FD_ZERO(&wfds);
  FD_SET(pipefd[1],&wfds);
 
  tv.tv_sec = 5;
  tv.tv_usec = 0;
 
  if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) {
    fprintf(stderr,"pipe error %d\n", errno);
  }
 
  if (FD_ISSET(pipefd[0], &rfds)) {
    ..
    // do reading
  }
  ..  
}