-1

1, I'm always told that each threads are execution contexts, but what exactly does a thread contain? It seems contain call stack, cpu registers, and actual execution code ?

2, There are also two types of thread - os level thread and user level thread. they are mapped to each other. When it comes to cpu runs thread, does cpu only run on either of them or only on os level thread?

3, It's a bit confusing to illustrate how cpu run thread. I mean, from what I know CPU can only run machine code which are 0 and 1. So what does context switching for cpu mean? if each thread contains binaries which are instructions and cpu just run different instructions?

Tony Lin
  • 922
  • 6
  • 25
  • AFAIK the concept of "user threads" vs "OS level threads" is only academic these days. Once there were experiments with usermode schedulers, fibers etc where several user threads shared one "real" thread.In mainstream Linux thread in user mode matches thread from the OS point of view. That's, when a thread makes a system call, it switches to kernel mode, and preserves its identity, Of course, transition to kernel mode requires certain change of execution context, especially,stack pointer. And more profound switch when the usermode code is 32 bit but kernel is 64 bit or different architecture. – ddbug Oct 17 '19 at 03:47

1 Answers1

1

To give the short and oversimplified version, assume:

  • You have one core and multiple streams of instructions (aka threads, processes or programs).
  • The CPU has a possibility to interrupt one execution stream and jump to another address, the interrupt handler. If a clock is connected to this interrupt pin, an external interrupt can be generated at regular intervals.

Running multiple threads or processes relies on a concept of context switching.

Context switching means the current stream of instructions (thread) is interrupted, all registers are saved on the stack, the stack pointer is changed and another context is loaded, and all the saved registers are restored from its stack.

Difference between lightweight threads and processes is if they share memory space and can communicate with shared memory or not.

prl
  • 11,716
  • 2
  • 13
  • 31
Simson
  • 3,373
  • 2
  • 24
  • 38
  • Thanks! The threads that are executed by cpu, are they kernel level threads or user level threads? – Tony Lin Oct 17 '19 at 03:40
  • they are the same from a concept level of context switching but at least the scheduler is a privileged kernel level process – Simson Oct 17 '19 at 03:44
  • maybe I should rephrase, say a java application uses thread which generated by its runtime, are these thread after compilation directly executed by cpu? or it's corresponding os level thread that runs? – Tony Lin Oct 17 '19 at 03:46
  • Take a look at this https://stackoverflow.com/a/15984127/391691 – Simson Oct 17 '19 at 03:51
  • 1
    @TonyLin `say a java application uses thread` - if you are interested in java specifically, perhaps, it is best to tag the question as 'java'. This has very little to do with 'assembly' *per se*. – tum_ Oct 17 '19 at 08:13