Linux下C语言修改进程名称的方法
本文实例讲述了Linux下C语言修改进程名称的方法。分享给大家供大家参考。具体如下:
#include<stdio.h> #include<string.h> #include"./util/setproctitle.c" //externchar**environ; //intmain(intargc,char*argv[]) //{ //inti; //printf("argc:%d\n",argc); //for(i=0;i<argc;++i){ //printf("0x%x\n",argv[i]); //printf("argv[%d]:%s\n",i,argv[i]); //} //printf("evriron=%x\n",environ[0]); //return0; //} intmain(intargc,char**argv){ spt_init(argc,argv); setproctitle("设置进程名为:thisisatest"); sleep(1000); return0; }
setproctitle.c文件如下:
/*========================================================================== *setproctitle.c-Linux/Darwinsetproctitle. *-------------------------------------------------------------------------- *Copyright(C)2010WilliamAhern *Copyright(C)2013SalvatoreSanfilippo *Copyright(C)2013StamHe * *Permissionisherebygranted,freeofcharge,toanypersonobtaininga *copyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andtopermit *personstowhomtheSoftwareisfurnishedtodoso,subjecttothe *followingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbeincluded *inallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESS *ORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.IN *NOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM, *DAMAGESOROTHERLIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOR *OTHERWISE,ARISINGFROM,OUTOFORINCONNECTIONWITHTHESOFTWAREORTHE *USEOROTHERDEALINGSINTHESOFTWARE. *========================================================================== */ #ifndef_GNU_SOURCE #define_GNU_SOURCE #endif #include<stddef.h>/*NULLsize_t*/ #include<stdarg.h>/*va_listva_startva_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>/*errnoprogram_invocation_nameprogram_invocation_short_name*/ #if!defined(HAVE_SETPROCTITLE) #defineHAVE_SETPROCTITLE(defined__NetBSD__||defined__FreeBSD__||defined__OpenBSD__) #endif #if!HAVE_SETPROCTITLE #if(defined__linux||defined__APPLE__) externchar**environ; staticstruct{ /*originalvalue*/ constchar*arg0; /*titlespaceavailable*/ char*base,*end; /*pointertooriginalnulcharacterwithinbase*/ char*nul; _Boolreset; interror; }SPT; #ifndefSPT_MIN #defineSPT_MIN(a,b)(((a)<(b))?(a):(b)) #endif staticinlinesize_tspt_min(size_ta,size_tb); staticintspt_clearenv(void); staticintspt_copyenv(char*oldenv[]); staticintspt_copyargs(intargc,char*argv[]); voidspt_init(intargc,char*argv[]); voidsetproctitle(constchar*fmt,...); staticinlinesize_tspt_min(size_ta,size_tb){ returnSPT_MIN(a,b); }/*spt_min()*/ /* *Fordiscussionontheportabilityofthevariousmethods,see *http://lists.freebsd.org/pipermail/freebsd-stable/2008-June/043136.html */ staticintspt_clearenv(void){ #if__GLIBC__ clearenv(); return0; #else externchar**environ; staticchar**tmp; if(!(tmp=malloc(sizeof*tmp))) returnerrno; tmp[0]=NULL; environ=tmp; return0; #endif }/*spt_clearenv()*/ staticintspt_copyenv(char*oldenv[]){ externchar**environ; char*eq; inti,error; if(environ!=oldenv) return0; if((error=spt_clearenv())) gotoerror; for(i=0;oldenv[i];i++){ if(!(eq=strchr(oldenv[i],'='))) continue; *eq='\0'; error=(0!=setenv(oldenv[i],eq+1,1))?errno:0; *eq='='; if(error) gotoerror; } return0; error: environ=oldenv; returnerror; }/*spt_copyenv()*/ staticintspt_copyargs(intargc,char*argv[]){ char*tmp; inti; for(i=1;i<argc||(i>=argc&&argv[i]);i++){ if(!argv[i]) continue; if(!(tmp=strdup(argv[i]))) returnerrno; argv[i]=tmp; } return0; }/*spt_copyargs()*/ voidspt_init(intargc,char*argv[]){ char**envp=environ; char*base,*end,*nul,*tmp; inti,error; if(!(base=argv[0])) return; nul=&base[strlen(base)]; end=nul+1; for(i=0;i<argc||(i>=argc&&argv[i]);i++){ if(!argv[i]||argv[i]<end) continue; end=argv[i]+strlen(argv[i])+1; } for(i=0;envp[i];i++){ if(envp[i]<end) continue; end=envp[i]+strlen(envp[i])+1; } if(!(SPT.arg0=strdup(argv[0]))) gotosyerr; if((error=spt_copyenv(envp))) gotoerror; if((error=spt_copyargs(argc,argv))) gotoerror; SPT.nul=nul; SPT.base=base; SPT.end=end; return; syerr: error=errno; error: SPT.error=error; }/*spt_init()*/ #ifndefSPT_MAXTITLE #defineSPT_MAXTITLE255 #endif voidsetproctitle(constchar*fmt,...){ charbuf[SPT_MAXTITLE+1];/*usebufferincaseargv[0]ispassed*/ va_listap; char*nul; intlen,error; if(!SPT.base) return; if(fmt){ va_start(ap,fmt); len=vsnprintf(buf,sizeofbuf,fmt,ap); va_end(ap); }else{ len=snprintf(buf,sizeofbuf,"%s",SPT.arg0); } if(len<=0) {error=errno;gotoerror;} if(!SPT.reset){ memset(SPT.base,0,SPT.end-SPT.base); SPT.reset=1; }else{ memset(SPT.base,0,spt_min(sizeofbuf,SPT.end-SPT.base)); } len=spt_min(len,spt_min(sizeofbuf,SPT.end-SPT.base)-1); memcpy(SPT.base,buf,len); nul=&SPT.base[len]; if(nul<SPT.nul){ *SPT.nul='.'; }elseif(nul==SPT.nul&&&nul[1]<SPT.end){ *SPT.nul=''; *++nul='\0'; } return; error: SPT.error=error; }/*setproctitle()*/ #endif/*__linux||__APPLE__*/ #endif/*!HAVE_SETPROCTITLE*/
希望本文所述对大家的C语言程序设计有所帮助。