我在寫一個僞shell程序,它從父進程創建一個子進程。父進程在繼續之前等待孩子完成。我的分叉代碼看起來是這樣的:C - 僞shell和併發
if(fork()==0)
{
execvp(cmd, args);
}
else
{
int status = 0;
wait(&status);
printf("Child exited with status of %d\n", status);
}
但是,我希望能夠解析「&」操作,這樣如果用戶在命令行的末尾進入它,父母也不會在提示用戶輸入其他命令之前等待孩子。我添加了一些代碼來處理這個問題,但我不確定它是否正確。它看起來是這樣的:
if(fork()==0)
{
execvp(cmd, args);
}
else
{
if(andOp = 1) //boolean to keep track of if '&' was encountered in parsing
{
shellFunc(); //main function to prompt user for a cmd line
}
int status = 0;
wait(&status);
printf("Child exited with status of %d\n", status);
}
這是否真的實現了由常規shell實現的併發性?