Skip to content

Instantly share code, notes, and snippets.

@travispaul
Created February 7, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travispaul/662ea28076418f653b94 to your computer and use it in GitHub Desktop.
Save travispaul/662ea28076418f653b94 to your computer and use it in GitHub Desktop.
pthread test
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
void *perform_work(void *argument)
{
int passed_in_value;
passed_in_value = *((int *) argument);
printf("Hello World! It's me, thread with argument %d!\n", passed_in_value);
sleep(1);
}
int main(void)
{
pthread_t thread;
int thread_arg = 1;
int result_code;
struct sched_param param;
int policy = 0;
result_code = pthread_create(&thread, NULL, perform_work, (void *) &thread_arg);
assert(0 == result_code);
policy = SCHED_FIFO;
result_code = pthread_setschedparam(thread, policy, &param);
perror(NULL);
fprintf(stderr, "result_code: %i, policy: %i\n", result_code, policy);
fprintf(stderr, "EINVAL=%i, ENOTSUP=%i, ESRCH=%i\n", EINVAL, ENOTSUP, ESRCH);
assert(0 == result_code);
result_code = pthread_join(thread, NULL);
assert(0 == result_code);
exit(EXIT_SUCCESS);
}
@travispaul
Copy link
Author

As unprivileged user:

SCHED_FIFO

Operation not permitted
result_code: 1, policy: 1
EINVAL=22, ENOTSUP=86, ESRCH=3
assertion "0 == result_code" failed: file "thread.c", line 39, function "main"
Abort (core dumped) 

SCHED_RR

Operation not permitted
result_code: 1, policy: 2
EINVAL=22, ENOTSUP=86, ESRCH=3
assertion "0 == result_code" failed: file "thread.c", line 39, function "main"
Abort (core dumped) 

SCHED_OTHER

Invalid argument
result_code: 22, policy: 0
EINVAL=22, ENOTSUP=86, ESRCH=3
assertion "0 == result_code" failed: file "thread.c", line 39, function "main"
Abort (core dumped) 

As root:

SCHED_FIFO (success)

Undefined error: 0
result_code: 0, policy: 1
EINVAL=22, ENOTSUP=86, ESRCH=3
Hello World! It's me, thread with argument 1!

SCHED_RR (success)

Undefined error: 0
result_code: 0, policy: 2
EINVAL=22, ENOTSUP=86, ESRCH=3
Hello World! It's me, thread with argument 1!

SCHED_OTHER

Invalid argument
result_code: 22, policy: 0
EINVAL=22, ENOTSUP=86, ESRCH=3
assertion "0 == result_code" failed: file "thread.c", line 40, function "main"
Abort 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment