IPC (Inter Processes Communication) of LINUX system programming
1. Signal
1. Signal Generation
Software interrupt, asynchronous communication, ctrl+c, kill function, kill command, hardware exception (segment fault), software exception
2. After the process receives the signal, it can be handled as follows:
Execute the system default action (termination), ignore this signal, and execute the custom signal handler.
3. Signal operation function
kill(), alarm(), raise(), abort(), pause()
typedef void (*sighandler_t)(int);signal()
sigemptyset (), sigfillset (), sigismember (), sigdelset (), sigaddset ()….
Signal blocking set: blocking signal reception sigprocmask();
2. Pipes/Named Pipes
1. Unnamed pipe: similar to a file, there are 2 file descriptors and only exist in memory
Half-duplex, data is input from the write end, read from the end, first in first out, fd[0] output, fd[1] write
The data has no format requirements, only a fixed size, no name, and its descriptors will be inherited with fork and vfork
Only used by the common ancestor process, such as the pipe created by the parent process, only the parent process and its child processes can access
int fd [2];
pipe(fd);
write();read();
close(fd);
read will block when reading data from the pipe. If there is data, it will read and return, and if there is no data, it will wait.
write writes data from the pipe, blocking until the data is read
If the reading process exits, the writing process will also exit when writing data. You can use fcntl() to set the blocking feature
dup(old) copies the old file descriptor and allocates a new one, the read and write positions are also copied
dup2(old, new) copies the file descriptor old, allocates a new file descriptor new, and new also identifies the file identified by old
Commonly used such as redirecting 0,1,2 descriptors
int fd_stdout=dup(1); copy stdout descriptor 1
dup2(fd_stdout, 1); reassign descriptor 1 to stdout
2. Named Pipe FIFO
Has a name, exists in the file system, and the content only exists in memory
Unrelated processes can also use
Named pipe will become a normal file after copying
mkfifo("./cmd_fifo", 0777);
open("./cmd_fifo", O_RDWR);
write();read();
close();
3. Message queue
The linked list maintained by the kernel, the message has a format (structure), and the message has a type, which can be randomly queried according to the type
Has an identifier, which can only be deleted by a kernel restart or manual deletion
The first member of the structure represents that the message type must be a long variable, and the others are defined by themselves
send message msgsent(msqid, &msg, sizeof(msg)-4, 0);
Receive message msgrcv(msqid, &msg, sizeof(msg)-4, type, 0); return length
control msgctl(msgqid, IPC_RMID, NULL)
typedef struct _msg
{
long mtype;
char mtext[50];
}MSG;
MSG msg;
key=ftok(".", 2012)
msgqid = msgget(key, IPC_CREAT|0666);
if(msgqid==-1)
perror("msgget");
msgrcv(msgqid, &msg, sizeof(msg.mtext), 10, 0);
msgctl(msgqid, IPC_RMID, NULL);
View message queue ipcs -q
delete message queue ipcrm -q msgqid
4. Shared memory
Multiple processes share a given storage space
Fastest way to communicate, access mutex
view ipcs -m
remove ipcrm -m shmid
create or open int shmid = shmget(key, size, flag);
Memory mapped shmat()
Unmap shmdt()
control shmctl()