Why not to use a socket (Unix socket or TCP/IP socket), push your
"0123456789" in one side and retrieve the same from the other side?

You'll need different code to open/close the socket, but writing/reading
will look the same.

--- Omer


On Wed, 2013-06-19 at 09:01 +0300, Elazar Leibovich wrote:
> I think I didn't explain myself correctly, so let me give a different
> example.
> 
> Let's make a file descriptor that counts to 9.
> 
> 
> I.e, we want to emulate the behavior:
> 
> 
>     int fd = make_counter_fd();
>     assert(write(fd, buf, 100) == 11);
>     assert(strcmp(buf, "0123456789") == 0);
> 
> 
> Let me describe a very simple version of the main poll loop:
> 
> 
>     struct poll_cb {
>         int fd;
>         int (*read_cb)(int fd);
>         int (*write_cb)(int fd);
>     };
>     while (poll(&pfd, nr, -1) != -1) {
>         for (int i=0; i < nr; i++) {
>             if (pfd[i].revent |= POLLIN)
>                 poll_cbs[i].read_cb(pfd[i].fd);
>             if (pfd[i].revent |= POLLOUT) 
>                 poll_cbs[i].write_cb(pfd[i].fd);
> 
>             if (pfd[i].revent |= POLLHUP)
>                 pfds[i].fd = -1; // don't poll again
>         }
>     }
> 
> 
> Now, in order to emulate the 0-9 file descriptor, we'll associate to a
> "/dev/zero" a read callback function that looks roughly like
> 
> 
>     int written = 0;
>     char buf[10];
>     int zero_to_nine_cb(int fd) {
>         for (int i=0; i< 10; i++) buf[i] = '0' + i;
>         close(fd); // note, we never touched the fd
>     }
> 
> 
> But I have to use a file descriptor! Otherwise poll will have no
> reason to call my callback.
> On Jun 19, 2013 7:47 AM, "Shachar Shemesh" <shac...@shemesh.biz>
> wrote:
>         On 18/06/13 22:16, Elazar Leibovich wrote:
>         
>         > I'm using it as a fake "always non-blocking" file
>         > descriptor. 
>         > 
>         > 
>         > My main libevent-like poll loop looks like:
>         > 
>         > 
>         >     poll(fds)
>         >     for fd in fds:
>         >        if fd.revents | POLLIN:
>         >            fd.read_callback()
>         >        if fd.revents | POLLOUT:
>         >            fd.write_callback()
>         > 
>         > 
>         > Now let's say I want a fake filedescriptor that always reads
>         > 'z's (a sleepy fd).
>         Why? What you just did was to turn the whole thing into a
>         non-sleeping loop. If that's the case, simply call poll with a
>         zero timeout, so it won't sleep, and call your callback at the
>         end of each loop. No need to artificially introduce another
>         file descriptor into the mix.
>         
>         Mind you, I still don't understand WHY you'd want such a
>         thing. This code will, by definition, consume 100% CPU all the
>         time.

-- 
As long as there are families which throw their teenager sons and
daughters out of home if they turn out to be gays or lesbians, Gay Pride
Parade events are needed.
As long as the most common cause of suicide by teenagers is their
finding out that they are gays or lesbians, Gay Pride Parade events are needed.
My own blog is at http://www.zak.co.il/tddpirate/

My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with which
I may be affiliated in any way.
WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html


_______________________________________________
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il

Reply via email to