在汇编中重现这些C类型? [英] Reproduce these C types in assembly?

查看:91
本文介绍了在汇编中重现这些C类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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 ).

I am trying to reproduce two opaque data types from the pthreads library in NASM. These data types are pthread_attr_t and cpu_set_t from pthread_attr_setaffinity_np (see http://man7.org/linux/man-pages/man3/pthread_attr_setaffinity_np.3.html).

我创建了一个简单的C程序来调用pthread_attr_setaffinity_np并与gdb一起逐步检查这两个位掩码的格式(pthread_attr_t是相似性掩码).

I created a simple C program to call pthread_attr_setaffinity_np and stepped through it with gdb to examine the format of those two bitmasks (pthread_attr_t is an affinity mask).

当我使用gdb调试C版本时,我会打印attr和cpus的值:

When I debug the C version with gdb, I print the values of attr and 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>}}

这两种类型的格式将汇编语言转换为什么?

What do those two type formats translate into for assembly language?

这是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;
}

非常感谢您的帮助.

推荐答案

使用pthread在NASM中创建线程相当容易,但是设置亲和力掩码是另一回事.事实证明,没有必要重现要在汇编语言中使用的不透明类型,这将非常困难.

It's fairly easy to create threads in NASM using pthreads, but setting the affinity mask is another matter. It turned out to be unnecessary to reproduce the opaque types to use in assembly language, which would be very difficult.

相反,我将C程序编译为一个目标文件,并将该目标文件与NASM目标文件链接在一起以生成最终的可执行文件. C语言中的main()函数使用不同的名称,因为它不会被编译为.exe,并且该函数名称在NASM程序中使用外部"进行引用.这是最终的C代码:

Instead, I compiled the C program to an object file, and linked that object file with the NASM object file to produce the final executable. The main() function in C got a different name because it's not to be compiled to an .exe, and that function name is referenced with an "extern" in the NASM program. Here's the final C code:

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

extern void * Test_fn();

int thread_create_in_C() {

    int numberOfProcessors = sysconf(_SC_NPROCESSORS_ONLN);

    if (numberOfProcessors >= 2){ // e.g. virtual cores
        numberOfProcessors = numberOfProcessors / 2; }

    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);
       printf("Core created %d\n", i);
       pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t),    &cpus);
       pthread_create(&threads[i], &attr, Test_fn, NULL);
    }

    for (int i = 0; i < numberOfProcessors; i++) {
        pthread_join(threads[i], NULL);
        printf("Core joined %d\n", i);
    }

    return numberOfProcessors;
}

在NASM代码中,我们有一个带有其他extern的"extern thread_create_in_C"指令来引用C代码,在C代码中,我们有extern void * Test_fn();引用每个线程要调用的NASM函数.

In the NASM code, we have an "extern thread_create_in_C" directive with the other externs, to reference the C code, and in the C code we have extern void * Test_fn(); to reference the NASM function to be called by each thread.

我们在NASM程序中的适当位置调用C程序,

We call the C program at the appropriate point in the NASM program with:

call thread_create_in_C wrt ..plt

我对需要为汇编语言中的线程设置亲和性掩码的任何人的建议是,使用上述C语言程序,而不是尝试在汇编中复制它.但是,对于没有亲和力掩码的简单线程创建,只需使用pthreads库即可.

My suggestion to anyone who needs to set affinity masks for threads in assembly language is to use a C program like the one above instead of trying to replicate it in assembly. But for simple thread creation without affinity masks, the pthreads library is all you need.

这篇关于在汇编中重现这些C类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆