processpriority

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2024 License: MIT Imports: 2 Imported by: 0

README

Process Priority

Go Reference

Easily get or set a process priority from a Golang program with transparent Linux/Windows support.

Under the hood, it will uses pre-defined nice values for Linux and the priority classes for Windows.

Notes

Linux

On Linux, a user can only decrease its priority (increase its nice value), not increase it. This means that even after lowering the priority, you won't be able to raise it to its original value.

To be able to increase it, the user must be root or have the CAP_SYS_NICE capability.

Windows

On Windows, the real time priority can only be set as an administrator. If you try to set it as a normal user, you will won't get an error but your process will be set at High instead.

Example

package main

import (
	"fmt"
	"os/exec"

	"github.com/hekmon/processpriority"
)

func main() {
	// Run command
	// cmd := exec.Command("sleep", "1") // linux binary
	cmd := exec.Command("./sleep.exe") // windows binary
	err := cmd.Start()
	if err != nil {
		panic(err)
	}
	// Get its current priority
	prio, rawPrio, err := processpriority.Get(cmd.Process.Pid)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Current process priority is %s (%d)\n", prio, rawPrio)
	// Change its priority
	newPriority := processpriority.BelowNormal
	fmt.Printf("Changing process priority to %s\n", newPriority)
	if err = processpriority.Set(cmd.Process.Pid, newPriority); err != nil {
		panic(err)
	}
	// Verifying
	if prio, rawPrio, err = processpriority.Get(cmd.Process.Pid); err != nil {
		panic(err)
	}
	fmt.Printf("Current process priority is %s (%d)\n", prio, rawPrio)
	// Wait for the cmd to end
	if err = cmd.Wait(); err != nil {
		panic(err)
	}
}
Linux output
Current process priority is Normal (0)
Changing process priority to Below Normal
Current process priority is Below Normal (5)
Windows output
Current process priority is Normal (32)
Changing process priority to Below Normal
Current process priority is Below Normal (16384)

Install

go get -u github.com/hekmon/processpriority

Documentation

Index

Constants

View Source
const (
	UnixPriorityIdle        = 19
	UnixPriorityBelowNormal = 5
	UnixPriorityNormal      = 0
	UnixPriorityAboveNormal = -5
	UnixPriorityHigh        = -10
	UnixPriorityRealTime    = -20
)

Opiniated nice values

Variables

This section is empty.

Functions

func GetRAW

func GetRAW(pid int) (priority int, err error)

GetRAW is an OS specific function to get the priority of a process. As priority values are not the same on all OSes, you should use the universal function Get() instead to be platform agnostic.

func Set

func Set(pid int, priority ProcessPriority) (err error)

Set is an universal wrapper for setting process priority. It uses OS specific convertion and calls OS specific implementation.

func SetRAW

func SetRAW(pid, priority int) (err error)

SetRAW is an OS specific function to set the priority of a process. As priority values are not the same on all OSes, you should use the universal function Set() instead to be platform agnostic.

Types

type ProcessPriority

type ProcessPriority int

ProcessPriority is a universal type for process priorities. It used with the universal wrapper Set() to be platform agnostic.

const (
	// PriorityOSSpecific is only used on Get(), it indicates that the current level is not a universal one from this package.
	OSSpecific ProcessPriority = iota
	Idle
	BelowNormal
	Normal
	AboveNormal
	High
	RealTime
)

func Get

func Get(pid int) (priority ProcessPriority, rawOSPriority int, err error)

Get is an universal wrapper for getting process priority. It uses OS specific convertion and calls OS specific implementation.

func (ProcessPriority) String

func (pp ProcessPriority) String() string

String implements the fmt.Stringer interface

Directories

Path Synopsis
sleep command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL