C でバックグラウンドとフォアグラウンドのプロセスをエミュレートしようとしています。最後に「&」記号がある場合は、親プロセスでその子プロセスを待つことを避けています。また、実行するすべてのバックグラウンド コマンドをリストに保存し、完了したらリストから削除しようとします。ただし、ls -l& の場合、出力はすぐに表示され、Enter キーを押すだけでプロセスが終了します。そのプロセスIDをキャッチして、リスト内の既存のpidと一致する場合にリストから削除する方法。
pid = fork();
//Code to add pid into a list if it is a background process
//this is done by parent as pid is always 0 for child processes
if(pid == 0){
if(proc_state=='&'){
setsid();
}
// logic for creating command
int ret= execvp( subcomm[0], subcomm );
// handling error
}
//Parent will execute this
//Child will never come here if execvp executed successfully
if(proc_sate != '&'){
for(i=0; i < count_pipe+1; i++){
int ret = waitpid(0, &flag ,0);
// Code to remove procid from list if 'ret' matches with existing procid in the list.
}
}
//Here proc_state just determines whether it is background or foreground.It is just a character. count_pipe is just a
//variable holding number of pipes
私がはっきりしていることを願っています。不明な点があれば質問してください