分享

linux execlp

 张岩峰 2011-12-12

//实现从命令行输入命令,然后执行

//
#include<string.h>
#include<sys/wait.h>
#include<unistd.h>
#define MAX_LINE 1024
int main()
{
char buf[ MAX_LINE ] ;
pid_t pid ;
int status ;
printf( "%%" ) ;// printf prompt % need %%
while( fgets( buf , MAX_LINE , stdin ) != NULL )
{
buf[ strlen( buf ) - 1 ] = 0 ;
if( ( pid = fork() ) < 0 )
{
printf( " fork error.\n" ) ;
}
else if( pid == 0 )
{
// 子进程
// 执行程序
execlp( buf , buf , ( char * ) 0) ; // 执行程序
printf( "%s" , buf ) ;
return 1 ;
}
// parent 进程,等待子进程进程结束
if( ( pid = waitpid( pid , &status , 0 ) ) < 0 )
{
printf( "waitpid error.\n" ) ;
}
printf( "%%" ) ;
}
return 0 ;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多