Front Threads
Front Threads
Threads
Process vs. Thread
• process:
• an address space with 1 or more threads
executing within that address space, and the
required system resources for those threads
• a program that is running
• thread:
• a sequence of control within a process
• shares the resources in that process
Advantages of Threads
• The overhead for creating a thread is
significantly less than that for creating a
process
• Multitasking, i.e., one process serves multiple
clients
• Switching between threads requires the OS to
do much less work than switching between
processes
Drawbacks of Threads
• Not as widely available as longer established
features
• Writing multithreaded programs require more
careful thought
• More difficult to debug than single threaded
programs
• For single processor machines, creating several
threads in a program may not necessarily produce
an increase in performance
main thread
• initial thread created when main() is invoked by
the process loader
• once in the main(), the application has the ability
to create daughter threads
• if the main thread returns, the process terminates
even if there are running threads in that process,
unless special precautions are taken
• to explicitly avoid terminating the entire process,
use pthread_exit()
Create thread
• int pthread_create( pthread_t *thread, pthread_attr_t
*attr, void *(*thread_function)(void *), void *arg );
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
pthread_join(t1, NULL);
return 0;
}