pthread_mutex() が実行されたときのプロセス状態は何ですか?

pthread_mutex() が実行されたときのプロセス状態は何ですか?

以下はLinuxプロセスの状態です。

R: running or runnable, it is just waiting for the CPU to process it
S: Interruptible sleep, waiting for an event to complete, such as input from the terminal
D: Uninterruptible sleep, processes that cannot be killed or interrupted with a signal, usually to make them go away you have to reboot or fix the issue
Z: Zombie, we discussed in a previous lesson that zombies are terminated processes that are waiting to have their statuses collected
T: Stopped, a process that has been suspended/stopped

2つの質問があります。

(1)プロセス/スレッドでmutex_lock()が呼び出されたときにロックが取得されるのを待っている場合(他のスレッドがすでにミューテックスロックをロックしている場合)、プロセスはまたはにS移動しますかD

(2)私はspinlock()がプロセスを忙しい待機状態にし、他のスレッドによってロックが解除されたことを確認することを知っています。しかし、mutex_lock()ではロックが解除され、ロックして続行できることをどのように知ることができますか? IE;sleepミューテックスが利用可能な場合(他のスレッドによってロック解除された場合)、ロックされたミューテックスでプロセスがどのように起きるのですか?

ベストアンサー1

さて、調べてみましょう:

#include <pthread.h>                                                            
#include <sys/types.h>                                                          
#include <unistd.h>                                                             
#include <stdio.h>                                                              
int main ()                                                                     
{                                                                               
    pthread_mutex_t    mtx;                                                     
    pid_t              pid;                                                     
    pthread_mutex_init (&mtx, NULL);                                            
                                                                                   
    pid = getpid();                                                                
    printf ("pid : %d\n", pid);                                                    
                                                                                   
    pthread_mutex_lock (&mtx);                                                     
    // Double lock. This results in a dead-lock
    pthread_mutex_lock (&mtx);                                                     
    pthread_mutex_destroy (&mtx);                                                  
}

編む:

gcc -lpthread -o prog prog.c

次に、次を実行します。

./prog

pid : 23537

(これは私の具体的なケースのpidです。実行すると他の結果が発生します)

次に、このプロセスの状態を見てみましょう。

ps aux | grep 23537

user      23537  0.0  0.0   6448   840 pts/4    S+   16:29   0:00 ./prog

これS+プロセスの状態です。

マニュアルページによると

PROCESS STATE CODES
   Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a
   process:

           D    uninterruptible sleep (usually IO)
           R    running or runnable (on run queue)
           S    interruptible sleep (waiting for an event to complete)
           T    stopped by job control signal
           t    stopped by debugger during the tracing
           W    paging (not valid since the 2.6.xx kernel)
           X    dead (should never be seen)
           Z    defunct ("zombie") process, terminated but not reaped by its parent

   For BSD formats and when the stat keyword is used, additional characters may be displayed:

           <    high-priority (not nice to other users)
           N    low-priority (nice to other users)
           L    has pages locked into memory (for real-time and custom IO)
           s    is a session leader
           l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
           +    is in the foreground process group

わかりました。フォアグラウンドプロセスグループで中断可能なスリープモード(イベントが完了するまで待機)

2番目の質問について:スレッドがロックされたミューテックスを取得しようとすると、カーネルはCPUからスレッドを削除してキューに入れます。所有者がロックを解除すると、カーネルはキューにスレッドがあることを確認し、そのスレッドを起動します(おそらく)。

これはスピンロックとは異なります。スピンロックは次のとおりです。

int lock = 1;
// 1 means locked, 0 means unlocked
while (lock) {};
// If we are here, it means the lock somehow changed to 0
lock = 1;
// We set it to one again to avoid another thread taking it

しかし、この実装は原子的ではありません。カーネルはそれを原子的に実行するためのいくつかの方法を提供します。

おすすめ記事