Pthread attr setting doesn't work before thread create?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Pthread attr setting doesn't work before thread create?
# 1  
Old 05-16-2011
Pthread attr setting doesn't work before thread create?

Hello everyone,
I created a test program for pthread priority set. Here's the code, very simple, 60 lines only.
I've tried this prog on my Fedora 13(on vbox), and on my 6410 arm linux 2.6.36. Both the same result.
Both environments are using root privileges.
Can any body tells me why the prio & policy set before thread create is not worked?
Thanks in advance.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>

static pthread_t ptchild;
void* childthread(void* arg)
{
    struct sched_param pr;
    int ret = 9;
    int policy;

    pthread_getschedparam(pthread_self(), &policy, &pr);
    printf("Child Thread Up PL%d PRI%d!\n", policy, pr.sched_priority); //The result here 

    policy = SCHED_RR;
    pr.sched_priority = 19;
    pthread_setschedparam(pthread_self(), policy, &pr);
    sleep(1);

    pthread_getschedparam(pthread_self(), &policy, &pr);
    printf("Child Thread Up PL%d PRI%d!\n", policy, pr.sched_priority); //resutl set

    sleep(1);
    printf("child exit\n");
    pthread_exit((void*)ret);
}

void main(void)
{
    pthread_attr_t attr;
    struct sched_param pr;
    int ret;
    void* childret;

    pr.sched_priority = 19;
    
#if 1
    printf("%d\n", pthread_attr_init(&attr));
    printf("%d\n", pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
    printf("%d\n", pthread_attr_setschedpolicy(&attr, SCHED_RR));
    printf("%d\n", pthread_attr_setschedparam(&attr, &pr));

#endif

    if(ret = pthread_create(&ptchild, &attr, &childthread, NULL)<0){
        printf("Thread Create Err %d\n", ret);
    }

    /* Wait write Process end, then cancel report thread */
    if(ret = pthread_join(ptchild, &childret) < 0){
        printf("Thread Join Err %d\n", ret);

    }
    else{
        printf("joined, ret = %d\n", (int)childret);
    }
    return;    

}

The result is:
Code:
0
0
0
0
Child Thread Up PL0 PRI0!   <- strange here, policy & prio not set
Child Thread Up PL2 PRI19!  <- in thread settings worked?
child exit
joined, ret = 9

# 2  
Old 05-17-2011
Perhaps your thread lacked PTHREAD_SCOPE_SYSTEM, i.e., was not a lwp, so the lwp and its scheduling nice-ness is that of the parent.
# 3  
Old 05-19-2011
Yeah, that's the classical trap when using real-time scheduling. You need to set the inheritsched attribute to inherit scheduling attribute at thread's creation. See:
Code:
man pthread_attr_setinheritsched

HTH, Loïc

---------- Post updated at 10:43 PM ---------- Previous update was at 10:35 PM ----------

Quote:
Originally Posted by DGPickett
Perhaps your thread lacked PTHREAD_SCOPE_SYSTEM, i.e., was not a lwp, so the lwp and its scheduling nice-ness is that of the parent.
I know only 2 UNIX variant that implements a N-M Scheduler: Solaris (up to 8) and Tru64. Does anyone know others?

AFAICS, only Tru64 managed to have an efficient version that worked. Though interesting, Solaris abandoned the M-N Scheduler for a (easier) 1-1 in Solaris 9.

Linux always used a 1-1 variant. There was an attempt with NGPT from IBM to have a M-N variant; it got quickly surpassed by the NPTL 1-1 implementation and the O(1) kernel scheduler class introduced in the 2.6. kernel series.

Cheers, Loïc
# 4  
Old 05-19-2011
So usually nice is per process, not per thread, for now?
# 5  
Old 05-20-2011
Quote:
Originally Posted by DGPickett
So usually nice is per process, not per thread, for now?
POSIX states that the nice is a process-wide attribute.

NPTL used to have a non-conformance bug concerning that point (i.e threads do no share a common nice value). I don't if it's fixed meanwhile.

Cheers, Loïc
# 6  
Old 05-25-2011
If the threads of one LWP had different scheduler priorities, on a N-M schedulet, does nice change values as threads are given the LWP? Process nice variations are not so good for handling this. I guess the LWP would need to stay at the max nice of any thread so as to prevent a low nice thread getting the LWP rolled out while the high pri thread is now needed to run.
# 7  
Old 05-27-2011
Hi DGPicket,

perhaps I am not following you completely...

When you say "LWP had different scheduler priorities", I assume you mean that these LWPs are using a real-time scheduling (like SCHED_FIFO or SCHED_RR for instance). In this particular case, changing the nice value of the process does not influence these threads, as stated in the POSIX standard:

Quote:
Calling the nice() function has no effect on the priority of processes or threads with policy SCHED_FIFO or SCHED_RR. The effect on processes or threads with other scheduling policies is implementation-defined.
Or I am missing something?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Thread / post doesn't open

Dear colleagues, this post doesn't open; error message: Anything we / I can do? Rgds Rüdiger (0 Replies)
Discussion started by: RudiC
0 Replies

2. Shell Programming and Scripting

Timeout doesn't work, please help me

#!/bin/sh trap "cleanup" TERM timeout=5 mainpid=$$ cleanup() { echo "at $i interupt" kill -9 0 } watchdog() { sleep $1 } (watchdog $timeout && kill -TERM $mainpid) & run_test() (10 Replies)
Discussion started by: yanglei_fage
10 Replies

3. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

4. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

5. Shell Programming and Scripting

echo doesn't work right

Hi,when I run my first shell script,I got something that doesn't work right. I wrote this code in the script. echo -e "Hello,World\a\n"But the screen print like this: -e Hello,World The "-e" wasn't supposed to be printed out. Can anyone help me out?:wall: Many thanks!:) (25 Replies)
Discussion started by: Demon
25 Replies

6. Shell Programming and Scripting

Help with script.. it Just doesn't work

Hello,, Im verry new to scripting and have some problems with this script i made.. What it does: It checks a directory for a new directory and then issues a couple of commands. checks sfv - not doing right now checks rar - it checks if theres a rar file and when there is it skips to... (1 Reply)
Discussion started by: atmosroll
1 Replies

7. UNIX for Advanced & Expert Users

remsh doesn't work

Hi, I need to use remsh inside a ksh script. The script would remsh to another machine (maybe different OS) and then execute commands. A Simple Script: #!/usr/bin/ksh remsh sun7656 -l myuser "cd /user.3/MyFolder; ls -lart" But this gives me the error: permission denied I also... (4 Replies)
Discussion started by: som.nitk
4 Replies

8. Shell Programming and Scripting

ls -d doesn't work on Solaris

Hello, the ls -d command to only list directories in a directory doesn't seem to work on Solaris and the man command says to use that combination: ls -d Anyone have the same problem and find a resolve? Thanks BobK (9 Replies)
Discussion started by: bobk544
9 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

10. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question