Thread: How a CPU with multi-core architecture run kernel with one main function ?

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    84

    How a CPU with multi-core architecture run kernel with one main function ?

    Hi,

    I hope it's the right forum to ask this question, I was doing a simple microcontroller session and one asked me this question which I didn't expect actually

    He asked me why in a C program, there's only one main function not two or more ?

    For a multi-core architecture, where the system calls source which is the main function is located ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When you run a program within a multi-tasking OS, it picks one CPU to run your program, starting at your single main().

    Perhaps at some point in the future (if the program is long lived), the OS may move it from CPU1 to CPU2 say.
    This is completely invisible to the program.

    For experienced programmers who can sensibly divide the work a program needs to do, there is
    pthread_create(3): create new thread - Linux man page
    After a thread has been created, there is scope for the OS to run the 'main' on one CPU and the 'thread' on another CPU.

    Distinct programs each have their own main() functions (like your mail reader and your web browser).
    Running multiple programs on a multi core processor works the same way. The OS picks a core and off you go.

    But in the world of micro controllers, you might not have an OS at all.
    Better get comfortable for a long read of lots of manuals.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    When you run a program within a multi-tasking OS, it picks one CPU to run your program, starting at your single main().
    OK, so my understanding for the desktop running concept is that windows is built over a kernel written in C.

    So the kernel of the CPU is the one that do the algorithms, logic and arithmetic operations, connect with RAM and HDDs with the help of southbridge.

    How a CPU with multi-core architecture run kernel with one main function ?-600px-chipset_schematic-svg-png

    Then, there's windows which is OS layer. And finally, the programs that run in windows environment.




    Perhaps at some point in the future (if the program is long lived), the OS may move it from CPU1 to CPU2 say.
    This is completely invisible to the program.
    Do you mean systems that have multiple processors on the motherboard ? Like this work station:

    How a CPU with multi-core architecture run kernel with one main function ?-ga-7pese3-ga-7pese4-jpg

    Or the multicore CPU, that the CPU distribute the tasks between the cores ?


    For experienced programmers who can sensibly divide the work a program needs to do, there is
    pthread_create(3): create new thread - Linux man page
    After a thread has been created, there is scope for the OS to run the 'main' on one CPU and the 'thread' on another CPU.
    I remember one day I tried to work with pthread library in C compiler, but I left it and got back to microcontrollers programming.

    I think there's a library for Arduino platform but I didn't investigate it yet, but I downloaded many multitasking libraries for the Arduino programming methods, styles .. etc. Most of them are using C++ to manage the functions calls. But I got back to my C programming.

    I'm working now on a LCD state machine code, it should not be so difficult, just working now on how to manage the functions' locks, when to lock the function and when to unlock it. But of course that is far from the scope of this thread question.


    But for the student who asked me, I better answer them, as they probably have a little idea of computer principals, to tell them that this question isn't that easy to answer, computer isn't easy to describe and how it works.

    Distinct programs each have their own main() functions (like your mail reader and your web browser).
    Running multiple programs on a multi core processor works the same way. The OS picks a core and off you go.
    Yes, pretty much the same of the pthread that you mentioned, the CPU schedule the tasks and put them in threads.


    But in the world of micro controllers, you might not have an OS at all.
    Yes, for big projects there has to be a code manager to manage the function calls, if it's a multitasking project.

    Better get comfortable for a long read of lots of manuals.
    Yes, but actually I left reading in datasheets and manuals.

    Now, I try to modify and improve my code, and if something struggles me, then it's the time to search about it.

    Like lately, I learned what is the benefits of using typedefs, which allowed me to use a variable between multiple source file.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You almost need to forget about the hardware when talking about the software above the assembly programming level.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Yes, I got you, you mean that the hardware is something done which is the CPU and how it handles tasks, that's the hardware part.

    But for the software part, it is the way how the core main function is handling everything from one main scope which is the question I've been asked, but to me as I've been doing coding for Arduino microcontrollers for several years, I learned a bit about computer and CPU but I don't care that much.

    In microcontroller world, it's one CPU core most the time, now that are microcontrollers with multi-core, but I know that there is a way the system in handling the cores.

    For example, for a microcontroller with dual-core, that is new, didn't get any bootloader. Then the chip doesn't know which core should do this task or the other one and where the main system caller is located in.

    There has to be a core kernel that tells which core handle this and which core handles that, but the question here is that does that mean that each core has a main system caller ? If so, then there has to be another function that keeps track of what the cores are doing, but again where this main function that keeps track of the core operation ? where does it locate ? in any core or what exactly ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Multi-core chips would seem to necessarily start in one core mode, until there is some kind of 'OS' running to make resource decisions.

    Without an OS, it's really down to reading the manuals.

    The micro controller vendors of such devices will no doubt have lots of application notes showing you how to make use of all the toys on their particular creations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    I liked the "toys" description

    Yes of course without OS, one has to read the datasheet and write the device drivers for that chip.

    Arduino; for example, is doing this by installing core libraries + application libraries with examples in its IDE, and that's how it's easy for beginners to start building skills in programming + freedom to do the library or the project in C or C++.

    So in this case, for a programmer who's coming from desktop programming world, he/she doesn't have to check the AVR chips datasheets and can easily write projects.

    If anyone wants a multitasking project, then there are many libraries for that, but I have to learn how to use that library to embed it in my project.

    So yeah basically there is no OS I guess.

    I tried before couple years to read about intel CPU or modern CPUs, but most I find is intel's old 8086 CPU. I want to read about i3, i5, ... etc. Or AMD CPUs, do you recommend me anything for reading ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. couple of questions concerning multi core cpu's
    By Masterx in forum Tech Board
    Replies: 6
    Last Post: 10-07-2009, 10:39 AM
  2. Multi-Core code
    By tinkerC in forum C Programming
    Replies: 6
    Last Post: 04-20-2009, 06:15 AM
  3. Multi-core affinity defaults
    By CodeMonkey in forum Tech Board
    Replies: 4
    Last Post: 12-08-2008, 03:47 PM
  4. Making use of 64 bit/multi core processers?
    By code2d in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2006, 04:48 PM
  5. PS-3 to use 4ghz multi-core Cell Processor
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-08-2005, 04:35 PM

Tags for this Thread