I'm new to Operating System, just learnt about process and now is about to learn about threads. From what I understand,
a program is an executable file, program code
a process is a program in execution
a thread is part of a process
in a process, there might be multiple threads. e.g. MS Word
in a programming code, there is the main function and there're might be other functions also
because there is only one main function, usually the program would just run from top of the main function to down.
My question are:
Q1. Is thread in operating system = function in programming language?
Q2. Is function in programming language = thread function in programming language?
Q3. Is thread in operating system = thread function in programming language?
Q4. Thread function allows multiple functions to be performed simultaneously, like having multiple main functions. Is this correct?
Q5. Where is thread written in operating system/ programming language?
I appreciate it greatly if somebody can explain these in simple terms, and examples from analogy or simple programming languages (C/C++/java).
EDIT: What I mean by thread function (I'm not sure if this is a correct term) is like,
- pthread.h in POSIX thread programming
- thr_create, thr_join etc in Thread Functions in Solaris and Windows 9x/NT
- CreateThread (included in windows.h?) in Create Threads using C in Windows.
I guess that "thread function" = "thread".
If so, then, from David's answer,
Q2. Is function in programming language = thread function in programming language?
- no.
And from a Q&A in Quora here, it says that
Q: What is the difference between a thread in a programming language, like Java or Python, and a Linux thread?
A: There is no difference; it is the same word with the same meaning (as opposed to one word that's overloaded to mean multiple things). The operating system will execute threads in parallel as requested by the software that creates the threads.
So, I guess that the answer for my third question,
Q3. Is thread in operating system = thread function in programming language?
- yes.
And, from POSIX Thread
Initially, your main() program comprises a single, default thread. All other threads must be explicitly created by the programmer
so I guess the answer for
Q4. Thread function allows multiple functions to be performed simultaneously, like having multiple main functions. Is this correct?
- I think yes.
Q6. Will the program not be able to do multithreading if it doesn't include these thread functions?
- I think yes.
Although I'm not sure how it is included/written in programming code that doesn't explicitly use thread functions directly like CreateThread etc., which is my fifth question.
For example, in Tetris code in C, I didn't see any "thread" word inside the code. Instead, it uses conio.h
, which is from wikipedia
conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C, nor is it defined by POSIX.
Q7. So does this mean this particular programming code in Tetris code in C is single-threaded only?
Q8. Is it by including windows.h
, a programming code can perform multithreading?
Please correct me if I'm mistaken.
The remaining questions are Q5, Q7, Q8.