Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Change the poll program to epoll implementation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The original instance is in the APUE (third edition) 17.2 UNIX domain socket

1. Use UNIX and sockets to poll XSI message queues (poll version, original version)

# include "apue.h" # include # define NQ 3 / / number of queues # define MAXMSZ 512 / / maximum message length # define KEY 0x123 / / the first key value of the message queue struct threadinfo {int qid; int fd;}; struct mymesg {long mtype Char mtext [MAXMSZ];}; void * helper (void * arg) {int n; struct mymesg m; struct threadinfo * tip = arg; for (;;) {printf ("helper qid% d, fd% d, tid% u\ n", tip- > qid, tip- > fd, (unsigned) pthread_self ()); memset (& m, 0, sizeof (m)) If ((n = msgrcv (tip- > qid, & m, MAXMSZ, 0, MSG_NOERROR))

< 0) { err_sys("msgrcv error"); } if (write(tip->

Fd, m.mtext, n)

< 0) { err_sys("write error"); } }}int main(){ int i, n, err; int fd[2]; int qid[NQ]; struct pollfd pfd[NQ]; struct threadinfo ti[NQ]; pthread_t tid[NQ]; char buf[MAXMSZ]; for (i = 0; i < NQ; ++i) { if ((qid[i] = msgget((KEY+i), IPC_CREAT|0666)) < 0) { //创建一个新队列 err_sys("msgget error"); } printf("queue %d ID is %d\n", i, qid[i]); if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fd) < 0) { //创建UNXI域套接字(fd管道) err_sys("socketpair error"); } printf("fd[0]:%d\n", fd[0]); printf("fd[1]:%d\n", fd[1]); pfd[i].fd = fd[0]; pfd[i].events = POLLIN; ti[i].qid = qid[i]; ti[i].fd = fd[1]; if ((err = pthread_create(&tid[i], NULL, helper, &ti[i])) != 0) { //创建线程 err_exit(err, "pthread_create error"); } } for (;;) { if (poll(pfd, NQ, -1) < 0) { //等待事件发生 err_sys("poll error"); } for (i = 0; i < NQ; ++i) { //printf("i:%d\n", i); if (pfd[i].revents & POLLIN) { if ((n = read(pfd[i].fd, buf, sizeof(buf))) < 0) { err_sys("read error"); } buf[n] = 0; printf("queue %d id %d, message %s\n", i, qid[i], buf); } } } exit(0);} 编译命令: gcc pollmsg.c -o pollmsg -lapue -lpthread -std=c99 2、使用UNIX与套接字轮询XSI消息队列(epoll版,改版) #include "apue.h"#include #include #include #include #define NQ 3 //队列的数量#define MAXMSZ 512 //消息的最大长度#define KEY 0x123 //消息队列的第一个key值#define FDSIZE 1000#define EPOLLEVENTS 100struct threadinfo { int qid; int fd;};struct mymesg { long mtype; char mtext[MAXMSZ];};void *helper(void *arg){ int n; struct mymesg m; struct threadinfo *tip = arg; for (;;) { printf("helper qid %d, fd %d, tid %u\n", tip->

Qid, tip- > fd, (unsigned) pthread_self (); memset (& m, 0, sizeof (m)); if (n = msgrcv (tip- > qid, & m, MAXMSZ, 0, MSG_NOERROR))

< 0) { err_sys("msgrcv error"); } if (write(tip->

Fd, m.mtext, n) < 0) {err_sys ("write error");} int main () {int I, n, err; int fd [2]; int qid [NQ]; int epollfd; struct epoll_event events [EPOLLEVENTS]; struct threadinfo ti [NQ]; pthread_t tid [NQ] Char buf [MAXMSZ]; epollfd = epoll_create (FDSIZE); / / create epoll file descriptor for (I = 0; I < NQ; + + I) {if ((QID [I] = msgget ((KEY+i), IPC_CREAT | 0666) < 0) {/ / create a new queue err_sys ("msgget error") } printf ("queue% d ID is% d\ n", I, qid [I]); if (socketpair (AF_UNIX, SOCK_DGRAM, 0, fd) < 0) {/ / create UNXI domain socket (fd pipe) err_sys ("socketpair error");} struct epoll_event ev Ev.events = EPOLLIN; ev.data.fd = fd [0]; epoll_ctl (epollfd, EPOLL_CTL_ADD, fd [0], & ev); / / register fd [0] to epoll ti.qid = qid [I]; ti [I] .fd = fd [1] If ((err = pthread_create (& tid [I], NULL, helper, & ti [I])! = 0) {/ / create thread err_exit (err, "pthread_create error");}} for (;;) {int occurred If ((occurred = epoll_wait (epollfd, events, EPOLLEVENTS,-1) < 0) {/ / wait for the event to occur err_sys ("epoll error");} if (occurred = = 0) {err_sys ("epoll timeout") } for (I = 0; I < occurred; + + I) {if (events [I] .events & EPOLLIN) {if ((n = read (events [I] .data.fd, buf, sizeof (buf)) < 0) {err_sys ("read error")) } buf [n] = 0; printf ("main thread% u, message% s\ n", (unsigned) pthread_self (), buf);} exit (0);}

Compile command:

Gcc epollmsg.c-o epollmsg-lapue-lpthread-std=c99

3. Send messages to XSI message queue (test program, original)

# include "apue.h" # include # define MAXMSZ 512struct mymesg {long mtype; char mtext [MAXMSZ];}; int main (int argc, char * argv []) {key_t key; long qid; size_t nbytes; struct mymesg m; if (argc! = 3) {fprintf (stderr, "usage: sendmsg KEY message\ n"); exit (1) } key = strtol (argv [1], NULL, 0); / / printf ("key:0xX\ n", (unsigned) key); if ((qid = msgget (key, 0) < 0) {/ / Open an existing queue err_sys ("can't open queue key% s", argv [1]);} memset (& m, 0, sizeof (m)) Strncpy (m.mtext, argv [2], MAXMSZ-1); nbytes = strlen (m.mtext); m.mtype = 1; / / printf ("qid:%ld\ n", qid); if (msgsnd (qid, & m, nbytes, 0) < 0) {/ / send a message to the specified message queue err_sys ("can't send message") } exit (0);}

Compile command:

Gcc sendmsg.c-o sendmsg-lapue-std=c99

Related readings:

1. Summary of the differences among select, poll and epoll [collation]

2. The use of poll function, the original text

3. APUE Reading Notes

* walker * *

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report