Is it possible to sleep a process by force externally?

1

1

Say we have a game that runs 100% cpu time via its global loop. However, we need to 'sleep()' its main process externally so that it goes to lower cpu time.

We do not have access to its source.

Is it possible? On windows.

I'd prefer methods who do not involve injecting the binary which are often illegal.

j riv

Posted 2011-02-14T08:55:14.553

Reputation: 2 162

You can use Process Explorer to suspend the process, but I'm not sure that will yield the same result as you expect @lel – Sathyajith Bhat – 2011-02-14T09:43:43.150

Answers

1

The best you can do externally without injecting is to lower the process priority, e.g to Idle (you can do this from Task Manager). If you do that, the process will still take 100 %, but only when no other processes want to use the CPU.

Some other tools which might help you in this (based only on their description, I did not try them) are Prio or Process Tamer.

Suma

Posted 2011-02-14T08:55:14.553

Reputation: 1 307

0

You could call NtSuspendProcess in a loop, suspending it for say 450 ms, and resuming it for 50 ms. Experiments with this on my end seem to have no obvious side effects. When the process comes to the foreground, I disable this loop.

Java example:

    boolean inForeground = false;

    for(;;) {
      updateProcesses();  // updates list of handles to throttle

      if(!inForeground) {
        for(HANDLE handle : handles.values()) {
          NtDll.INSTANCE.NtSuspendProcess(handle);
        }
      }

      Thread.sleep(480);

      int foregroundPid = getForegroundPid();

      inForeground = false;

      for(Map.Entry<Integer, HANDLE> entry : handles.entrySet()) {
        NtDll.INSTANCE.NtResumeProcess(entry.getValue());

        if(entry.getKey() == foregroundPid) {
          inForeground = true;
        }
      }

      Thread.sleep(20);
    }

john16384

Posted 2011-02-14T08:55:14.553

Reputation: 113

Do you mean the pinvoke.net: NtSuspendProcess (ntdll)? Could you give a deeper insight using an integrated code snippet?

– JosefZ – 2019-05-10T10:16:41.743

Added sample code (in Java, using JNA to do the calls into ntdll). – john16384 – 2019-05-10T14:28:00.653