How to show process state (blocking, non-blocking) in Linux

2024/9/21 10:48:11

Is there a way to query the state of processes in a Linux process table to be able to demonstrate if a process is running or blocked at the time the query is executed? My goal is to do this from 'outside' the process or program, as I hope to understand this in terms of the OS process, but any thought are welcome!

Here is the python code blocked process:

import subprocess
proc = subprocess.call('ls -lRa /', shell=True)

Here is the python code for a non-blocked process:

import subprocess
proc = subprocess.Popen('ls -lRa /', shell=True)

Here is the output of the 'ps -ef' showing the process ids:

UID        PID  PPID  C STIME TTY          TIME CMD
user1    14308  4145  0 15:30 pts/2    00:00:00 python call_b.py
user1    14309 14308  0 15:30 pts/2    00:00:00 /bin/sh -c ls -lRa /
user1    14310 14309 15 15:30 pts/2    00:00:30 ls -lRa /
root     14313     2  0 15:31 ?        00:00:00 [kworker/2:0]
user1    14318  2476  0 15:32 pts/4    00:00:00 -bash
user1    14442     1  0 15:33 pts/4    00:00:00 /bin/sh -c ls -lRa /
user1    14443 14442  6 15:33 pts/4    00:00:01 ls -lRa /

While these 'ls' commands are processing, I'd like to show which processes are blocking and which states the others are in. The question is intended to be a tool going forward, for learning about states as I learn multiprocessing with python, so while I believe PID 14309 to be blocking and PID 14442 to be non-blocking, although I may have that wrong. That is why it would be helpful for me to be able to see or test this for all of the PIDs shown.

Thanks to the venerable user 'ubuntu' and their response to this: Blocking and Non Blocking subprocess calls for providing the starter code.

The OS in this case is Ubuntu, but any debian or OS comment would be helpful.

Answer

Try ps -weo pid,stat,wchan:32,args. You'll get output like:

28325 S<l  poll_schedule_timeout            /usr/bin/pulseaudio --start --log-target=syslog
28328 Sl   poll_schedule_timeout            /usr/bin/krunner
28344 Sl   poll_schedule_timeout            /usr/bin/kmix -session 014f10adfdf000141320876500000291010026_1419380700_54458

Thats the pid, state flags (see below), where the process is currently blocked and the command line. Flags are:

       D    uninterruptible sleep (usually IO)R    running or runnable (on run queue)S    interruptible sleep (waiting for an event to complete)T    stopped, either by a job control signal or because it is being tracedW    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<    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 leaderl    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)+    is in the foreground process group
https://en.xdnf.cn/q/72067.html

Related Q&A

Removing quotation marks from list items

I am running this program:f = open( "animals.txt", "r") g = f.read() g1 = g.split(,) #turning the file into list print g1And I want this to come out:[ELDEN, DORSEY, DARELL, BRODERIC…

Handle multiple questions for Telegram bot in python

Im programming a telegram bot in Python using the Telegram bot API. Im facing the problem of managing questions that need an answer of the user. The problem arises when the program is waiting for an an…

Which GTK+ elements support which CSS properties?

While applying my own CSS to my GTK+ application, I noticed, that some elements ignore some CSS properties and others ignore others or dont ignore them, which leads me to search for an overview of whic…

Self import of subpackages or not?

Suppose you have the following b b/__init__.py b/c b/c/__init__.py b/c/d b/c/d/__init__.pyIn some python packages, if you import b, you only get the symbols defined in b. To access b.c, you have to exp…

why is my text not aligning properly in wxPython?

Im using wxPython to build a GUI and Im trying to align some text but its not working at all. Im trying align three different static text items in three places (right aligned, center aligned, and left …

Python subprocess check_output decoding specials characters

Im having some issues with python encoding. When I try to execute this:subprocess.check_output("ipconfig", shell=True)it gives me an output with special characters in it, like:"Statut du…

Python - SystemError: NULL result without error in PyObject call

The story: Im trying to interface from C to Python in order to use the faster computational speed of C for an existing Python code. I already had some success, also with passing NumPy arrays - but now …

Fix jumping of multiple progress bars (tqdm) in python multiprocessing

I want to parallelize a task (progresser()) for a range of input parameters (L). The progress of each task should be monitored by an individual progress bar in the terminal. Im using the tqdm package f…

How to access data stored in QModelIndex

The code below create a single QListView with the data and proxy models "attached". Clicking one of the radio buttons calls for buttonClicked() function. This function calls models .data(inde…

Pythons read and write add \x00 to the file

I have come across a weird problem when working with files in python. Lets say I have a text file and a simple piece of code that reads the contents of the file and then rewrites it with unaltered cont…