Interprocess_Communications_In_Linux
时间:2007-02-14 来源:ffjnfj
file主要是使用magic文件来判断文件类型。
Each line of the file specifies a test to be performed. A test compares
the data starting at a particular offset in the file with a 1-byte,
2-byte, or 4-byte numeric value or a string. If the test succeeds, a message
is printed.
使用ulimit可以看到进程的资源限制,比如最多有几个打开的文件描述符
ulimit -Ha
SIGSTOP/SIGKILL是不能ignore/catch的
if the process is a session leader and its controlling terminal is the
controlling terminal of the session, then each process in the foreground
process group of this controlling terminal is sent a SIGHUP signal, and the
termi‐ nal is disassociated from this session, allowing it to be acquired by a
new controlling process.
If the exit of the process causes a process group to become orphaned, and if
any member of the newly orphaned pro‐ cess group is stopped, then a SIGHUP
signal followed by a SIGCONT signal will be sent to each process in this pro‐
cess group.
The mutex functions are not async-signal safe
None of the mutex functions is a cancellation point
条件变量的作用
如果用mutex代替条件变量,比如当条件满足时,unlock这个mutex。这个判断这个条件
的任务就在unlock的thread上,如果有n个条件在等,那该怎么办?
使用条件变量,则判断条件发生在wait的任务上,有n个线程在等m个条件的话,就让
他们自己去等,去判断,可能改变这些条件的任务只要signal他们就行了
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t v = PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&m);
while (!test_condition()) /* get resource */
pthread_cond_wait(&v, &m);
/* do critical section, possibly changing test_condition() */
pthread_cond_signal(&v); /* inform another thread */
pthread_mutex_unlock(&m);
要注意每个thread都有自己的errno,所以如果线程想要得到别人的errno,就只有通过
传递参数了
when a signal is caught, the signal that caused the event is automatically
blocked on entry to the signal handler. With a multithreaded application,
nothing prevents another signal of the same type from being delivered to
another thread that has the signal unblocked. It is possible to have multiple
threads executing within the same signal handler
一般的signal都是process-wide,除了那种只跟特定thread相关的,比如SIGFPE。
用pthread_kill可以只发送signal到特定的thread,不过如果这样会导致该thread死了
的话,那么整个进程也会死,这是process的语义。
一般的multi-thread编程,会有一个专门的thread来处理signal,使用sigwait,这个
函数的好处是可以使用async-signal-safe的函数,因为调用的地方不是在signal中
One difference between a semaphore and a mutex is that a thread acquiring a
semaphore with sem_wait or sem_trywait need not be the same thread that
increments the value with sem_post. A mutex must be locked and unlocked by the
same thread.
这才是mutex的本意
Another important feature of semaphores is that a thread can call the sem_post
function at any time, regardless of whether there are other threads waiting in
a sem_wait call. The fact that the semaphore’s value was incremented is
retained, even if there are no waiting threads.
Each line of the file specifies a test to be performed. A test compares
the data starting at a particular offset in the file with a 1-byte,
2-byte, or 4-byte numeric value or a string. If the test succeeds, a message
is printed.
使用ulimit可以看到进程的资源限制,比如最多有几个打开的文件描述符
ulimit -Ha
SIGSTOP/SIGKILL是不能ignore/catch的
if the process is a session leader and its controlling terminal is the
controlling terminal of the session, then each process in the foreground
process group of this controlling terminal is sent a SIGHUP signal, and the
termi‐ nal is disassociated from this session, allowing it to be acquired by a
new controlling process.
If the exit of the process causes a process group to become orphaned, and if
any member of the newly orphaned pro‐ cess group is stopped, then a SIGHUP
signal followed by a SIGCONT signal will be sent to each process in this pro‐
cess group.
The mutex functions are not async-signal safe
None of the mutex functions is a cancellation point
条件变量的作用
如果用mutex代替条件变量,比如当条件满足时,unlock这个mutex。这个判断这个条件
的任务就在unlock的thread上,如果有n个条件在等,那该怎么办?
使用条件变量,则判断条件发生在wait的任务上,有n个线程在等m个条件的话,就让
他们自己去等,去判断,可能改变这些条件的任务只要signal他们就行了
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t v = PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&m);
while (!test_condition()) /* get resource */
pthread_cond_wait(&v, &m);
/* do critical section, possibly changing test_condition() */
pthread_cond_signal(&v); /* inform another thread */
pthread_mutex_unlock(&m);
要注意每个thread都有自己的errno,所以如果线程想要得到别人的errno,就只有通过
传递参数了
when a signal is caught, the signal that caused the event is automatically
blocked on entry to the signal handler. With a multithreaded application,
nothing prevents another signal of the same type from being delivered to
another thread that has the signal unblocked. It is possible to have multiple
threads executing within the same signal handler
一般的signal都是process-wide,除了那种只跟特定thread相关的,比如SIGFPE。
用pthread_kill可以只发送signal到特定的thread,不过如果这样会导致该thread死了
的话,那么整个进程也会死,这是process的语义。
一般的multi-thread编程,会有一个专门的thread来处理signal,使用sigwait,这个
函数的好处是可以使用async-signal-safe的函数,因为调用的地方不是在signal中
One difference between a semaphore and a mutex is that a thread acquiring a
semaphore with sem_wait or sem_trywait need not be the same thread that
increments the value with sem_post. A mutex must be locked and unlocked by the
same thread.
这才是mutex的本意
Another important feature of semaphores is that a thread can call the sem_post
function at any time, regardless of whether there are other threads waiting in
a sem_wait call. The fact that the semaphore’s value was incremented is
retained, even if there are no waiting threads.
相关阅读 更多 +