这是一个程序 system.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("./client &");
printf("WE are in main program\n");
printf("WE are in main program\n");
printf("WE are in main program\n");
printf("WE are in main program\n");
printf("WE are in main program\n");
return 0;
}
这是 client.c
#include <stdio.h>
int main()
{
printf("We are in background process\n");
do {
//doing some process on message queue to receive messages
} while (1);
return 0;
}
如何获得输出 ./ system 从linux终端运行,如下所示
We are in background process
WE are in main program
WE are in main program
WE are in main program
WE are in main program
WE are in main program
任何人都可以更清晰地解释一下吗?
答案 0 :(得分:0)
也许看看这个并激励自己。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid;
int status;
pid = fork();
if (pid == -1)
{
// handle error
}
if (pid == 0)
{
// exec child process
execl("./client", "client", NULL);
}
else
{
wait(&status);
}
printf("WE are in main program\n");
printf("WE are in main program\n");
printf("WE are in main program\n");
printf("WE are in main program\n");
printf("WE are in main program\n");
return 0;
}