OS Multi-Threading Concepts
OS Multi-Threading Concepts
It comprises
a thread ID
a program counter
a register set and
a stack
It shares with other threads belonging to the same process
its:
code section
data section and
other OS resources such as open files & signals
The server will create a separate thread that listens for client requests.
When a request is made, rather than creating another process, the server will
create a new thread to service the request and resume listening for additional
requests.
Finally, most operating system kernels are now multithreaded; several threads operate
in the kernel, and each thread performs a specific task, such as managing devices or
interrupt handling.
For example, Solaris creates a set of threads in the kernel specifically for interrupt
handling; Linux uses a kernel thread for managing the amount of free memory in the
system.
Benefits:
The benefits of multithreaded programming can
be broken down into 4 categories
2) Balance : Programmers must also ensure that the tasks perform equal work of
equal value.
3) Data splitting. Just as applications are divided into separate tasks, the data
accessed and manipulated by the tasks must be divided to run on
separate cores.
4) Data dependency. programmers must ensure that the execution of the tasks is
synchronized to accommodate the data dependency.
The Java thread API allows threads to be created and managed directly in
Java programs.
#include <pthread.h>
#include <stdio.h>
int sum; I* this data is shared by the thread(s) *I
void *runner(void *param); I* the thread *I
int main(int argc, char *argv[])
{
pthread_t tid; I* the thread identifier *I
pthread_attr_t attr; I* set of thread attributes *I
if (argc != 2) {
fprintf(stderr,"usage: a.out <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr,"%d must be>= 0\n",atoi(argv[1]));
return -1;
}