2
$\begingroup$

I'm new to Operating System, just learnt about process and now is about to learn about threads. From what I understand,

  1. a program is an executable file, program code

  2. a process is a program in execution

  3. a thread is part of a process

  4. in a process, there might be multiple threads. e.g. MS Word

  5. in a programming code, there is the main function and there're might be other functions also

  6. 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,


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.

$\endgroup$
1
  • $\begingroup$ For the future, please ask only one question per post. If you have multiple questions, you can post them separately. Also, Q7 and Q8 off-topic; see our help center. $\endgroup$
    – D.W.
    Commented Feb 20, 2017 at 23:21

1 Answer 1

3
$\begingroup$

because there is only one main function, usually the program would just run from top of the main function to down.

That's a long way from being the case. Loops, conditional statements and function calls mean that the control flow of any non-trivial program is much more complex than just running from the top of main() to the bottom.

  1. Is thread in operating system = function in programming language?

No. A thread is, essentially, a subprocess. A process may have several threads, and each of these threads executes semi-independently while sharing some common information. A function, on the other hand, is typically a somewhat self-contained unit of work.

An analogy might be that a process is like a company (there can be many different processes, they're kept separate from each other but they can arrange to cooperate with each other in various ways, governed by the law of the country they're in), a thread is like an employee of a company (employees share access to the company's resources and cooperate closely under rules defined by the company) and a function is a specific task that might be performed by an employee (making coffee, typing a letter, attaching a door to a car, etc.)

  1. Is function in programming language = thread function in programming language?
  2. Is thread in operating system = thread function in programming language?

The things you're calling "thread functions" are just programming language libraries that access the operating system's interface for creating and managing threads

  1. Thread function allows multiple functions to be performed simultaneously, like having multiple main functions. Is this correct?

Well, threads allow multiple functions to be performed simultaneously. main() is just the entry point to the program as a whole: it's the place where execution starts.

  1. Where is thread written in operating system/ programming language?

I'm not sure what you mean by this but it's going to depend on the OS and programming language in question.

  1. Will the program not be able to do multithreading if it doesn't include these thread functions?

A program will need some way of telling the operating system to make a new thread. In practical terms, using a library that somebody has already written is going to be more convenient than trying to manage the interaction with the operating system yourself.

$\endgroup$
4
  • $\begingroup$ The analogy is very helpful. I've edited the question and added what I mean by thread functions (e.g. pthread, CreateThread, etc.). $\endgroup$
    – kate
    Commented Feb 18, 2017 at 16:38
  • $\begingroup$ I've updated my answer. But please note that this isn't a discussion site, so this kind of back and forth doesn't tend to work too well. $\endgroup$ Commented Feb 18, 2017 at 17:05
  • $\begingroup$ Thank you David. I think I get it more clearly than before. So it's up to the developer/programmer to request to the operating system for multithreading by using the library or by using his/her own words. $\endgroup$
    – kate
    Commented Feb 18, 2017 at 18:01
  • 1
    $\begingroup$ @kate Yes. In practice, you'd surely use a library; but it's not the only way. $\endgroup$ Commented Feb 18, 2017 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.