How To make your program mulch-processing or multi-threading:
This question made me curious so I started to dig around this.I first read articals from internet and I found it quit easy.But has always when I implemented it ,it took lot time to for getting actualy my code running.In this blog I will let you know problems that I faced but first let us start with theory of threads.I am making notes from advance-Linux-programming book.
Thread:
-Threads are like process,a mechanism to allow a program to do more than one thing at a time
-A thread is sometimes referred to as a lightweight process.A thread will share all global variables and files descriptor of the parent process which allows the programmer to separate multiple tasks easily within a process.
-Advantage of using thread is that they will automatically take advantage of machine with multiple processors
-using mutiple threads will also use fewer system resources compared to forking a child process to handle a connection request
-A thread shares most of its resources with the parent process,so a thread will use fewer resources than a process would
-it shares everything except it has its own program counter,stack and registers
-however static variable inside the thread will be shared between threads.
Thread Creation:
-Each thread in a process is identified by a thread ID
stored.
2. A pointer to a thread attribute object.This object controls details of how the
thread interacts with the rest of the program. If you pass NULL as the thread
attribute, a thread will be created with the default thread attributes.Thread
attributes are discussed in Section 4.1.5, “Thread Attributes.”
3. A pointer to the thread function.This is an ordinary function pointer, of this
type:
void* (*) (void*)
4. A thread argument value of type void*. Whatever you pass is simply passed as
the argument to the thread function when the thread begins executing.
-Thread can be exited in two ways
1)is by returning from the thread function
2)Alternately, a thread can.
exit explicitly by calling pthread_exit.
This question made me curious so I started to dig around this.I first read articals from internet and I found it quit easy.But has always when I implemented it ,it took lot time to for getting actualy my code running.In this blog I will let you know problems that I faced but first let us start with theory of threads.I am making notes from advance-Linux-programming book.
Thread:
-Threads are like process,a mechanism to allow a program to do more than one thing at a time
-A thread is sometimes referred to as a lightweight process.A thread will share all global variables and files descriptor of the parent process which allows the programmer to separate multiple tasks easily within a process.
-Advantage of using thread is that they will automatically take advantage of machine with multiple processors
-using mutiple threads will also use fewer system resources compared to forking a child process to handle a connection request
-A thread shares most of its resources with the parent process,so a thread will use fewer resources than a process would
-it shares everything except it has its own program counter,stack and registers
-however static variable inside the thread will be shared between threads.
Thread Creation:
-Each thread in a process is identified by a thread ID
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void *), void *arg)
1. A pointer to a pthread_t variable, in which the thread ID of the new thread is
stored.
2. A pointer to a thread attribute object.This object controls details of how the
thread interacts with the rest of the program. If you pass NULL as the thread
attribute, a thread will be created with the default thread attributes.Thread
attributes are discussed in Section 4.1.5, “Thread Attributes.”
3. A pointer to the thread function.This is an ordinary function pointer, of this
type:
void* (*) (void*)
4. A thread argument value of type void*. Whatever you pass is simply passed as
the argument to the thread function when the thread begins executing.
-Thread can be exited in two ways
1)is by returning from the thread function
2)Alternately, a thread can.
exit explicitly by calling pthread_exit.
No comments:
Post a Comment