日期:2014-05-16  浏览次数:20658 次

关于shell的一道题目
写一个类似于下面代码的shell,但是包含足够的实际工作的代码,这样读者可以测试它,读者还可以增加某些功能,如输入输出重定向,管道以及后台作业等。

#define TRUE 1

while(TRUE){
    type_prompt();
    read_command(command,parameters);
    if(fork()!=0){
       waitpid(-1,&status,0);
    }
    else{
       execve(command,parameters,0);
    }
}

题目出自《现代操作系统》第三版
从未接触过这类题目,无从下手,希望大神们写出结果的同时能给出一些指导,谢谢!
------解决方案--------------------
how about this :

#!/bin/bash
while true
do
  read -p "[localhost $PWD]$ " cmd
  if [ "$cmd" = "end" ]
  then
    exit
  fi
  eval "$cmd"
done

------解决方案--------------------
去看看perl吧,shell能干的perl能干得更好,也更好控制。
------解决方案--------------------
大致功能应该是在shell当中模拟fork,exec和waitpid的操作。
#!/bin/bash

while true
do
read -p "Input command to execute: " cmd
(sleep 3; $cmd) &
pid=$!
while ps -ef 
------解决方案--------------------
grep $pid 
------解决方案--------------------
grep -v grep >/dev/null
do
echo "wait child process to be terminated"
sleep 1
done
wait $pid
status=$?
echo "The exit status of the child process is $status"
done