Sponsored Content
Top Forums UNIX for Advanced & Expert Users Pthread attr setting doesn't work before thread create? Post 302522853 by ss1969 on Monday 16th of May 2011 10:12:30 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PTHREAD_SETSCHEDPARAM(3)				     Linux Programmer's Manual					  PTHREAD_SETSCHEDPARAM(3)

NAME
pthread_setschedparam, pthread_getschedparam - set/get scheduling policy and parameters of a thread SYNOPSIS
#include <pthread.h> int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param); int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param); Compile and link with -pthread. DESCRIPTION
The pthread_setschedparam() function sets the scheduling policy and parameters of the thread thread. policy specifies the new scheduling policy for thread. The supported values for policy, and their semantics, are described in sched(7). The structure pointed to by param specifies the new scheduling parameters for thread. Scheduling parameters are maintained in the follow- ing structure: struct sched_param { int sched_priority; /* Scheduling priority */ }; As can be seen, only one scheduling parameter is supported. For details of the permitted ranges for scheduling priorities in each schedul- ing policy, see sched(7). The pthread_getschedparam() function returns the scheduling policy and parameters of the thread thread, in the buffers pointed to by policy and param, respectively. The returned priority value is that set by the most recent pthread_setschedparam(), pthread_setschedprio(3), or pthread_create(3) call that affected thread. The returned priority does not reflect any temporary priority adjustments as a result of calls to any priority inheritance or priority ceiling functions (see, for example, pthread_mutexattr_setprioceiling(3) and pthread_mutex- attr_setprotocol(3)). RETURN VALUE
On success, these functions return 0; on error, they return a nonzero error number. If pthread_setschedparam() fails, the scheduling pol- icy and parameters of thread are not changed. ERRORS
Both of these functions can fail with the following error: ESRCH No thread with the ID thread could be found. pthread_setschedparam() may additionally fail with the following errors: EINVAL policy is not a recognized policy, or param does not make sense for the policy. EPERM The caller does not have appropriate privileges to set the specified scheduling policy and parameters. POSIX.1 also documents an ENOTSUP ("attempt was made to set the policy or scheduling parameters to an unsupported value") error for pthread_setschedparam(). ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +-------------------------+---------------+---------+ |Interface | Attribute | Value | +-------------------------+---------------+---------+ |pthread_setschedparam(), | Thread safety | MT-Safe | |pthread_getschedparam() | | | +-------------------------+---------------+---------+ CONFORMING TO
POSIX.1-2001, POSIX.1-2008. NOTES
For a description of the permissions required to, and the effect of, changing a thread's scheduling policy and priority, and details of the permitted ranges for priorities in each scheduling policy, see sched(7). EXAMPLE
The program below demonstrates the use of pthread_setschedparam() and pthread_getschedparam(), as well as the use of a number of other scheduling-related pthreads functions. In the following run, the main thread sets its scheduling policy to SCHED_FIFO with a priority of 10, and initializes a thread attributes object with a scheduling policy attribute of SCHED_RR and a scheduling priority attribute of 20. The program then sets (using pthread_attr_setinheritsched(3)) the inherit scheduler attribute of the thread attributes object to PTHREAD_EXPLICIT_SCHED, meaning that threads created using this attributes object should take their scheduling attributes from the thread attributes object. The program then creates a thread using the thread attributes object, and that thread displays its scheduling policy and priority. $ su # Need privilege to set real-time scheduling policies Password: # ./a.out -mf10 -ar20 -i e Scheduler settings of main thread policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr' policy=SCHED_RR, priority=20 inheritsched is EXPLICIT Scheduler attributes of new thread policy=SCHED_RR, priority=20 In the above output, one can see that the scheduling policy and priority were taken from the values specified in the thread attributes object. The next run is the same as the previous, except that the inherit scheduler attribute is set to PTHREAD_INHERIT_SCHED, meaning that threads created using the thread attributes object should ignore the scheduling attributes specified in the attributes object and instead take their scheduling attributes from the creating thread. # ./a.out -mf10 -ar20 -i i Scheduler settings of main thread policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr' policy=SCHED_RR, priority=20 inheritsched is INHERIT Scheduler attributes of new thread policy=SCHED_FIFO, priority=10 In the above output, one can see that the scheduling policy and priority were taken from the creating thread, rather than the thread attributes object. Note that if we had omitted the -i i option, the output would have been the same, since PTHREAD_INHERIT_SCHED is the default for the inherit scheduler attribute. Program source /* pthreads_sched_test.c */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #define handle_error_en(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) static void usage(char *prog_name, char *msg) { if (msg != NULL) fputs(msg, stderr); fprintf(stderr, "Usage: %s [options] ", prog_name); fprintf(stderr, "Options are: "); #define fpe(msg) fprintf(stderr, " %s", msg); /* Shorter */ fpe("-a<policy><prio> Set scheduling policy and priority in "); fpe(" thread attributes object "); fpe(" <policy> can be "); fpe(" f SCHED_FIFO "); fpe(" r SCHED_RR "); fpe(" o SCHED_OTHER "); fpe("-A Use default thread attributes object "); fpe("-i {e|i} Set inherit scheduler attribute to "); fpe(" 'explicit' or 'inherit' "); fpe("-m<policy><prio> Set scheduling policy and priority on "); fpe(" main thread before pthread_create() call "); exit(EXIT_FAILURE); } static int get_policy(char p, int *policy) { switch (p) { case 'f': *policy = SCHED_FIFO; return 1; case 'r': *policy = SCHED_RR; return 1; case 'o': *policy = SCHED_OTHER; return 1; default: return 0; } } static void display_sched_attr(int policy, struct sched_param *param) { printf(" policy=%s, priority=%d ", (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER" : "???", param->sched_priority); } static void display_thread_sched_attr(char *msg) { int policy, s; struct sched_param param; s = pthread_getschedparam(pthread_self(), &policy, &param); if (s != 0) handle_error_en(s, "pthread_getschedparam"); printf("%s ", msg); display_sched_attr(policy, &param); } static void * thread_start(void *arg) { display_thread_sched_attr("Scheduler attributes of new thread"); return NULL; } int main(int argc, char *argv[]) { int s, opt, inheritsched, use_null_attrib, policy; pthread_t thread; pthread_attr_t attr; pthread_attr_t *attrp; char *attr_sched_str, *main_sched_str, *inheritsched_str; struct sched_param param; /* Process command-line options */ use_null_attrib = 0; attr_sched_str = NULL; main_sched_str = NULL; inheritsched_str = NULL; while ((opt = getopt(argc, argv, "a:Ai:m:")) != -1) { switch (opt) { case 'a': attr_sched_str = optarg; break; case 'A': use_null_attrib = 1; break; case 'i': inheritsched_str = optarg; break; case 'm': main_sched_str = optarg; break; default: usage(argv[0], "Unrecognized option "); } } if (use_null_attrib && (inheritsched_str != NULL || attr_sched_str != NULL)) usage(argv[0], "Can't specify -A with -i or -a "); /* Optionally set scheduling attributes of main thread, and display the attributes */ if (main_sched_str != NULL) { if (!get_policy(main_sched_str[0], &policy)) usage(argv[0], "Bad policy for main thread (-m) "); param.sched_priority = strtol(&main_sched_str[1], NULL, 0); s = pthread_setschedparam(pthread_self(), policy, &param); if (s != 0) handle_error_en(s, "pthread_setschedparam"); } display_thread_sched_attr("Scheduler settings of main thread"); printf(" "); /* Initialize thread attributes object according to options */ attrp = NULL; if (!use_null_attrib) { s = pthread_attr_init(&attr); if (s != 0) handle_error_en(s, "pthread_attr_init"); attrp = &attr; } if (inheritsched_str != NULL) { if (inheritsched_str[0] == 'e') inheritsched = PTHREAD_EXPLICIT_SCHED; else if (inheritsched_str[0] == 'i') inheritsched = PTHREAD_INHERIT_SCHED; else usage(argv[0], "Value for -i must be 'e' or 'i' "); s = pthread_attr_setinheritsched(&attr, inheritsched); if (s != 0) handle_error_en(s, "pthread_attr_setinheritsched"); } if (attr_sched_str != NULL) { if (!get_policy(attr_sched_str[0], &policy)) usage(argv[0], "Bad policy for 'attr' (-a) "); param.sched_priority = strtol(&attr_sched_str[1], NULL, 0); s = pthread_attr_setschedpolicy(&attr, policy); if (s != 0) handle_error_en(s, "pthread_attr_setschedpolicy"); s = pthread_attr_setschedparam(&attr, &param); if (s != 0) handle_error_en(s, "pthread_attr_setschedparam"); } /* If we initialized a thread attributes object, display the scheduling attributes that were set in the object */ if (attrp != NULL) { s = pthread_attr_getschedparam(&attr, &param); if (s != 0) handle_error_en(s, "pthread_attr_getschedparam"); s = pthread_attr_getschedpolicy(&attr, &policy); if (s != 0) handle_error_en(s, "pthread_attr_getschedpolicy"); printf("Scheduler settings in 'attr' "); display_sched_attr(policy, &param); s = pthread_attr_getinheritsched(&attr, &inheritsched); printf(" inheritsched is %s ", (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" : (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" : "???"); printf(" "); } /* Create a thread that will display its scheduling attributes */ s = pthread_create(&thread, attrp, &thread_start, NULL); if (s != 0) handle_error_en(s, "pthread_create"); /* Destroy unneeded thread attributes object */ if (!use_null_attrib) { s = pthread_attr_destroy(&attr); if (s != 0) handle_error_en(s, "pthread_attr_destroy"); } s = pthread_join(thread, NULL); if (s != 0) handle_error_en(s, "pthread_join"); exit(EXIT_SUCCESS); } SEE ALSO
getrlimit(2), sched_get_priority_min(2), pthread_attr_init(3), pthread_attr_setinheritsched(3), pthread_attr_setschedparam(3), pthread_attr_setschedpolicy(3), pthread_create(3), pthread_self(3), pthread_setschedprio(3), pthreads(7), sched(7) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2017-09-15 PTHREAD_SETSCHEDPARAM(3)
All times are GMT -4. The time now is 06:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy