多线程编程 sigaction ,mutex, pthread
来源:互联网 发布:淘宝怎么拆分订单 编辑:程序博客网 时间:2024/05/20 09:09
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);struct sigaction {
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
int sigpending(sigset_t *set);
int sigsuspend(const sigset_t *mask);
void (*sa_handler)(int);sa_restorer element is obsolete and should not be used. POSIX does not specify a sa_restorer element.
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
}
sa_handler specifies the action to be associated with signum and may be SIG_DFL for the default action, SIG_IGN to ignore this signal, or a pointer to a signal handling function. This function receives the signal number as its only argument.
sa_sigaction also specifies the action to be associated with signum. This function receives the signal number as its first argument, a pointer to a siginfo_t as its second argument and a pointer to a ucontext_t (cast to void *) as its third argument.
sa_mask gives a mask of signals which should be blocked during execu tion of the signal handler. In addition, the signal which triggered the handler will be blocked, unless the SA_NODEFER or SA_NOMASK flags are used.
sa_flags specifies a set of flags which modify the behaviour of the signal handling process. It is formed by the bitwise OR of zero or more of the following.
Signal Value Action Comment
-------------------------------------------------------------------------
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at tty
SIGTTIN 21,21,26 Stop tty input for background process
SIGTTOU 22,22,27 Stop tty output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
Next the signals not in the POSIX.1 standard but described in SUSv2
and SUSv3 / POSIX 1003.1-2001.
Signal Value Action Comment
-------------------------------------------------------------------------
SIGBUS 10,7,10 Core Bus error (bad memory access)
SIGPOLL Term Pollable event (Sys V). Synonym of SIGIO
SIGPROF 27,27,29 Term Profiling timer expired
SIGSYS 12,-,12 Core Bad argument to routine (SVID)
SIGTRAP 5 Core Trace/breakpoint trap
SIGURG 16,23,21 Ign Urgent condition on socket (4.2 BSD)
SIGVTALRM 26,26,28 Term Virtual alarm clock (4.2 BSD)
SIGXCPU 24,24,30 Core CPU time limit exceeded (4.2 BSD)
SIGXFSZ 25,25,31 Core File size limit exceeded (4.2 BSD)
2. Mutex
Mutex一般用于对于文件的读写,凡是要操作文件的动作,都必须首先定义在该文件上的mutex,互斥锁。
3. pthread_create#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
The pthread_create() function is used to create a new thread, with attributes specified by attr, within a process.If attr is NULL,the default attributes are used.If the attributes specified by attr are modified later, the thread's attributes are not affected.
Upon successful completion,pthread_create()stores the ID of the created thread in the location referenced by thread.
The thread is created executing start_routine with arg as its sole argument. If the start_routine returns, the effect is as if there was an implicit call to pthread_exit() using the return value of start_routine as the exit status.
Note that the thread in which main() was originally invoked differs from this. When it returns from main(), the effect is as if there was an implicit call to exit() using the return value of main() as the exit status.
The signal state of the new thread is initialised as follows:
- The signal mask is inherited from the creating thread.
- The set of signals pending for the new thread is empty.
If pthread_create() fails, no new thread is created and the contents of the location referenced by thread are undefined.
4. pthread_sigmask
int pthread_sigmask(int how, const sigset_t *restrict set,sigset_t *restrict oset);
int sigprocmask(int how, const sigset_t *restrict set,sigset_t *restrict oset);
The pthread_sigmask() function shall examine or change (or both) the calling thread's signal mask, regardless of the number of threads in the process. The function shall be equivalent to sigprocmask(), without the restriction that the call be made in a single-threaded process.
In a single-threaded process, the sigprocmask() function shall examine or change (or both) the signal mask of the calling thread.
If the argument set is not a null pointer, it points to a set of signals to be used to change the currently blocked set.
The argument how indicates the way in which the set is changed, and the application shall ensure it consists of one of the following values:
- SIG_BLOCK
- The resulting set shall be the union of the current set and the signal set pointed to by set.
- SIG_SETMASK
- The resulting set shall be the signal set pointed to by set.
- SIG_UNBLOCK
- The resulting set shall be the intersection of the current set and the complement of the signal set pointed to by set.
If the argument oset is not a null pointer, the previous mask shall be stored in the location pointed to by oset. If set is a null pointer, the value of the argument how is not significant and the thread's signal mask shall be unchanged; thus the call can be used to enquire about currently blocked signals.
If there are any pending unblocked signals after the call to sigprocmask(), at least one of those signals shall be delivered before the call to sigprocmask() returns.
It is not possible to block those signals which cannot be ignored. This shall be enforced by the system without causing an error to be indicated.
If any of the SIGFPE, SIGILL, SIGSEGV, or SIGBUS signals are generated while they are blocked, the result is undefined, unless the signal was generated by the kill() function, the sigqueue() function, or the raise() function.
5. sigqueueint sigqueue(pid_t pid, int sig, const union sigval value)
The
The conditions under which a process has permission to queue a signal to another process with
The
pid
- Is a process ID or process group ID.
- sig
- Specifies the signal to be sent.
- value
- Is a user-specified value to be passed along with the signal to the receiving process. The receiving process can find the value by examining the si_value field of the siginfo_t structure.
- On success,
sigqueue() returns a value of zero. On failure, it returns a value of -1, does not send a signal, and sets errno to one of the following values:
- EAGAIN
- There are not enough resources available to queue the signal. The process has already queued SIGQUEUE_MAX signals that are still pending at the receivers, or a system-wide resource limit has been exceeded.
- EINVAL
- The value of sig is an invalid or unsupported signal number.
- EPERM
- The user ID of the sending process is not privileged; its real or effective user ID does not match the real or saved user ID of the receiving process. Or, the process does not have permission to send the signal to any receiving process
- ESRCH
- No process or process group can be found that corresponds to the one that pid specifies.
- If sigprocmask() fails, the thread's signal mask shall not be changed.
- The use of the sigprocmask() function is unspecified in a multi-threaded process.
- 多线程编程 sigaction ,mutex, pthread
- 【PTHREAD】linux 多线程编程---Mutex实现Service线程和work线程
- pthread/Linux多线程编程
- pthread多线程编程
- pthread多线程编程整理
- pthread多线程编程
- pthread多线程编程
- pthread/Linux多线程编程
- pthread多线程编程
- pthread/Linux多线程编程
- pthread多线程编程整理
- pthread/Linux多线程编程
- pthread 多线程编程
- pthread多线程编程整理
- pthread多线程编程整理
- pthread多线程编程整理
- pthread多线程编程+锁
- pthread多线程编程-1
- DOS系统功能调用与BIOS中断调用
- 关于编程的一些概念
- iis5.1的配置问题
- 多线程编程 execvp, getpid, pthread, fork(),setsid
- PipeAdvertisement的使用
- 多线程编程 sigaction ,mutex, pthread
- 对等点如何彼此定位
- Blog One
- 不同局域网内经Internet的P2P通信技术总结
- 写完感言
- JXTA初探
- 咨询管理公司
- MiluGPS(迷路者GPS导航软件)
- 我的奥运,我的2008