您的位置:首页 > 编程语言

UNIX环境高级编程(第二版) 读书代码(1-5)

2006-10-03 21:35 323 查看
/*
* DESCRIPTION: process control that simulate a simple shell;
* three function : fork(), exec(), waitpid();
* author:liy
* DATE:8-23-2006
*/

#include "apue.h"
#include <sys/wait.h>
int main(void)
{
char buf[MAXLINE]; /* MAXLINE from apue.h */
pid_t pid;
int status;

puts("###########A simple shell##########");
printf("%%"); /* simulate a dos prompt */
while(fgets(buf, MAXLINE, stdin) != NULL){
if(buf[strlen(buf) - 1] == '/n'){
buf[strlen(buf) - 1] = 0; /* replace new line null */
}
if((pid = fork()) < 0){
err_sys("fork error");
}
else if(pid == 0){ /* child */
execlp(buf, buf, (char *)0);
err_ret("Could'n execute %s", buf);
exit(127);
}

/* parent */
if((pid = waitpid(pid, &status, 0)) < 0){
err_sys("watipid error");
}
printf("%%");
}
puts("bye ^_^");
exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: