| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1683 人关注过本帖
标题:两个进程间通过服务器双向通信的问题,自己搞了半天才弄的基本可以通信了, ...
只看楼主 加入收藏
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:10 
两个进程间通过服务器双向通信的问题,自己搞了半天才弄的基本可以通信了,不过好像没解决同步互斥问题
______server.c_____
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
 
#define SIZE 1024
 
sem_t sem[4];
 
void f_exchange1_2(void);
void f_exchange3_4(void);
 
int main()
{
     if(0 > mkfifo("/tmp/FIFO1", 0666));    //建4个管道
     {
         if(EEXIST != errno)
         {
             perror("mkfifo1");
             exit(-1);
         }
     }
     
     if(0 > mkfifo("/tmp/FIFO2", 0666));
     {
         if(EEXIST != errno)
         {
             perror("mkfifo2");
             exit(-1);
         }
     }
 
     if(0 > mkfifo("/tmp/FIFO3", 0666));
     {
         if(EEXIST != errno)
         {
             perror("mkfifo3");
             exit(-1);
         }
     }
 
     if(0 > mkfifo("/tmp/FIFO4", 0666));
     {
         if(EEXIST != errno)
         {
             perror("mkfifo4");
             exit(-1);
         }
     }
 
     if(0 >sem_init(&sem[0], 0, 1))    //四个信号灯,准备用来解决同步互斥的,还没用上,所以这儿应该有安全隐患
     {
         perror("sem_init0");
         exit(-1);
     }
 
     if(0 >sem_init(&sem[1], 0, 0))
     {
         perror("sem_init1");
         exit(-1);
     }
     
     if(0 >sem_init(&sem[2], 0, 1))
     {
         perror("sem_init2");
         exit(-1);
     }
 
     if(0 >sem_init(&sem[3], 0, 0))
     {
         perror("sem_init3");
         exit(-1);
     }
 
     pthread_t pid[2];
     int ret1 = pthread_create(&pid[0], NULL, (void *)f_exchange1_2, NULL); //建第一个线程
     if(0 > ret1)
     {
         printf("pthread_create0:%s",strerror(ret1));
         exit(-1);
     }   
   
     int ret2 = pthread_create(&pid[1], NULL, (void *)f_exchange3_4, NULL);//第二个线程
     if(0 > ret2)
     {
         printf("pthread_create0:%s",strerror(ret2));
         exit(-1);
     }
     
     pthread_join(pid[0], NULL);   //等线程一
     pthread_join(pid[1], NULL); //等线程二
     
    return 0;
 
}
 
void f_exchange1_2(void)      //线程一调用函数,用于管道1的数据传到管道2(即客户1的数据传给客户2)
{   
     char buf1[SIZE] = {0};    //buf1用于中间交换的字符数组(之后都是buf用于中间缓存)
     int fd1 = open("/tmp/FIFO1", O_RDWR);
     if(0 > fd1)
     {
         printf("open1:%s\n",strerror(fd1));
         exit(-1);
     }
     
     int fd2 = open("/tmp/FIFO2", O_RDWR);
     if(0 > fd2)
     {
         printf("open2:%s\n",strerror(fd2));
         exit(-1);
     }
     
     while(1)
     {    //sem_wait(&sem[0]);
         read(fd1, buf1, sizeof(buf1)-1);
         write(fd2,buf1,sizeof(buf1));
         //sem_post(&sem[1]);
     }
 }

void f_exchange3_4(void)   //线程一调用函数,用于管道3的数据传到管道4(即客户2的数据传给客户1)
{
     char buf2[SIZE] = {0};
     int fd4 = open("/tmp/FIFO4", O_RDWR);
     if(0 > fd4)
     {
         printf("open4:%s\n",strerror(fd4));
         exit(-1);
     }
     
     int fd3 = open("/tmp/FIFO3", O_RDWR);
     if(0 > fd3)
     {
         printf("open3:%s\n",strerror(fd3));
         exit(-1);
     }
     
     while(1)
     {    //sem_wait(&sem[2]);
         read(fd3, buf2, sizeof(buf2)-1);
         write(fd4,buf2,sizeof(buf2));
         //sem_post(&sem[3]);
     }
}

 ________quest1.c_________
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
 
#define SIZE 1024
 
sem_t sem[2];
pthread_t pid[2];

void f_write(void );
void f_read(void );
 
int main()
{
 
     if(0 >sem_init(&sem[0], 0, 1))
     {
         perror("sem_init0");
         exit(-1);
     }
 
     if(0 >sem_init(&sem[1], 0, 0))
     {
         perror("sem_init1");
         exit(-1);
     }
 
     int ret1 = pthread_create(&pid[0], NULL, (void *)f_write, NULL); //客户1的第1个线程
     if(0 > ret1)
     {
         printf("pthread_create0:%s",strerror(ret1));
         exit(-1);
     }   
   
     int ret2 = pthread_create(&pid[1], NULL, (void *)f_read, NULL); //第二个线程
     if(0 > ret2)
     {
         printf("pthread_create0:%s",strerror(ret2));
         exit(-1);
     }
     
     pthread_join(pid[0], NULL);
     pthread_join(pid[1], NULL);
     
    return 0;
 
}
 
void f_write(void)    //线程1调用的函数,用于管道1中写数据(即发消息给客户2)
{
     int fd1 = open("/tmp/FIFO1", O_RDWR);
     if(0 > fd1)
     {
         printf("open1:%s\n",strerror(fd1));
         exit(-1);
     }
     
     char buf[SIZE] = {0};
     while(1)
     {   
        //sem_wait(&sem[0]);
         printf("Enter a string:\n");
         fgets(buf, sizeof(buf)-1,stdin);
         write(fd1, buf, sizeof(buf)-1);
         //sem_post(&sem[1]);
     }
     
}
void f_read(void)  //线程2的调用函数,用于读管道4(即接收客户2发的消息)
{   
     int fd4 = open("/tmp/FIFO4", O_RDWR);
     if(0 > fd4)
     {
         printf("open4:%s\n",strerror(fd4));
         exit(-1);
     }
     
     char buf[SIZE] = {0};
     while(1)
     {
         //sem_wait(&sem[1]);
         read(fd4, buf, sizeof(buf));
         printf(":");
         puts(buf);
         memset(buf, 0, sizeof(buf));
         //sem_post(&sem[0]);
     }
 
}
 ________quest2.c_________
 
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
 
#define SIZE 1024
 
sem_t sem[2];
pthread_t pid[2];

void f_write(void );
void f_read(void );
 
int main()
{
     if(0 >sem_init(&sem[0], 0, 1))
     {
         perror("sem_init0");
         exit(-1);
     }
 
     if(0 >sem_init(&sem[1], 0, 0))
     {
         perror("sem_init1");
         exit(-1);
     }
 
     int ret1 = pthread_create(&pid[0], NULL, (void *)f_write, NULL);  //客户2的第一个线程
     if(0 > ret1)
     {
         printf("pthread_create0:%s",strerror(ret1));
         exit(-1);
     }   
   
     int ret2 = pthread_create(&pid[1], NULL, (void *)f_read, NULL);  //客服2的第二个线程
     if(0 > ret2)
     {
         printf("pthread_create0:%s",strerror(ret2));
         exit(-1);
     }
     
     pthread_join(pid[0], NULL); //等待线程1
     pthread_join(pid[1], NULL);//等待线程2
     
    return 0;
 
}
 

void f_write(void) //线程1调用函数,用于写管道3(即客户2给客户1发消息)
{
     int fd3 = open("/tmp/FIFO3", O_RDWR);
     if(0 > fd3)
     {
         printf("open3:%s\n",strerror(fd3));
         exit(-1);
     }
     
     char buf[SIZE] = {0};
     while(1)
     {   
        //sem_wait(&sem[0]);
         printf("Enter a string:\n");
         fgets(buf, sizeof(buf)-1,stdin);
         write(fd3, buf, sizeof(buf)-1);
         //sem_post(&sem[1]);
     }
     
}
void f_read(void) // 线程2调用的函数,用于读管道2(即接收客户1发的消息)
{   
     int fd2 = open("/tmp/FIFO2", O_RDWR);
     if(0 > fd2)
     {
         printf("open2:%s\n",strerror(fd2));
         exit(-1);
     }
     
     char buf[SIZE] = {0};
     while(1)
     {
         //sem_wait(&sem[1]);
         read(fd2, buf, sizeof(buf));
         printf(":");
         puts(buf);
         memset(buf, 0, sizeof(buf));
         //sem_post(&sem[0]);
     }
 
}


各位大哥不好意思,昨天写完就很晚了,所以就没说明就发上来了,由于初学逻辑上肯定没把握好,所以显得有点乱,请各位谅解!我会进步的。我想请教的是线程之间的同步互斥问题,我也看了书了,还是不怎么明白,怎么用信号灯或互斥锁解决这问题,还有就是函数回调问题?望指点小弟,谢谢各位!

[ 本帖最后由 遗矢的老人 于 2012-8-16 18:58 编辑 ]
搜索更多相关主题的帖子: void 通信 include 
2012-08-16 02:40
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
不好意思,写的有点杂,我刚刚才学这个,就是同步互斥问题不怎么搞得懂
2012-08-16 02:46
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:4 
你把 include 都隐藏掉是什么意思?有意不让我们拷贝调试?
2012-08-16 09:20
long0042
Rank: 2
等 级:论坛游民
帖 子:38
专家分:50
注 册:2008-3-5
收藏
得分:4 
哟。还是linux的程序啊。linux下程序接触少啊,帮不了你。不过建议你去看下信号量啊。 这个可以同步的。具体没弄过。
2012-08-16 09:22
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:4 
估计网上复制的。
2012-08-16 10:42
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 2楼 遗矢的老人
呵呵 不是的  我也不知道,上传就自己隐藏的,不好意思
2012-08-16 11:46
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 2楼 遗矢的老人
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
2012-08-16 11:48
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 5楼 Devil_W
瞎说,我昨晚自己写的代码怎么就变成网上复制啦
2012-08-16 11:50
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:4 
好复杂 学习了
2012-08-16 11:50
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 2楼 遗矢的老人
怎么把图片发上来?

[ 本帖最后由 遗矢的老人 于 2012-8-16 13:37 编辑 ]
2012-08-16 12:17
快速回复:两个进程间通过服务器双向通信的问题,自己搞了半天才弄的基本可以通信 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017620 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved