Operating System-process/thread internal communication-semaphore and PV operations, system-pv
This article introduces two core concepts of operating system process management:
Semaphores
PV operation
I. Introduction to semaphores 1.1 Introduction to semaphores
Semaphore was introduced by Dijkstra in 1965. Generally, a sema
2015.3.4Wednesday Cloudyinterprocess communication: IPCFile object: Record file descriptor, file switch, etc.IPC Identifier: System global serial numberTwo processes to communicate, open is the only object to communicate, through the key operationXSI IPC: Message Queuing, semaphore, shared memory.IPCS view IP object Shared memory, semaphore, message queue, etc.IP
to select a type of executionThere are 0 blocking waits, ipc_nowait (non-blocking wait), Sem_undo (as its name does nothing, all PV operations performed in this process are reverted to the value of the semaphore at the end of the process to avoid errors in this process)The semaphore implements the code for a mutex lockThe contrast shows that there is no interleaving of the AB because of mutually exclusive
Message Queue, semaphore, and shared memory for IPC communication, ipc queue
Three IPC types are called xsi ipc, namely message queue, semaphore, and shared memory. Xsi ipc comes from the IPC function of System V. Xsi ipc is often criticized for not using File System namespaces But constructing their own namespaces.
Similarities: the IPC structure in each kernel
Process-related functionspid_t Fork ();header file :unistd.h,sys/types.hrole : Establish a new process (child process) that shares the code snippet with the original process (the parent process) and has a copy of the parent process's other resources (data, stacks, and so on). (Because all States of the parent process are inherited, the child process runs down from the Fork ). (Note: The child process inherits all the data from the parent process, but when executed, the data is separate and no lo
++)printf ("i=%d", I);}The default action of the SIGALRM is to end the process, so the program ends in 1 seconds, so that you can see how much the system is counting within 1s, so that you feel the performance of the system.PrecautionsThe process of 0.signal () series function processing is related to the specific system, and different systems have different processing modes.1. In the multi-threaded environment, its behavior is uncertain, not recommended;2. The underlying implementation is based
words, the signal processing function can be interrupted during its execution and called again. It is critical that it continue to operate correctly when it returns to the first call. This is not just a question of recursion, but a reentrant (that is, it can be completely entered and executed again). In the case of Linux, the kernel will need to re-enter the interrupt service routines that handle multiple devices at the same time, because higher-priority interrupts may "insert" during the execu
I. pipe)
Compile the running result:
Compile the running result:
(mkfifo(FIFO, ) == -((fifofd = open(FIFO, OPEN_MODE))
After the compilation is successful, we first run the server (create a FIFO, wait for the customer to send a message, and the FIFO server is blocked ):
After compilation, the running result is as follows:
Linux/Sem. h> Linux/Ipc. h>
(semop(semid, sops, )
(
The compilation result is as follows (because we set the signal value to 3, the fourth resource request failed
Java Semaphore semaphore, java signal SemaphoreSemaphore
Semaphore is divided into two types: single value and multi-value. The former can only be obtained by one thread, and the latter can be obtained by several threads.
The functions implemented by Semaphore are similar to five pitfall in the restroom. If 10 people n
Multi-process + Semaphore semaphore, process signal Semaphore
The following is an example.
from multiprocessing import Processfrom multiprocessing import Semaphoreimport datetimeimport timeimport multiprocessingdef worker(s, i): s.acquire() print(multiprocessing.current_process().name + " acquire",datetime.datetime.now()) time.sleep(i) print(multiproc
interface
The function interface of the POSIX semaphore is shown in the following illustration:
2.1 The creation and deletion of famous semaphores
#include
Sem_open is used to create or open a semaphore that is identified by the name parameter, the semaphore. The name of the POSX IPC can be referenced in UNIX Network programming Volume 2: interprocess
Semaphore is a count semaphore. Conceptually, semaphores maintain a set of licenses. If necessary, each one is blocked before the license is available acquire() , and then the license is acquired. Each release() add a license, which may release a blocked fetch. However, instead of using the actual license object, Semaphore only the number of available licenses is
Semaphore is a count semaphore. Conceptually, semaphores maintain a set of licenses. If necessary, each one is blocked before the license is available acquire() , and then the license is acquired. Each release() add a license, which may release a blocked fetch. However, instead of using the actual license object, Semaphore only the number of available licenses is
In the following article we will look at what is Semaphore (semaphore) in Python. Learn about Python semaphores and how Semaphore (semaphores) can be used in Python programming.
Semaphore (semaphore)
mutexes allow only one thread to change data at the same time, while
In many cases, there may be multiple threads that require access to a small number of resources. It is assumed that several threads are running on the server that answer client requests. These threads need to connect to the same database, but at any one timeOnly a certain number of database connections can be obtained. How will you be able to effectively allocate these fixed number of database connections to a large number of threads?A: 1. Add a synchronous lock to the method to ensure that only
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.