VIGNAN UNIVERSITY
Class: M.TECH EXP NO: ROLL NO:131FB06008
MUTEX
AIM: To write a program for MUTEX in real time operating system.
Apparatus:
-PC
- LINUX Terminal.
ALGORITHM:
Step 1: Initialize local variable threads rthread,cthread.
Step 2: initialize a mutex object and set initial value to ‘0’.
Step 3: create rthread
Step 4: if result not equal to zero print mutex initialisation failed,goto step 9.
Step 5: if result =’0’ then initialize rthread.
Step 6: if entered text is stop terminate,if not go to step 7.
Step 7: if mutex is unlock then lock mutex,if mutex is locked increment n.
Step 8: goto step 6.
Step 9: create cthread.
Step 10:if result not equal to zero terminate cthread and delete mutex ,go to step 16.
Step 11: initialize cthread
Step 12: if mutex is locked decrement n by 1.
Step 13: if mutex is unlock check entered text .
Step 14: if entered text is stop then terminate.
Step 15: if text is not stop convert text to upper case,goto step 12.
Step 16: end
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
1
VIGNAN UNIVERSITY
Class: M.TECH EXP NO: ROLL NO:131FB06008
#include<string.h>
#include<ctype.h>
#include<pthread.h>
#define BUFFER_SIZE 1024
pthread_mutex_t mutex;
char buffer[BUFFER_SIZE];
void *read_thread(void *arg)
{
while(strncmp("stop",buffer,4) != 0){
printf("Enter text: ");
fgets(buffer, BUFFER_SIZE, stdin);
pthread_mutex_unlock(&mutex); // unlocking mutex
}
pthread_exit("read_thread exit successful");
}
void *convert_thread() {
pthread_mutex_lock(&mutex);
int i;
// pthread_mutex_lock(&mutex); // locking mutex
while(strncmp("stop", buffer, 4) != 0) {
printf("Converted text: ");
for(i=0;i<strlen(buffer);i++)
printf("%c",toupper(buffer[i]));
pthread_mutex_lock(&mutex); // locking mutex
}
pthread_exit("convert_thread exit successful");
}
2
VIGNAN UNIVERSITY
Class: M.TECH EXP NO: ROLL NO:131FB06008
int main()
{
int result;
pthread_t rthread,cthread;
void *thread_result;
result = pthread_mutex_init(&mutex, NULL); // initializition of mutex
if(result != 0){
printf("Mutex initialization failed");
exit(1);
}
printf("Enter text, the program will convert it into upper case,to stop enter 'stop'\n");
pthread_mutex_lock(&mutex); // locking mutex
result = pthread_create(&cthread, NULL, convert_thread, NULL);
if(result != 0){
printf("convert_thread creation failed");
exit(1);
}
result = pthread_create(&rthread, NULL, read_thread, NULL);
if(result != 0){
printf("read_thread creation failed");
exit(1);
}
result = pthread_join(rthread, &thread_result);
3
VIGNAN UNIVERSITY
Class: M.TECH EXP NO: ROLL NO:131FB06008
if(result != 0){
printf("read_thread join failed");
exit(1);
}
printf("read_thread joined, %s\n",thread_result);
result = pthread_join(cthread, &thread_result);
if(result != 0)
{
printf("convert_thread join failed");
exit(1);
}
printf("convert_thread joined, %s\n",thread_result);
pthread_mutex_destroy(&mutex); // tidies up mutex
exit(0);
}
4
VIGNAN UNIVERSITY
Class: M.TECH EXP NO: ROLL NO:131FB06008
OUT PUT:
RESULT:
HENCE THE PROGRAM IS EXECUTED SUCCESSFULLY AND
OUTPUT VERIFIED.