linux
来源:互联网 发布:淘宝男风时尚生活假货 编辑:程序博客网 时间:2024/06/05 04:26
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
int main()
{
int fd[2];
int nbytes;
char readbuffer1[100];
char readbuffer2[100];
int st1, st2;
int *write_fd=&fd[1];
int *read_fd=&fd[0];
int result=-1;
result=pipe(fd);
if(result==-1)
{
printf("fail to create pipe\n");
return -1;
}
pid_t p1=fork();
if(-1==p1)
{
printf("fail to fork\n");
return -1;
}
if(0==p1)
{
char string1[]="Child process 1 is sending a message!";
close(*read_fd);
result=write(*write_fd,string1,strlen(string1));
return 0;
}
//else
//{
waitpid(p1,&st1,0);
close(*write_fd);
nbytes=read(*read_fd,readbuffer1,sizeof(readbuffer1));
printf("%s \n",readbuffer1);
//}
waitpid(p1,&st1,0);
pid_t p2=fork();
if(-1==p2)
{
printf("fail to fork\n");
return -1;
}
if(0==p2)
{
char string2[]="Child process 2 is sending a message!";
close(*read_fd);
result=write(*write_fd,string2,strlen(string2));
return 0;
}
//else
//{
waitpid(p2,&st2,0);
close(*write_fd);
nbytes=read(*read_fd,readbuffer2,sizeof(readbuffer2));
printf("%s \n",readbuffer2);
//}
printf("end!\n");
return 0;
}
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
int main()
{
int fd[2];
int nbytes;
char readbuffer1[100];
char readbuffer2[100];
int st1, st2;
int *write_fd=&fd[1];
int *read_fd=&fd[0];
int result=-1;
result=pipe(fd);
if(result==-1)
{
printf("fail to create pipe\n");
return -1;
}
pid_t p1=fork();
if(-1==p1)
{
printf("fail to fork\n");
return -1;
}
if(0==p1)
{
char string1[]="Child process 1 is sending a message!";
close(*read_fd);
result=write(*write_fd,string1,strlen(string1));
return 0;
}
//else
//{
waitpid(p1,&st1,0);
close(*write_fd);
nbytes=read(*read_fd,readbuffer1,sizeof(readbuffer1));
printf("%s \n",readbuffer1);
//}
waitpid(p1,&st1,0);
pid_t p2=fork();
if(-1==p2)
{
printf("fail to fork\n");
return -1;
}
if(0==p2)
{
char string2[]="Child process 2 is sending a message!";
close(*read_fd);
result=write(*write_fd,string2,strlen(string2));
return 0;
}
//else
//{
waitpid(p2,&st2,0);
close(*write_fd);
nbytes=read(*read_fd,readbuffer2,sizeof(readbuffer2));
printf("%s \n",readbuffer2);
//}
printf("end!\n");
return 0;
}
0 0
- linux
- linux
- Linux
- Linux
- LINUX!
- Linux
- linux
- linux
- linux
- Linux
- linux
- linux
- linux
- linux
- Linux
- linux
- linux
- Linux
- Java多线程编程4--Lock的实例--顺序打印
- LeetCode 71. Simplify Path
- getRequestDispatcher()用法介绍
- per_cpu机制的详解
- Git的纯命令操作,Install,Clone , Commit,Push,Pull,版本回退,撤销更新,分支的创建/切换/更新/提交/合并,代码冲突
- linux
- 3001
- Linux大文件的切割与合并
- Jenkins——应用篇——插件使用——Extended E-mail Notification Plugin
- 前端跨域技术之跨文档消息传输
- 深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件
- Spring加载resource时classpath*:与classpath:的区别
- 二叉苹果树 树形DP
- VS全局变量的修改