免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: jqx55ah
打印 上一主题 下一主题

两进程间通信问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-09-13 13:31 |只看该作者
#define IPCKEY 0x111

char path[256];

    sprintf( path, "%s/etc/config.ini", (char*)getenv("HOME") );
    msgid=ftok( path, IPCKEY );


我用这种方法的结果 还是“shmat error!”

论坛徽章:
0
12 [报告]
发表于 2008-09-13 14:21 |只看该作者
给你个例子把
int GetMessageQueue(key_t key, int flag)
{
        int imqid=0;

             if (flag == 1)                //创建
             {
                  imqid = msgget(key, IPC_CREAT|0666);
      }
             else                        //获取
             {
                 imqid = msgget(key, 0666);
      }

             if (imqid == -1)
             {
                  return -1;
             }

             return imqid;
}

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
13 [报告]
发表于 2008-09-13 17:02 |只看该作者
推荐UNIX Network Programming Volume 2 Second Edition lnterprocess ommunications by W. Richard Stevens

下面我写个例子,简单的生成者-消费者问题实现,一个程序里用父子进程方式实现生产者和消费者,分开两个进程写原理也类似,为了节省空间就偷个懒啦。共享内存保存数据,Posix基于内存的信号量同步数据访问
#include <stdio.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define PATHNAME "/tmp/.what_the_hell_is_this"

typedef struct Item {
    sem_t full, empty;
    int   data;
} Item;

void sys_err(const char* msg);

int main() {
    key_t    key;
    pid_t    pid;
    int        shmid;
    Item*    addr;

    /* remove a shared memory segment from the system */
    key = ftok(PATHNAME, 1);
    if (key == -1)
        sys_err("ftok");
    shmid = shmget(key, 0, SHM_R|SHM_W);
    shmctl(shmid, IPC_RMID, NULL);
   
    /* create a new shared memory segment */
    shmid = shmget(key, sizeof(Item), IPC_CREAT|IPC_EXCL|SHM_W|SHM_R);
    if (shmid == -1)
        sys_err("shmget");
    addr = (Item*)shmat(shmid, NULL, 0);
    if (addr == (Item*)-1)
        sys_err("shmat");
   
    /* initialize addr */
    sem_init(&addr->full, 1, 0);
    sem_init(&addr->empty, 1, 1);
    shmdt(addr);
   
    pid = fork();
    if (pid > 0) { /* parent for writing */
        shmid = shmget(key, 0, SHM_W);
        if (shmid == -1)
            sys_err("parent shmget");
        addr = (Item*)shmat(shmid, NULL, 0);
        if (addr == (Item*)-1)
            sys_err("parent shmat");
        while (1) {
            sem_wait(&addr->empty);
            fprintf(stdout, "parent: ");
            fscanf(stdin, "%d", &addr->data);
            sem_post(&addr->full);
        }   
    } else if (pid == 0) { /* child for reading */
        shmid = shmget(key, 0, SHM_R);
        if (shmid == -1)
            sys_err("child shmget");
        addr = (Item*)shmat(shmid, NULL, 0);
        if (addr == (Item*)-1)
            sys_err("child shmat");
        while (1) {
            sem_wait(&addr->full);
            fprintf(stdout, "child: %d\n", addr->data);
            sem_post(&addr->empty);
        }   
    } else {
        sys_err("fork");
    }
   
    return 0;
}

void sys_err(const char* msg) {
    perror(msg);
    exit(0);
}



[lgr@localhost lab]$ gcc shm.c -lrt
[lgr@localhost lab]$ touch /tmp/.what_the_hell_is_this
[lgr@localhost lab]$ ./a.out
parent: 13
child: 13
parent: 4567
child: 4567
parent:

论坛徽章:
0
14 [报告]
发表于 2008-09-13 22:39 |只看该作者
chao@chao-laptop:~/c-study/cu$ cat shmread.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <errno.h>

#define BUF_SIZE&nbsp;&nbsp;&nbsp;&nbsp;1024
#define MYKEY&nbsp;&nbsp;&nbsp;&nbsp;24

int main()
{
&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;shmid;
&nbsp;&nbsp;&nbsp;&nbsp;char *&nbsp;&nbsp;&nbsp;&nbsp;shmptr;
&nbsp;&nbsp;&nbsp;&nbsp;key_t&nbsp;&nbsp;&nbsp;&nbsp;key;

&nbsp;&nbsp;&nbsp;&nbsp;if (( key = ftok(".", MYKEY)) == -1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("ftok error!\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;if (( shmid = shmget(key, BUF_SIZE, IPC_CREAT | 0666)) == -1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("shmget error!\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;if (( shmptr = shmat(shmid, 0, 0)) == (void *)-1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("shmat error!\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;while(1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("string: %s\n", shmptr);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(*shmptr != '\0')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shmptr++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(3);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}
chao@chao-laptop:~/c-study/cu$ cat shmwrite.c
#include <stdio.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>

#define BUF_SIZE&nbsp;&nbsp;&nbsp;&nbsp;1024
#define&nbsp;&nbsp;&nbsp;&nbsp;MYKEY&nbsp;&nbsp;&nbsp;&nbsp;24
int main()
{
&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;shmid;
&nbsp;&nbsp;&nbsp;&nbsp;char *&nbsp;&nbsp;&nbsp;&nbsp;shmptr;
&nbsp;&nbsp;&nbsp;&nbsp;key_t&nbsp;&nbsp;&nbsp;&nbsp;key;

&nbsp;&nbsp;&nbsp;&nbsp;if(( key = ftok(".", MYKEY)) == -1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("ftok error\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;if (( shmid = shmget(key, BUF_SIZE, IPC_CREAT | 0666)) == -1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("shmget error\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;if (( shmptr = shmat(shmid, 0, 0)) == (void *)-1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("shmat error\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;while(1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("writer: ");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fgets(shmptr, BUF_SIZE, stdin);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(*shmptr != '\n')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shmptr++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*shmptr = '\0';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//getchar();

&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
}


我也写了一个,是按照LZ的改的。编译好后,开2个终端,在写的地方输入,读的地方就能读到。不输入的话读出来是空的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP