1.要求实现AB进程对话
A进程先发送一句话给B进程,B进程接收后打印
B进程再回复一句话给A进程,A进程接收后打印
重复1.2步骤,当收到quit后,要结束AB进程
msg1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
umask(0);
// 创建有名管道1
if (mkfifo("./myfifo1", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道1已存在\n");
}
else
{
printf("管道1创建成功\n");
}
// 创建有名管道2
if (mkfifo("./myfifo2", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道2已存在\n");
}
else
{
printf("管道2创建成功\n");
}
int fd1 = open("./myfifo1", O_WRONLY);
if (fd1 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道1成功(只写)\n");
}
int fd2 = open("./myfifo2", O_RDONLY);
if (fd2 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道2成功(只读)\n");
}
char buf[128] = "";
ssize_t res = 0;
while (1)
{
printf("请输入>>>\n");
fgets(buf, sizeof(buf), stdin);
fprintf(stderr,"等待回复...\n");
buf[strlen(buf) - 1] = 0;
if (write(fd1, buf, sizeof(buf)) < 0)
{
perror("write");
return -1;
}
// 读取管道
bzero(buf, sizeof(buf));
res = read(fd2, buf, sizeof(buf));
if (res < 0)
{
perror("read");
return -1;
}
else if (0 == res)
{
printf("写端退出\n");
break;
}
printf("MSG1:%s\n", buf);
if (strcasecmp(buf, "quit") == 0)
{
break;
}
}
close(fd1);
close(fd2);
return 0;
}
msg2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
umask(0);
// 创建有名管道1
if (mkfifo("./myfifo1", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道1已存在\n");
}
else
{
printf("管道1创建成功\n");
}
// 创建有名管道2
if (mkfifo("./myfifo2", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道2已存在\n");
}
else
{
printf("管道2创建成功\n");
}
int fd1 = open("./myfifo1", O_RDONLY);
if (fd1 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道1成功(只读)\n");
}
int fd2 = open("./myfifo2", O_WRONLY);
if (fd2 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道2成功(只写)\n");
}
char buf[128] = "";
ssize_t res = 0;
while (1)
{
bzero(buf, sizeof(buf));
res = read(fd1, buf, sizeof(buf));
if (res < 0)
{
perror("read");
return -1;
}
else if (0 == res)
{
printf("写端退出\n");
break;
}
printf("MSG2:%s\n", buf);
printf("请输入>>>\n");
fgets(buf, sizeof(buf), stdin);
fprintf(stderr, "等待回复...\n");
buf[strlen(buf) - 1] = 0;
if (write(fd2, buf, sizeof(buf)) < 0)
{
perror("write");
return -1;
}
if (strcasecmp(buf, "quit") == 0)
{
break;
}
}
close(fd1);
close(fd2);
return 0;
}
现象:
2. 捕获2 3 20号信号
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
void handler(int sig)
{
printf("触发了sig=%d信号\n", sig);
}
int main(int argc, const char *argv[])
{
// 捕获2
__sighandler_t s = signal(2, handler);
if (SIG_ERR == s)
{
perror("signal");
return -1;
}
// 捕获3
s = signal(3, handler);
if (SIG_ERR == s)
{
perror("signal");
return -1;
}
// 捕获20
s = signal(20, handler);
if (SIG_ERR == s)
{
perror("signal");
return -1;
}
while (1)
{
printf("等待信号...\n");
sleep(1);
}
return 0;
}
现象:
3. 附加题:在第1题的基础上,能够实现随时收发
msg1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
int fd1, fd2;
char buf[128] = "";
ssize_t res = 0;
int createfifo()
{
umask(0);
// 创建有名管道1
if (mkfifo("./myfifo1", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道1已存在\n");
}
else
{
printf("管道1创建成功\n");
}
// 创建有名管道2
if (mkfifo("./myfifo2", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道2已存在\n");
}
else
{
printf("管道2创建成功\n");
}
fd1 = open("./myfifo1", O_WRONLY);
if (fd1 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道1成功(只写)\n");
}
fd2 = open("./myfifo2", O_RDONLY);
if (fd2 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道2成功(只读)\n");
}
return 0;
}
void *TX(void *arg)
{
while (1)
{
printf("请输入>>>\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (write(fd1, buf, sizeof(buf)) < 0)
{
perror("write");
return -1;
}
if (strcasecmp(buf, "quit") == 0)
{
break;
}
}
pthread_exit(0);
}
void *RX(void *arg)
{
while (1)
{
// 读取管道
bzero(buf, sizeof(buf));
res = read(fd2, buf, sizeof(buf));
if (res < 0)
{
perror("read");
return -1;
}
else if (0 == res)
{
sleep(1);
printf("等待消息...\n");
}
printf("MSG1:%s\n", buf);
if (strcasecmp(buf, "quit") == 0)
{
break;
}
}
pthread_exit(0);
}
int main(int argc, const char *argv[])
{
if (createfifo())
{
return -1;
};
pthread_t tidA; // A线程发送
pthread_t tidB; // B线程接受
if (pthread_create(&tidA, NULL, TX, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_detach(tidA);
if (pthread_create(&tidB, NULL, RX, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
close(fd1);
close(fd2);
return 0;
}
msg2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
int fd1, fd2;
char buf[128] = "";
ssize_t res = 0;
int createfifo()
{
umask(0);
// 创建有名管道1
if (mkfifo("./myfifo1", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道1已存在\n");
}
else
{
printf("管道1创建成功\n");
}
// 创建有名管道2
if (mkfifo("./myfifo2", 0777) < 0)
{
// 成功返回0,失败返回-1,如果错误码==17则问题不大
if (17 != errno)
{
perror("mkfifo");
return -1;
}
printf("管道2已存在\n");
}
else
{
printf("管道2创建成功\n");
}
fd1 = open("./myfifo1", O_RDONLY);
if (fd1 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道1成功(只读)\n");
}
fd2 = open("./myfifo2", O_WRONLY);
if (fd2 < 0)
{
perror("open");
return -1;
}
else
{
printf("打开管道2成功(只写)\n");
}
return 0;
}
void *TX(void *arg)
{
while (1)
{
printf("请输入>>>\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (write(fd2, buf, sizeof(buf)) < 0)
{
perror("write");
break;
}
if (strcasecmp(buf, "quit") == 0)
{
break;
}
}
pthread_exit(0);
}
void *RX(void *arg)
{
while (1)
{
// 读取管道
bzero(buf, sizeof(buf));
res = read(fd1, buf, sizeof(buf));
if (res < 0)
{
perror("read");
return NULL;
}
else if (0 == res)
{
sleep(1);
printf("等待消息...\n");
}
printf("MSG2:%s\n", buf);
if (strcasecmp(buf, "quit") == 0)
{
break;
}
}
pthread_exit(0);
}
int main(int argc, const char *argv[])
{
if (createfifo())
{
return -1;
};
pthread_t tidA; // A线程发送
pthread_t tidB; // B线程接受
if (pthread_create(&tidA, NULL, RX, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_detach(tidA);
if (pthread_create(&tidB, NULL, TX, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
close(fd1);
close(fd2);
return 0;
}