Experiment 35: Illustration of Thread Management On Windows-Nt AIM: To Write A Program To Illustrate of Thread Management Functions Theory
Experiment 35: Illustration of Thread Management On Windows-Nt AIM: To Write A Program To Illustrate of Thread Management Functions Theory
Posix Function
pthread-create
pthread-cancel
pthread-detach
pthread-equal
Description
Create a thread
Terminates another thread
Used to release resources
Test 2 threads for equality
pthread-exit
Exiting a thread
pthread-kill
Send a signal to a thread
pthread-join
Wait for a thread
pthread-self
To get owner of thread
REQUIREMENTS:
HARDWARE
: PIII Processor , 128 MB RAM, 10GB
SOFTWARE
: OS: LINUX
PROGRAM:
#include<unistd.h>
#include<pthread.h>
int s,w;
pthread_t tid,t2id;
void *create(void *arg)
{
printf("The thread with id %d is created\n",pthread_self());
}
void thid(void)
{
pthread_t tid;
tid=pthread_self();
printf("The main thread has an id=%d\n",tid);
}
void equal(void)
{
int e;
e=pthread_equal(t2id,tid);
if(e==0)
printf("Threads are not equal\n");
else
printf("Threads are equal\n");
}
void detach(void)
{
int x;
x=pthread_detach(tid);
if(x==0)
printf("Successfully detached thread with the id %d\n",tid);
else
printf("Thread is not detached\n");
}
void joint(void)
{
int c;
int *code;
c=pthread_join(t2id,(void**)&code);
if(c==0)
printf("Joined back thread with the id=%d\n",t2id);
else
printf("Could not join\n");
}
int main(void)
{
int i,a,b;
a=pthread_create(&tid,NULL,create,&s);
b=pthread_create(&t2id,NULL,create,&w);
while(1)
{
printf("Which one u want\n");
printf("1.Thread id\n");
printf("2.Join:\n");
printf("3.Test for equality\n");
printf("4.Detach a thread\n");
printf("5.Exit\n");
scanf("%d",&i);
switch(i)
{
case 1:
thid();
break;
case 2:
joint();
break;
case 3:
equal();
break;
case 4:
detach();
break;
case 5:
pthread_exit(NULL);
}
}
}
OUTPUT:
[david@vasavi-edu os-lab]$ cc -o prg18.o prg18.c -pthread
[david@vasavi-edu os-lab]$ ./prg18.o
The thread with id 1026 is created
The thread with id 2051 is created
Which one u want
1.Thread id
2.Join:
3.Test for equality
4.Detach a thread
5.Exit
1
The main thread has an id=1024
Which one u want
1.Thread id
2.Join:
3.Test for equality
4.Detach a thread
5.Exit
2
Joined back thread with the id=2051
Which one u want
1.Thread id
2.Join:
3.Test for equality
4.Detach a thread
5.Exit
3
Threads are not equal
Which one u want
1.Thread id
2.Join:
3.Test for equality
4.Detach a thread
5.Exit
5
[david@vasavi-edu os-lab]$
Function
Pthread_attr_destroy
Pthread_attr_init
Pthread_attr_getdetachstate
Pthread_attr_setdetachstate
The init function initializes a thread attribute object with default values
The destroy function sets the value of attribute object to be invalid
The pthread_attr_getdetachstate function gets state of an attribute object,
The pthread_attr_setdetachstate function sets the state of an attribute object.
REQUIREMENTS:
HARDWARE
SOFTWARE
PROGRAM:
#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
void *proc(void * arg)
{
printf("The thread created with the id : %d\n",pthread_self());
}
int main(void)
{
pthread_t tid;
int fd,i,m,y,val;
pthread_attr_t attr;
void *proc(void *arg);
fd=open("abc",O_RDONLY);
i=pthread_attr_init(&attr);
if(i==-1)
printf("Failed to initialized\n");
else
printf("Default values are initialized\n");
m=pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if(m==-1)
printf("Failed to set state of the attribute\n");
else
printf("Successful to set state of attribute\n");
y=pthread_create(&tid,&attr,proc,&fd);
if(y==-1)
printf("Failed to create\n");
else
printf("Thread created\n");
m=pthread_attr_getdetachstate(&attr,&val);
printf("Thread state is %x\n",val);
}
OUTPUT:
[david@vasavi-edu os-lab]$ cc -o prg19.o prg19.c -pthread
[david@vasavi-edu os-lab]$ ./prg19.o
Default values are initialized
Successful to set state of attribute
The thread created with the id : 1026
Thread created
Thread state is 1
If mutex is locked it has distinguished thread that holds and owns the mutex, if no
thread holds then it is unlocked
PROGRAM:
#include<pthread.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<string.h>
pthread_mutex_t mutex;
void * func(void *arg)
{
int i;
i=pthread_mutex_lock(&mutex);
if(i==0)
printf("Mutex locked by the thread - %d\n",pthread_self());
sleep(2);
i=pthread_mutex_unlock(&mutex);
if(i==0)
printf("Mutex unlocked by the thread - %d\n",pthread_self());
}
int main(void)
{
int x,y,z,k,fd;
pthread_t tid1,tid2,tid3;
pthread_mutex_t mutex;
pthread_mutexattr_t mattr;
x=pthread_mutex_init(&mutex,&mattr);
if(x==-1)
printf("Failed to initialize mutex\n");
else
printf("Successful in default initialization\n);
y=pthread_create(&tid1,NULL,func,&fd);
if(y==-1)
printf("Failed to create Thread 1\n");
else
printf("Successful in creating Thread 1 - %d\n",tid1);
z=pthread_create(&tid2,NULL,func,&fd);
if(z==-1)
illustrated successfully.