Inter-process communication and synchronization
Half-Duplex piping
Prototype: intpipe (int filedes[2]);
Essentially create two file descriptors, Filedes[0] for reading, filedes[1] for writing.
When the number of written data is less than PIPE_BUF, the write is atomic, and when it is greater than pipe_buf, it may not be atomic.
If the pipeline is empty, then read is blocked, and if the pipeline is full, write is blocked.
Named pipes
Prototype: Intmkfifo (const char *pathname,mode_t mode); Use shell command Mkfifo to create
FIFO, you must use an open function to explicitly establish a channel that joins to the pipeline. Generally the FIFO is always in a blocked state, and if Read permission is set when the FIFO is open, the read process will block until the other process opens the FIIFO and writes data to the pipeline, and if a process opens a pipeline to write data, the write pipeline operation is also blocked when no process is reading the data from the pipeline. If blocking occurs when you do not want a named pipe operation, you can call the O_noblock flag in the open () call to turn off the default blocking action
Message Queuing
Message buffer Structure MSGBUF
structmsgbuf{
Longmtype;
CHARMTEXT[1];
}
The mtext is variable in length and the buffer maximum is #definemsgmax 8192 including mtype4 bytes.
The internal structure:
structmsqid_ds{
Structipc_perm Msg_perm;
Time_tmsg_stime; Timestamp of the last message sent to the queue
Time_tmsg_rtime; Timestamp of the last message fetched from the queue
Time_tmsg_ctime; Timestamp of the last change to the queue
Unsignedlong __msg_cbytes; Total number of bytes residing on the queue
Msgqnum_tmsg_qnum; The number of messages currently in the queue
Msglen_tmsg_qbytes; Maximum number of bytes that can be tolerated in a queue
Pid_tmsg_lspid; The PID of the last message process is sent
PID_TMSG_LRPIF; Accept the PID of the last message process
};
structipc_perm{
Key_tkey; Key value used by function Msgget ()
Uid_tuid; UID of the user
Gid_tgid; User's GID
Uid_tcuid; UID of the Creator
Git_tcgid; GID of the Creator
Unsignedshort mode; Permissions
Unsignedshort seq; Serial number
};
Key value Constructor Ftok ():
Prototype: Key_tftok (const char *pathname,int proj_id);
Pathname must be a directory that already exists, and PROJ_ID is a 8-bit value, usually represented by a, B, and so on.
Get the message Msgget () function:
Prototype: Intmsgget (Key_tkey,int MSGFLG);
The first parameter is the key value, which can be generated with the Ftok () function, Msgflag can specify permissions, or the operation IPC_CREATE,IPC_EXCL can be specified. IPC_EXCL itself doesn't work, but with ipc_create, you can return an error when a message queue already exists.
Successful return identifier, error return-1
Send Message function msgsnd ()
Prototype: intmsgsnd (int msqid,conset void *msgp,size_t msgsz,int MSGFLG);
Msgfls can be either 0 or ipc_nowait, and messages are not written if the message queue is full. If ipc_nowait is not specified, it is blocked.
Accept Message MSGRCV ()
Prototype: SSIZE_TMSGRCV (int msqid,void *msgp,size_t msgsz,long msgtyp,int msgflg);
MSGTYPE Specifies the type of get message if you pass 0, you get the oldest message, ignoring the type. MSGSZ represents the size of the message buffer structure and does not include the length of the mtype.
If MSGFLG is ipc_nowait, the enomsg is returned when there is no message, otherwise it is blocked.
Message control Msgctl () function
Prototype: intmsgctl (int msqid,int cmd,struct msgqid_ds *buf);
CMD as follows
Ipc_stat |
Get the MSQID_DS structure of the queue and store it in the address referred to by the BUF variable |
Ipc_set |
Sets the Ipc_perm member value of the MSQID_DS structure of the queue, which gets the value from BUF |
Ipc_rmd |
Remove the queue from the kernel |
Signal Volume:
Signal volume data structure:
unionsemun{
intval; Shaping variables
Structsemid_ds *buf; SEMID_DS Structure Pointers
Unsignedshort *array; Array type
Structseminfo *__buf; Internal structure of signal volume
};
New semaphore function Semget ()
Prototype: Intsemget (Key_t,int nsems,int SEMFLG);
Used to create a new semaphore collection, or to access an existing collection. Key is the key value generated by Ftok, Nsems can specify the number of semaphores that should be created in the new collection. The third parameter, SEMFLG, is the way the semaphore is opened.
SEMFLG specify permissions, or you can add ipc_create, ipc_excl
Semaphore operation function Semop ()
Prototype: intsemop (int semid,struct sembuf *sops,unsigned nsops);
The second argument is a pointer to an array that will perform operations on the semaphore collection, and the third parameter is the number of operations in that array.
structsembuf{
Ushortsem_num; The number of the semaphore
Shortsem_op; Operation of Signal volume
SHORTSEM_FLG; Operation Flag of the semaphore
}
Sem_op if positive, add a value from the semaphore, minus one negative, and 0 to set the process to sleep until the semaphore value is 0.
Control semaphore parameter Semctl ()
Prototype: intsemctl (int semid,int semnum,int cmd,...);
Semid is the value returned by the call to Semget. Semnum is the number of semaphores to perform the operation, and for the first semaphore, his index value is 0. The cmd parameter represents the command that will be executed in the collection
CMD value:
GETALL
Ipc_stat |
Gets the SEMID_DS structure of a collection, It is stored in the address specified by the BUF parameter of the Semun union |
Ipc_set |
Sets the value of the Ipc_perm member of the SEMID_DS structure of a collection, which takes The value is taken from the BUF parameter of the Semun union to |
Ipc_rmid |
Remove the collection from the kernel |
Gets the value of all semaphores in the collection value, and the integer value is placed in an array of unsigned short integers the array is specified by the array member of the Union |
GETNCN T |
Returns the number of processes that are currently waiting for a resource |
Getpid |
Returns the pid |
Getval |
Returns the value of a semaphore in the collection |
getzcnt |
Returns the number of processes waiting for resource utilization up to 100% |
SETALL |
Sets the value of all semaphores in the collection to the corresponding value contained by the Union array member |
Setval |
Sets the value of a single semaphore in the collection to the value of the Val member of the union |
Shared Memory:
Create a shared memory function shmget ()
Prototype: Intshmget (key_t key,size_t size,int SHMFLG);
Get the shared memory address function Shmat ()
Prototype: void* shmat (int shmid,const void *shmaddr,int shmflg);
If the shmaddr is 0, the kernel tries to find a region that is mapped.
Shm_rnd can be manipulated with the flag parameter to align the routed address page
Shm_rdonly, the flag shared memory field is read-only.
Delete Shared memory Shmdt ()
Prototype: INTSHMDT (const void *SHMADDR);
Disconnect the process from the memory segment and not really delete it.
Control function Shmctl ()
Prototype: intshmctl (int shmid,int cmd,struct shmid_ds *buf);
Exactly like the msgctl.
Signal:
Signal intercept function signal ()
Prototype: sighandler_tsignal (int signum,sighandler_t handler);
handler function prototype: Voidsighandler (Intsigno); Signo will pass in the signal value
Send signal to process kill () and raise ()
Prototype: Intkill (pid_t pid,int SIG);
int raise (int sig);
The Kill function when the PID is 0 o'clock mass all processes. Raise is to bootstrap a signal in the current process.
This article is from http://qianyang.blog.51cto.com/, please be sure to indicate the source
Inter-process communication and synchronization