zoukankan      html  css  js  c++  java
  • prctl系统调用设置进程名

    1. prctl 系统调用函数

    $ man 2 prctl
    //prctl - operations on a process
    //#include <sys/prctl.h>
    
    int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
    
    //return 0 on success.  On error, -1 is returned, and errno is set appropriately.

    2. prctl设置进程名内核响应

    SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, unsigned long, arg4, unsigned long, arg5)//kernel/sys.c
    {
        ...
        switch (option) {
        ...
        case PR_SET_NAME:
            comm[sizeof(me->comm) - 1] = 0;
            if (strncpy_from_user(comm, (char __user *)arg2, sizeof(me->comm) - 1) < 0)
                return -EFAULT;
            set_task_comm(me, comm);
            proc_comm_connector(me);
            break
        ...
        }
        ...
    }

    可见 arg2 就是要设置的进程名,arg1需要为 PR_SET_NAME,其它参数不用管。

    3. 实验程序

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/prctl.h>
    
    int main()
    {
        int ret = prctl(PR_SET_NAME, "hello_world", NULL, NULL, NULL);
        printf("ret=%d
    ", ret);
        while(1) {
            sleep(1);
        }
        return 0;
    }

    测试结果:

    $ gcc prctl_test.c -o pp
    $ ./pp &
    [2] 39844
    $ ret=0
    
    $ cat /proc/39844/comm 
    hello_world
    $ ps -AT | grep 39844
     39844  39844 pts/13   00:00:00 hello_world
  • 相关阅读:
    一转眼,十年间~~~
    怎样安装PHPnow,并开始使用?
    js的事件类型
    ie兼容性问题 前传
    外层div撑不开的解决办法
    CSS3 新属性搜罗整理
    webApp学习之路
    花式使用CSS3 transition
    20种新颖的按钮风格和效果【附源码】
    酷酷的文本效果
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/15506178.html
Copyright © 2011-2022 走看看