自学内容网 自学内容网

嵌入式4-8

今日作业: 1:实现2个终端之间的互相聊天 要求:千万不要做出来2个终端之间的消息发送是一读一写的,一定要能够做到,一个终端发送n条消息,另一个终端一条消息都不回复都是没有问题的
2:再把计算长方形 三角形面积的题,重新写一遍

1、

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
int stop=0;
void sighandler(int signum)
{
if(signum==SIGPIPE)
{
printf("读端关闭\n");
exit(1);
}else if(signum==SIGTSTP)
{
stop=stop==1?0:1;
}
}
int main(int argc, const char *argv[])
{
signal(SIGPIPE,sighandler);
signal(SIGTSTP,sighandler);
int res=fork();
if(res>0)
{
char myRX[32] = "./myfifo1";
if(access(myRX,F_OK) == -1)
{
mkfifo(myRX,0666);
}
int fd = open(myRX,O_RDONLY);
char buf[128] = {0};
int len = 0;
while(1)
{
memset(buf,0,len);
len = read(fd,buf,128);
printf("对方消息:%s\n",buf);
}
close(fd);
}else if(res==0)
{
char myTX[32] = "./myfifo2";
if(access(myTX,F_OK)==-1)
{
mkfifo(myTX,0666);
}
int fd = open(myTX,O_WRONLY | O_TRUNC);
char buf[128] = {0};
int len = 0;
while(1)
{
while(stop);
memset(buf,0,len);
printf("发送内容:\n");
scanf("%128s",buf);
while(getchar()!=10);
len=strlen(buf);
write(fd,buf,len);
}
close(fd);
}else if(res<0)
{
perror("fork");
return -1;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
int stop=0;
void sighandler(int signum)
{
if(signum==SIGPIPE)
{
printf("读端关闭\n");
exit(1);
}else if(signum==SIGTSTP)
{
stop=stop==1?0:1;
}
}
int main(int argc, const char *argv[])
{
signal(SIGPIPE,sighandler);
signal(SIGTSTP,sighandler);
int res=fork();
if(res>0)
{
char myRX[32] = "./myfifo2";
if(access(myRX,F_OK) == -1)
{
mkfifo(myRX,0666);
}
int fd = open(myRX,O_RDONLY);
char buf[128] = {0};
int len = 0;
while(1)
{
memset(buf,0,len);
len = read(fd,buf,128);
printf("对方消息:%s\n",buf);
}
close(fd);
}else if(res==0)
{
char myTX[32] = "./myfifo1";
if(access(myTX,F_OK)==-1)
{
mkfifo(myTX,0666);
}
int fd = open(myTX,O_WRONLY | O_TRUNC);
char buf[128] = {0};
int len = 0;
while(1)
{
while(stop);
memset(buf,0,len);
printf("发送内容:\n");
scanf("%128s",buf);
while(getchar()!=10);
len=strlen(buf);
write(fd,buf,len);
}
close(fd);
}else if(res<0)
{
perror("fork");
return -1;
}
return 0;
}

2、

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
int main(int argc, const char *argv[])
{
int pipefd[2]={0};
int buf[3]={0};
if(pipe(pipefd)==-1){
perror("pipe");
return 1;
}
int res=fork();
if(res>0)
{
close(pipefd[0]);
while(1)
{
memset(buf,0,sizeof(buf));
printf("请输入边长:\n");
scanf("%d %d %d",&buf[0],&buf[1],&buf[2]);
write(pipefd[1],buf,sizeof(buf));
sleep(0.5);
}
}else if(res==0)
{
close(pipefd[1]);
char rfd[4]={0};
sprintf(rfd,"%d",pipefd[0]);
execl("./2","2",rfd,NULL);
perror("execl");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
int main(int argc, const char *argv[])
{ 
int rfd=atoi(argv[1]);
int buf[3]={0};
while(1)
{
read(rfd,buf,sizeof(buf));
if(buf[2]==0)
{
printf("长方形面积为:%d\n",buf[0]*buf[1]);
}else{
float p=(buf[0]+buf[1]+buf[2])/2;
printf("三角形面积为:%f\n",sqrt(p*(p-buf[0])*(p-buf[1])*(p-buf[2])));
}
}
return 0;
}


原文地址:https://blog.csdn.net/qq_30432963/article/details/137519998

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!