Linux下用C语言编写一个应用程序,命名为b。这个程序不断地输出如下行:Those output come from child,[系统时间] 。另外写一个应用程序,命名为a。这个程序创建一个子进程,执行b。
时间: 2024-02-18 11:05:54 浏览: 83
好的,我来回答你的问题。
首先,我们需要在Linux下使用C语言编写应用程序b。下面是一个简单的示例:
```c
#include <stdio.h>
#include <time.h>
int main() {
while (1) {
time_t t = time(NULL);
printf("Those output come from child,[%s]\n", ctime(&t));
sleep(1); // 每隔1秒输出一次
}
return 0;
}
```
这个程序会不断地输出类似于"Those output come from child,[系统时间]"的信息,并且每隔1秒输出一次。接下来,我们需要编写应用程序a来创建子进程并执行b。
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork(); // 创建子进程
if (pid == 0) {
// 子进程执行应用程序b
execl("./b", "b", NULL);
exit(0); // 子进程执行完毕后退出
} else if (pid > 0) {
// 父进程等待子进程执行完毕
waitpid(pid, NULL, 0);
} else {
printf("Failed to create child process!\n");
exit(1);
}
return 0;
}
```
这个程序会创建一个子进程,并在子进程中执行应用程序b。父进程会等待子进程执行完毕后退出。注意,需要把应用程序b的可执行文件命名为"b"并放在同一目录下。
希望这个回答能够解决你的问题!