发布网友 发布时间:2022-04-27 04:34
共3个回答
热心网友 时间:2022-06-26 07:20
实验6 Linux进程并发程序设计
1 实验目的:
掌握Linux环境下的进程并发程序及管道应用程序的编写要点。
2 实验内容和实验步骤:
(1) 调试并运行3.10的并发程序设计实例,显示结果是什么,并分析之。
通过pipeline.c这个文件,调用child1.c和father1.c这两个程序,father1.c写入管理,然后再通过child1.c读管道,因此输出为以上结果。
(2) 编写一个并发程序,父进程打印“The Parent is running”;子进程打印“The Child is running”;
#include<stdio.h>
#include<unistd.h>
main()
{
int p1;
while((p1=fork())==-1);
if(p1>0)
{
wait(0);
printf("The Parent is running.\n");
}
else
{
printf("The Child is running.\n");
exit(0);
}
}
结果为:
The Child is running.
The Parent is running.
(3) 编写一个管道应用程序,父进程通过管道提供字符串“put the string into the pipe.”给子进程,子进程通过管道接收这条信息,然后打印输出。
#include<stdio.h>
#include<unistd.h>
main()
{
int p1,fd[2];
char outpipe[50]; //定义读缓冲区
char inpipe[50]="put the string into the pipe."; //定义写缓冲区
pipe(fd); //创建无名管道fd
while((p1=fork())==-1);
if (p1>0) //父进程返回
{
write(fd[1],inpipe,50); //写信息到管道
wait(0);
}
else //子进程返回
{
read(fd[0],outpipe,50); //从管道读信息到读缓冲区
printf("%s\n",outpipe); //显示读到的信息
exit(0);
}
}
结果为:
put the string into the pipe.
3.实验要求:写出实验报告并将结果上传到FTP SERVER上自己的作业目录。
热心网友 时间:2022-06-26 07:20
自己找本并行编程或者多线程书看看 里面会有很多的问题 成功的程序写不出来问题还不好找吗
热心网友 时间:2022-06-26 07:21
这个很好写的,写一下实训内容,在加点自己的感受,就OK了