Chinaunix首页 | 论坛 | 博客
  • 博客访问: 912994
  • 博文数量: 132
  • 博客积分: 9976
  • 博客等级: 中将
  • 技术积分: 1781
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-30 20:40
文章分类

全部博文(132)

文章存档

2013年(1)

2011年(1)

2010年(15)

2009年(77)

2008年(36)

2007年(2)

我的朋友

分类: C/C++

2009-02-23 10:21:32

exec 系列函数的用法

本文专注于 exec 系列函数的用法,不设计到如何实现的问题。 这些函数如下所示:

1. execl
2. execlp
3. execle
4. execv
5. execvp

6. execve(2)

man manual info:
.......................................................................
NAME
       execl, execlp, execle, execv, execvp - execute a file

SYNOPSIS
       #include

       extern char **environ;

       int execl(const char *path, const char *arg, ...);
       int execlp(const char *file, const char *arg, ...);
       int execle(const char *path, const char *arg,
                  ..., char * const envp[]);
       int execv(const char *path, char *const argv[]);
       int execvp(const char *file, char *const argv[]);

DESCRIPTION
       The exec() family of functions replaces the current process image with a new process image.  The functions described in this manual page are front-
       ends for the function execve(2).  (See the manual page for execve(2) for detailed information about the replacement of the current process.)

       The initial argument for these functions is the pathname of a file which is to be executed.

       The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn.   Together
       they  describe  a  list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.  The
       first argument, by convention, should point to the filename associated with the file being executed.  The list of arguments must be terminated by a
       NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

       The  execv()  and  execvp() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new
       program.  The first argument, by convention, should point to the filename associated with the file being executed.  The array of pointers  must  be
       terminated by a NULL pointer.

       The execle() function also specifies the environment of the executed process by following the NULL pointer that terminates the list of arguments in
       the parameter list or the pointer to the argv array with an additional parameter.  This additional parameter is an array of pointers to null-termi‐
       nated strings and must be terminated by a NULL pointer.  The other functions take the environment for the new process image from the external vari‐
       able environ in the current process.
...........................................................................

===========================================================================
1. execl

用法格式:
int execl(const char *path, const char *arg, ...);

例如
execl
( "/bin/sh", "sh", "-c", COMMAND_name, command_parameters, NULL );

例子代码1:
.................................................
#include
#include
#include

int
main(int argc, char *argv[])
{
  int execl_ret = -1;
  execl_ret = execl("/bin/sh", "sh", "-c", "ls", "-l", (char *)0);
  if (-1 == execl_ret) {
    perror("error execute command");
    return 1;
  }
  return 0;
}
.................................................

例子代码2:(from goole, wmmmnn.c in
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>

#include <>
#include <>
#include <>

#include ""
#include ""

#include ""
#include ""

#include "" /* cdrom.* and mixer.* from */
#include "" /* wmrack by oliver graf. */

char *MAILCOMMAND;
char *PPPCOMMAND;
char *NETDEVICE;
char *MAILDIR;
int UPMAX;
int DOWNMAX;
int IOMAX;

char *ProgName;
FILE *fp_rc;
FILE *fp_meminfo;
FILE *fp_stat;
FILE *fp_loadavg;
FILE *fp_ppp;

long cpustat[5],cpuidle[5],cpustatlast,cpuidlelast;
long iostat[5],iostatlast;
long memused, memtotal, swapused, swaptotal;
long upstat[5], upidle[5], upstatlast;
long dostat[5], doidle[5], dostatlast;
int roll=1, mail=0, mmail=0, wmnup[20], wmndo[20], wmnetmode=1;


/* functions */

void wmmmnn_routine(int, char **);
void update_stat_cpu( void );
void update_stat_io( void );
void update_stat_mem( void );
void update_stat_ppp( void );
void defaults( void );

void main(int argc, char *argv[]) {

ProgName = argv[0];
if (strlen(ProgName) >= 5)
ProgName += (strlen(ProgName) - 5);

defaults();

wmmmnn_routine(argc, argv);

}


void wmmmnn_routine(int argc, char **argv) {


CD *cd;
MIXER *mixer;
int st=0, trax=0, song=0, songt, songo;
char *device="/dev/cdrom";
int changevolume=0, xvol, mixwhich=1, curvol;
int curmixer[5]= { 0, 4, 3, 6, 8 };
int curmixvol[5]= {0, 0, 0, 0, 0 };
char *mdevice="/dev/mixer";

XEvent Event;
struct stat St;
unsigned long i;
int xpm_X=0, xpm_Y=0, qq=0, qqq=0, qqqq, mode=0;
char *nscommand, *pp, *tempp;
const char *tokenss = " \t\n";
nscommand=(char *)malloc(256);
fp_meminfo = fopen("/proc/meminfo", "r");
fp_stat = fopen("/proc/stat", "r");
fp_ppp = fopen("/proc/net/dev", "r");

openXwindow(argc, argv, wmmmnn_master_xpm, wmmmnn_mask_bits, wmmmnn_mask_width, wmmmnn_mask_height);

for(i=0;i<20;i++)
wmnup[i]=wmndo[i]=0;

xpm_X = 0;
setMaskXY(0, 0);

cd=cd_open(device,0);
cd_getStatus(cd,0,1);

mixer=mixer_open(mdevice);

while (1) {

waitpid(0, NULL, WNOHANG);

if(!mode) {
roll++;
if(roll==5)
roll=0;

update_stat_cpu();
update_stat_io();
update_stat_ppp();

if(mail) {
if(qqq++==20) {
if(mmail) {
mmail=0;
copyXPMArea(4,99,22,12,4,32);
}
else {
mmail=1;
copyXPMArea(28,99,22,12,4,32);
}
qqq=0;
}
}

if(qq++==150) {

if (!stat(MAILDIR, &St))
if (St.st_mtime >= St.st_atime)
mail=1;
if (St.st_size == 0)
{
mail=0;
copyXPMArea(28,99,22,12,4,32);
}

fflush(fp_meminfo);
fflush(fp_ppp);
update_stat_mem();
qq=0;

}


}
else if(mode) {

usleep(60000L);

trax=cd_list(cd,tracks);
st=cd_getStatus(cd,0,0);
song=cd_cur(cd,track);

if(cd_cur(cd,mode)==1) {
copyXPMArea(51,72,11,13,15,4);
copyXPMArea(68,4,11,13,4,4);
}
else if(cd_cur(cd,mode)==2) {
copyXPMArea(79,4,11,13,15,4);
copyXPMArea(68,4,11,13,4,4);
}
else if(cd_cur(cd,mode)==3) {
copyXPMArea(79,4,11,13,15,4);
copyXPMArea(51,85,11,13,4,4);
}



songt=(song+1)/10;
songo=(song+1)-(songt*10);
copyXPMArea(4*songt,112,4,8,8,33);
copyXPMArea(4*songo,112,4,8,14,33);

copyXPMArea(92,4,32,54,28,4);

curvol=(curmixvol[1])*0.32;
copyXPMArea(32, 59, curvol, 12, 28, 4);
curvol=(curmixvol[2])*0.32;
copyXPMArea(32, 59, curvol, 12, 28, 18);
curvol=(curmixvol[3])*0.32;
copyXPMArea(32, 59, curvol, 12, 28, 32);
curvol=(curmixvol[4])*0.32;
copyXPMArea(32, 59, curvol, 12, 28, 46);
}




RedrawWindowXY(xpm_X, xpm_Y);

while (XPending(display)) {
XNextEvent(display, &Event);
switch (Event.type) {
case Expose:
RedrawWindowXY(xpm_X, xpm_Y);
break;
case DestroyNotify:
XCloseDisplay(display);
exit(0);
break;
case ButtonPress:
if(mode==0) {
switch (Event.xbutton.button) {
case 1:
if(Event.xbutton.y>32&&Event.xbutton.y<48&&Event.xbutton.x<26)
if(fork()==0) {
execl( "/bin/sh", "sh", "-c", MAILCOMMAND, NULL );
exit(15);
}
if(Event.xbutton.y<32&&Event.xbutton.x<26)
if(fork()==0) {
execl( "/bin/sh", "sh", "-c", PPPCOMMAND, NULL);
exit(15);
}
if(Event.xbutton.y>48&&Event.xbutton.x<26)
if(fork()==0) {
execl( "/bin/sh", "sh", "-c", "netscape", NULL);
exit(15);
}
break;
case 2:
if(Event.xbutton.y<32&&Event.xbutton.x<26) {
if(wmnetmode)
wmnetmode=0;
else
wmnetmode=1;
}
if(Event.xbutton.y>48&&Event.xbutton.x<26)
{
tempp=XFetchBytes(display,&qqqq);
pp=strtok(tempp, tokenss);
sprintf( nscommand, "%s%s%s", "netscape -remote 'openURL(",pp,")'" );
if(fork()==0) {
execl("/bin/sh", "sh", "-c", nscommand, NULL);
exit(15);
}
}
break;
case 3:
if(Event.xbutton.y>48&&Event.xbutton.x<26)
{
tempp=XFetchBytes(display,&qqqq);
pp=strtok(tempp,tokenss);
sprintf( nscommand, "%s%s%s", "netscape -remote 'openURL(",pp,", newwindow)'" );
if(fork()==0) {
execl( "/bin/sh", "sh", "-c", nscommand, NULL);
exit(15);
}
}
if(Event.xbutton.x>26) {
mode=1;
copyXPMArea(68,4,56,54,4,4);
}

break;
}
}



else if(mode==1) {
cd_getStatus(cd,0,0);
switch (Event.xbutton.button) {
case 1:
if(Event.xbutton.x<15&&Event.xbutton.y<17) {
if(cd_cur(cd,mode)==1||cd_cur(cd,mode)==2)
{
cd_doStop(cd);
song=cd_play(cd,cur_track);
}
else if(cd_cur(cd,mode)==-1) {
cd_getStatus(cd,1,1);
trax=cd_list(cd,tracks);
}
else
cd_doEject(cd);
}
else if(Event.xbutton.x<26&&Event.xbutton.x>15&&Event.xbutton.y<17) {
if(cd_cur(cd,mode)==1||cd_cur(cd,mode)==2) {
cd_doPause(cd);
}
else if(cd_cur(cd,mode)==-1) {
if(cd_getStatus(cd,1,0))
trax=cd_list(cd,tracks);
cd_doPlay(cd,0);
}
else
cd_doPlay(cd,song);
}

else if(Event.xbutton.x<15&&Event.xbutton.y>17&&Event.xbutton.y<32) {
if(--song<0)
song=trax-1;
cd_doPlay(cd,song);
}
else if(Event.xbutton.x<26&&Event.xbutton.x>15&&Event.xbutton.y<32&&Event.xbutton.y>17) {
if(++song>=trax)
song=0;
cd_doPlay(cd,song);
}
else if(Event.xbutton.x>27) {
mixwhich=((Event.xbutton.y/16)+1);
changevolume=1;
xvol=Event.xbutton.x;
curvol=(xvol-28)*3;
if(curvol<8)
curvol=0;
if(curvol>92)
curvol=100;
mixer_setvols(mixer,curmixer[mixwhich],curvol,curvol);
curmixvol[mixwhich]=curvol;
}
break;
case 3:
mode=0;
copyXPMArea(68,59,56,54,4,4);
qq=149;
break;
}

}

break;
case MotionNotify:
if(changevolume==1) {
xvol=Event.xmotion.x;
curvol=(xvol-28)*3;
if(curvol<8)
curvol=0;
if(curvol>92)
curvol=100;
mixer_setvols(mixer,curmixer[mixwhich],curvol,curvol);
curmixvol[mixwhich]=curvol;
}
break;

case ButtonRelease:
if(changevolume==1)
changevolume=0;
break;

}
}
usleep(40000L);
}
}

void update_stat_cpu( void ) {

long bisi=0, aidl=0;
long cistat=0, cidle=0;
int i,j;
char *temp, *p, *tokens = " \t\n";
temp=(char*)malloc(128);


rewind(fp_stat);
while (fgets(temp, 128, fp_stat)) {
if (strstr(temp, "cpu")) {
p = strtok(temp, tokens);
for (i=0; i<3; i++) {
p = strtok(NULL, tokens);
bisi += atol(p);
}
p = strtok(NULL, tokens);
aidl = atol(p);
}
}

cpustat[roll] = bisi - cpustatlast;
cpustatlast = bisi;
cpuidle[roll] = aidl - cpuidlelast;
cpuidlelast = aidl;

for(i=0;i<5;i++) {
cistat += cpustat[i];
cidle += cpuidle[i];
}

copyXPMArea(0, 59, 32, 12, 28, 4);
j = (cistat + cidle);
if(j)
j=(cistat * 32)/j;
if(j > 32)
j=32;
copyXPMArea(32, 59, j, 12, 28, 4);

free(temp);
}

void update_stat_io( void ) {

long bisi=0;
long ostat=0,oidle=0;
int i,j;
char *temp, *p, *tokens = " \t\n";
temp=(char*)malloc(128);

rewind(fp_stat);
while (fgets(temp, 128, fp_stat)) {
if (strstr(temp, "disk_rio") || strstr(temp, "disk_wio")) {
p = strtok(temp, tokens);
for (i=0; i<4; i++) {
p = strtok(NULL, tokens);
bisi += atol(p);
}
}
}

iostat[roll] = bisi - iostatlast;
iostatlast = bisi;

for(i=0;i<5;i++) {
ostat += iostat[i];
oidle += IOMAX;
}

copyXPMArea(0, 59, 32, 12, 28, 18);
j = (ostat + oidle);
if (j)
j=(ostat * 32)/j;
if (j > 32)
j=32;
copyXPMArea(32, 59, j, 12, 28, 18);

free(temp);
}

void update_stat_mem( void ) {

int j;
char *temp, *p, *tokens = " \t\n";
temp=(char*)malloc(128);

rewind(fp_meminfo);
fgets(temp, 128, fp_meminfo);
fgets(temp, 128, fp_meminfo);
p = strtok(temp, tokens);
p = strtok(NULL, tokens);
memtotal = atol(p);

p = strtok(NULL, tokens);
memused = atol(p);

p = strtok(NULL, tokens);
p = strtok(NULL, tokens);
p = strtok(NULL, tokens);
memused -= atol(p);
p = strtok(NULL, tokens);
memused -= atol(p);
fgets(temp, 128, fp_meminfo);
p = strtok(temp, tokens);
p = strtok(NULL, tokens);
swaptotal = atol(p);
p = strtok(NULL, tokens);
swapused = atol(p);


copyXPMArea(0, 59, 32, 12, 28, 32);
j = memtotal;
if (j)
j = (memused * 32) / j;
if (j > 32)
j = 32;
copyXPMArea(32, 59, j, 12, 28, 32);

copyXPMArea(0, 59, 32, 12, 28, 46);
j = swaptotal;
if (j)
j = (swapused * 32) / j;
if (j > 32)
j = 32;
copyXPMArea(32, 59, j, 12, 28, 46);


free(temp);
}



void update_stat_ppp( void ) {

long bisi, pisi;
long pupstat,pupidle,pdostat,pdoidle;
int i,j;
char *temp, *pp, *tokens = " :\t\n";
temp=(char*)malloc(128);
bisi=pisi=pupstat=pupidle=pdostat=pdoidle=0;

rewind(fp_ppp);
while(fgets(temp, 128, fp_ppp)) {
if(strstr(temp, NETDEVICE)) {
pp = strtok(temp, tokens);
pp = strtok(NULL, tokens);
bisi=atol(pp);

pp = strtok(NULL, tokens);
pp = strtok(NULL, tokens);
pp = strtok(NULL, tokens);
pp = strtok(NULL, tokens);
pp = strtok(NULL, tokens);
pp = strtok(NULL, tokens);
pp = strtok(NULL, tokens);
pp = strtok(NULL, tokens);
pisi=atol(pp);
}
}

dostat[roll] = bisi - dostatlast;
dostatlast = bisi;
doidle[roll] = DOWNMAX - dostat[roll];
upstat[roll] = pisi - upstatlast;
upstatlast = pisi;
upidle[roll] = UPMAX - upstat[roll];

for(i=0;i<5;i++)
{
pupstat += upstat[i];
pupidle += upidle[i];
pdostat += dostat[i];
pdoidle += doidle[i];
}

for(i=0;i<19;i++)
{
wmnup[i]=wmnup[i+1];
wmndo[i]=wmndo[i+1];
}

copyXPMArea(28, 72, 22, 26, 4, 4);
j = (pdostat + pdoidle);
if (j)
j = (pdostat * 26) / j;
if (j > 26)
j = 26;
wmndo[19]=j;

j = (pupstat + pupidle);
if (j)
j = (pupstat * 26) / j;
if (j > 26)
j = 26;
wmnup[19]=j;


if(wmnetmode) {
for(i=0;i<20;i++)
{
copyXPMArea(16, 72, 1, wmnup[i], 5+i, 4);
copyXPMArea(5, 98-wmndo[i], 1, wmndo[i], 5+i, 30-wmndo[i]);
}
}
else {
copyXPMArea(4,98-wmndo[19], 10, wmndo[19], 4, 30-wmndo[19]);
copyXPMArea(16,72,10,wmnup[19],16,4);
}


free(temp);
}

void defaults(void) {

char temp[128],*token;
char *tokens=" =\n";
char *rcfilename=".wmmmnnrc";
char *rcfile;
char *HOME;
char *NAME;

HOME=getenv("HOME");
NAME=getenv("USER");


rcfile=(char*)malloc((strlen(HOME)+strlen(rcfilename)+1)*sizeof(char));
sprintf(rcfile,"%s%s%s",HOME,"/",rcfilename);

fp_rc=fopen(rcfile,"r");
if(!fp_rc) {
printf("cannot find %s\n", rcfile);
exit(13);
}

MAILDIR="";
UPMAX=0;
DOWNMAX=0;
IOMAX=0;


while(fgets(temp,128,fp_rc)) {
if(strstr(temp,"MAILCOMMAND")) {
token=strtok(temp,tokens);
MAILCOMMAND=strdup(strcpy(token,(temp+12)));
}
if(strstr(temp,"PPPCOMMAND")) {
token=strtok(temp,tokens);
PPPCOMMAND=strdup(strtok(NULL,tokens));
}
if(strstr(temp,"NETDEVICE")) {
token=strtok(temp,tokens);
NETDEVICE=strdup(strtok(NULL,tokens));
}
if(strstr(temp,"MAILDIR")) {
token=strtok(temp,tokens);
MAILDIR=strdup(strtok(NULL,tokens));
}
if(strstr(temp,"UPMAX")) {
token=strtok(temp,tokens);
token=strdup(strtok(NULL,tokens));
UPMAX=atoi(token);
}
if(strstr(temp,"DOWNMAX")) {
token=strtok(temp,tokens);
token=strdup(strtok(NULL,tokens));
DOWNMAX=atoi(token);
}
if(strstr(temp,"IOMAX")) {
token=strtok(temp,tokens);
token=strdup(strtok(NULL,tokens));
IOMAX=atoi(token);
}
}

fclose(fp_rc);


if(MAILDIR=="") {
MAILDIR=(char*)malloc((strlen("/var/spool/mail/")+strlen(NAME)+1)*sizeof(char));
sprintf(MAILDIR,"%s%s","/var/spool/mail/",NAME);
}
if(!UPMAX)
UPMAX=300;
if(!DOWNMAX)
DOWNMAX=300;
if(!IOMAX)
IOMAX=2;

}
..........................................................

例子代码3:
(from: google,
logrotate.c:)
...........................................................
static int runScript(char *logfn, char *script)
{
int rc;

if (debug) {
message(MESS_DEBUG, "running script with arg %s: \"%s\"\n",
logfn, script);
return 0;
}

if (!fork()) {
execl("/bin/sh", "sh", "-c", script, NULL);
exit(1);
}

wait(&rc);
return rc;
}

...........................................................

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================

===========================================================================


阅读(1232) | 评论(0) | 转发(0) |
0

上一篇:PPPOE协议

下一篇:My C error log

给主人留下些什么吧!~~