简体   繁体   English

多线程程序在今天的multicore-cpu机器上是如何通过linux编译运行的?

[英]How does multithreaded programs compile and run by linux in multicore-cpu machine today?

I wrote multithreaded programs using PThreads. My system has 4 cores and 8 logical cores, os x84-64 and I don't know what the compiler will compile to support the multithreaded task to run on multiple cpus.我使用 PThreads 编写了多线程程序。我的系统有 4 个内核和 8 个逻辑内核,os x84-64,我不知道编译器将编译什么以支持多线程任务在多个 cpu 上运行。 I think that a multi-core CPU must have some method to execute program exactly by CPU switching.我认为多核 CPU 必须有某种方法可以通过 CPU 切换来准确执行程序。 One of the cpus starts first, and then the other cpus.其中一个 cpu 先启动,然后另一个 cpu。 Is this logic done through the management of the operating system or by some intel asm instruction?这个逻辑是通过操作系统的管理还是通过一些intel asm指令完成的? I'm also not sure how this multi-core CPU boot first cpu and switches other cpus, and how the compiler generates the same multi-threaded code(written by c, by linux programming interface book) for machines with only one CPU and multiple cpus.我也不清楚这个多核CPU是如何先启动cpu然后切换其他cpu的,以及编译器如何为只有一个CPU和多个CPU的机器生成相同的多线程代码(c编写,linux编程接口书)处理器。 So my words are a little messy.所以我的话有点乱。

/*************************************************************************\
*                  Copyright (C) Michael Kerrisk, 2022.                   *
*                                                                         *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the   *
* Free Software Foundation, either version 3 or (at your option) any      *
* later version. This program is distributed without any warranty.  See   *
* the file COPYING.gpl-v3 for details.                                    *
\*************************************************************************/

/* Listing 29-1 */

#include <pthread.h>
#include "tlpi_hdr.h"

static void *
threadFunc(void *arg)
{
    char *s = arg;

    printf("%s", s);

    return (void *) strlen(s);
}

int
main(int argc, char *argv[])
{
    pthread_t t1;
    void *res;
    int s;

    s = pthread_create(&t1, NULL, threadFunc, "Hello world\n");
    if (s != 0)
        errExitEN(s, "pthread_create");

    printf("Message from main()\n");
    s = pthread_join(t1, &res);
    if (s != 0)
        errExitEN(s, "pthread_join");

    printf("Thread returned %ld\n", (long) res);

    exit(EXIT_SUCCESS);
}

Task switching and the usage of multiple CPUs is done by the operating system.任务切换和多CPU的使用是由操作系统完成的。 The C compiler does nothing in that respect. C 编译器在这方面什么都不做。 Your code example is a C program that make calls to the pthread library.您的代码示例是一个调用 pthread 库的 C 程序。 All the C compiler can do is to generate code that calls that library, but it has no idea what the library does. C 编译器所能做的就是生成调用该库的代码,但它不知道该库的作用。 You may now ask: but the operating system is written in C and latest somewhere down in the kernel there must be code to do all the task switching and stuff?您现在可能会问:但是操作系统是在 C 中编写的,最新的某个地方在 kernel 中必须有代码来完成所有任务切换和其他事情吗? Yes.是的。 And the Linux kernel is written mostly in C, but the task switching and CPU handling is implemented based on the processor specification and the correct usage of registers and interrupts and not by C language features.而 Linux kernel 大部分是在 C 中编写的,但是任务切换和 CPU 处理是根据处理器规格和寄存器和中断的正确使用来实现的,而不是通过 C 语言功能实现的。

That is also the answer to the other part of your question “how the compiler generates the same multi-threaded code … for machines with only one CPU and multiple cpus“: the generated code is the same, because it's solely the job of the operating system to assign code to CPUs.这也是您问题的另一部分的答案“编译器如何为只有一个 CPU 和多个 CPU 的机器生成相同的多线程代码”:生成的代码是相同的,因为它完全是操作的工作系统将代码分配给 CPU。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM