日期:2014-05-16 浏览次数:20860 次
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 4096
int main(int argc, char* argv[])
{
    char  str[MAXLINE];
    pid_t pid;
    
    while(1)
    {
        putchar('%');
        gets(str);
        
        if (str == NULL)
            return (1);
        
        if (strcmp(str, "q") == 0 || strcmp(str, "Q") == 0)
            break;
        if ((pid = fork()) < 0)
        {
            perror("fork error");
        }
        else if (pid == 0)
        {
            if (execl("/bin/sh", "sh", "-c", str, (char *)0) < 0)
            { 
                perror("execl error");
                continue ;
            }
        }
        else  {
            if(waitpid(pid, 0, 0) < 0)
            {
                perror("waitpid error");
                return 1;
            }
        }
    }
    return 0;
}
------解决方案--------------------