实现2个终端之间的互相聊天 要求:千万不要做出来2个终端之间的消息发送是一读一写的,一定要能够做到,一个终端发送n条消息,另一个终端一条消息都不回复都是没有问题的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
//读数据
char myfifo[32]="./myfifo";
int res=access(myfifo,F_OK);
if(res==-1)
{
mkfifo(myfifo,0666);
}
int rea=fork();
if(rea>0)
{
int fd=open(myfifo,O_RDONLY);
char buf[128]={0};
int len=0;
while(1)
{
memset(buf,0,len);
len=read(fd,buf,128);
printf("收到的数据位:%128s\n",buf);
}
close(fd);
}
else if(rea==0)
{
int fd2=open(myfifo,O_WRONLY|O_TRUNC);
char buf[128]={0};
int len=0;
while(1)
{
memset(buf,0,len);
printf("请输入要发送的数据:\n");
scanf("%128s",buf);
while(getchar()!=10);
len=strlen(buf);
write(fd2,buf,len);
}
close(fd2);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
//写数据
char myfifo[32]="./myfifo";
int res=access(myfifo,F_OK);
if(res==-1)
{
mkfifo(myfifo,0666);
}
int rea=fork();
if(rea>0)
{
int fd=open(myfifo,O_WRONLY|O_TRUNC);
char buf[128]={0};
int len=0;
while(1)
{
memset(buf,0,len);
printf("请输入要发送的数据:\n");
scanf("%128s",buf);
while(getchar()!=10);
len=strlen(buf);
write(fd,buf,len);
}
close(fd);
}
else if(rea==0)
{
int fd2=open(myfifo,O_RDONLY);
char buf[128]={0};
int len=0;
while(1)
{
memset(buf,0,len);
len=read(fd2,buf,128);
printf("接到的数据为:%128s\n",buf);
}
close(fd2);
}
return 0;
}
2:再把计算长方形 三角形面积的题,重新写一遍
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
int pipefd[2]={0};
pipe(pipefd);
int res=fork();
if(res>0)
{
while(1)
{
double data[3]={0};
printf("请输入三角形的三个边或者是长方形的长和宽:");
scanf("%lf %lf %lf",data,data+1,data+2);
while(getchar()!=10);
write(pipefd[1],data,24);
sleep(1);
}
}
else if(res==0)
{
//z准备一个字符数组,用来
char rfd[4]={0};
sprintf(rfd,"%d",pipefd[0]);
execl("./4","4",rfd,NULL);
perror("execl");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <math.h>
int main(int argc, const char *argv[])
{
double data[3]={0};
double s=0;
int rfd=atoi(argv[1]);
while(1)
{
read(rfd,data,24);
if(data[2]==0)
{
s=data[0]*data[1];
printf("长方形的面积为%g\n",s);
}
else
{
double a=data[0];
double b=data[1];
double c=data[2];
double p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
printf("三角形的面积为:%g\n",s);
}
}
return 0;
}