Inter-Process Communication Linux # # # semaphores (semophore)

Is essentially a semaphore counter ( not provided is because the global variable are independent of each inter-process, which may not be able to see, there is no guarantee see reference count ++ atomic operations ), it may be used to control a plurality of processes access to shared resources. It is often used as a locking mechanism to prevent access to shared resources is a process, other processes can also access the resource. As well as the main inter-process synchronization and mutual exclusion means between the different threads within the same process. It piping is different, it is not the primary purpose to transmit data, it is mainly used to protect shared resources (semaphores also belong to critical resources), so that resources in only one process at a time exclusive.

Semaphore (Semaphore), sometimes referred to as lights, is a facility for use in a multithreaded environment, it can be used to ensure that two or more critical sections are not concurrent calls. Before entering a critical section, the thread must acquire a semaphore; once the critical section is complete, then the thread must release the semaphore. Other threads that want to enter the critical section must wait until the first thread releases the semaphore. To complete the process, create a semaphore Vl, and then Acquire Semaphore VI Release Semaphore VI were placed in each end of the first critical code section. Semaphore VI confirm these references is the amount of the initial signal is created.

Use semaphores are mainly used to protect shared resources, so that resources at a time only one process (thread) owned. To prevent a series of problems due to multiple programs simultaneously access a shared resource triggered, we need a method that can be authorized by generating and using a token, the critical region at any one time there can be only one thread of execution access code . It refers to execute the critical region of code data updates need to be performed exclusive. The semaphore can provide an access mechanism for the same time a critical section only one thread accessing it, that semaphores are used to co-ordination process access to shared resources.

The easiest is to only take the amount of signal variables 0 and 1, which is the most common form of semaphore, called a binary semaphore. And the amount of the signal may take a plurality of positive integers is called a common semaphore. Here we focus on a binary semaphore.

Linux provides two semaphores

  • (1) the amount of the core signal used by the kernel control path 
  • (2) an amount of a signal used by user mode process, the amount of such a signal is divided into SYSTEM V semaphores and POSIX semaphores.

 

Kernel Semaphore

Kernel semaphore spinlock is similar, because when the lock is closed, it does not allow the kernel control path continues. However, when trying to obtain core kernel control path semaphore lock busy protection resources, corresponding process was suspended. Only when resources are released, the process was again becomes operational. Sleep can only function in order to obtain kernel semaphore; interrupt handlers and deferrable functions can not be used kernel semaphore.

Kernel semaphore type struct semaphore object, which is defined in <asm / semaphore.h> of:

/* Please don't access any members of this structure directly */
struct semaphore {
	raw_spinlock_t		lock;
	unsigned int		count;
	struct list_head	wait_list;
};

When a task is not due to some condition is not satisfied, it is linked to sleep waiting queue. When the condition is met, the task is moved out of the waiting queue, at this time does not mean that the task will be executed immediately, as it is moved into the work queue waiting for CPU resources, it is scheduled at the appropriate time. Kernel semaphore is waiting in the queue for internal use, which means that the waiting queue is hidden from the user, without user intervention.

void down(struct semaphore *sem)
{
    unsigned long flags;

    raw_spin_lock_irqsave(&sem->lock, flags);
    if (likely(sem->count > 0))
        sem->count--;
    else
        __down(sem);//挂到等待队列中睡眠
    raw_spin_unlock_irqrestore(&sem->lock, flags);
}

 

 

User mode processes semaphore

 

POSIX semaphores

Posix is "Portable Operating System Interface shorthand initials (Portable Operating System Interface), but it is not a single standard, but an Institute of Electrical and Electronic Engineering, namely the development of a series of IEEE standards, it is by the ISO (International .Posix international standard ISO standards) and IEC (international Electrotechnical Commission) adopted are becoming increasingly popular, many manufacturers began to use this standard. gradually there is a trend to replace SYSTEM V semaphores.

Based on the amount of memory Posix signal, i.e. the signal value is placed in shared memory, it is made possible corresponding to the path name of the file system to identify the name.

Famous semaphore:   stored in the shared memory area corresponding to the pathname of the file system to identify the name, generally used for process synchronization. Can also be used threads, not even related processes. Similarly named pipe. Since the value of well-known semaphore is stored in a file, so for the relevant process, the child process is inherited from the parent process file descriptor, then the child process inherits file descriptors pointed file is the same as the parent process of course, the famous semaphore value stored on shared file inside.

Unnamed semaphore:   storing in the shared memory area, generally used for thread synchronization. But also for synchronization between related processes. That is, the unnamed semaphore must be multiple processes (threads) shared variables, variables unnamed semaphore to be protected must also be multiple processes (threads) shared variables, these two conditions are indispensable. Similar unnamed pipes.

 

SYSTEM V semaphore

System v is a branch of the many versions of the Unix operating system, which was originally developed by AT & T was first released in 1983, a total of four versions of System v, and the most successful is System V Release 4, otherwise known as SVR4. It would appear that a standard Unix is one of (Another criterion is Open Group), is a branch of one of the many versions of Unix (Linux with other branches as well as BSD). PV operation is Dijkstra with Dutch defined as in Dutch by called passeren, called release vrijgeven, PV operation hence the name. This is one of the few examples of English is not expressed in computer terms.

System v semaphore kernel-based testing, it is placed inside the core.

System v is the set of signal values, rather than a single semaphore. Associated semaphore operation by the function <sys/ipc.h>reference. semaphore system V kernel maintenance, including the amount of the binary signal, counting semaphores, counting semaphores set.

  • 1. The amount of the binary signal (binary semaphore): or semaphore value 0 or 1. This is similar to the mutex, if the resources are locked, the signal value is 0, then the signal if resources are available in amounts of 1.
  • 2. counting semaphores (counting semaphore): a value of the signal amount between 0 and a limit value (for Posix semaphore, this value must be at least 32,767). The semaphore value is the number of resources available.
  • The above two types of signals, waiting (the wait) operation are waiting for the semaphore's value becomes greater than 0, then decrement it. Hang out value (POST) sucked semaphore operation by one. So as to awaken is waiting for the signal value becomes greater than zero for any thread.
  • 3. counting semaphore set (set of counting semaphore): the amount of one or more signals (constituting a set), each of which is counting semaphores. The number of each set of signals are present a limitation in the general order of magnitude of 25.

 

Semaphore (semophore) Standard and System V sub POSIX standard, detailed reference

Room # Linux # # System V IPC Standard & POSIX standard

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103731012