Interface OperatingSystem

All Known Implementing Classes:
AbstractOperatingSystem, AixOperatingSystem, FreeBsdOperatingSystem, LinuxOperatingSystem, MacOperatingSystem, OpenBsdOperatingSystem, SolarisOperatingSystem, WindowsOperatingSystem

@ThreadSafe public interface OperatingSystem
An operating system (OS) is the software on a computer that manages the way different programs use its hardware, and regulates the ways that a user controls the computer.

Considered thread safe, but see remarks for the getSessions() method.

  • Method Details

    • getFamily

      String getFamily()
      Get the Operating System family.
      Returns:
      the family
    • getManufacturer

      String getManufacturer()
      Get the Operating System manufacturer.
      Returns:
      the manufacturer
    • getVersionInfo

      Get Operating System version information.
      Returns:
      version information
    • getFileSystem

      FileSystem getFileSystem()
      Instantiates a FileSystem object.
      Returns:
      A FileSystem object.
    • getInternetProtocolStats

      InternetProtocolStats getInternetProtocolStats()
      Instantiates a InternetProtocolStats object.
      Returns:
      a InternetProtocolStats object.
    • getProcesses

      default List<OSProcess> getProcesses()
      Gets currently running processes. No order is guaranteed.
      Returns:
      A list of OSProcess objects for the specified number (or all) of currently running processes, sorted as specified. The list may contain null elements or processes with a state of OSProcess.State.INVALID if a process terminates during iteration.
    • getProcesses

      List<OSProcess> getProcesses(Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit)
      Gets currently running processes, optionally filtering, sorting, and limited to the top "N".
      Parameters:
      filter - An optional Predicate limiting the results to the specified filter. Some common predicates are available in OperatingSystem.ProcessSorting. May be null for no filtering.
      sort - An optional Comparator specifying the sorting order. Some common comparators are available in OperatingSystem.ProcessSorting. May be null for no sorting.
      limit - Max number of results to return, or 0 to return all results
      Returns:
      A list of OSProcess objects, optionally filtered, sorted, and limited to the specified number.

      The list may contain processes with a state of OSProcess.State.INVALID if a process terminates during iteration.

    • getProcesses

      default List<OSProcess> getProcesses(Collection<Integer> pids)
      Gets information on a Collection of currently running processes. This has potentially improved performance vs. iterating individual processes.
      Parameters:
      pids - A collection of process IDs
      Returns:
      A list of OSProcess objects for the specified process ids if it is running
    • getProcess

      OSProcess getProcess(int pid)
      Gets information on a currently running process
      Parameters:
      pid - A process ID
      Returns:
      An OSProcess object for the specified process id if it is running; null otherwise
    • getChildProcesses

      List<OSProcess> getChildProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit)
      Gets currently running child processes of provided parent PID, optionally filtering, sorting, and limited to the top "N".
      Parameters:
      parentPid - The Process ID whose children to list.
      filter - An optional Predicate limiting the results to the specified filter. Some common predicates are available in OperatingSystem.ProcessSorting. May be null for no filtering.
      sort - An optional Comparator specifying the sorting order. Some common comparators are available in OperatingSystem.ProcessSorting. May be null for no sorting.
      limit - Max number of results to return, or 0 to return all results
      Returns:
      A list of OSProcess objects representing the currently running child processes of the provided PID, optionally filtered, sorted, and limited to the specified number.

      The list may contain processes with a state of OSProcess.State.INVALID if a process terminates during iteration.

    • getDescendantProcesses

      List<OSProcess> getDescendantProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit)
      Gets currently running processes of provided parent PID's descendants, including their children, the children's children, etc., optionally filtering, sorting, and limited to the top "N".
      Parameters:
      parentPid - The Process ID whose children to list.
      filter - An optional Predicate limiting the results to the specified filter. Some common predicates are available in OperatingSystem.ProcessSorting. May be null for no filtering.
      sort - An optional Comparator specifying the sorting order. Some common comparators are available in OperatingSystem.ProcessSorting. May be null for no sorting.
      limit - Max number of results to return, or 0 to return all results
      Returns:
      A list of OSProcess objects representing the currently running descendant processes of the provided PID, optionally filtered, sorted, and limited to the specified number.

      The list may contain processes with a state of OSProcess.State.INVALID if a process terminates during iteration.

    • getProcessId

      int getProcessId()
      Gets the current process ID (PID).
      Returns:
      the Process ID of the current process
    • getCurrentProcess

      default OSProcess getCurrentProcess()
      Gets the current process.
      Returns:
      the current process
    • getProcessCount

      int getProcessCount()
      Get the number of processes currently running
      Returns:
      The number of processes running
    • getThreadId

      int getThreadId()
      Makes a best effort to get the current thread ID (TID). May not be useful in a multithreaded environment. The thread ID returned may have been short lived and no longer exist.

      Thread IDs on macOS are not correlated with any other Operating System output.

      Returns:
      the Thread ID of the current thread if known, 0 otherwise.
    • getCurrentThread

      OSThread getCurrentThread()
      Makes a best effort to get the current thread. May not be useful in a multithreaded environment. The thread returned may have been short lived and no longer exist.

      On macOS, returns the oldest thread in the calling process.

      Returns:
      the current thread if known; an invalid thread otherwise.
    • getThreadCount

      int getThreadCount()
      Get the number of threads currently running
      Returns:
      The number of threads running
    • getBitness

      int getBitness()
      Gets the bitness (32 or 64) of the operating system.
      Returns:
      The number of bits supported by the operating system.
    • getSystemUptime

      long getSystemUptime()
      Get the System up time (time since boot).
      Returns:
      Number of seconds since boot.
    • getSystemBootTime

      long getSystemBootTime()
      Get Unix time of boot.
      Returns:
      The approximate time at which the system booted, in seconds since the Unix epoch.
    • isElevated

      default boolean isElevated()
      Determine whether the current process has elevated permissions such as sudo / Administrator
      Returns:
      True if this process has elevated permissions
    • getNetworkParams

      NetworkParams getNetworkParams()
      Instantiates a NetworkParams object.
      Returns:
      A NetworkParams object.
    • getServices

      default List<OSService> getServices()
      Gets the all services on the system. The definition of what is a service is platform-dependent.
      Returns:
      An array of OSService objects
    • getSessions

      default List<OSSession> getSessions()
      Gets currently logged in users.

      On macOS, Linux, and Unix systems, the default implementation uses native code (see man getutxent) that is not thread safe. OSHI's use of this code is synchronized and may be used in a multi-threaded environment without introducing any additional conflicts. Users should note, however, that other operating system code may access the same native code.

      The Who.queryWho() method produces similar output parsing the output of the Posix-standard who command, and may internally employ reentrant code on some platforms. Users may opt to use this command-line variant by default using the oshi.os.unix.whoCommand configuration property.

      Returns:
      A list of OSSession objects representing logged-in users
    • getDesktopWindows

      default List<OSDesktopWindow> getDesktopWindows(boolean visibleOnly)
      Gets windows on the operating system's GUI desktop.

      On Unix-like systems, reports X11 windows only, which may be limited to the current display and will not report windows used by other window managers.

      While not a guarantee, a best effort is made to return windows in foreground-to-background order. This ordering may be used along with OSDesktopWindow.getOrder() to (probably) determine the frontmost window.

      Parameters:
      visibleOnly - Whether to restrict the list to only windows visible to the user.

      This is a best effort attempt at a reasonable definition of visibility. Visible windows may be completely transparent.

      Returns:
      A list of OSDesktopWindow objects representing the desktop windows.