/* rt - a utility to set the realtime priority and scheduling policy */ /* includes */ #include #include #include #include #define _GNU_LIBRARY__ #include /* defines */ #define RUNSTRING "usage: rt [-f] [-v] prio [--] runstring \n \ or: rt [-f] [-v] -p PID prio\\ \n\n \ where: prio specifies the realtime priority \n \ -f set scheduling policy to SCHED_FIFO \n \ -v turns on verbose mode. \n \ -p PID specifies an existing process to modify \n \ runstring is a process and parameters \n \ (use '--' if runstring contains options). \n" #define POLICY(x) x ? x-1 ? "SCHED_RR" : "SCHED_FIFO" : "SCHED_OTHER" /* prototypes */ void print_usage(char *[]); /* globals */ int verbose=0; /* 0=none, !0=verbose */ main(int argc, char *argv[]) { struct sched_param prio_struct; int policy = -1; int pid = 0; int pidopt = 0; int optprobs = 0; /* problems parsing? */ int c; /* generic single character */ /* "standard" option parsing... */ while ( (c=getopt(argc, argv, "+fp:v?")) != EOF) { switch (c) { case 'f': /* set FIFO mode */ policy = SCHED_FIFO; break; case 'p': /* read PID */ sscanf(optarg,"%d",&pid); pidopt=1; break; case 'v': verbose=1; /* verbosity */ break; case '?': /* help? */ printf("%s",RUNSTRING); exit(0); default: /* something went wrong */ optprobs=1; /* we'll deal with this problem later */ break; } } if (optprobs) { fprintf(stderr,RUNSTRING); exit(1); } if((argc - optind) < 2-pidopt) { print_usage(argv); } sscanf(argv[optind], "%d", &(prio_struct.sched_priority)); /* sanity checking... */ if ( (prio_struct.sched_priority != 0) && (policy < 0 ) ) { policy=SCHED_RR; if (verbose) printf("Defaulting sched policy to %s.\n", POLICY(policy)); } if ( (prio_struct.sched_priority == 0 ) && (policy != SCHED_OTHER) ) { policy=SCHED_OTHER; fprintf(stderr,"Priority of %d implies sched policy of %s.\n", prio_struct.sched_priority, POLICY(policy)); } policy = (prio_struct.sched_priority)? policy : SCHED_OTHER; if( sched_setscheduler(pid,policy,&prio_struct)){ perror("Priority out of range"); print_usage(argv); } if ( pid ) exit(0); argv+=optind; /* adjust argv to point to the runstring */ argv++; execvp(argv[0],argv); perror("exec failed.."); } void print_usage(char * who[]) { printf("%s",RUNSTRING); exit (1); }