FIFO和线程的使用
2017-06-21 22:51
162 查看
做一个类似聊天的工具
第一个程序的代码
#include
#include
#include
#include
#include
#include
#include
#include
int res, r_fd, s_fd;
pthread_t th_r, th_w;
int kflag = 0;
void *thread_w()
{
int tmp;
char p[1000];
while(gets(p))
{
tmp = mkfifo("/ktmp/kfifo1", 0111);
if(tmp < -1)
printf("Create the fifo pipe failed\n");
s_fd = open("/ktmp/kfifo1", O_WRONLY, 0111);
write(s_fd, p, strlen(p) + 1);
close(s_fd);
}
}
void *thread_r()
{
int tmp;
char p[1000];
struct stat kstat;
while(1){
while((r_fd = open("/ktmp/kfifo2", O_RDONLY, 0111)) ==
-1);
if(kflag == 0)
{
printf("Connecting success\n");
kflag = 1;
}
stat("/ktmp/kfifo1", &kstat);
while((tmp = read(r_fd, p, 200)) == 0);
printf("%s\n", p);
close(r_fd);
unlink("/ktmp/kfifo2");
}
}
void main()
{
res = pthread_create(&th_w, NULL, thread_w, NULL);
if(res != 0)
printf("Create writing thread failed\n");
res = pthread_create(&th_r, NULL, thread_r, NULL);
if(res != 0)
printf("Create reading thread failed\n");
while(1);
}
第二个程序的代码
#include
#include
#include
#include
#include
#include
#include
#include
int res, r_fd, s_fd;
pthread_t th_r, th_w;
int kflag = 0;
void *thread_w()
{
int tmp;
char p[1000];
while(gets(p)){
tmp = mkfifo("/ktmp/kfifo2", 0111);
if(tmp < -1)
printf("Create the fifo pipe failed\n");
s_fd = open("/ktmp/kfifo2", O_WRONLY, 0111);
write(s_fd, p, strlen(p) + 1);
close(s_fd);
}
}
void *thread_r()
{
int tmp;
char p[1000];
struct stat kstat;
while(1){
while((r_fd = open("/ktmp/kfifo1", O_RDONLY, 0111)) ==
-1);
if(kflag == 0)
{
printf("Connecting success\n");
kflag = 1;
}
stat("/ktmp/kfifo1", &kstat);
while((tmp = read(r_fd, p, 200)) == 0);
printf("%s\n", p);
close(r_fd);
unlink("/ktmp/kfifo1");
}
}
void main()
{
res = pthread_create(&th_w, NULL, thread_w, NULL);
if(res != 0)
printf("Create writing thread failed\n");
res = pthread_create(&th_r, NULL, thread_r, NULL);
if(res != 0)
printf("Create reading thread failed\n");
while(1);
}
实现的结果就是
第一个程序的代码
#include
#include
#include
#include
#include
#include
#include
#include
int res, r_fd, s_fd;
pthread_t th_r, th_w;
int kflag = 0;
void *thread_w()
{
int tmp;
char p[1000];
while(gets(p))
{
tmp = mkfifo("/ktmp/kfifo1", 0111);
if(tmp < -1)
printf("Create the fifo pipe failed\n");
s_fd = open("/ktmp/kfifo1", O_WRONLY, 0111);
write(s_fd, p, strlen(p) + 1);
close(s_fd);
}
}
void *thread_r()
{
int tmp;
char p[1000];
struct stat kstat;
while(1){
while((r_fd = open("/ktmp/kfifo2", O_RDONLY, 0111)) ==
-1);
if(kflag == 0)
{
printf("Connecting success\n");
kflag = 1;
}
stat("/ktmp/kfifo1", &kstat);
while((tmp = read(r_fd, p, 200)) == 0);
printf("%s\n", p);
close(r_fd);
unlink("/ktmp/kfifo2");
}
}
void main()
{
res = pthread_create(&th_w, NULL, thread_w, NULL);
if(res != 0)
printf("Create writing thread failed\n");
res = pthread_create(&th_r, NULL, thread_r, NULL);
if(res != 0)
printf("Create reading thread failed\n");
while(1);
}
第二个程序的代码
#include
#include
#include
#include
#include
#include
#include
#include
int res, r_fd, s_fd;
pthread_t th_r, th_w;
int kflag = 0;
void *thread_w()
{
int tmp;
char p[1000];
while(gets(p)){
tmp = mkfifo("/ktmp/kfifo2", 0111);
if(tmp < -1)
printf("Create the fifo pipe failed\n");
s_fd = open("/ktmp/kfifo2", O_WRONLY, 0111);
write(s_fd, p, strlen(p) + 1);
close(s_fd);
}
}
void *thread_r()
{
int tmp;
char p[1000];
struct stat kstat;
while(1){
while((r_fd = open("/ktmp/kfifo1", O_RDONLY, 0111)) ==
-1);
if(kflag == 0)
{
printf("Connecting success\n");
kflag = 1;
}
stat("/ktmp/kfifo1", &kstat);
while((tmp = read(r_fd, p, 200)) == 0);
printf("%s\n", p);
close(r_fd);
unlink("/ktmp/kfifo1");
}
}
void main()
{
res = pthread_create(&th_w, NULL, thread_w, NULL);
if(res != 0)
printf("Create writing thread failed\n");
res = pthread_create(&th_r, NULL, thread_r, NULL);
if(res != 0)
printf("Create reading thread failed\n");
while(1);
}
实现的结果就是
相关文章推荐
- Linux下C语言实现的简单使用线程向FIFO里写入与读取数据的例子
- 轻松使用线程 1 zt -- 今天又把这篇找出来看了一下
- 在你的服务器端代码中使用线程和创建异步处理
- COM 经验的八个教训(7):使用线程或异步调用来避免 DCOM 超时设定太长
- WebRequest 使用不当可能会造成线程阻塞
- 不错的线程使用示例
- 如何使用J2ME中的线程
- 编程经验点滴(四)——《使用类的成员函数作为线程的执行函数》
- 如何使用J2ME中的线程
- 在Global中使用线程隔时处理任务
- 在你的服务器端代码中使用线程和创建异步处理(2)
- python 进行多线程编程,使用Semaphore控制线程数
- 在WinFrom应用中,如果使用后台线程来操作界面应调用Control.Invoke()方法
- 支持多线程的日志类,可以在线程中使用,用来与主窗口通讯
- 使用Ruby启动新线程并获取输出。
- 在你的服务器端代码中使用线程和创建异步处理(3)
- 在你的服务器端代码中使用线程和创建异步处理(4)
- 线程本地存储(Thread Local Storage, TLS)分析与使用
- 如何使用J2ME中的线程
- pthread库学习(2): 线程的同步,使用信号量