Back to index

glibc  2.9
tst-sched1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <pthread.h>
00021 #include <stdio.h>
00022 #include <unistd.h>
00023 #include <sys/types.h>
00024 
00025 
00026 static int global;
00027 
00028 static void *
00029 tf (void *a)
00030 {
00031   global = 1;
00032 
00033   return 0;
00034 }
00035 
00036 
00037 int
00038 do_test (void)
00039 {
00040   pthread_t th;
00041   pthread_attr_t at;
00042 
00043   if (pthread_attr_init (&at) != 0)
00044     {
00045       puts ("attr_init failed");
00046       return 1;
00047     }
00048 
00049   if (pthread_attr_setschedpolicy (&at, SCHED_OTHER) != 0)
00050     {
00051       puts ("attr_setschedpolicy failed");
00052       return 1;
00053     }
00054 
00055   struct sched_param pa;
00056   if (sched_getparam (getpid (), &pa) != 0)
00057     {
00058       puts ("sched_getschedparam failed");
00059       return 1;
00060     }
00061 
00062   if (pthread_attr_setschedparam (&at, &pa) != 0)
00063     {
00064       puts ("attr_setschedparam failed");
00065       return 1;
00066     }
00067 
00068   if (pthread_attr_setinheritsched (&at, PTHREAD_EXPLICIT_SCHED) != 0)
00069     {
00070       puts ("attr_setinheritsched failed");
00071       return 1;
00072     }
00073 
00074   if (pthread_create (&th, &at, tf, NULL) != 0)
00075     {
00076       puts ("create failed");
00077       return 1;
00078     }
00079 
00080   int e = pthread_join (th, NULL);
00081   if (e != 0)
00082     {
00083       printf ("join failed: %d\n", e);
00084       return 1;
00085     }
00086 
00087   if (global == 0)
00088     {
00089       puts ("thread didn't run");
00090       return 1;
00091     }
00092 
00093   return 0;
00094 }
00095 
00096 
00097 #define TEST_FUNCTION do_test ()
00098 #include "../test-skeleton.c"