Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <pthread.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/resource.h>
- #include <sys/syscall.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define gettid() (syscall(SYS_gettid))
- pthread_barrier_t barrier;
- static void burn_cpu(char *caller)
- {
- long int i = 0;
- while (1) {
- long int foo = mrand48();
- i++;
- if ((i % 10000000) == 0) {
- printf("%s %ld\n", caller, i / 10000000);
- }
- }
- }
- static void *bg_worker_func(void *arg)
- {
- int tid = gettid();
- setpriority(PRIO_PROCESS, tid, 10);
- pthread_barrier_wait(&barrier);
- burn_cpu("bg_worker");
- pthread_exit(NULL);
- }
- int main( void )
- {
- pid_t child;
- pthread_t bg_worker;
- child = fork();
- if (child == 0) {
- pthread_barrier_init(&barrier, NULL, 2);
- pthread_create(&bg_worker, NULL, bg_worker_func, NULL);
- setpriority(PRIO_PROCESS, getpid(), 20);
- pthread_barrier_wait(&barrier);
- burn_cpu("main");
- pthread_join(bg_worker, NULL);
- exit(EXIT_SUCCESS);
- } else {
- sleep(3);
- int rv = setpriority(PRIO_PROCESS, child, 0);
- printf("setpriority called! rv = %d\n", rv);
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement