我正在尝试从NASM的pthreads库中复制两种不透明的数据类型。这些数据类型是pthread_attr_setaffinity_np中的pthread_attr_t和cpu_set_t(请参见http://man7.org/linux/man-pages/man3/pthread_attr_setaffinity_np.3.html)。
我创建了一个简单的C程序来调用pthread_attr_setaffinity_np,并与gdb一起逐步检查这两个位掩码的格式(pthread_attr_t是一个亲和力掩码)。
当我用gdb调试C版本时,我打印attr和cpus的值:
(gdb) p attr
$2 = {__size = '\000' <repeats 17 times>, "\020", '\000' <repeats 37 times>, __align = 0}
(gdb) p cpus
$3 = {__bits = {1, 0 <repeats 15 times>}}
这两种类型的格式将汇编语言翻译成什么?
这是C代码:
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* DoWork(void* args) {
printf("ID: %lu, CPU: %d\n", pthread_self(), sched_getcpu());
return 0;
}
int main() {
int numberOfProcessors = sysconf(_SC_NPROCESSORS_ONLN);
printf("Number of processors: %d\n", numberOfProcessors);
pthread_t threads[numberOfProcessors];
pthread_attr_t attr;
cpu_set_t cpus;
pthread_attr_init(&attr);
for (int i = 0; i < numberOfProcessors; i++) {
CPU_ZERO(&cpus);
CPU_SET(i, &cpus);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpus);
pthread_create(&threads[i], &attr, DoWork, NULL);
}
for (int i = 0; i < numberOfProcessors; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
非常感谢您的帮助。
我正在尝试从NASM的pthreads库中复制两种不透明的数据类型。这些数据类型是pthread_attr_setaffinity_np中的pthread_attr_t和cpu_set_t(请参见http://man7.org/linux/man-pages/man3/pthread_attr_setaffinity_np.3.html)。
我创建了一个简单的C程序来调用pthread_attr_setaffinity_np,并与gdb一起逐步检查这两个位掩码的格式(pthread_attr_t是一个亲和力掩码)。
当我用gdb调试C版本时,我打印attr和cpus的值:
(gdb) p attr
$2 = {__size = '\000' <repeats 17 times>, "\020", '\000' <repeats 37 times>, __align = 0}
(gdb) p cpus
$3 = {__bits = {1, 0 <repeats 15 times>}}
这两种类型的格式将汇编语言翻译成什么?
这是C代码:
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* DoWork(void* args) {
printf("ID: %lu, CPU: %d\n", pthread_self(), sched_getcpu());
return 0;
}
int main() {
int numberOfProcessors = sysconf(_SC_NPROCESSORS_ONLN);
printf("Number of processors: %d\n", numberOfProcessors);
pthread_t threads[numberOfProcessors];
pthread_attr_t attr;
cpu_set_t cpus;
pthread_attr_init(&attr);
for (int i = 0; i < numberOfProcessors; i++) {
CPU_ZERO(&cpus);
CPU_SET(i, &cpus);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpus);
pthread_create(&threads[i], &attr, DoWork, NULL);
}
for (int i = 0; i < numberOfProcessors; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
非常感谢您的帮助。