University of Central Punjab: Objective
University of Central Punjab: Objective
Threads
Objective
What is a Thread?
A thread is a flow of control within a process. A process can
contain multiple threads.
Why Multithreading?
A thread is also known as lightweight process. The idea is to
achieve parallelism by dividing a process into multiple
threads. For example, in a browser, multiple tabs can be
different threads. MS Word uses multiple threads: one thread
to format the text, another thread to process inputs, etc.
More advantages of multithreading are discussed below Process
vs Thread?
The primary difference is that threads within the same
process run in a shared memory space, while processes run in
separate memory spaces.
Threads are not independent of one another like processes
are, and as a result threads share with other threads their
code section, data section, and OS resources (like open files
and signals). But, like process, a thread has its own program
counter (PC), register set, and stack space.
Advantages of Thread over Process
1 Responsiveness: If the process is divided into multiple
threads, if one thread completes its execution, then its
output can be immediately returned.
struct two_numbers {
int a;
int b;
};
pthread_create(&pid1,NULL,&f1,(void*)numbers);
pthread_create(&pid2,NULL,&f2,ptr);
pthread_join(pid1,&stat1);
pthread_join(pid2,&stat2);
if(stat1!=NULL && stat2 != NULL)
printf("Sum are %ld and %ld
University of Central Punjab
FOIT (Operating Systems)
GL- 9
Threads
\n",(long)stat1,(long)stat2);
return 0;
}
#include <pthread.h>
int pthread_cancel(pthread_t
thread); Compile and link with
- pthread.
University of Central Punjab
FOIT (Operating Systems)
GL- 9
Threads
#include <pthread.h>
PTHREAD_CANCEL_ENABLE
The thread is cancelable. This is the default cancelability
state in all new threads, including the initial thread. The
thread's cancelability type determines when a cancellable
thread will respond to a cancellation request.
PTHREAD_CANCEL_DISABLE
University of Central Punjab
FOIT (Operating Systems)
GL- 9
Threads
PTHREAD_CANCEL_DEFERRED
A cancellation request is deferred until the thread next calls
a function that is the cancellation point. This is the
default cancelability type in all new threads, including the
initial thread. Even with deferred cancellation, a
cancellation point in an asynchronous signal handler may
still be acted upon and the effect is as if it was an
asynchronous cancellation.
PTHREAD_CANCEL_ASYNCHRONOUS
The thread can be canceled at any time. (Typically, it will
be canceled immediately upon receiving a cancellation
request, but the system doesn't guarantee this.)
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
void* f1()
{
pthread_setcanceltype( PTHREAD_CANCEL_A
SYNCHRONOUS,NULL);
while(1)
printf("\nThis is thread 1");
}
void* f2()
{ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
while(1){
for(int i=0;i<2;i++)
{
printf("\nThis is thread 2");
}
pthread_testcancel();
}
}
int main()
{
pthread_t pid1,pid2; pthread_create(&pid1,NULL,&f1,
(void*)NULL); pthread_create(&pid2,NULL,&f2,(void*)NULL);
sleep(5);
pthread_cancel(pid1); sleep(5);
pthread_cancel(pid2);
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
return 0;
}
#include <pthread.h>
University of Central Punjab
FOIT (Operating Systems)
GL- 9
Threads
#include <pthread.h>
int main()
{
pthread_t pid1; size_t stacksize;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr, &stacksize); printf("\
nCurrent size of stack is
%d",(int)stacksize);
pthread_attr_setstacksize(&attr,1024*1024*16); /*
setting stack to 16 MB*/
pthread_create(&pid1,&attr,&f1,(void*)NULL);
pthread_join(pid1,NULL);
pthread_attr_destroy(&attr);
University of Central Punjab
FOIT (Operating Systems)
GL- 9
Threads
return 0;
}
Practce Tasks
All codes must be compiled using –Wall –Werror flags.
tan(x):
Since
arctan(1) = pi/4, we can compute pi = 4*[1
- 1/3 + 1/5 - 1/7 + 1/9 - . . . ]
Run your program as pi_mutex <number of threads> <n>