实验内容:
设有⼆元函数 f(x, y) = f(x) + f(y)
其中:
f(x) = f(x-1) * x (x >1)
f(x) = 1 (x=1)
f(y) = f(y-1) + f(y-2) (y > 2)
f(y) = 1 (y=1, 2)
请编程建⽴ 3 个并发协作进程或线程,它们分别完成 f(x, y)、f(x)、f(y)
线程间协作
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int pipe1[2],pipe2[2];
pthread_t thrd1, thrd2,thrd3;
void task1(int *x)
{
printf("task1\n");
int fx=1;
for(int i=1;i<=*x;i++)fx*=i;
write(pipe1[1], &fx, sizeof(int));
printf("thread1 write %d to thread3\n",fx);
close(pipe1[1]);
}
int solvey(int x)
{
if(x==1||x==2)return 1;
return solvey(x-1)+solvey(x-2);
}
void task2(int *y)
{
printf("task2\n");
int fy=solvey(*y);
write(pipe2[1], &fy, sizeof(int));
printf("thread2 write %d to thread3\n",fy);
close(pipe2[1]);
}
void task3(int *num)
{
printf("task3\n");
int fx,fy;
int r1=0,r2=0;
do{
read(pipe1[0], &fx, sizeof(int));
printf("thread3 read from thread1: %d\n", fx); r1=1;
read(pipe2[0], &fy, sizeof(int));
printf("thread3 read from thread2: %d\n", fy); r2=1;
}while(r1==0||r2==0);
int fxy=fx+fy;
printf("f(x,y)= %d\n",fxy);
close(pipe1[0]);
close(pipe2[0]);
}
int main()
{
int ret,x,y;
// 使⽤ pipe() 系统调⽤建⽴两个⽆名管道。建⽴不成功程序退出,执⾏终⽌
if (pipe(pipe1) < 0)
{
perror("pipe1 not create");
exit(EXIT_FAILURE);
}
if (pipe(pipe2) < 0)
{
perror("pipe2 not create");
exit(EXIT_FAILURE);
}
//printf("");
scanf("%d%d",&x,&y);
//创建三个线程,分别完成f(x),f(y),f(xy)
ret = pthread_create(&thrd1, NULL, (void *) task1, (void *) &x);
if (ret) {
perror("pthread_create: task1");
exit(EXIT_FAILURE);
}
ret = pthread_create(&thrd2, NULL, (void *) task2, (void *) &y);
if (ret) {
perror("pthread_create: task2");
exit(EXIT_FAILURE);
}
int num3=3;
ret = pthread_create(&thrd3, NULL, (void *) task3, (void *) &num3);
if (ret) {
perror("pthread_create: task2");
exit(EXIT_FAILURE);
}
pthread_join(thrd3, NULL);
pthread_join(thrd2, NULL);
pthread_join(thrd1, NULL);
exit(EXIT_SUCCESS);
}
进程间协作:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pid1,pid2;
//创建四个无名管道用来父进程和两个子进程之间通信
int pipe11[2],pipe12[2],pipe21[2],pipe22[2];
if(pipe(pipe11)<0)
{
perror("pipe not create");
exit(EXIT_FAILURE);
}
if(pipe(pipe12)<0)
{
perror("pipe not create");
exit(EXIT_FAILURE);
}
if(pipe(pipe21)<0)
{
perror("pipe not create");
exit(EXIT_FAILURE);
}
if(pipe(pipe22)<0)
{
perror("pipe not create");
exit(EXIT_FAILURE);
}
//创建两个子进程,分别完成f(x),f(y).父进程完成f(xy)
pid1=fork();
if(pid1<0)
{
perror("process not create");
exit(EXIT_FAILURE);
}
else if(pid1==0)
{
int x,fx=0;
do{
read(pipe11[0],&x,sizeof(int));
printf("child%d read %d from father%d\n",getpid(),x,getppid());
fx=1;
for(int i=1;i<=x;i++)fx*=i;
write(pipe12[1], &fx, sizeof(int));
}while(fx==0);
return 0;
}
pid2=fork();
if(pid2<0)
{
perror("process not create");
exit(EXIT_FAILURE);
}
else if(pid2==0)
{
int y,fy=0;
do{
read(pipe21[0],&y,sizeof(int));
printf("child%d read %d from father%d\n",getpid(),y,getppid());
int f1=1,f2=1;
if(y==1||y==2)fy=1;
else{
for(int i=3;i<=y;i++)
{
fy=f1+f2;
f1=f2;
f2=fy;
}
}
write(pipe22[1],&fy,sizeof(int));
}while(fy==0);
return 0;
}
//printf("%d\n",pid2);
int x,y;
scanf("%d %d",&x,&y);
int r1=0,r2=0;
do{
int fx,fy;
write(pipe11[1],&x,sizeof(int));
write(pipe21[1],&y,sizeof(int));
read(pipe12[0],&fx,sizeof(int));
printf("father read %d from child1\n",fx); r1=1;
read(pipe22[0],&fy,sizeof(int));
printf("father read %d from child2\n",fy); r2=1;
int fxy=fx+fy;
printf("f(%d,%d)= %d\n",x,y,fxy);
}while(r1==0||r2==0);
return EXIT_SUCCESS;
}