当前位置:网站首页>Communication mode between processes

Communication mode between processes

2022-07-07 09:27:00 wrdoct


Preface

Communication between processes .

Communication between processes

Interprocess communication (IPC): The data transfer 、 Notification event 、 Resource sharing 、 Process control .

 The signal ;  //Unix
 The Conduit ;  //Unix
 Famous pipeline (FIFO);  //Unix

 Message queue ;
 Shared memory ;
 Semaphore ;

Socket Socket .

One 、 The Conduit

Used to have genetic relationship Communication between processes ;
A pipe is a Byte stream ;
The data transmitted through the pipeline is The order Of , The order in which bytes are read from the pipeline is exactly the same as the order in which they are written to the pipeline ( It's like a queue );
The direction of data transmission in the pipeline is A one-way Of , Write at one end and read at the other , yes Half duplex Of ;
Pipeline read data is Disposable Of .
Terminal command :

ls | wc -l   //wc—— Number of statistical files 
ulimit -a  // View pipe buffer size 

function :

int pipe(int pipefd[2]);// Create an anonymous pipeline , Used for interprocess communication .
long fpathconf(int fd, int name);// View pipe buffer size 

【 notes 】 Why pipes are used for inter process communication with kinship ( Why can the processes of kinship communicate through pipelines )?
Because the parent process fork Come out with a sub process , Will be able to Virtual address space replication One copy , There is one in the virtual address space of the parent process Document descriptor table , Point to the read end and write end , Then the child process also has a file descriptor table , Point to the read end and write end , So it can communicate .
【 notes 】 Why should one close the read side and the other close the write side ?
To avoid the father ( Son ) process Write Then father ( Son ) Process again read .

Two 、 Famous pipeline FIFO

The famous pipeline provides a Pathname Associated with it , therefore FIFO The process created does not have the restriction of kinship , As long as the process can access the path , You can pass FIFO Mutual communication .
Terminal command :

mkfifo  name   // establish FIFO The Conduit 

function :

int mkfifo(const char *pathname, mode_t mode); // establish FIFO The Conduit 

Reading and writing characteristics of pipeline

 Read pipeline :
     There's data in the pipeline :
    	read Returns the actual number of bytes read 
     No data in the pipeline :
    	 The write side is completely closed ,read return 0( Equivalent to reading to the end of the file )
    	 The write side is not completely closed ,read Block waiting 
    
 Write the pipeline :
     All readers are turned off :
    	 Process aborted ( Process received SIGPIPE The signal )
     The reader is not all closed :
         The pipe is full ,write Blocking 
         The pipe is not full ,write Write data to , And return the actual number of bytes written 

3、 ... and 、 Memory mapping

(I/O) Disk file Data mapping to Memory , adopt Modify memory to modify files .
The process communication realized by memory mapping is Non blocking Of .
function :

// Map the data of a file or device to memory 
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
// Free memory map 
int munmap(void *addr, size_t length);

Four 、 Shared memory

Shared memory allows two or more processes Shared physical memory The same area of ( Often referred to as segments ).
No kernel intervention is required .
Terminal command :

ipcs -a // Print all ipc Information 
ipcs -m // Print shared memory ipc Information 
ipcs -q // Print message queue ipc Information 
ipcs -s // Print signal ipc Information 

ipcrm -M shmkey // remove shmkey Shared memory segment created 
ipcrm -m shmid // remove shmid The shared memory segment identified 
ipcrm -Q msgkey // remove msgkey Created message queue 
ipcrm -q msgid // remove msgid Identified message queue 
ipcrm -S semkey // remove semkey Created signals 
ipcrm -s semid // remove semid Signal of identification 

function :

// Create a new shared memory segment , Or get the ID of an existing shared memory segment . The data in the newly created memory segment will be initialized to 0
int shmget(key_t key, size_t size, int shmflg);
// Associate with the current process 
void *shmat(int shmid, const void *shmaddr, int shmflg);
// signal communication  addr
// Release :
// Disassociate the current process from shared memory 
int shmdt(const void *shmaddr);
// Delete shared memory , Call it once , All associated processes are disassociated before calling 
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
 How does the operating system know how many processes are associated with a piece of shared memory ?
 answer :	 Shared memory maintains a structure struct shmid_ds,
	 There is a member in this structure  shm_nattch,
	shm_nattach  The number of associated processes is recorded 

5、 ... and 、 The signal

The signal is Linux One of the oldest ways of interprocess communication , Is the of the process at the time of the event A notification mechanism , Sometimes called Software interrupt , It is a simulation of interrupt mechanism at software level , It's a kind of asynchronous communication The way .
Signals can cause one running process to be interrupted by another running asynchronous process , To deal with an emergency .

 Characteristics of signal :
	 Simple ; Can't carry a lot of information ; Send only when a certain condition is met ; High priority .

Terminal command :

kill -l  // Look at all the signals 

Common signals :

SIGINT  // Terminate the process ——ctrl+C
SIGQUIT  // Terminate the process ——ctrl+\ 
SIGKILL  // Kill process 
SIGCSTOP  // Stop the process 
SIGCONT  // Continue the process 
 The signal  5  A default processing action :
	Term  Terminate the process 
	Ign  The current process ignores this signal 
	Core  Terminate the process , And generate a Core file , Used to save error messages  //
	Stop  Pause the current process 
	Cont  Continue executing the currently suspended process 

【 notes 】Core Use :

ulimit -a
ulimit -c unlimited  // Change the upper limit of available resources 

g++ ./a.out -g
gdb a.out core-file core // see core File error message 

The state of the signal : produce outstanding ( The signal is generated and not processed )、 Blocking ( The blocking signal is processed , Do not block signal generation ).

int kill(pid_t pid, int sig);// To any process or process group pid,  Send any signal  sig
int raise(int sig);// Send a signal to the current process 
void abort(void);// send out SIGABRT Signal to the current process , Kill the current process 

// Set the timer ( alarm clock ). Function call , Start countdown , When the countdown is 0 When , The function sends a signal to the current process :SIGALARM, Terminate the current process 
unsigned int alarm(unsigned int seconds);
alarm(0); // Cancel timer 
// Set the timer ( alarm clock ). Can replace alarm function . Precision is subtle us, Periodic timing can be realized ( Do one thing every few seconds )
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);

// Set the capture behavior of a signal  
sighandler_t signal(int signum, sighandler_t handler); 
// Check or change signal processing . Signal capture 
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);           

【 notes 】 Use SIGCHLD Signals solve the problem of zombie process .

Multiple signals can use one called Signal set The data structure of .

int sigemptyset(sigset_t *set);// Clear the data in the signal set , Position all the signs in the signal set as 0
int sigfillset(sigset_t *set);// Position all the signs in the signal set as 1
int sigaddset(sigset_t *set, int signum);// Set the flag bit corresponding to a signal in the signal set to 1, Indicates blocking this signal 
int sigdelset(sigset_t *set, int signum);// Set the flag bit corresponding to a signal in the signal set to 0, Indicates that the signal is not blocked 
int sigismember(const sigset_t *set, int signum);// Determine whether a signal is blocked 

int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);// Set the data in the custom signal set into the kernel ( Set blocking , unblocked , Replace )
int sigpending(sigset_t *set);// Get the pending semaphore set in the kernel 

6、 ... and 、Socket Socket

TCP/IP Protocol family communication socket Introduction and programming

原网站

版权声明
本文为[wrdoct]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070648212298.html