4

My CS professor told to the class that the OS has no idea that an application has launched threads. Is this true?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Cody Smith
  • 2,732
  • 3
  • 31
  • 43
  • I think he is pretty much correct. A thread is a programming construct, unlike a process which the operating system manages. The OS only really knows of address space and the applications machine instructions, I think. – James Wierzba Oct 05 '15 at 20:55
  • 2
    http://unix.stackexchange.com/questions/892/is-there-a-way-to-see-details-of-all-the-threads-that-a-process-has-in-linux – Sotirios Delimanolis Oct 05 '15 at 20:56
  • http://stackoverflow.com/questions/268680/how-can-i-monitor-the-active-thread-count-of-a-process-jvm-on-linux – Sotirios Delimanolis Oct 05 '15 at 20:58
  • Since threads are created by the operating system, what you say sounds odd. Perhaps given the context and the exact phrasing it can be understood differently. For example, he could mean the old "Green Threads" that were used by Java a long time ago. – RealSkeptic Oct 05 '15 at 21:00
  • 2
    Seriously, what's the connection between this question and the duplicate? The OP did not even mention the word "process". – RealSkeptic Oct 05 '15 at 21:06
  • Maybe @SotiriosDelimanolis got mixed with my edit after I retagged the question. I removed the `[process]` tag to avoid confusions. – Luiggi Mendoza Oct 05 '15 at 21:09
  • @RealSkeptic What is a thread without a process? The post goes into detail about the different between user space threads and kernel threads and states _A thread is the basic unit to which the operating system allocates processor time_. If the OS did not know about the thread, it could not do that. – Sotirios Delimanolis Oct 05 '15 at 21:09
  • @SotiriosDelimanolis The professor is alluding to user-space threading libraries. There are also threading libraries that the kernel is aware of, see my answer. – missimer Oct 05 '15 at 21:11
  • @missimer That's up for clarification from the OP, but the duplicate post went into detail about that as well. – Sotirios Delimanolis Oct 05 '15 at 21:13
  • 1
    @SotiriosDelimanolis Threads can be implemented either on the operating system level or the application level. It seems odd that the prof says that threads in general are not known to the operating system, but it *is* true for some applications. Oh, and if I were to find this question having a similar one, and go to the duplicate - I would not know how it answers my question, without being an expert, which means I would not be having the question in the first place. – RealSkeptic Oct 05 '15 at 21:13
  • @RealSkeptic _Stack Overflow is a question and answer site for professional and enthusiast programmers._ There's a level of knowledge and research that's expected. Anyway, do what you think is right. – Sotirios Delimanolis Oct 05 '15 at 21:20

2 Answers2

5

It depends on the type of thread. Threads implemented purely at user-level would be unknown to the operating system. This can be done with signals and setjmp and longjmp (see www.gnu.org/s/pth/rse-pmt.ps for details). Alternatively, if you are talking about something such as Linux pthreads, which only implements of subset of the pthreads specification, specifically the part that involves create new threads of execution that the kernel are aware of and schedules then the kernel is aware.

If you want to see more detail about how the kernel is aware you can look at the the clone system call. This system call can be used to create a new thread of execution that shares the address space of the calling process.

Also in the case of user-space implemented threading you will not get true parallelism, in the sense that two threads will be executing at the exact same time on different cores/hardware threads, because the operating system, which does the scheduling, does not know about the multiple threads.

missimer
  • 4,022
  • 1
  • 19
  • 33
1

It depends upon the operating system. Older operating system had no threads. Programming libraries would implement threads (e.g., Ada tasks) with timers. The library included a thread scheduler.

It is increasingly common now for operating systems to schedule threads for execution. There, the OS is aware of threads.

user3344003
  • 20,574
  • 3
  • 26
  • 62