0% found this document useful (0 votes)
57 views

Experiment 35: Illustration of Thread Management On Windows-Nt AIM: To Write A Program To Illustrate of Thread Management Functions Theory

A C program is implemented to illustrate thread synchronization using mutex locks. The program initializes a mutex, creates three threads that call a function to lock and unlock the mutex, and prints messages indicating which thread has locked or unlocked the mutex. Mutex locks ensure only one thread at a time can access a shared resource or critical section of code. The program successfully demonstrates synchronization of threads using mutex locks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Experiment 35: Illustration of Thread Management On Windows-Nt AIM: To Write A Program To Illustrate of Thread Management Functions Theory

A C program is implemented to illustrate thread synchronization using mutex locks. The program initializes a mutex, creates three threads that call a function to lock and unlock the mutex, and prints messages indicating which thread has locked or unlocked the mutex. Mutex locks ensure only one thread at a time can access a shared resource or critical section of code. The program successfully demonstrates synchronization of threads using mutex locks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Experiment 35: ILLUSTRATION OF THREAD MANAGEMENT ON WINDOWS-NT

AIM: To write a program to Illustrate Of Thread Management Functions


THEORY :
1. The thread-create function creates a thread. The four parameters are id of the
thread,
an attribute object, attribute name of the function and pointer to
function
2. The pthread-detach function gets the thread internal options to specify that
storage
for the thread can be reclaimed when the thread exits
3. pthread-self() - It is used to return id of the current thread and does not require
any parameter
4. pthread-equal() - This function is used to check whether the threads whose ids
are
passed as parameter are equal or not
5. pthread-join():-this function joins two threads at the required location

Figure illustrating threaded architecture


Thread management functions

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]$

Result: A program in c to illustrate the thread management functions is


implemented successfully.

Experiment 36 To Illustrate The Attributes Of The States Of A Thread


AIM : To write a C program to Illustrate The Attributes Of The States Of A Thread
THEORY :
Posix takes the object oriented approach to represent and assigning of
properties by encapsulating properties into an object of type pthread_attr_t. The
attribute object affects a thread only at the time of the creation.
First create the object and associate properties with the attribute object by
passing it to pthread-create
Property
Attribute objects
State

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

: PIII Processor , 128 MB RAM, 10GB


: OS: LINUX

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

Result: A program in C to illustrate the attributes of states of a thread is


implemented successfully.

Experiment 37: To Implement Thread Synchronization Using Mutex Locks


AIM: To write a C program to Implement Thread Synchronization Using Mutex Locks
THEORY :
Mutexes (mutual-exclusion locks) are used to implement critical sections and protect
shared mutable data structures against concurrent accesses.
A mutex is a special variable that can either be in locked state or unlocked state
Mutual exclusion locks (mutexes) are a comon method of serializing thread
execution. Mutual exclusion locks synchronize threads, usually by ensuring that
only one thread at a time executes a critical section of code. Mutex locks can also
preserve single-threaded code.
Mutex attributes may be associated with every thread.
The function pthread_mutex_init() to initialize the mutex, it is prototyped by:
int pthread_mutex_init(pthread_mutex_t *mp, const pthread_mutexattr_t *mattr);

Here, pthread_mutex_init() initializes the mutex pointed at by mp to its default value if


mattr is NULL, or to specify mutex attributes that have already been set with
pthread_mutexattr_init().
A mutex lock must not be reinitialized or destroyed while other threads might be
using it. Program failure will result if either action is not done correctly. If a mutex is
reinitialized or destroyed, the application must be sure the mutex is not currently in
use. pthread_mutex_init() returns zero after completing successfully. Any other
returned value indicates that an error occurred.
The function pthread_mute_lock() is used to lock a mutex, it is prototyped by:
int pthread_mutex_lock(pthread_mutex_t *mp);

locks the mutex pointed to by mp. When the mutex is already


locked, the calling thread blocks and the mutex waits on a prioritized queue. When
pthread_mute_lock() returns, the mutex is locked and the calling thread is the owner.
pthread_mute_lock() returns zero after completing successfully. Any other returned
value indicates that an error occurred.
pthread_mute_lock()

If mutex is locked it has distinguished thread that holds and owns the mutex, if no
thread holds then it is unlocked

To unlock a mutex use the function pthread_mutex_unlock() whose prototype is:


int pthread_mutex_unlock(pthread_mutex_t *mp);

Clearly, this function unlocks the mutex pointed to by mp.


The function pthread_mutex_destroy() may be used to destroy any state associated with
the mutex. It is prototyped by:
int pthread_mutex_destroy(pthread_mutex_t *mp);

and destroys a mutex pointed to by mp.


Note: that the space for storing the mutex is not freed. pthread_mutex_destroy() returns
zero after completing successfully. Any other returned value indicates that an error
occurred.
Contents of Pthread.h File
# include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t
*mutexattr);
int pthread_mutex-destroy(pthread_mutex-t *mutex);
int pthread-mutex-lock(pthread_mutex-t *mutex);
int pthrea-mutex-unlock(pthread_mutex_t * mutex);
The lock function blocks until mutex is available. The while try block always returns
immediately after the unlock function releases the specified mutex
Mutex locks are ideal for making changes to data structures in which state of data
structure is temporarily inconsistent as and when updating pointers in a shared link

Figure llustrating Thread management functions using mutex locks.


REQUIREMENTS:
HARDWARE
SOFTWARE

: PIII Processor , 128 MB RAM, 10GB


: OS: LINUX

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)

printf("Failed to create Thread 2\n");


else
printf("Successful in creating Thread 2 - %d\n",tid2);
k=pthread_create(&tid3,NULL,func,&fd);
if(k==-1)
printf("Failed to create Thread 3\n");
else
printf("Successful in creating Thread 3 - %d\n",tid3);
pthread_exit(NULL);}
OUTPUT:
[david@vasavi-edu os-lab]$ cc -o prg20.o prg20.c -pthread
[david@vasavi-edu os-lab]$ ./prg20.o
Successful in default initialization
Mutex locked by the thread - 1026
Successful in creating Thread 1 - 1026
Successful in creating Thread 2 - 2051
Successful in creating Thread 3 - 3076
Mutex unlocked by the thread - 1026
Mutex locked by the thread - 2051
Mutex unlocked by the thread - 2051
Mutex locked by the thread - 3076
Mutex unlocked by the thread - 3076
[david@vasavi-edu os-lab]$
Result: A program in C to Implement Thread Synchronization Using Mutex Locks

illustrated successfully.

You might also like