#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>

static void terminate(const char *msg, int code)
{
    perror(msg);
    exit(code);
}

int main(int argc, char **argv)
{
    int pid, old, new;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <priority: -20 to 19>\n", argv[0]);
        return 1;
    }

    new = atoi(argv[1]);

    pid = fork();
    if (pid == -1)
        terminate("fork", 1);

    if (pid == 0) {
        getchar();
    } else {
        old = getpriority(PRIO_PROCESS, pid);
        if (new < old) {
            fprintf(stderr, "Cannot elevate priority as non-root user\n");
            return 1;
        }
        setpriority(PRIO_PROCESS, pid, new);
        new = getpriority(PRIO_PROCESS, pid);
        printf("%d %d\n", old, new);
        wait(NULL);

    }
    return 0;
}
