rt_sched.lua
#! /usr/bin/env lua
local M = require 'posix.sched'
local mypid = require 'posix.unistd'.getpid()
local function get_ps_sched(pid)
local fp = io.popen(string.format('ps --no-headers -o "policy,rtprio" %s', pid))
local res, err = fp:read'*a'
assert(res, err)
fp:close()
local policy, rtprio = string.match(res, '([^%s]+)%s+([^%s]+)')
assert(policy)
assert(rtprio)
return policy, rtprio
end
do
local res, err = M.sched_getscheduler(0) assert(res == M.SCHED_OTHER)
local res, err = M.sched_getscheduler() assert(res == M.SCHED_OTHER)
local policy, rtprio = get_ps_sched(mypid)
assert(policy== 'TS')
assert(rtprio== '-')
local res, err = M.sched_setscheduler(0, p.SCHED_FIFO, 10 )
assert(res, err)
local policy, rtprio = get_ps_sched(mypid)
assert(policy == 'FF')
assert(rtprio == '10')
local res, err = M.sched_getscheduler(0)
assert(res == M.SCHED_FIFO)
local res, err = M.sched_setscheduler(0, M.SCHED_RR, 11 )
assert(res, err)
local policy, rtprio = get_ps_sched(mypid)
assert(policy == 'RR')
assert(rtprio == '11')
local res, err = M.sched_getscheduler(0)
assert(res == M.SCHED_RR)
local res, err = M.sched_setscheduler()
assert(res, err)
local policy, rtprio = get_ps_sched(mypid)
assert(policy == 'TS')
assert(rtprio == '-')
local res, err = M.sched_getscheduler(0)
assert(res == M.SCHED_OTHER)
end
do
local U = require 'posix.unistd'
local r, w = U.pipe()
local childpid = U.fork()
if childpid == 0 then
U.close(w) local b = U.read(r,1)
U._exit(0)
end
U.close(r)
do local res, err = M.sched_getscheduler(childpid)
assert(res == M.SCHED_OTHER)
local policy, rtprio = get_ps_sched(childpid)
assert(policy == 'TS')
assert(rtprio == '-')
local res, err = M.sched_setscheduler(childpid, M.SCHED_FIFO, 10 )
assert(res, err)
local policy, rtprio = get_ps_sched(childpid)
assert(policy == 'FF')
assert(rtprio == '10')
local res, err = M.sched_getscheduler(childpid)
assert(res == M.SCHED_FIFO)
local res, err = M.sched_setscheduler(childpid, M.SCHED_RR, 11 )
assert(res, err)
local policy, rtprio = get_ps_sched(childpid)
assert(policy == 'RR')
assert(rtprio == '11')
local res, err = M.sched_getscheduler(childpid)
assert(res == M.SCHED_RR)
local res, err = M.sched_setscheduler(childpid)
assert(res, err)
local policy, rtprio = get_ps_sched(childpid)
assert(policy == 'TS')
assert(rtprio == '-')
local res, err = M.sched_getscheduler(childpid)
assert(res == M.SCHED_OTHER)
end
U.write(w, 'stop')
U.close(w)
require 'posix.sys.wait'.wait(childpid)
end