Skip to content

Instantly share code, notes, and snippets.

@khrm
Created July 2, 2011 00:07
Show Gist options
  • Save khrm/1059613 to your computer and use it in GitHub Desktop.
Save khrm/1059613 to your computer and use it in GitHub Desktop.
#!/bin/sh
cat > wje_q.c <<EOD
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
void *second_thread_run_function(void *arg)
{
printf("inside second thread\n");
return NULL;
} /* second_thread_run_function() */
int main(void)
{
int status;
pthread_t second_thread_id;
pthread_attr_t thread_attr;
struct sched_param thread_param;
if(pthread_attr_init(&thread_attr))
{
fprintf(stderr,
"error pthread_attr_init()\n"
);
exit(1);
}
if(pthread_attr_setschedpolicy(&thread_attr,
SCHED_RR
)
)
{
fprintf(stderr,
"error pthread_attr_setschedpolicy()\n"
);
exit(1);
}
printf("%d %d\n",
sched_get_priority_min(SCHED_RR),
sched_get_priority_max(SCHED_RR)
);
thread_param.sched_priority=(sched_get_priority_min(SCHED_RR)
+sched_get_priority_max(SCHED_RR)
)
/2;
if(pthread_attr_setschedparam(&thread_attr,
&thread_param
)
)
{
fprintf(stderr,
"error pthread_attr_setschedparam()\n"
);
exit(1);
}
if(pthread_attr_setinheritsched(&thread_attr,
PTHREAD_EXPLICIT_SCHED
)
)
{
fprintf(stderr,
"error pthread_attr_setinheritsched()\n"
);
exit(1);
}
status=pthread_create(&second_thread_id,
&thread_attr,
second_thread_run_function,
NULL
);
if(status!=0)
{
if(status==EPERM)
{
fprintf(stderr,
"pthread_create() got EPERM\n"
);
}
else
if(status==EINVAL)
{
fprintf(stderr,
"pthread_create() got EINVAL\n"
);
}
else
{
fprintf(stderr,
"pthread_create() got neither EPERM nor EINVAL\n"
);
}
fprintf(stderr,
"pthread_create() got error %d\n",
status
);
errno=status;
perror("pthread_create()");
exit(1);
}
if(pthread_join(second_thread_id,
NULL
)
)
{
fprintf(stderr,
"error pthread_join()\n"
);
exit(1);
}
return 0;
} /* main() */
EOD
gcc -pthread -Wall wje_q.c -o wje_q
./wje_q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment