public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jesse Pollard <pollard@tomcat.admin.navo.hpc.mil>
To: olaf@bigred.inka.de, linux-kernel@vger.kernel.org
Subject: Re: light weight user level semaphores
Date: Fri, 20 Apr 2001 09:19:37 -0500 (CDT)	[thread overview]
Message-ID: <200104201419.JAA50024@tomcat.admin.navo.hpc.mil> (raw)
In-Reply-To: <E14qXEU-0005xo-00@g212.hadiko.de>

Olaf Titz <olaf@bigred.inka.de>:
> > Ehh.. I will bet you $10 USD that if libc allocates the next file
> > descriptor on the first "malloc()" in user space (in order to use the
> > semaphores for mm protection), programs _will_ break.
> 
> Of course, but this is a result from sloppy coding. In general, open()
> can just return anything and about the only case where you can even
> think of ignoring its result is this:
>  close(0); close(1); close(2);
>  open("/dev/null", O_RDWR); dup(0); dup(0);
> (which is even not clean for other reasons).
> 
> I can't imagine depending on the "fact" that the first fd I open is 3,
> the next is 4, etc. And what if the routine in question is not
> malloc() but e.g. getpwuid()? Both are just arbitrary library
> functions, and one of them clearly does open file descriptors,
> depending on their implementation.
> 
> What would the reason[1] be for wanting contiguous fd space anyway?
> 
> Olaf
> 
> [1] apart from not having understood how poll() works of course.

Optimization use in select: If all "interesting" file id's are known
to be below "n", then only the first "n" bits in a FD_ISSET need to
be examined. As soon as the bits are scattered, it takes MUCH longer
to check for activity....

It may not be the "best" way, but what I tend to do is:

 Umm - this is snipped from a multiplexed logger using FIFOs for
 and indeterminate amount of data from differet utilities sending
 text buffers (normally one line at a time but could be more).

static void fd_init(argc,argv)
    int         argc;                   /* number of parameters         */
    char        **argv;                 /* parameter list               */
{
    int         i,j;            /* scratch counters                     */
    static char str[50];

    pnames = argv;
    FD_ZERO(&in_files);         /* init all file descriptor sets        */

    for (i = 0; i <= MAX_LOG && i < argc; i++) {
        sprintf(str,"/tmp/%s",pnames[i]);
        mkfifo(str,0600);       /* assume it exists */
        inlogfd[i] = open(str,O_RDONLY | O_NDELAY);
        FD_SET(inlogfd[i],&in_files);
    }
    used = i;
}


Then I can scan for any activity by:

    do {
        while (select(MAX_LOG,&active,NULL,NULL,NULL) >= 0) {
            for(i = 0; i <= used; i++) {
                if (FD_ISSET(inlogfd[i],&active)) {
                    r=ioctl(inlogfd[i],FIONREAD,&n);
                    while (n > 0) {
                        r = (n > BUF_MAX - 1) ? BUF_MAX - 1: n;
                        read(inlogfd[i],buf,r);
                        printbuf(pnames[i],r);
                        n -= r;
                    }
                }
            }
            active = in_files;
        }
    } while (errno == EINTR);

-------------------------------------------------------------------------
Jesse I Pollard, II
Email: pollard@navo.hpc.mil

Any opinions expressed are solely my own.

  reply	other threads:[~2001-04-20 14:20 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20010417114433.D1108@w-mikek2.sequent.com>
2001-04-17 19:48 ` light weight user level semaphores Linus Torvalds
2001-04-18 18:13   ` Bernd Eckenfels
2001-04-18 19:35   ` Ulrich Drepper
2001-04-19  8:20     ` Alon Ziv
2001-04-19  8:52       ` Abramo Bagnara
2001-04-19  9:08         ` Alexander Viro
2001-04-19 10:44           ` Abramo Bagnara
2001-04-19 16:11         ` Linus Torvalds
2001-04-19 16:33           ` Alexander Viro
2001-04-19 16:43             ` Linus Torvalds
2001-04-19 17:33               ` Alexander Viro
2001-04-19 17:38                 ` Linus Torvalds
2001-04-19 18:24                   ` Alexander Viro
2001-04-19 19:26                   ` Ulrich Drepper
2001-04-19 19:35                     ` Alan Cox
2001-04-19 20:06                       ` Ulrich Drepper
2001-04-19 20:11                         ` Alan Cox
2001-04-19 20:26                           ` Ulrich Drepper
2001-04-19 20:22                     ` Ingo Oeser
2001-04-19 20:40                       ` Ulrich Drepper
2001-04-19 20:51                       ` Linus Torvalds
2001-04-19 21:38                       ` Alan Cox
2001-04-19 20:49                     ` Linus Torvalds
2001-04-19 21:18                       ` Ulrich Drepper
2001-04-19 21:41                         ` Linus Torvalds
2001-04-19 22:46                           ` Ulrich Drepper
2001-04-20  1:35                             ` Alexander Viro
2001-04-20  2:45                               ` Ulrich Drepper
2001-04-19 16:43           ` Abramo Bagnara
2001-04-19 20:47           ` Ingo Oeser
2001-04-19 20:54             ` Linus Torvalds
2001-04-19  9:08       ` Ingo Oeser
2001-04-19 11:51       ` Alan Cox
2001-04-19 16:03       ` Linus Torvalds
2001-04-19 16:38         ` Alan Cox
2001-04-19 16:46           ` Linus Torvalds
2001-04-19 17:12             ` Alan Cox
2001-04-19 22:35               ` Rogier Wolff
2001-04-20  9:29             ` Olaf Titz
2001-04-20 14:19               ` Jesse Pollard [this message]
2001-04-20 18:36                 ` Olaf Titz
2001-04-20 23:33               ` Linus Torvalds
2001-04-21  4:06                 ` fd allocation [was: light weight user level semaphores] Edgar Toernig
2001-04-22  9:48                   ` Olaf Titz
2001-04-22 11:41                     ` light weight user level semaphores Alon Ziv
2001-04-22 12:44                       ` Alan Cox
2001-04-22 15:19                         ` Alon Ziv
2001-04-22 14:31                           ` Alexander Viro
2001-04-22 16:08                             ` Alon Ziv
2001-04-22 11:41                     ` Alon Ziv
2001-04-22 14:18                     ` David Woodhouse
2001-04-23 13:19                       ` David Howells
2001-04-23 14:48                         ` Alon Ziv
2001-04-23 15:40                           ` David Howells
2001-04-21 10:13                 ` Olaf Titz
2001-04-23 15:34                 ` Jeff Garzik
2001-04-23 19:18             ` Ingo Oeser
2001-04-24  0:19             ` David Wagner
2001-04-24  0:41               ` Alexander Viro
2001-04-19 19:47           ` Ulrich Drepper
2001-04-19 18:48         ` Olaf Titz
2001-04-19 13:59 George Talbot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200104201419.JAA50024@tomcat.admin.navo.hpc.mil \
    --to=pollard@tomcat.admin.navo.hpc.mil \
    --cc=linux-kernel@vger.kernel.org \
    --cc=olaf@bigred.inka.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox