This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

pthread_attr_setsched behaviour


Hi,

i just recognized, that calling:

ret |= pthread_attr_setschedpolicy (..)
ret |= pthread_attr_setschedparam (..)
ret |= pthread_create (..)

doesn't return an error and the values set by pthread_attr_setschedparam and
pthread_attr_setschedpolicy aren't applied to the thread.

The APPLICATION USAGE of the functions say:

   After  these attributes have been set, a thread can be created with the
   specified attributes using pthread_create(). Using these routines  does
   not affect the current running thread.

So I didn't expect that I need to call:
pthread_attr_setinheritsched before pthread_create.

I would like to improve the current behaviour, with a little patch;
please give me some directions...

Is there a reason why e.g. pthread_attr_set_schedparam doesn't set
iattr->flags |= ATTR_FLAG_NOTINHERITSCHED

Or is it better to return an error code in pthread_attr_setschedparam, etc if
ATTR_FLAG_NOTINHERITSCHED isn't set?

If both is bad, at least the man page should mention the need of calling
pthread_attr_setinheritsched..

Thanks,

  Manuel

---

The attached example produces the following output (with glibc-2.17):

 % sudo ./prio
f: prio 0
 % sudo ./prio bla
f: prio 80

---

#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

void *f (void *a)
{
    struct sched_param sched;
    int ret;

    ret = sched_getparam (0, &sched);

    if (ret)
         fprintf (stderr, "f: sched_getparam %d", strerror (ret));

    printf ("f: prio %d\n", sched.sched_priority);

    return NULL;
}

int main (int argc, char **argv)
{
    pthread_t t;
    pthread_attr_t a;
    int ret = 0;
    struct sched_param sched = {
        .sched_priority = 80,
    };

    ret = pthread_attr_init (&a);
    if (ret)
        fprintf (stderr, "pthread_attr_init: %s\n", strerror (ret));

    if (argc > 1) {
        ret =  pthread_attr_setinheritsched (&a, PTHREAD_EXPLICIT_SCHED);
        if (ret)
            fprintf (stderr, "pthread_attr_setinheritsched: %s\n", strerror (ret));
    }

    ret = pthread_attr_setschedpolicy (&a, SCHED_FIFO);
    if (ret)
        fprintf (stderr, "pthread_attr_setschedpolicy: %s\n", strerror (ret));

    ret = pthread_attr_setschedparam (&a, &sched);
    if (ret)
        fprintf (stderr, "pthread_attr_setschedparam: %s\n", strerror (ret));

    ret = pthread_create (&t, &a, f, NULL);
    if (ret) {
        fprintf (stderr, "pthread_create f %s\n", strerror (ret));
        return ret;
    }

    pthread_join (t, NULL);

    return ret;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]