Advertisement
gsvelto

setpriority() test program

Apr 11th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include <sys/time.h>
  7. #include <sys/resource.h>
  8. #include <sys/syscall.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #define gettid() (syscall(SYS_gettid))
  13.  
  14. pthread_barrier_t barrier;
  15.  
  16. static void burn_cpu(char *caller)
  17. {
  18.     long int i = 0;
  19.  
  20.     while (1) {
  21.         long int foo = mrand48();
  22.        
  23.         i++;
  24.        
  25.         if ((i % 10000000) == 0) {
  26.             printf("%s %ld\n", caller, i / 10000000);
  27.         }
  28.     }
  29. }
  30.  
  31. static void *bg_worker_func(void *arg)
  32. {
  33.     int tid = gettid();
  34.    
  35.     setpriority(PRIO_PROCESS, tid, 10);
  36.  
  37.     pthread_barrier_wait(&barrier);
  38.  
  39.     burn_cpu("bg_worker");
  40.  
  41.     pthread_exit(NULL);
  42. }
  43.  
  44. int main( void )
  45. {
  46.     pid_t child;
  47.     pthread_t bg_worker;
  48.  
  49.     child = fork();
  50.  
  51.     if (child == 0) {
  52.         pthread_barrier_init(&barrier, NULL, 2);
  53.         pthread_create(&bg_worker, NULL, bg_worker_func, NULL);
  54.  
  55.         setpriority(PRIO_PROCESS, getpid(), 20);
  56.         pthread_barrier_wait(&barrier);
  57.  
  58.         burn_cpu("main");
  59.    
  60.         pthread_join(bg_worker, NULL);
  61.         exit(EXIT_SUCCESS);
  62.     } else {
  63.         sleep(3);
  64.         int rv = setpriority(PRIO_PROCESS, child, 0);
  65.         printf("setpriority called! rv = %d\n", rv);
  66.     }
  67.    
  68.     exit(EXIT_SUCCESS);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement