Priority Scheduling
In Morinaga OS, optimizing system performance is crucial, especially when running resource-intensive applications. One effective method is adjusting the niceness of processes to prioritize critical tasks. This section explains how to change the niceness of a process, handled by a user-space daemon called Watson running under X11.
Understanding Niceness
In Unix-like operating systems, niceness is a value that influences process scheduling priority. A lower niceness value means higher priority. Adjusting niceness allows the system to allocate more CPU time to important processes.
Watson: Managing Process Priorities
Watson is a background process that monitors the active window in the X11 environment. It adjusts the niceness of the currently focused application to improve responsiveness. Here's how Watson operates:
- Detects the active window using X11 APIs.
- Retrieves the process ID (PID) associated with the window.
- Adjusts the niceness of the process using system calls.
Implementation Details
Below is a simplified Python script illustrating how Watson can adjust process niceness:
import time
import subprocess
import os
def get_active_window_pid():
# Use xprop to get the active window ID
window_id = subprocess.check_output(['xprop', '-root', '_NET_ACTIVE_WINDOW']).decode()
window_id = window_id.strip().split()[-1]
# Use xprop to get the PID of the window
pid = subprocess.check_output(['xprop', '-id', window_id, '_NET_WM_PID']).decode()
pid = pid.strip().split()[-1]
return int(pid)
def adjust_niceness(pid):
# Reset niceness of all processes
subprocess.call(['renice', '0', '-u', os.getlogin()])
# Set niceness of the active process to -10
subprocess.call(['renice', '-10', '-p', str(pid)])
def main():
previous_pid = None
while True:
try:
active_pid = get_active_window_pid()
if active_pid != previous_pid:
adjust_niceness(active_pid)
previous_pid = active_pid
time.sleep(1)
except Exception as e:
print(f"Error: {e}")
time.sleep(1)
if __name__ == "__main__":
main()
Note: This script requires appropriate permissions to adjust process priorities. It should be run with the necessary privileges or capabilities.
Running Watson at Startup
To ensure Watson runs automatically:
- Create a systemd service file
/etc/systemd/system/watson.service
with the following content:
[Unit]
Description=Watson Priority Manager
After=graphical.target
[Service]
Type=simple
ExecStart=/usr/local/bin/watson.py
User=root
Environment=DISPLAY=:0
[Install]
WantedBy=graphical.target
- Enable and start the service:
sudo systemctl enable watson.service
sudo systemctl start watson.service