
// Simple program to set the scheduler (one of FIFO, RR and OTHER)
// of the given process (see "man sched_setscheduler")
// By
// 	> chown root.cdwriter setScheduler
// 	> chmod 4750 setScheduler
// root can offer the group cdwriter to set the scheduler of mkisofs.
//
// Author: Jörg Haeger, 06.11.2000

#include <sched.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *args[])
{
	int pid, pri;
	if (argc != 4
		|| sscanf(args[1], "%d", &pid) != 1
		|| (strcmp(args[2], "FIFO") != 0
			&& strcmp(args[2], "RR") != 0
			&& strcmp(args[2], "OTHER") != 0)
		|| sscanf(args[3], "%d", &pri) != 1)
	{
		printf("Usage: %s pid [FIFO | RR | OTHER] priority\n",
			args[0]);
		return 1;
	}

	int policy = SCHED_OTHER;
	if (strcmp(args[2], "FIFO") == 0)
		policy = SCHED_FIFO;
	else
	if (strcmp(args[2], "RR") == 0)
		policy = SCHED_RR;

	struct sched_param schedParam;
	sched_getparam(pid, &schedParam);

	int oldPolicy = sched_getscheduler(pid);
	if (oldPolicy < 0)
	{
		printf("pid %d does not exists!\n", pid);
		return 2;
	}

	printf("old: scheduler %d, priority %d\n",
		oldPolicy, schedParam.sched_priority);

	schedParam.sched_priority = pri;
	if (sched_setscheduler(pid, policy, &schedParam) < 0)
	{
		printf("Cannot set %s, priority %d.\n", args[2], pri);
		return 3;
	}

	sched_getparam(pid, &schedParam);
	printf("new: scheduler %d, priority %d\n",
		sched_getscheduler(pid), schedParam.sched_priority);

	return 0;
}
