打印

linux C语言 修改进程名称 (转 )

[复制链接]
558|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Violin11|  楼主 | 2017-10-7 16:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
linux C语言 修改进程名称


本文实例讲述了Linux下C语言修改进程名称的方法。分享给大家供大家参考。具体如下:



main.c

#include <stdio.h>
#include <string.h>
#include "setproctitle.c"


int main(int argc, char **argv)
{
    spt_init(argc,argv);
    setproctitle("new_name_process");
    sleep(1000);
    return 0;
}




setproctitle.c

/* ==========================================================================
* setproctitle.c - Linux/Darwin setproctitle.
* --------------------------------------------------------------------------
* Copyright (C) 2010  William Ahern
* Copyright (C) 2013  Salvatore Sanfilippo
* Copyright (C) 2013  Stam He
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
* ==========================================================================
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif


#include <stddef.h>   /* NULL size_t */
#include <stdarg.h>   /* va_list va_start va_end */
#include <stdlib.h>   /* malloc(3) setenv(3) clearenv(3) setproctitle(3) getprogname(3) */
#include <stdio.h>    /* vsnprintf(3) snprintf(3) */


#include <string.h>   /* strlen(3) strchr(3) strdup(3) memset(3) memcpy(3) */


#include <errno.h>    /* errno program_invocation_name program_invocation_short_name */


#if !defined(HAVE_SETPROCTITLE)
#define HAVE_SETPROCTITLE (defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__)
#endif




#if !HAVE_SETPROCTITLE
#if (defined __linux || defined __APPLE__)


extern char **environ;


static struct {
    /* original value */
    const char *arg0;


    /* title space available */
    char *base, *end;


     /* pointer to original nul character within base */
    char *nul;


    _Bool reset;
    int error;
} SPT;




#ifndef SPT_MIN
#define SPT_MIN(a, b) (((a) < (b))? (a) : (b))
#endif


static inline size_t spt_min(size_t a, size_t b);
static int spt_clearenv(void);
static int spt_copyenv(char *oldenv[]);


static int spt_copyargs(int argc, char *argv[]) ;
void spt_init(int argc, char *argv[]);
void setproctitle(const char *fmt, ...);




static inline size_t spt_min(size_t a, size_t b) {
    return SPT_MIN(a, b);
} /* spt_min() */




/*
* For discussion on the portability of the various methods, see
* http://lists.freebsd.org/pipermail/freebsd-stable/2008-June/043136.html
*/
static int spt_clearenv(void) {
#if __GLIBC__
    clearenv();



相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

717

主题

1010

帖子

3

粉丝