1.exec族函数的作用
我们用fork函数创建新的进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序,因为调用exec函数并不创建新进程,所以前后进程的ID没有改变。调用成功不返回,不再执行原程序;失败返回-1,并且继续原程序。
2.execl
#include <unistd.h> //所需头文件
int execl(const char *path, const char *arg, ...);//执行成功不返回数值,执行不成功返回-1;
//const char *path(可执行文件的路径名字)
//const char *arg(可执行所带的参数,第一个参数为可执行文件名字,没有带路径,且以NULL结束)
3.验证execl函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("before execl\n");
if(execl("/bin/date","date",NULL,NULL) == -1)//(通过whereis date查询date的绝对路径)
{
printf("execl failed!\n");
perror(why:);//显示错误信息
}
printf("after execl\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("before execl\n");
if(execl("/bin/ls","ls","-l",NULL) == -1)
{
printf("execl failed!\n");
}
printf("after execl\n");
return 0;
}
4.execlp
#include <unistd.h> //所需头文件
int execlp(const char *file, const char *arg, ...);//能通过PATH查找到可执行文件
//const char *file(可执行文件的名字)
//const char *arg(可执行所带的参数,第一个参数为可执行文件名字,没有带路径,且以NULL结束)
5.验证execlp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("before execl\n");
if(execlp("date","date",NULL,NULL) == -1)
{
printf("execl failed!\n");
}
printf("after execl\n");
return 0;
}
6.execvp(与execlp作用相同,表现形式不同)
#include <unistd.h> //所需头文件
int execvp(const char *file, char *const argv[]);
7.验证execvp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("before execl\n");
char *argv[] = {"date",NULL,NULL};
if(execvp("date",argv) == -1)
{
printf("execl failed!\n");
}
printf("after execl\n");
return 0;
}
8.execv(与execvp相同,就是需加绝对路径)
#include <unistd.h> //所需头文件
int execv(const char *path, char *const argv[]);
9.使用fork和open函数模拟等待信号(等待1)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
pid_t get1;
int data;
while(1){
printf("please input data\n");
scanf("%d",&data);
if(data == 1){
get1 = fork();
if(get1 > 0 ){
}else if(get1 == 0){
int fd1;
char *fdBuf=NULL;
fd1 = open("/home/CLC/text/text.config",O_RDWR);
if(fd1 < 0){
printf("open shibai\n");
exit(-1);
}
int size = lseek(fd1,0,SEEK_END);
lseek(fd1,0,SEEK_SET);
fdBuf=(char *)malloc(sizeof(char)*size+8);
int n_read = read(fd1,fdBuf,size);
printf("read success %d",n_read);
if(n_read < 0){
printf("duq nairon zhijie shibai\n");
exit(-1);
}
char *p = strstr(fdBuf,"LENG=");//此时光标在L
if(p == NULL){
printf("chaizhao shibai\n");
exit(-1);
}
p = p + strlen("LENG=");
*p = '6';
lseek(fd1,0,SEEK_SET);
write(fd1,fdBuf,strlen(fdBuf));
close(fd1);
}
}else{
printf("dendaishuru...\n");
}
}
return 0;
}
使用execl后:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
pid_t get1;
int data;
while(1){
printf("please input data\n");
scanf("%d",&data);
if(data == 1){
get1 = fork();
if(get1 > 0 ){
wait(NULL);
}else if(get1 == 0){
execl("./shiugai","shiugai","text.config",NULL);//需当前路径
exit(0);
}
}else{
printf("dendaishuru...\n");
}
}
return 0;
}
10.system()函数调用/bin/sh来执行参数指定的命令
#include <stdlib.h>
int system(const char *command);//返回进程的状态值;当sh不能执行时,返回127;失败时返回-1。
//调用成功,继续执行原程序;失败返回-1,并且继续原程序。
//作用与exec族函数相同
11.验证system
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
pid_t get1;
int data;
while(1){
printf("please input data\n");
scanf("%d",&data);
if(data == 1){
get1 = fork();
if(get1 > 0 ){
wait(NULL);
}else if(get1 == 0){
//execl("./shiugai","shiugai","text.config",NULL);
system("./shiugai");
}
}else{
printf("dendaishuru...\n");
}
}
return 0;
}
12.popen
#include <stdio.h>
FILE *popen(const char *command, const char *type);//相比于system,可以获取运行的结果
//const char *type:用什么方式打开。
13.验证popen函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
char a[1024] = {0};
FILE *fp;
fp = popen("ps","r");
int n_read = fread(a,1,1024,fp);
printf("n_read=%d,a=%s\n",n_read,a);
return 0;
}