/**
 * Filename: limits.c
 * Author: Neil Matthews & Richard Stones
 * Source: 'Beginning Linux Programming'
 * Purpose: Demonstrate the setting of resources and limits
 *          under Linux
 **/

#include <math.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>

/* Write a string to a temporary file 10000 times and then */
/* perform some arithmetic to generate a load on the CPU   */
void work() {

  FILE *f;
  int i;
  double x = 4.5;
  
  f = tmpfile();
  for(i = 0; i < 10000; i++) {
    fprintf(f, "Do some output\n");
    if (ferror(f)) {
      fprintf(stderr, "Error writing to temporary file\n");
      exit(1);
    }
  }

  for(i = 0; i < 10000; i++) {
    x = log(x * x + 3.21);
  }
}

int main() {

  struct rusage r_usage;
  struct rlimit r_limit;
  int priority;

  /* Call work() function and then use getrusage to report CPU usage */
  work();
  getrusage(RUSAGE_SELF, &r_usage);

  printf("CPU usage: User= %ld.%06ld, System = %ld.%06ld\n",
	 r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
	 r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);

  /* Find the current priority and file size limits */
  priority = getpriority(PRIO_PROCESS, getpid());
  printf("Current priority = %d\n", priority);

  getrlimit(RLIMIT_FSIZE, &r_limit);
  printf("Current FSIZE limit: soft = %ld, hard = %ld\n",
	 r_limit.rlim_cur, r_limit.rlim_max);

  /* Set a file size limit and call work() again */
  r_limit.rlim_cur = 2048;
  r_limit.rlim_max = 4096;
  printf("Setting a 2K file size limit\n");
  setrlimit(RLIMIT_FSIZE, &r_limit);

  work();

  exit(0);
}
