通过pidin观察进程信息

通过pidin观察进程信息

pidin在qnx里是一个非常有用的命令,该命令提供了多个选项来获取关于进程的详细信息,包括进程ID(PID)、进程状态、CPU使用情况、内存使用情况、线程信息等。

pidin通过threads参数可以观察进程ID,线程ID,线程名字,线程状态等。

Show the process ID, thread ID (in QNX Neutrino 7.0 or later), short process name, thread name, thread state, and what the thread is blocked on. If a thread doesn’t have a name, pidin displays the thread ID (tid) again.

线程信息总览

把以下代码编译成可执行文件test。

#include <thread>

int main(int argc, char* argv[]) {
    std::thread t = std::thread([]{
        while (true) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });
    t.join();
}

把test运行起来后,执行pidin -p test threads

# pidin -p test threads 
     pid tid name               thread name          STATE       Blocked                     
 1232925   1 ./test             1                    JOIN        2                           
 1232925   2 ./test             2                    NANOSLEEP           

从中看可以看到进程ID是1232925,进程名字是test,一共两个线程,名字分别为1,2。其中线程2处于sleep状态,线程1处于被线程2阻塞的join状态。

给线程起名字

我们可以通过pthread_setname_np给线程设置名字,以方便区分不同的线程。

#include <thread>

int main(int argc, char* argv[]) {
    pthread_setname_np(pthread_self(), "main");
    std::thread t = std::thread([]{
        while (true) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });
    pthread_setname_np(t.native_handle(), "test");
    t.join();
}

重新编译并运行,在另一个终端执行pidin -p test threads

# pidin -p test threads 
     pid tid name               thread name          STATE       Blocked                     
 1286173   1 ./test             main                 JOIN        2                           
 1286173   2 ./test             test                 NANOSLEEP         

可以看到线程名字已经变成了我们设置的名字。

设置线程优先级

我们可以通过pthread_setschedprio设置线程优先级。

#include <thread>

int main(int argc, char* argv[]) {
    pthread_setname_np(pthread_self(), "main");
    pthread_setschedprio(pthread_self(), 10);
    std::thread t = std::thread([]{
        while (true) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });
    pthread_setname_np(t.native_handle(), "test");
    pthread_setschedprio(t.native_handle(), 50);
    t.join();
}

重新编译并运行,在另一个终端执行pidin -p test -F "%a %b %N %h %J %B %p"

# pidin -p test  -F "%a %b %N %h %J %B %p"
     pid tid name               thread name          STATE       Blocked                      prio
 1351699   1 ./test             main                 JOIN        2                             10r
 1351699   2 ./test             test                 NANOSLEEP                                 50r

可以看到prio列显示的是我们设置的优先级。

观察启动参数和环境变量

可以通过pidin的arguments参数观察已启动程序的启动参数

#include <cstdio>
#include <unistd.h>

int main(int argc, char* argv[]) {
    for(int32_t i=0; i<argc; ++i) {
        printf("argument[%d]: %s\n", i, argv[i]);
    }
    pause();
}

重新编译,运行时加入几个参数,比如./test args0 args2

# ./test args0 args2
argument[0]: ./test
argument[1]: args0
argument[2]: args2

在另外一个终端执行pidin -p test arguments

# pidin -p test arguments
     pid Arguments
  639000 ./test args0 args2

同理,启动时加入环境变量TEST_ENV=hello_world ./test args0 args2

# pidin -p test environment 
     pid name               Environment
  675864 ./test             _=./test TEST_ENV=hello_world PATH=/proc/boot:/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/ti_fs:/ti_fs/bin:/ti_fs/sbin:/ti_fs/usr/bin:/ti_fs/usr/sbin:/ti_fs/tibin:/ti_fs/vision_apps:/ti_fs/scripts SHELL=/bin/sh USER=root LD_LIBRARY_PATH=/proc/boot:/lib:/usr/lib:/lib/dll:/opt/lib:/ti_fs/lib:/ti_fs/usr/lib:/ti_fs/lib/dll/mmedia:/ti_fs/lib/dll:/ti_fs/tilib:/ti_fs/vision_apps:/ti_fs/usr/lib/graphics/jacinto7 HOME=/ SSH_TTY=/dev/ttyp0 SYSNAME=nto LOGNAME=root

观察fd资源

可以通过pidin的fds参数观察已启动程序打开的fd资源

#include <unistd.h>
#include <fstream>

int main(int argc, char* argv[]) {
    std::ifstream f("/dev/zero");
    pause();
}

重新编译并运行,然后在另外终端执行pidin -p test fds

# pidin -p test fds 
     pid name               
  757784 ./test             
       0  536595 rw        0 /dev/ttyp0
       1  536595 rw        0 /dev/ttyp0
       2  536595 rw        0 /dev/ttyp0
       3       1 r-        0 /dev/zero
       0s      1 <procnto>:1

可以观察到3 1 r- 0 /dev/zero是我们代码里打开的/dev/zero,0,1,2则分别是标准输入,标准输出和标准错误。明白为什么我们的fd一般是从3开始了吗?因为0,1,2已经被他们占了。至于0s 1 <procnto>:1是什么我也不清楚,有了解的朋友可以解释下。

参考文档

pidin功能非常强大,更详细的信息请参考官方手册。

pidin

Processes and Threads

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值