关于linux进程间的通信问题的一道编程题

2024-12-05 08:25:47
推荐回答(2个)
回答1:

#include
#include
int main()
{
int childpid = 0;
int retpid = 0;
int status = 0;

childpid= fork();
if(childpid < 0)
{
printf("fail\n");
}
else if(childpid == 0)
{
printf("son\n");
}
else
{
printf("father");

retpid = waitpid(childpid,&status,0);
if(retpid == childpid)
{
printf("son finished, ready to start father...\n");
}
}

return 0;
}

回答2:

这个不是进程间通信吧……
//编译:gcc -o proc proc.c -lm

#include
#include
#include
#include
int main(int argc, char **argv)
{
void do_something();
int id=0, status=0;

printf("Parent process running.\n");
if ((id=fork())==-1)
{
fprintf(stderr, "Fork error: %s\a\n", strerror(errno));
exit(1);
}
if (id==0)
{
printf("Child process running, doing some calculations.\n");
do_something();
return 0;
}
else
{
wait(&status);
printf("Child %ld terminated.\n", id);
return 0;
}
}

void do_something()
{
double n=1;
while (n<1E6)
asin(n++);
}