创建A B进程,要求:
a.A先发一句话给B,B接受后打印在终端上
b.在A要求后,B发送回一句话给A,A接受后打印在终端上
c.重复a.b.步骤,直到接受或者发送quit后,结束A B两线程
代码:
a线程:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int read_from_pipe(int fd,char *buf,int *flag){
bzero(buf,sizeof(buf));
*flag = 1;
int res = read(fd,buf,sizeof(buf));
if (res < 0){
perror("read");
return -1;
}else if(res == 0){
printf("nothing to read\n");
return -1;
}else{
printf("%s\n",buf);
}
}
void write_from_pipe(int fd,char *buf,int *flag){
*flag = 0;
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf) - 1,stdin);
buf[strlen(buf) - 1] = 0;
int res = write(fd,buf,strlen(buf));
}
int main(int argc, const char *argv[])
{
umask(0);
mkfifo("./commu1",0777);
mkfifo("./commu2",0777);
int fd[2];
fd[0] = open("./commu1",O_RDONLY);
fd[1] = open("./commu2",O_WRONLY);
if(fd[0] < 0 || fd[1] < 0){
perror("open");
return -1;
}
char buf[128] = "";
ssize_t res = 0;
int flag = 1;
while(1){
if(flag == 0){
int res = read_from_pipe(fd[0],buf,&flag);
if(res == -1){
break;
}
}else if(flag == 1){
write_from_pipe(fd[1],buf,&flag);
}
if(strcasecmp(buf,"quit") == 0){
break;
}
}
close(fd[0]);
close(fd[1]);
return 0;
}
b进程:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int read_from_pipe(int fd,char *buf,int *flag){
bzero(buf,sizeof(buf));
*flag = 1;
int res = read(fd,buf,sizeof(buf));
if (res < 0){
perror("read");
return -1;
}else if(res == 0){
printf("nothing to read\n");
return -1;
}else{
printf("%s\n",buf);
}
}
void write_from_pipe(int fd,char *buf,int *flag){
*flag = 0;
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf) - 1,stdin);
buf[strlen(buf) - 1] = 0;
int res = write(fd,buf,strlen(buf));
}
int main(int argc, const char *argv[])
{
umask(0);
mkfifo("./commu1",0777);
mkfifo("./commu2",0777);
int fd[2];
fd[0] = open("./commu1",O_WRONLY);
fd[1] = open("./commu2",O_RDONLY);
if(fd[0] < 0 || fd[1] < 0){
perror("open");
return -1;
}
char buf[128] = "";
ssize_t res = 0;
int flag = 0;
while(1){
if(flag == 0){
int res = read_from_pipe(fd[1],buf,&flag);
if(res == -1){
break;
}
}else if(flag == 1){
write_from_pipe(fd[0],buf,&flag);
}
if(strcasecmp(buf,"quit") == 0){
break;
}
}
close(fd[0]);
close(fd[1]);
return 0;
}
任务2:两个进程间自由通信:
进程a:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
struct info{
int fd[2];
char buf[128];
int flag;
pthread_t tid1,tid2;
};
int read_from_pipe(int fd,char *buf,int *flag){
bzero(buf,sizeof(buf));
//*flag = 1;
int res = read(fd,buf,sizeof(buf));
if (res < 0){
perror("read");
return -1;
}else if(res == 0){
return -1;
}else{
printf("%s\n",buf);
}
}
int write_from_pipe(int fd,char *buf,int *flag){
//*flag = 0;
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf) - 1,stdin);
buf[strlen(buf) - 1] = 0;
int res = write(fd,buf,strlen(buf));
return 0;
}
void* read_thread(void *arg){
while(1){
int res = read_from_pipe(((struct info*)arg)->fd[0],((struct info*)arg)->buf,&((struct info*)arg)->flag);
if(res == -1 || strcasecmp(((struct info*)arg) -> buf,"quit") == 0){
break;
}
}
close(((struct info*)arg) -> fd[0]);
close(((struct info*)arg) -> fd[1]);
pthread_cancel(((struct info*)arg)->tid2);
pthread_exit(NULL);
}
void* write_thread(void *arg){
while(1){
int res = write_from_pipe(((struct info*)arg)->fd[1],((struct info*)arg)->buf,&((struct info*)arg)->flag);
if(res == -1 || strcasecmp(((struct info*)arg) -> buf,"quit") == 0){
break;
}
}
close(((struct info*)arg) -> fd[0]);
close(((struct info*)arg) -> fd[1]);
pthread_cancel(((struct info*)arg)->tid1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
mkfifo("./commu1",0777);
mkfifo("./commu2",0777);
struct info myInfo;
myInfo.fd[0] = open("./commu1",O_RDONLY);
printf("Fifo1 opened on a\n");
myInfo.fd[1] = open("./commu2",O_WRONLY);
printf("Fifo2 opened on a\n");
strcpy(myInfo.buf,"");
myInfo.flag = 0;
if(myInfo.fd[0] < 0 || myInfo.fd[1] < 0){
perror("open");
return -1;
}
if(pthread_create(&myInfo.tid1,NULL,read_thread,&myInfo) != 0){
printf("thread creation failed\n");
return -1;
}
if(pthread_create(&myInfo.tid2,NULL,write_thread,&myInfo) != 0){
printf("thread creation failed\n");
return -1;
}
pthread_join(myInfo.tid1,NULL);
pthread_join(myInfo.tid2,NULL);
return 0;
}
进程b:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
struct info{
int fd[2];
char buf[128];
int flag;
pthread_t tid1,tid2;
};
int read_from_pipe(int fd,char *buf,int *flag){
bzero(buf,sizeof(buf));
//*flag = 1;
int res = read(fd,buf,sizeof(buf));
if (res < 0){
perror("read");
return -1;
}else if(res == 0){
return -1;
}else{
printf("%s\n",buf);
}
}
int write_from_pipe(int fd,char *buf,int *flag){
//*flag = 0;
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf) - 1,stdin);
buf[strlen(buf) - 1] = 0;
int res = write(fd,buf,strlen(buf));
return 0;
}
void* read_thread(void *arg){
while(1){
int res = read_from_pipe(((struct info*)arg)->fd[1],((struct info*)arg)->buf,&((struct info*)arg)->flag);
if(res == -1 || strcasecmp(((struct info*)arg)->buf,"quit") == 0){
break;
}
}
close(((struct info*)arg) -> fd[0]);
close(((struct info*)arg) -> fd[1]);
pthread_cancel(((struct info*)arg)->tid2);
pthread_exit(NULL);
}
void* write_thread(void *arg){
while(1){
int res = write_from_pipe(((struct info*)arg)->fd[0],((struct info*)arg)->buf,&((struct info*)arg)->flag);
if(res == -1 || strcasecmp(((struct info*)arg)->buf,"quit") == 0){
break;
}
}
close(((struct info*)arg) -> fd[0]);
close(((struct info*)arg) -> fd[1]);
pthread_cancel(((struct info*)arg)->tid1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
mkfifo("./commu1",0777);
mkfifo("./commu2",0777);
struct info myInfo;
myInfo.fd[0] = open("./commu1",O_WRONLY);
printf("Fifo1 opened on b\n");
myInfo.fd[1] = open("./commu2",O_RDONLY);
printf("Fifo2 opened on b\n");
strcpy(myInfo.buf,"");
myInfo.flag = 0;
if(myInfo.fd[0] < 0 || myInfo.fd[1] < 0){
perror("open");
return -1;
}
if(pthread_create(&myInfo.tid1,NULL,read_thread,&myInfo) != 0){
printf("thread creation failed\n");
return -1;
}
if(pthread_create(&myInfo.tid2,NULL,write_thread,&myInfo) != 0){
printf("thread creation failed\n");
return -1;
}
pthread_join(myInfo.tid1,NULL);
pthread_join(myInfo.tid2,NULL);
return 0;
}