What does pid 0 mean for sched_setaffinity()
I'm seeing such code in nginx sourc开发者_开发知识库e:
if (sched_setaffinity(0, 32, (cpu_set_t *) &cpu_affinity) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"sched_setaffinity(0x%08Xl) failed", cpu_affinity);
}
Why here pid
is 0
instead of getpid()
?
From the sched_setaffinity(2)
manpage:
The affinity mask is actually a per-thread attribute that can be adjusted independently for each of the threads in a thread group. The value returned from a call to
gettid(2)
can be passed in the argument pid. Specifyingpid
as 0 will set the attribute for the calling thread, and passing the value returned from a call togetpid(2)
will set the attribute for the main thread of the thread group. (If you are using the POSIX threads API, then usepthread_setaffinity_np(3)
instead ofsched_setaffinity()
.)
精彩评论