/*************************************************************************
> File Name: pro_2.c
> Author: wangweihua
> Mail: 1821895103@qq.com
> Created Time: 2020年10月8日 星期四 00时12分07秒
************************************************************************/
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd[2];
pid_t p1, p2, p3;
char buf[50], s[50];
pipe(fd);
while((p1 = fork()) < 0);
if(p1 == 0){
sprintf(buf, "This is son1\n");
write(fd[1], buf, 50);
exit(0);
}
else{
wait(0);
read(fd[0], s, 50);
printf("%s",s);
while((p2 = fork()) < 0);
if(p2 == 0){
sprintf(buf, "This is son2\n");
write(fd[1], buf, 50);
exit(0);
}
else{
wait(0);
read(fd[0], s, 50);
printf("%s",s);
while((p3 = fork()) < 0);
if(p3 == 0){
sprintf(buf, "This is son3\n");
write(fd[1], buf, 50);
exit(0);
}
else{
wait(0);
read(fd[0], s, 50);
printf("%s",s);
}
}
}
return 0;
}
/*************************************************************************
> File Name: pro1.c
> Author: wangweihua
> Mail: 1821895103@qq.com
> Created Time: 2020年10月09日 星期五 22时45分06秒
************************************************************************/
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<math.h>
int main()
{
int count = 0;
pid_t pid;
pid = fork();
if(pid < 0) printf("error in fork!\n");
else if(pid == 0){ //子
for(int i = 1; i <= 10; ++ i){
printf("*"); count ++;
}
printf("\n");
for(int i = 1; i <= 8; ++ i){
printf("*"); count ++;
for(int j = 2; j <= 9; ++ j) printf(" ");
printf("*\n"); count ++;
}
for(int i = 1; i <= 10; ++ i){
printf("*"); count ++;
}
printf("\n");
exit(0);
}
else{ //父
wait(0);
for(int i = 1; i <= 7; ++ i){
int t = fabs(4 - i);
for(int j = 1; j <= t; ++ j) printf(" ");
printf("*");
count ++;
if(5 >= t * 2){
for(int j = 1; j <= 5 - t * 2; ++ j){
printf(" ");
}
printf("*");
count ++;
}
printf("\n");
}
}
printf("count = %d\n",count);
return 0;
}