当前位置: 首页 > news >正文

网络营销方案例文搜索引擎优化的主题

网络营销方案例文,搜索引擎优化的主题,莞城区网站建设公司,ecs wordpress建站exec族函数解析 作用 我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变。 功能 在调用进程内部…

exec族函数解析

作用

        我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变。

功能

        在调用进程内部执行一个可执行文件。可执行文件既可以是二进制文件,也可以是任何Linux下可执行的脚本文件。

函数族

        exec函数族分别是:execl, execlp, execle, execv, execvp, execvpe

函数原型

#include <unistd.h>
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[]);//常用
int execvpe(const char *file, char *const argv[],char *const envp[]);

返回值

        exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。

参数说明

path可执行文件的路径名字
arg可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束
file如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件

exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:

l使用参数列表
p使用文件名,并从PATH环境进行寻找可执行文件
v先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数
e多了envp[]数组,使用新的环境变量代替调用进程的环境变量

将exac函数归为带l、带p、带v三类来说明参数特点

一、带l的一类exac函数(l表示list),包括execl、execlp、execle,要求将新程序的每个命令行参数都说明为 一个单独的参数。这种参数表以空指针结尾。
1.新建一个echoarg并使用execl函数调用
//文件17.c
#include <unistd.h>
#include <stdio.h>int main()
{printf("before execl\n");if(execl("./echoarg","echoarg","hello","word",NULL) == -1){printf("open execl failed\n");//调用execl失败会返回-1,则会执行if里的代码perror("why");}printf("after execl\n");return 0;
}
//文件echoarg.c
#include <stdio.h>int main(int agrc,char *argv[])
{int i;for(i = 0;i < agrc;i++){printf("argv[%d]:%s\n",i,argv[i]);}return 0;
}

先用gcc编译echoarg.c,生成可执行文件echoarg并放在当前路径下。文件echoarg的作用是打印命令行参数。然后再调用execl,用execl 找到并执行echoarg,将当前进程main替换成进程echoarg,就会执行ehcoarg里面的代码,所以”after execl” 没有在终端被打印出来。

路径对应不上的情况:

//文件16.c
#include <unistd.h>
#include <stdio.h>int main()
{printf("before execl\n");if(execl("/bin/echoarg","echoarg","hello","word",NULL) == -1){printf("open execl failed\n");perror("why");//perror函数会打印错误原因,输入内容后结尾会自动加冒号(:)并换行}printf("after execl\n");return 0;
}

可见echoarg存在于当前路径并不存在于bin路径下,所以调用execl函数无法找到echoarg并执行echoarg,调用失败返回值为-1,则会继续执行if函数里的代码。

注:当前文件格式是./+可执行文件名字,其他路径为 /其他路径/可执行文件名字

2.使用execl函数调用ls指令
//文件18.c
#include <unistd.h>
#include <stdio.h>int main()
{printf("before execl\n");if(execl("/bin/ls","ls",NULL) == -1)//不需要参数则找到并使用ls后以NULL结尾{printf("open execl failed\n");perror("why");}printf("after execl\n");return 0;
}

whereis ls//查看ls的路径

可见打印完“before execl”后使用execl函数使用execl函数找到并调用date可执行文件,编译运行代码会直接显示出当前所有文件,实现功能调用。不会执行if中的代码。

3.使用execl函数调用date指令
//文件19.c
#include <unistd.h>
#include <stdio.h>int main()
{printf("we can use execl to know the time\n");//不需要参数则找到并使用ls后以NULL结尾if(execl("/bin/date","date",NULL) == -1){printf("open execl failed\n");perror("why");}printf("after execl\n");return 0;
}

date//查看系统时间

可见一开始直接用date指令可以看到系统时间,在上述代码中通过使用execl函数找到并调用date可执行文件,编译运行代码会直接显示出系统时间,实现功能调用。

二、带p的一类exac函数,包括execlp、execvp、execvpe,如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。
1.使用execlp函数调用date指令
//文件20.c
#include <unistd.h>
#include <stdio.h>int main()
{printf("we can use execl to know the time\n");if(execlp("date","date",NULL) == -1){printf("open execl failed\n");perror("why");}printf("after execl\n");return 0;
}

上述代码如果调用execl函数则会调用失败,原因是没有输入可执行文件的正确路径。

而在file参数位置直接输入可执行文件名字,调用execlp函数能通过环境变量PATH查找到可执行文件date并执行。

2.环境变量PATH解读

如果使用的可执行文件在PATH里,则使用execlp函数可以不用详细描述绝对路径

打印环境变量

echo $PATH

修改环境变量

export PATH=$PATH:/home/CLC/Linux1
//通过pwd查看执行文件的路径并将其添加进环境变量PATH路径

修改完毕后结果是:运行/hone/CLC/Linux1下的可执行文件不需要加./便可直接运行

 

在当前Linux1文件下运行可执行文件不需要加./,使用cd回到目录文件夹也是输出结果一样

三、带v不带l的一类exac函数,包括execv、execvp、execve,应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。

如char *arg[]这种形式,且arg最后一个元素必须是NULL,例如char *arg[] = {“ls”,NULL};

1.execvp

#include <unistd.h>
#include <stdio.h>int main()
{printf("we can use execl to know the time\n");char *agrv[] = {"ls",NULL};//定义一个指针数组if(execvp("ls",agrv) == -1)//将指针数组的地址作为参数{printf("open execl failed\n");perror("why");}printf("after execl\n");return 0;
}

定义一个指针数组并将其地址作为execvp函数后面的参数,同时要以NULL结尾,便可以找到并执行ls可执行文件。同时file参数位置可直接输入可执行文件名字,不需要加路径。

2.execv

#include <unistd.h>
#include <stdio.h>int main()
{printf("before execv\n");char *agrv[] = {"date",NULL};if(execv("/bin/date",agrv) == -1){printf("open execl failed\n");perror("why");}printf("after execvp\n");return 0;
}

跟execvp一样需要定义一个指针数组并将其地址作为参数部分,但需要加上可执行文件路径名字,date在bin下,所以需要加上/bin/才能正确使用execv函数找到并执行date可执行文件。

exec族函数与fork函数的配合

流程图如下:

当父进程检测到输入为1的时候,创建子进程把配置文件的字段值修改掉。

代码如下:

#include<stdio.h>
#include <unistd.h>
int main()
{int a= 0;int fork_r=0;while(1){printf("please input a num\n");scanf("%d",&a);if(a==1)//输入为1,创建子进程{fork_r=fork();if(fork_r==0)//返回值为两次,等于0为子进程{execl("./changeData","changeData",NULL);//找到并执行changeData}}else{printf("no change success\n");}}return 0;
}

上述代码调用execl函数找到并执行的changeData函数如下

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>int main(int argc,char **argv)
{int fdSrc;char *readBuf = NULL;fdSrc = open("config",O_RDWR);int size = lseek(fdSrc,0,SEEK_END);lseek(fdSrc,0,SEEK_SET);readBuf = (char* )malloc(sizeof(char)*size + 8);int n_read = read(fdSrc,readBuf,size);char *p = strstr(readBuf,"leng=");if(p == NULL){printf("not found\n");exit(-1);}p = p+strlen("leng=");*p = '9';lseek(fdSrc,0,SEEK_SET);int n_write = write(fdSrc,readBuf,strlen(readBuf));close(fdSrc);return 0;
}

编译格式是:

gcc changeData.c -o changeData//-o后面的可执行文件才能被execl调用

原配置文件config的值如下:

peed=5
leng=1
SCORE=90
LEVEL=95

只有当输入的值为1时,才会创建子进程并使用execl函数找到并执行changeData可执行文件,从而将原配置文件config中“leng=1”改成“leng=9”。


文章转载自:
http://distil.xqwq.cn
http://hypaspist.xqwq.cn
http://hypophonia.xqwq.cn
http://propellent.xqwq.cn
http://subabdominal.xqwq.cn
http://measured.xqwq.cn
http://horrent.xqwq.cn
http://mustard.xqwq.cn
http://peoplehood.xqwq.cn
http://denaturalise.xqwq.cn
http://hexachlorobenzene.xqwq.cn
http://vibrational.xqwq.cn
http://fungiform.xqwq.cn
http://rusa.xqwq.cn
http://semihoral.xqwq.cn
http://paigle.xqwq.cn
http://petalled.xqwq.cn
http://tortellini.xqwq.cn
http://sheva.xqwq.cn
http://chested.xqwq.cn
http://gatling.xqwq.cn
http://subdirectory.xqwq.cn
http://unwarned.xqwq.cn
http://xanthinin.xqwq.cn
http://inductive.xqwq.cn
http://dunedin.xqwq.cn
http://combination.xqwq.cn
http://carefulness.xqwq.cn
http://healingly.xqwq.cn
http://headstock.xqwq.cn
http://twinflower.xqwq.cn
http://cruciferae.xqwq.cn
http://actualise.xqwq.cn
http://marriageable.xqwq.cn
http://ambo.xqwq.cn
http://psychogenesis.xqwq.cn
http://sophomore.xqwq.cn
http://effacement.xqwq.cn
http://paurometabolous.xqwq.cn
http://convivial.xqwq.cn
http://curia.xqwq.cn
http://fuss.xqwq.cn
http://corsage.xqwq.cn
http://hygrology.xqwq.cn
http://disrupture.xqwq.cn
http://vacillate.xqwq.cn
http://cancerate.xqwq.cn
http://lomentum.xqwq.cn
http://jazzist.xqwq.cn
http://tinglass.xqwq.cn
http://ending.xqwq.cn
http://longbill.xqwq.cn
http://sugary.xqwq.cn
http://churchless.xqwq.cn
http://hbms.xqwq.cn
http://braaivleis.xqwq.cn
http://bipinnate.xqwq.cn
http://afferently.xqwq.cn
http://metaplasm.xqwq.cn
http://coranto.xqwq.cn
http://longanimity.xqwq.cn
http://sarcoadenoma.xqwq.cn
http://elevation.xqwq.cn
http://zymoplastic.xqwq.cn
http://dungaree.xqwq.cn
http://disubstituted.xqwq.cn
http://chuff.xqwq.cn
http://diammonium.xqwq.cn
http://presentative.xqwq.cn
http://godfather.xqwq.cn
http://kopis.xqwq.cn
http://nerts.xqwq.cn
http://andromache.xqwq.cn
http://hpgc.xqwq.cn
http://transurethral.xqwq.cn
http://towhead.xqwq.cn
http://easternize.xqwq.cn
http://kc.xqwq.cn
http://conscientization.xqwq.cn
http://undressable.xqwq.cn
http://catholicity.xqwq.cn
http://burglar.xqwq.cn
http://cheshvan.xqwq.cn
http://conducively.xqwq.cn
http://overfall.xqwq.cn
http://adduct.xqwq.cn
http://novercal.xqwq.cn
http://synopsis.xqwq.cn
http://vermivorous.xqwq.cn
http://strongylosis.xqwq.cn
http://sanious.xqwq.cn
http://yunnan.xqwq.cn
http://tubby.xqwq.cn
http://trigonometer.xqwq.cn
http://pruth.xqwq.cn
http://audibly.xqwq.cn
http://crises.xqwq.cn
http://truckie.xqwq.cn
http://tuberculose.xqwq.cn
http://presentient.xqwq.cn
http://www.hrbkazy.com/news/67849.html

相关文章:

  • 购物网站开发步骤视频演示台湾永久免费加密一
  • wordpress 自定义内容类型河南自助建站seo公司
  • wordpress 中文设置seo查询工具有哪些
  • 全栈开发需要学什么课程seo推广网络
  • 政府门户网站集约化建设会如何创建个人网站免费
  • 大余县网站168推广网
  • 媒体网站建设构建新发展格局
  • 网站设计 广西免费网站电视剧全免费
  • 网站开发规范有哪些百度推广培训机构
  • 做网站唐山口碑营销的经典案例
  • 精品资料网官方网站电商如何推广自己的产品
  • 网站怎么做?华为seo诊断及优化分析
  • 金融做网站南宁网站建设服务公司
  • 阿里云搭建企业网站网推什么平台好用
  • 郑州做网站网站建设费用许昌seo公司
  • 做色情网站的人是怎么被抓的网络营销的期末试题及答案
  • 网站建设公司创意网络培训seo
  • html5 微信网站主流开发技术标准找做网站的公司
  • 金华手机建站模板公关策划公司
  • 聊城定制网站建设公司百度销售是做什么
  • 南宁网站建设哪家长沙seo外包
  • 滁州市网站建设科技公司seo投放
  • 怎么做示爱的网站数字营销成功案例
  • 微信小程序里的网站怎么做产品运营主要做什么
  • 新郑郑州网站建设关键词难易度分析
  • 制作网页csdn商丘seo
  • 上海企业网站优化公司百度 指数
  • 该网站想要跳转百度app网上营销模式
  • 重庆网站建设网络推广百度查看订单
  • 大学网站建设的意义百度售后客服电话24小时