Linux进程程序替换以及简单的shell
2017-03-01 19:01
423 查看
进程程序替换:程序从硬盘加载到内存中并通过地址空间映射。进程程序替换过程只是将代码和数据替换,并没有创建新的进程,因为pid不变。
exec函数家族包括6种:execl、execlp、execle、execv、execvp、execve
execve是最标准的系统调用函数,前5种都是基于execve函数。
只要exec函数调用成功,后续代码全部失效,所以不需要返回值来判断其是否调用成功。(适用于所有的exec开头的函数)
shell的运行原理:创建一个子进程程序,然后用子进程程序替换目标程序,(在此用的是exec函数家族),父进程等待,一旦出错立即退出,回收期退出码。
代码如下:
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
char cmd[128];
while(1)
{
printf("[test@my-host-name myshell]# ");
fflush(stdout);
//sleep(5);
ssize_t _s=read(0,cmd,sizeof(cmd)-1);
if(_s>0)
{
cmd[_s-1]='\0';
}
else
{
perror("read");
return 1;
}
//ls -a -l -n -i
char *_argv[32];
_argv[0]=cmd;
int i=1;
char *start=cmd;
while(*start)
{
if(isspace(*start))
{
*start='\0';
start++;
_argv[i]=start;
i++;
printf("i=%d\n",i);
continue;
}
printf("i=%d\n",i);
start++;
}
_argv[i]=NULL;
printf("%s\n",cmd);
pid_t id=fork();
if(id<0)
{
perror("fork");
}
else if(id==0)
{
//child ->run cmd
execvp(_argv[0],_argv);
exit(1);
}
else
{
int status=0;
pid_t ret=waitpid(id,&status,0);
if(ret>0 && WIFEXITED(status))
{
printf("exit code:%d\n",WEXITSTATUS(status));
}
else
{
perror("waitpid");
}
}
}
}
在上述代码中,read函数的返回值_s存在3种情况
_s>0:表示文件读取成功,且读取成功的数值小于等于sizeof(cmd)-1;
_s=0:表示做所读取的文件描述符已读到文件的结尾;
_s<0:表示读取文件出错。
进程程序替换:程序从硬盘加载到内存中并通过地址空间映射。进程程序替换过程只是将代码和数据替换,并没有创建新的进程,因为pid不变。
exec函数家族包括6种:execl、execlp、execle、execv、execvp、execve
execve是最标准的系统调用函数,前5种都是基于execve函数。
只要exec函数调用成功,后续代码全部失效,所以不需要返回值来判断其是否调用成功。(适用于所有的exec开头的函数)
shell的运行原理:创建一个子进程程序,然后用子进程程序替换目标程序,(在此用的是exec函数家族),父进程等待,一旦出错立即退出,回收期退出码。
代码如下:
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
char cmd[128];
while(1)
{
printf("[test@my-host-name myshell]# ");
fflush(stdout);
//sleep(5);
ssize_t _s=read(0,cmd,sizeof(cmd)-1);
if(_s>0)
{
cmd[_s-1]='\0';
}
else
{
perror("read");
return 1;
}
//ls -a -l -n -i
char *_argv[32];
_argv[0]=cmd;
int i=1;
char *start=cmd;
while(*start)
{
if(isspace(*start))
{
*start='\0';
start++;
_argv[i]=start;
i++;
printf("i=%d\n",i);
continue;
}
printf("i=%d\n",i);
start++;
}
_argv[i]=NULL;
printf("%s\n",cmd);
pid_t id=fork();
if(id<0)
{
perror("fork");
}
else if(id==0)
{
//child ->run cmd
execvp(_argv[0],_argv);
exit(1);
}
else
{
int status=0;
pid_t ret=waitpid(id,&status,0);
if(ret>0 && WIFEXITED(status))
{
printf("exit code:%d\n",WEXITSTATUS(status));
}
else
{
perror("waitpid");
}
}
}
}
在上述代码中,read函数的返回值_s存在3种情况
_s>0:表示文件读取成功,且读取成功的数值小于等于sizeof(cmd)-1;
_s=0:表示做所读取的文件描述符已读到文件的结尾;
_s<0:表示读取文件出错。
相关文章推荐
- 进程替换以及简单实现简易shell
- 用shell编写的简单密码加密程序,涉及到ASCII与字符的相互转换,以及命令结果输出到变量
- Linux 简单shell创建自己的守护进程,自动重启,纪录进程运行状态,日志切割压缩
- linux下的简单的shell程序
- 在linux中安装jdk以及tomcat并shell脚本关闭启动的进程
- Linux--shell中的命令替换``与$()以及eval命令
- 用shell编写的简单密码加密程序,涉及到ASCII与字符的相互转换,以及命令结果输出到变量
- linux之一个简单的shell程序
- Linux下的进程3——进程创建,文件替换,进程程序替换
- UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的shell->在shell中启动另一个程序
- linux程序守护进程(shell)
- shell脚本程序中的部分常用环境变量和参数变量的说明以及简单shell脚本示例
- 进程的程序替换与shell的实现
- linux bash shell 子程序、函数的简单声明以及调用
- 用C语言编写一个Linux下的简单shell程序
- demopu教你学linux,shell实现简单的进程监控脚本
- 用C语言编写一个Linux下的简单shell程序
- Linux---进程替换及编写一个自己的shell
- linux简单的TCP与UDP的socket程序以及机器大小端的判断程序
- shell之什么时候使用shell以及最简单的shell程序