1. Pipes and named pipes:
Pipes can be used for communication between related processes. In addition to the functions of pipes, famous pipes also allow communication between unrelated processes.
2. Signal:
Signal is a simulation of interrupt mechanism on the software level. This is a complex communication mode, which is used to inform that a process event has occurred. The effect of a process receiving a signal is the same as that of a processor receiving an interrupt request.
3. Message Queuing:
Message queue is a linked list of messages, which overcomes the shortcomings of limited semaphores in the above two communication methods, and the process with write permission can add new information to the message queue according to certain rules; A process with read access to the message queue can read information from the message queue.
The message buffer communication technology was first put forward by Hansen, and its basic idea is: according to the principle of "producer-consumer", the information exchange between processes is realized by using the public message buffer in memory.
Open several message buffers in memory to store messages. Whenever a process sends a message to another process, it will apply for a message buffer, send the prepared message to the buffer, then insert the message buffer into the message queue of the receiving process, and finally notify the receiving process. After receiving the notification of the transmission mileage, the receiving process picks up a message buffer from the message queue of this process, takes out the required information, and then gives the message buffer to the system irregularly.
A process can send messages to several processes, on the contrary, a process can receive messages from different processes. Obviously, the operation of message queues in the process is a key area. When the sending process adds a message to the message queue of the receiving process, the receiving process cannot receive and send messages from the message queue at the same time, and vice versa.
The message buffering communication mechanism includes the following contents:
(1) message buffer, which is a data structure consisting of the following items:
1, message length
2. Message body
3. transmitter
4. Message queue pointer
(2) Message queue header pointer m-q is generally stored in PCB.
(1) mutex semaphore m, with an initial value of 1, is used for mutex access to the message queue and is set in PCB.
(2) Synchronization semaphore m-syn, with an initial value of 0, is used for message counting and is set in PCB.
(3) sending message primitive send
(4) Receiving the message primitive (A)
4.*** Shared memory:
It can be said that this is the most useful way of inter-process communication. It enables multiple processes to access the same memory space, and different processes can see the update of data in memory by another process in time. This method needs to rely on some synchronous operations, such as mutex and semaphore.
This communication method needs to solve two problems: the first problem is how to provide * * * access to memory; Second, public * * * memories are mutually exclusive, which is the responsibility of program developers.
5. Semaphore:
It is mainly used as a means of synchronization and mutual exclusion between processes and between different threads of the same process.
6. Socket;
This is a general interprocess communication mechanism, which can be used for interprocess communication between different machines in the network and is widely used.
t; /* Number of processes waiting to increase in semval */
Usport semzcnt/* Number of processes waiting for semval = 0 */
}
# include & ltsys/types . h & gt;
# include & ltsys/IPC . h & gt;
# include & ltsys/SEM . h & gt;
int semget(key_t key,int nsems,int flag);
Key is the keyword of IPC structure mentioned above. The flag will determine whether to create a new semaphore set or reference an existing semaphore set in the future. Nsems is the number of semaphores in the collection. If you are creating a new collection (usually in the server), you must specify nsems;; If it refers to an existing semaphore set (usually in the client), specify nsems as 0.
Semctl function is used to operate on semaphores.
int semctl(int semid,int semnum,int cmd,union semun arg);
Different operations are realized through cmd parameters. Seven different operations are defined in the header file sem.h, which can be used as a reference in actual programming.
The semop function automatically executes the array of operations on the semaphore collection.
int semop(int semid,struct sembuf semoparray[],size _ t nops);
Semoparray is a pointer to an array of semaphore operations. Nops specifies the operands in the array.
Next, let's look at a concrete example. It creates a keyword with a specific IPC structure and a semaphore, establishes the index of this semaphore, modifies the value of the semaphore pointed by the index, and finally clears the semaphore. In the following code, the function ftok generates the unique IPC keyword we mentioned above.
# include & ltstdio.h & gt
# include & ltsys/types . h & gt;
# include & ltsys/SEM . h & gt;
# include & ltsys/IPC . h & gt;
void main() {
Key_t unique key; /* define an IPC keyword */
int id
struct sembuf lock _ it
union semun options
int I;
unique_key = ftok(",",' a '); /* Generate keywords, and the character "a" is a random seed */
/* Create a new semaphore collection */
id = semget(unique_key, 1,IPC _ CREAT | IPC _ EXCL | 0666);
Printf ("semaphore id=%d\n", id);
options . val = 1; /* Set variable value */
semctl(id,0,SETVAL,options); /* Set the semaphore of index 0 */
/* Print the value of the semaphore */
i = semctl(id,0,GETVAL,0);
Printf ("The value of the semaphore at index 0 is %d\n", i);
/* Reset the following traffic lights */
lock _ it . SEM _ num = 0; /* Set which semaphore */
lock _ it . SEM _ op =- 1; /* Define operation */
Lock _ it.sem _ flg = IPC _ NOWAIT/* operation mode */
if (semop(id,& amplock_it, 1) == - 1) {
Printf ("Unable to lock semaphore. \ n ");
Exit (1);
}
i = semctl(id,0,GETVAL,0);
Printf ("The value of the semaphore at index 0 is %d\n", i);
/* Clear semaphore */
semctl(id,0,IPC_RMID,0);
}
semget()
You can use the system call semget () to create a new semaphore set or access an existing semaphore set:
System call: semget ();
Prototype: int semget (key _ tkey, int nsems, int semflg);
Return value: If successful, return the IPC identifier of the semaphore set. If it fails, it returns-1: errno = access (no permission).
EEXIST (semaphore set already exists and cannot be created)
EIDRM (semaphore set has been deleted)
ENOENT (semaphore set does not exist and IPC_CREAT is not used)
ENOMEM (insufficient memory to create a new semaphore set)
ENOSPC (overrun)
The first parameter of the system call semget () is the keyword value (usually returned by the system call ftok ()). The system kernel compares this value with the keyword values of other semaphore sets existing in the system. Open and access operations are related to the contents in the parameter semflg. IPC_CREAT creates a semaphore set if it does not exist in the system kernel. When IPC_EXCL is used with IPC_CREAT, if the semaphore set already exists, the call will fail. If IPC_CREAT is used alone, semget () returns either the identifier of the newly created semaphore set or the identifier of the semaphore with the same key value that already exists in the system. If IPC_EXCL and IPC_CREAT are used together, the identifier of the newly created semaphore set or-1 is returned. IPC_EXCL alone is meaningless. The parameter nsems indicates the number of semaphores that should be created in the new semaphore set. The maximum number of semaphores in the semaphore set is defined in linux/sem.h:
# definesemmsl 32/* & lt; = 5 12 maxnumofsemaphoresperid */
The following is a program to open and create a semaphore set:
into pen _ semaphore _ set(key _ t keyval,int numsems)
{
intsid
If (! numsems)
return(- 1);
if((sid=semget(mykey,numsems,IPC_CREAT|0660))==- 1)
{
return(- 1);
}
Return (sid);
}
};
==============================================================
semop()
System call: semop ();
Call prototype: int semop (int semid, struct sembuf * SOPs, unsigned ednsops);
Return value: 0, if successful. -1, in case of failure: errno=E2BIG(nsops is greater than the maximum number of ops).
EACCESS (insufficient privileges)
EAGAIN (IPC_NOWAIT is used, but the operation cannot continue)
Default (the address pointed by SOPS is invalid)
EIDRM (semaphore set has been deleted)
EINTR (other signals received during sleep)
EINVAL (semaphore set does not exist or semid is invalid)
ENOMEM (SEM_UNDO is used, but there is not enough memory to create the required data structure)
ERANGE (signal amplitude out of range)
The first parameter is the keyword value. The second parameter is a pointer to the array to be manipulated. The third parameter is the operand in the array. The parameter sops points to an array composed of sembuf. This array is defined in linux/sem.h:
/*semop systemcall accepts these arrays */
structsembuf{
Semaphore index in ushortsem _ num/* array */
Shortsem _ op/* semaphore operation */
Shortsem _ flg/* operation flag */
Sem_num number of semaphores to be processed.
Operations performed by SEM _ op
Sem_flg operation flag.
If sem_op is negative, the semaphore will subtract its value. This is related to the resources controlled by semaphores. If IPC_NOWAIT is not used, the calling process will go to sleep until the semaphore-controlled resource can be used. If sem_op is a positive number, the semaphore increases its value. In other words, the process releases resources controlled by semaphores. Finally, if sem_op is 0, the calling process will call sleep () until the value of the semaphore is 0. This is used when a process is waiting for a completely free resource.
===============================================================
semctl()
System call: semctl ();
Prototype: int semctl (int semid, int semnum, int cmd, union semunarg);
Return value: If successful, it is a positive number.
If it fails, it is-1: errno = access (insufficient permissions).
Default (the address pointed by arg is invalid)
EIDRM (semaphore set has been deleted)
EINVAL (semaphore set does not exist or semid is invalid)
EPERM(EUID has no right to use cmd)
ERANGE (signal amplitude out of range)
The system calls semctl to control the semaphore set. This is very similar to the system call msgctl in the message queue. But the parameters of these two system calls are slightly different. Because semaphores are generally used as a group of semaphores, not a single semaphore. Therefore, in the operation of the semaphore set, we should not only know the IPC keyword value, but also know the specific semaphores in the semaphore set. Both system calls use the parameter cmd, which is used to indicate the specific command to operate. The last parameter in the two system calls is also different. In the system call msgctl, the last parameter is a pointer to the data structure used in the kernel. We use this data structure to get some information about the message queue and set or change the access rights and users of the queue. However, semaphores support additional optional commands, which require more complex data structures.
The first parameter of the system call semctl () is the keyword value. The second parameter is the number of semaphores.
Commands that can be used in the parameter cmd are as follows:
IPC_STAT reads the data structure semid_ds of the semaphore set and stores it in the buf parameter in semun.
IPC_SET sets ipc_perm in the data structure semid_ds of the semaphore set, and its value is taken from buf in semun.