rinogo
rinogo

Reputation: 9163

Set CPU Affinity in PHP?

Pretty straightforward question - is there a way to set the CPU affinity via PHP? Any of the following will do:

The only option I've found so far is the last one, using a tool named schedutils, and it seems it may only be supported on more recent kernels.

Schedutils

Upvotes: 4

Views: 1142

Answers (2)

luzrain
luzrain

Reputation: 11

You can do this natively starting with PHP 8.4

pcntl_setcpuaffinity(posix_getpid(), [0,1]);

Upvotes: 1

Joni
Joni

Reputation: 111349

The way to set the CPU affinity is with the sched_setaffinity C function. It's not available through the standard PHP API so you probably would have to write an extension.

An alternative is running taskset program through system. For example this binds the PHP process to cores 0 and 1:

system('taskset -cp 0,1 '.getmypid());

Upvotes: 9

Related Questions