I have a running process which has created multiple user mode threads. If the kernel changes the state of the process to TASK_UNINTERRUPTIBLE (or TASK_INTERRUPTIBLE) do the threads created by the process automatically get suspended?
This is not a homework question. I’m reading an operating systems book which describes how a semaphore is implemented. In their implementation the semaphore struct maintains a linked list of processes currently waiting for the semaphore. From what I’ve learned so far, such a semaphore could only be used to synchronize processes, not threads. Correct? The threads in the linked list are put into a TASK_INTERRUPTIBLE state until the semaphore is available, at which point one process is woken up by setting its state to TASK_RUNNING.
In Linux each thread is a separate
taskrunning within a process scope. See/proc/self/task/. They are even created with the same kernel function as a new process. Threads in Linux originated as “lightweight processes”.Each task has a unique task id (
tid), similar to the process id (pid) and indeed the master thread (the one executing main()) has the sametidas the processpid.The only functional difference in Linux between threads and processes is that all threads (tasks) share all process resources apart from
TASK_UNINTERRUPTIBLE,TASK_INTERRUPTIBLE)main()thread identifies the processSo
TASK_INTERRUPTIBLEcan be applied to each thread individually.As such semaphores are perfectly valid to use for synchronising threads. In this case if one thread blocks on a semaphore, it’s jus that one thread.