1:实现2个终端之间的互相聊天
要求:千万不要做出来2个终端之间的消息发送是一读一写的,一定要能够做到,一个终端发送n条消息,另一个终端一条消息都不回复都是没有问题的

2:有2个.c文件1.c,2.c
1.c的代码负责:从键盘输入三角形的三边长 或者 长方形的长和宽
2.c的代码负责:根据1.c 输入的数据,计算三角形 或者 长方形的面积


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
void *task(){
char myfifo[32]="./youfifo";
if( access(myfifo,F_OK)== -1){
mkfifo(myfifo,0666);
}
int fd = open(myfifo,O_WRONLY | O_TRUNC);
char buf[128]={0};
int len = 0;
while(1){
memset(buf,0,len);
printf("send:");
scanf("%128s",buf);
while(getchar()!=10);
len = strlen(buf);
write(fd,buf,len);
}
close(fd);
}
//可读进程
int main(int argc, const char *argv[])
{
char myfifo[32]="./myfifo";
pthread_t id;
pthread_create(&id,NULL,task,NULL);
pthread_detach(id);
if( access(myfifo,F_OK) == -1 ){
mkfifo(myfifo,0666);
}
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("message:%s\n",buf);
}
close(fd);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
void *task()
{
char myfifo[32]="./youfifo";
if( access(myfifo,F_OK) == -1 ){
mkfifo(myfifo,0666);
}
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("message:%s\n",buf);
}
close(fd);
}
int main(int argc, const char *argv[])
{
pthread_t id;
pthread_create(&id,NULL,task,NULL);
pthread_detach(id);
char myfifo[32]="./myfifo";
if( access(myfifo,F_OK)== -1){
mkfifo(myfifo,0666);
}
int fd = open(myfifo,O_WRONLY | O_TRUNC);
char buf[128]={0};
int len = 0;
while(1){
memset(buf,0,len);
printf("send:");
scanf("%128s",buf);
while(getchar()!=10);
len = strlen(buf);
write(fd,buf,len);
}
close(fd);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.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("请输入三角形或长方形的边长\n");
scanf("%lf %lf %lf",data,data+1,data+2);
while(getchar()!=10);
write(pipefd[1],data,24);
sleep(1);
}
}
else if(res == 0)
{
char rfd[4]={0};
sprintf(rfd,"%d",pipefd[0]);
execl("./7","7",rfd,NULL);
perror("execl");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.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.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("a=%g b=%g c=%g\n",a,b,c);
printf("三角形面积:%g\n",s);
}
}
return 0;
}
文章描述了如何使用C语言实现在两个终端间进行全双工通信,同时展示了一个例子,其中一个终端接收用户输入的三角形或长方形边长,另一个终端计算并显示面积。通过管道和多线程技术实现数据交换。
1191

被折叠的 条评论
为什么被折叠?



