Advertisement
m027

Untitled

Jul 7th, 2022
718
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None
  1. #include<stdio.h>           // printf
  2. #include<time.h>            // timespec, clock_gettime, CLOCK_PROCESS_CPUTIME_ID ...
  3. #include<stdlib.h>          // atoi, atol
  4. #include<unistd.h>          // getpid
  5. #include<sys/resource.h>    // setPriority, PRIO_PROCESS
  6.  
  7.  
  8. /*
  9.  This program generates a specified amount of load on the CPU depending on the arguments it was given.
  10.  arguments:
  11.     $label = a label for this process. Used to differenciate between different processes' outputs.
  12.     $num_slices = the number of slices before the process ends
  13.     $prio = the Unix-priority for the Job 0..19
  14.     $load = (optional) the size of a single slice. Replaces hardcoded value.
  15. */
  16. int main(int argc, char *args[]){
  17.     // first let's make sure, the number of arguments is acceptable.
  18.     // your program should be called with four arguments.
  19.     // Let's check the number of arguments.
  20.     if (argc < 5 ){
  21.         printf("usage: %s $LABEL, $NUM_SLICES, $PRIO, $LOAD\n", args[0]);
  22.         return -1 ;
  23.     }
  24.    
  25.     // use variables to store the arguments and make the code readable.
  26.     // Hint: int atoi(char *) https://linux.die.net/man/3/atoi
  27.     // Hint: int atol(char *) https://linux.die.net/man/3/atol
  28.     char *programName   = args[0] ;
  29.     char *label         = args[1] ;
  30.     int  num_slices     = atoi(args[2]) ;
  31.     int  prio       = atoi(args[3]) ;
  32.     long load       = atoi(args[4]) ; // for large numbers long must be used instead of int
  33.    
  34.    
  35.     // Set the priority of this process to what was given as argument.
  36.     if (setpriority (PRIO_PROCESS, getpid(), prio ) == -1) {
  37.         perror("Error when using setpriority()");
  38.     }
  39.    
  40.    
  41.     /*
  42.     Loop example: num_slices = 2, load = 3
  43.    
  44.     >-time-->
  45.    
  46.     |<--iter-->|<--iter-->|<--iter-->|<--iter-->|<--iter-->|<--iter-->|
  47.     |<------------slice------------->|<------------slice------------->|
  48.     |<----------------------------program---------------------------->|
  49.     */
  50.    
  51.     // get the zero time point
  52.     struct timespec tP0;
  53.     clock_gettime(CLOCK_REALTIME, &tP0);
  54.     // Now print the output as requested in the exercise.
  55.     printf("%s %s %3d %10ld %9ld\n",
  56.         programName ,
  57.         label ,
  58.         0,
  59.         tP0.tv_sec,
  60.         tP0.tv_nsec
  61.         );
  62.  
  63.     // create variable for the Completion Time
  64.         struct timespec tPC;   
  65.  
  66.     // (outer)loop for slices
  67.     for (int slice = 1; slice <= num_slices ; slice++){
  68.         // (inner)loop for iterations of single slice. This (inner)loop will take one second each time to process.
  69.         long dummy;
  70.         for (int iteration = 0; iteration < load ; iteration++){
  71.             dummy = dummy + 1;  // just to calculate someting and generate load
  72.         }
  73.         // get the Completion Time
  74.         clock_gettime(CLOCK_REALTIME, &tPC);
  75.         // Now print the output as requested in the exercise.
  76.         printf("%s %s %4d %10ld %9ld\n",
  77.             programName ,
  78.             label ,
  79.             slice ,
  80.             tPC.tv_sec,
  81.             tPC.tv_nsec
  82.             );
  83.     }
  84.    
  85.     return 0;
  86. }
  87.  
Advertisement
RAW Paste Data Copied
Advertisement