Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322271
  • 博文数量: 69
  • 博客积分: 2090
  • 博客等级: 大尉
  • 技术积分: 708
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-23 09:31
文章分类

全部博文(69)

文章存档

2012年(1)

2011年(4)

2010年(48)

2009年(14)

2008年(2)

我的朋友

分类: C/C++

2010-08-20 10:52:56

linux C中为我们提供了调用shell命令的函数--system

system函数调用/bin/sh  执行特定的shell命令,阻塞当前的进程知道shell命令执行完毕。
#include
int system(const char *command);

执行system实际上是调用了fork函数(产生新进程)、exec函数(在新进程中执行新任务)、waitpid函数(等待新进程结束)。

system函数举例



#include <stdio.h>
#include <stdlib.h>

int main(int argc,char **argv)
{
    int ret;
    
    printf("当前进程的进程号为%d\n",getpid());
    ret = system("lfs -l");  //调用shell命令 ls -l
    printf("ret = %d\n",ret);
    return 0;
}


阅读(5585) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~