0
void execute(char **argv,int num)
{
int i;
pid_t pid;
int status;
int child_status;
if ((pid = fork()) < 0)
{ /* fork a child process*/
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0)
{ /* for the child process: */
int c;
if (c==execvp(argv[0], argv) < 0)
{ /* execute the command */
printf("%d\n", c);
printf("*** ERROR: exec failed\n");
perror(" ");
exit(1);
}
}
else if(bg!=1){
while (waitpid(pid,&status,WNOHANG|WUNTRACED));
}
else if(bg==1)
{
wait (&child_status);
if (WIFEXITED (child_status))
{
printf("the child process exited normally, with exit code %d\n",
WEXITSTATUS (child_status));
}
else
printf ("the child process exited abnormally\n");
}
}
これは私のカスタムシェルでの実行機能です。 gedit &
のようなものを実行すると、次のプロンプトが表示される前に終了ステータスが出力されます。これをどうやって解決するのですか? 私は何が間違っていますか?プロセスの終了ステータスは、終了する前に出力されます。
"以前に印刷されました"とはどういう意味ですか?後に印刷しますか? –
子プロセスが終了すると、終了ステータスが正しく表示されますか? geditのように&バックグラウンドでテキストエディタを開きます。そのプロセスを閉じるをクリックすると、終了ステータスが表示されます。その前にプロセスはまだ実行中です。 –
すでに実行中のgeditインスタンスがあり、それを再度実行すると、新しいインスタンスがすぐに終了します。多くのGUIプログラムがこれを行いますので、GUI以外のものを試してみてください。 – redneb