Analysis and program design based on the object-oriented operating system development platform (OSKit) (3)
Source: Internet
Author: User
Article title: analysis and program design based on the object-oriented operating system development platform (OSKit) (3 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: Tang Haijing
Thread communication in thread analysis
This article is the third part of "analysis and program design based on OSKIT". The author will analyze thread communication.
As we all know, in Linux, processes must communicate with each other in order to coordinate work on the same task. For example, in a shell pipe, the output of the first process must be transmitted to the second process. Therefore, a well-structured communication mode should be used between processes that need to communicate.
Linux supports many different forms of inter-process communication mechanisms. under specific circumstances, they have their own advantages and disadvantages. for example, the biggest disadvantage of using pipelines for communication is that, only the child processes that become the descendants of the processes that the system calls pipe can use pipelines. Therefore, unrelated processes cannot communicate through pipelines.
OSKit developers fully consider the advantages and disadvantages of various communication methods and use the following two methods of inter-thread communication: message queue and Semaphore. Although there are still many shortcomings, it is improved compared with System V.
Inter-thread communication
3.1.1 Message Queue
The message queue mechanism provided by OSKit is very similar to UNIX. one or more threads can write messages to the message queue, and one or more threads can read messages from the message queue, this mechanism of inter-thread communication is like a client/server. a client sends a request message to the server, and the server reads the message and executes the corresponding request.
OSKit describes a message as an internal linked list in the kernel address space. each message queue is uniquely identified by an ipc id.
3.1.2 signal
Although most threads communicate with each other well planned, they also need to handle unpredictable communication problems. For example, a user needs to list all the content of a large file using a text editor, but then he realizes that this operation is not important. This requires a way to stop the editor. for example, we can press the DEL key. Pressing the DEL key actually sends a signal to the editor. when the editor receives this signal, it immediately stops printing the file content. Signals can also be used to report specific interruptions captured by hardware, such as illegal commands and floating point operation overflow and timeout.
In OSKit, the signal is an important inter-thread communication mechanism. the thread knows what is happening in the system through the signal. One characteristic of the signal is its asynchronization, which indicates that the thread can receive signals at any time during its execution, or even receive signals when the thread is executing a system call, therefore, the thread must prepare the corresponding signal at any time.
Next, let's talk about signal generation and delivery. when the first occurrence of a signal event occurs, the signal is considered to be generated by the thread, that is, the signal is sent to the thread. Examples of such events include: time slice, terminal activation, hardware fault detection, and kill () calls. In some cases, an event can generate signals for multiple threads. Each thread uses the corresponding action to respond to each signal defined by the system (see signal Table 3.1 ).
The signal is suspended between signal generation and delivery, but the interval cannot be detected at the user level. Each thread has a signal mask (sig_mask) that specifies the signal set that is currently blocked and cannot be delivered to the thread. The signal mask of a thread is inherited from the parent thread. The sigaction (), sigprocmask (), and sigsuspend () functions are used to operate the signal mask.
There are three types of signal-related actions: SIG_DFL, SIG_IGN, or pointer to a function. Before entering main (), all signals will be set to SIG_DFL or SIG_IGN.
Default action for SIG_DFL signal:
If the default action is to suspend a thread, the thread's execution is temporarily suspended. When the thread is paused, no additional signals sent to the thread are delivered until the thread starts executing, except for SIGKILL.
Set the signal action of the suspended signal to SIG_DFL, and the default action is to ignore the signal (SIGCHLD ).
SIG_IGN ignores signals
The delivery of this signal has no effect on the thread
The system does not allow the operation of the SIGKILL or SIGTOP signal to be set to SIG_DFL.
Pointer to function-capture signal
Once the signal is delivered, the receiving thread executes the signal capture program on the specified address. After the signal capture function returns, the receiving thread must resume execution at the Being breakpoint.
Use the C language function call method to access the signal capturing program:
Void func (signo)
Int signo;
Func () is the specified signal capturing function, and signo is the code of the signal being delivered
If the SIGFPE, SIGILL, or SIGSEGV signal is not generated by the kill () or raise () function defined by the C standard, then from the signal SIGFPE, SIGILL, after the signal capture function of SIGSEGV returns normally, the thread behavior is undefined.
The system does not allow threads to capture SIGKILL and SIGSTOP signals.
If the thread establishes a signal capture function for the SIGCHLD signal and the thread has a subthread that is not waiting for termination, there is no rule on whether to generate a SIGCHLD signal to specify the subthread.
Each signal is given a symbolic name by OSKit. for a 32-bit i386 platform, there are 32 types of signals with 32 characters. The following table provides common symbol names, descriptions, and their signal values.
Whether the description of the symbol name signal value complies with POSIX
SIGHUP 1 detected hanging up on the control terminal or the control thread died
SIGINT 2 interaction notice signal is
SIGQUIT 3 interaction stop signal is
Which of the following commands does SIGILL 4 detect illegal hardware?
SIGTRAP 5 does not return from the trap
SIGABRT 6 abnormal termination signal is
SIGEMT 7 Command no
The incorrect arithmetic operation signal of SIGFPE 8 is
SIGKILL 9 termination signal is
SIGBUS 10 bus error No
SIGSEGV 11 detected that invalid memory call is
SIGSYS 12 system call error parameter no
SIGPIPE 13 is written in a reader-less pipeline
When SIGALRM 14 is reported, the signal is
SIGTERM 15 termination signal is
SIGURG 16 I/O channel emergency signal no
SIGSTOP 17 pause signal is
SIGTSTP 18 interaction pause signal is
SIGCONT 19 If paused, continue to be
SIGCHLD 20 sub-thread termination or suspension is
A member of the background thread group of SIGTTIN 21 tries to read
SIGTTOU 22 background thread group members try to write to the control terminal is
SIGIO 23 allows I/O signal no
SIGXCPU 24 exceeds CPU limit no
SIGXFSZ 25 exceeds the file size limit no
SIGVTALRM 26 Virtual time alarm no
SIGPROF 27 side Time alert no
SIGWINCH 28 window size change no
SIGINFO 29 message request no
SIGUSR1 30 reserved as user-defined Signal 1 is
SIGUSR2 31 is reserved as a user-defined signal
Request signal processing according to default rules
SIG_DFL (void (*) (int) 0
Request ignore signal
SIG_IGN (void (*) (int) 1
Note: a maximum of 64 signals are allowed in the signal queue.
3.2 pthreads/pthread_ipc.c
This source code file includes a complete message queue-type thread communication mechanism. message queue is the main means of thread communication. by reading the following function analysis, it will help readers understand what functions OSKit uses to implement signal queues for thread communication.
3.2.1 message sending:
Note: when a thread sends a message to another thread, OSKit uses the function defined below to transmit a message to another thread.
Dst: Destination address
* Msg: pointer to the message
Msg_size: message size (32-bit unsigned integer)
Timeout: timeout
3.2.2 send algorithm
Send a synchronous message until the receiver terminates the message or times out. To avoid deadlocks, add a lock to no one and close the locks.
Check whether the target thread is waiting for acceptance through conditions:
A: The target thread's wait sign & THREAD_WS_IPCRECV_WAIT (the thread receives the wait bit)
B: The target thread number exists.
C: The target thread is equal to any thread.
D: The message size is not 0.
Condition: a & (B | c) & d = 1 before sending
If (target-> waitflags & THREAD_WS_IPCRECV_WAIT
& (Target-> ipc_state.tid
= Pthread-> tid | target-> ipc_state.tid = ANYTID ))
{If (msg_size)
{Memcpy (target-> ipc_state.msg,
Msg, MIN (msg_size, target-> ipc_state.msg_size ));
If (msg_size> target-> ipc_state.msg_size)
Err = OSKIT_ERANGE;
}
}
Tell the receiving thread the data size and sender ID:
Target-> waitflags & = ~ THREAD_WS_IPCRECV_WAIT;
Pthread_unlock (& target-> waitlock );
Note: flagA & = ~ In fact, flagB clears the flagB space equal to 1 in flagB.
Sending queue:
Queue_enter (& target-> ipc_state.senders, pthread, pthread_thread_t *,
Ipc_state.senders_chain)
& Target-> ipc_state.senders: ID of the sending thread pthread:
Pthread_thread_t *: the pointer to the message is of the pthread_thread_t type.
Ipc_state.senders_chain: Pointer of the sending queue
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service