设计两个程序,一个hello程序,输出”Hello, I am process 进程id”;另一程序获得hello程序的输出结果并显示。可用pipe或popen和pclose函数实现。
3_7_A:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
int main()
{
printf("I am process %d.", getpid());
return 0;
}
3_7_B:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
int main()
{
FILE* fp;
fp = popen("./E3_7_A", "r");
char buf[30];
if(fp != NULL)
{
puts("Congratulations!\n");
fgets(buf, sizeof(buf), fp);
puts(buf);
}
else
{
puts("Failure.\n");
}
return 0;
}