被调用的linux系统函数system的是如何实现的!
2012-04-14 14:14
701 查看
用mysystem()函数来说明被调用的系统函数system是如何被实现出来的!其实是调用了linux里面的shell命令解释器来进行命令调用的,是通过fork()函数创建一个父进程和子进程,
然后在子进程中进程调用execl()来引入shell解释器,从而实现命令解析的功能!
下面请看代码吧
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
void mysystem(char *string);
int main()
{
char *string;
char str[1024];
string=str;
gets(string);
mysystem(string);
}
void mysystem(char *string)
{
pid_t pid;
pid=fork();
if(-1==pid)
{
printf("error!\n");
}
else if (pid==0)
{
execl("/bin/sh","sh","-c",string,NULL);
exit(1);
}
else
{
exit(1);
}
}
然后在子进程中进程调用execl()来引入shell解释器,从而实现命令解析的功能!
下面请看代码吧
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
void mysystem(char *string);
int main()
{
char *string;
char str[1024];
string=str;
gets(string);
mysystem(string);
}
void mysystem(char *string)
{
pid_t pid;
pid=fork();
if(-1==pid)
{
printf("error!\n");
}
else if (pid==0)
{
execl("/bin/sh","sh","-c",string,NULL);
exit(1);
}
else
{
exit(1);
}
}
相关文章推荐
- 如何来实现一个Linux内核的系统调用(基于tiny4412开发板)
- Linux操作系统下,通过系统调用和库函数分别实现对文件的拷贝
- 驱动函数write-linux系统调用如何实现
- write的奥秘-linux系统调用如何实现?
- 如何在linux C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理(转)
- Linux下c++调用自己编写的matlab函数:通过shell script system command实现
- (转)如何在linux C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理
- 函数调用是如何在系统中实现的-以C为例
- linux系统调用system()函数详解
- linux 调用系统函数来实现 ATM 自动取款机功能 (进程间通信)
- linux 调用系统函数来实现 ATM 自动取款机功能 (进程间通信)
- linux系统调用如何实现write
- c++里的system()函数调用系统命令如何获取打印信息
- 如何实现一个新的linux系统调用
- 2.6版本Linux上替换系统调用函数实现隐藏文件学习
- linux 系统信号忽略和system函数返回值问题(system的实现)
- PHP如何执行Linux系统命令呢?system();exec();popen();函数
- LDD chapter3:write的奥秘-linux系统调用如何实现?
- 如何来实现一个Linux内核的系统调用(基于tiny4412开发板)
- (cc)2.6版本Linux上替换系统调用函数实现隐藏文件学习