php c消息队列通信,PHP与C(或其它语言)通过消息队列进行通讯,完整代码_PHP教程...

该博客详细介绍了使用C语言实现基于消息队列的进程间通信(IPC)过程,包括创建消息队列、消息发送与接收函数、线程处理函数。通过示例代码展示了如何在多线程环境中使用msgget、msgctl、msgrcv和msgsnd等系统调用。此外,还涉及错误处理和内存管理。
摘要由CSDN通过智能技术生成

#include #define MAX_MSG_LEN 512

static int php_msg = -1;

static int php_msg1 = -1;

static int running = 1;

static void *php_msg_handler_thread(void *arg);

static int msg_send(int msg_id, int fd, char *data);

struct msg_st {

long mtype;

char mtext[MAX_MSG_LEN];

};

void logst(struct msg_st some_data);

int main(int argc,char **argv)

{

printf("go 1 \n");

if((php_msg= msgget((key_t)1,0666|IPC_CREAT)) == -1)

{

perror("php_msg create");

return 0;

}

if((php_msg1= msgget((key_t)2,0666|IPC_CREAT)) == -1)

{

perror("php_msg create");

return 0;

}

/

pthread_t php_msg_pthread;

int rs = pthread_create(&php_msg_pthread, NULL, (void*(*)(void*))php_msg_handler_thread, (void *)NULL);

if(rs!=0)

{

perror("php_msg_pthread create");

return 0;

}

pthread_join(php_msg_pthread, NULL);

return 0;

}

static void *php_msg_handler_thread(void *arg)

{

struct msg_st php_data;

printf("sizeof(struct msg_st)=%d\n",sizeof(struct msg_st));

int msg_to_recevie = 0;

char* data;

data = malloc(MAX_MSG_LEN);

char *pre = "You told me:";

while(running){

if(msgrcv(php_msg,(void *) &php_data, MAX_MSG_LEN, msg_to_recevie , 0) == -1)

{

perror("msgrcv");

if(errno==E2BIG)

{

if(msgctl(php_msg,IPC_RMID,0) == -1)

{

fprintf(stderr,"msgctl(IPC_RMID) failed \n");

}

}

else if(errno == EINVAL)

{

sleep(1);

}

}else{

printf("recevier mssage : %s , type= %d, msg_to_recevie= %d;\n", php_data.mtext, php_data.mtype,msg_to_recevie);

memset(data, '\0', MAX_MSG_LEN);

memcpy(data, pre, strlen(pre));

memcpy(data+strlen(pre), php_data.mtext, strlen(php_data.mtext));

msg_send(php_msg1, 2, data);

bzero(php_data.mtext, strlen(php_data.mtext));

}

//break;

}

free(data);

}

static int msg_send(int msg_id, int fd, char *data)

{

struct msg_st* some_data;

some_data = malloc( sizeof(struct msg_st) );

memcpy(some_data->mtext, data, strlen(data) + 1);

some_data->mtext[strlen(data)] = '\0';

some_data->mtype= fd;

printf("will send %s \n", &some_data->mtext);

if((msgsnd(msg_id,(void *) some_data, MAX_MSG_LEN,0)) == -1)

{

perror("msgsnd");

return 0;

}

return 1;

}

void logst(struct msg_st some_data)

{

FILE *fp;

fp = fopen("file.dat", "w+");

if (!fp)

{

printf("open file error!");

return;

}

fwrite(&some_data, sizeof(struct msg_st), 1, fp);

fclose(fp);

}

运行:./m

再运行:php msg_write.php

http://www.bkjia.com/PHPjc/477913.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477913.htmlTechArticle?php /* * class msg * Use for communication between php and php; * Create at: 12:08 2012/10/31 * Author: leixun(lein_urg@163.com) * version 1 - 14:01 2012/10/31 */ class msg{ privat...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值