1、要求实现AB进程对话
a、A进程先发送一句话给B进程,B进程接收后打印
b、B进程再回复一句话给A进程,A进程接收后打印
c、重复1.2步骤,当收到quit后,要结束AB进程
文件A代码。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
int main(int argc, char const *argv[])
{
int ff = mkfifo("./test.txt", 0666);
if (ff < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo create success\n");
int ff1 = mkfifo("./test1.txt", 0666);
if (ff1 < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo1 create success\n");
int op = open("./test.txt", O_RDWR);
if (op < 0)
{
perror("open");
return -1;
}
int op1= open("./test1.txt", O_RDONLY);
if (op1<0)
{
perror("open");
return -1;
}
char buf[36];
while (1)
{
printf("A线程写入数据是:");
fgets(buf, sizeof(buf), stdin);
if (write(op, buf, sizeof(buf)) < 0)
{
perror("write");
return -1;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
ssize_t res = read(op1, buf, sizeof(buf));
if (res < 0)
{
perror("read");
return -1;
}
if (res == 0)
{
printf("file read end or file is empty\n");
return -1;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
printf("A线程读出数据是:");
printf("%s\n", buf);
}
close(op);
close(op1);
return 0;
}
文件B代码。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
int main(int argc, char const *argv[])
{
int ff = mkfifo("./test.txt", 0666);
if (ff < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo create success\n");
int ff1 = mkfifo("./test1.txt", 0666);
if (ff1 < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo1 create success\n");
int op = open("./test.txt", O_RDONLY);
if (op < 0)
{
perror("open");
return -1;
}
int op1 = open("./test1.txt", O_RDWR);
if (op1 < 0)
{
perror("open");
return -1;
}
char buf[36];
while (1)
{
ssize_t res = read(op, buf, sizeof(buf));
if (res < 0)
{
perror("write");
return -1;
}
if (res == 0)
{
printf("file read end or file is empty\n");
return -1;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
printf("B线程读出数据是:");
printf("%s\n", buf);
bzero(buf, sizeof(buf));
printf("B线程写入数据是:");
fgets(buf, sizeof(buf), stdin);
if (write(op1, buf, sizeof(buf)) < 0)
{
perror("write");
return -1;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
}
close(op);
close(op1);
return 0;
}
2、在上述练习基础上实现AB进程对话,要求AB进程能够随时收发。
a、提示:多进程,多线程。
第一个文件代码。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include<pthread.h>
void *hang(void *arg)
{
int op = open("./test.txt", O_RDONLY);
if (op < 0)
{
perror("open");
return NULL;
}
char buf[36];
while (1)
{
ssize_t res = read(op, buf, sizeof(buf));
if (res < 0)
{
perror("read");
return NULL;
}
if (res == 0)
{
printf("file read end or file is empty\n");
return NULL;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
printf("B线程读出数据是:");
printf("%s\n", buf);
}
close(op);
pthread_cancel(*(pthread_t *)arg);
pthread_exit(NULL);
}
void *yi(void *arg)
{
int op = open("./test1.txt", O_WRONLY);
if (op < 0)
{
perror("open");
return NULL;
}
char buf[36];
while (1)
{
bzero(buf, sizeof(buf));
printf("B线程写入数据是:");
fgets(buf, sizeof(buf), stdin);
if (write(op, buf, sizeof(buf)) < 0)
{
perror("write");
return NULL;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
}
close(op);
pthread_cancel(*(pthread_t *)arg);
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
int ff = mkfifo("./test.txt", 0666);
if (ff < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo create success\n");
int ff1 = mkfifo("./test1.txt", 0666);
if (ff1 < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo1 create success\n");
pthread_t tid, tid1;
if (pthread_create(&tid, NULL, hang, &tid1) != 0)
{
printf("tread create fail\n");
return -1;
}
if (pthread_create(&tid1, NULL, yi, &tid) != 0)
{
printf("tread create fail\n");
return -1;
}
pthread_join(tid, NULL);
pthread_join(tid1, NULL);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include<pthread.h>
void *hang(void *arg)
{
int op = open("./test.txt", O_WRONLY);
if (op < 0)
{
perror("open");
return NULL;
}
char buf[36];
while (1)
{
printf("A线程写入数据是:");
fgets(buf, sizeof(buf), stdin);
if (write(op, buf, sizeof(buf)) < 0)
{
perror("write");
return NULL;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
}
close(op);
pthread_cancel(*(pthread_t *)arg);
pthread_exit(NULL);
}
void *yi(void *arg)
{
int op = open("./test1.txt", O_RDONLY);
if (op < 0)
{
perror("open");
return NULL;
}
char buf[36];
while (1)
{
ssize_t res = read(op, buf, sizeof(buf));
if (res < 0)
{
perror("read");
return NULL;
}
if (res == 0)
{
printf("file read end or file is empty\n");
return NULL;
}
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, "quit") == 0)
{
break;
}
printf("A线程读出数据是:");
printf("%s\n", buf);
}
close(op);
pthread_cancel(*(pthread_t *)arg);
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
int ff = mkfifo("./test.txt", 0666);
if (ff < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo create success\n");
int ff1 = mkfifo("./test1.txt", 0666);
if (ff1 < 0)
{
if (errno != 17)
{
perror("mkfifo");
}
}
printf("fifo1 create success\n");
pthread_t tid, tid1;
if (pthread_create(&tid, NULL, hang, &tid1) != 0)
{
printf("tread create fail\n");
return -1;
}
if (pthread_create(&tid1, NULL, yi, &tid) != 0)
{
printf("tread create fail\n");
return -1;
}
pthread_join(tid, NULL);
pthread_join(tid1, NULL);
return 0;
}