Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Operating System - Assignment 5

18F-0123 Amina Javed


Q1): Write a detail note on mutex. How it works. (explain with the
help of example)

MUTEX:
It is a programming concept that is frequently used to solve multi-threading problems. Its
purpose is to provide locking mechanism. It is used to provide mutual exclusion to a section of
code, which means only one process, can work on a particular code section at a time. It works in
user space. The requirement of mutual exclusion was first identified and solved by Edger W.
Dijkstra.

Working of MUTEX:
The data of a mutex is simply an integer in memory. Its value is 0 in starting which means that it
is unlocked. If you wish to lock the mutex, you check if it is zero and then assign one. The mutex
is now locked, and you are the owner of it.

Example:
Imagine a big office (your program), with many assets (shared resources) and many employees
(threads). For example, all employees share one common resource - a microwave. They have
agreed to use a label on microwave’s door (mutex).

When an employee wants to use the microwave, he checks a label on its door (locks a mutex). If
it’s "engaged" he waits (blocks on a mutex). When it is "free", he uses the microwave and
changes the label to "engaged" (mutex lock succeeds). When employee finishes his business with
the microwave, he closes it and changes the label to "free" (unlocks mutex).

Many people can use microwaves at the same time, forming a queue (many threads block on a
mutex). Then it is up to a company's policy (threads implementation), who is going to use the
microwave next. Usually, it is the first person in the queue (FIFO algorithm).
Q2): Write a simple program that create two threads sender and
receiver. The sender thread send a message string to receiver
thread. This program uses semaphore so as to achieve
synchronization.
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<iostream>
#include<pthread.h>
#include<fstream>
#define size 100
char text[size];
pthread_mutex_t locker;
using namespace std;
void *sender(void *argc)
{
cout<<endl<<"@Sender\n";
cout<<"Enter your Text: ";
cin.get(text,100);
cout<<endl<<"Your Msg has been successfully sent"<<endl<<endl;
pthread_mutex_unlock(&locker);
}
void *receiver(void *argc)
{
pthread_mutex_lock(&locker);
cout<<endl<<"\n@Receiver\n";
cout<<"The Text is Here: ";
cout<<text<<endl;
pthread_mutex_unlock(&locker);
}
int main()
{
pthread_t id[2];

pthread_mutex_init(&locker,NULL);
pthread_mutex_lock(&locker);
pthread_create(&id[0],NULL,sender,NULL);
pthread_create(&id[1],NULL,receiver,NULL);
pthread_join(id[0],NULL);
pthread_join(id[1],NULL);
pthread_mutex_destroy(&locker);
pthread_exit(NULL);
}

Q3): Write a simple program that create two threads sender and
receiver. The sender thread send a message string to receiver
thread. This program uses condition variables to achieve
synchronization.
#include<iostream>
#include<pthread.h>
#include<fstream>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#define size 100
char msg[size];
pthread_mutex_t my_sync;
pthread_cond_t rx;
#define true 1
#define false 0
int isdone = false;
pthread_mutex_t locker;
using namespace std;
void *sender(void *argc)
{
pthread_mutex_lock(&my_sync);
cout<<"@sender"<<endl;
cout<<"Enter message string:";
cin.get(msg,100);
cout<<"Message Delivered!"<<endl;
isdone = true;
pthread_cond_signal(&rx);
pthread_mutex_unlock(&my_sync);

}
void *reciver(void *argc)
{
pthread_mutex_lock(&my_sync);
while (!isdone) pthread_cond_wait(&rx, &my_sync);
cout<<"@reciver"<<endl;
cout<<"your Message:"<<msg<<endl;
pthread_mutex_unlock(&my_sync);
}
int main()
{
pthread_t tid1, tid2;
pthread_attr_t attr;
pthread_cond_init(&rx, NULL);
pthread_mutex_init(&my_sync, NULL);
pthread_attr_init(&attr);
pthread_create(&tid1, &attr, sender, NULL);
pthread_create(&tid2, &attr, reciver, NULL);
sleep(20);
return 0;
}
Q4): Write a program that contains 3 threads. Each thread
increment the global variable and then adds that value to the result
which is another global variable. Initialize result and other variable
with 0. Output the final value of result and the other global variable.
(Synchronize the code so that result does not contains the
inconsistent)
Q5): Suppose, you have a bank account with some balance amount
Rs. 100. It is not enough to meet one month expenditure, so you
asked your dad and brother and cousin to deposit some money in
your account. ……………………………….
#include<iostream>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>

pthread_mutex_t MYsync;
using namespace std;
void* threadNo1(void* argc);
void* threadNo2(void* argc);
void* threadNo3(void* argc);
int CurrentBalance = 100;
void* threadNo1(void* argc)
{
pthread_mutex_lock(&MYsync);
int DAD = 10000;
int oldBalanceOfUser = 100;
int OldBalance = CurrentBalance;
int NewBalance = OldBalance + DAD;
CurrentBalance = NewBalance;

cout << "\n*\ttDAD's Info\t*\n";


cout << "Already existing Amount in Account = " << oldBalanceOfUser;
cout << "\nNew Amount Deposited = " << DAD ;
cout << "\nUpdated Balance in Account = " << CurrentBalance<<"\n";
pthread_mutex_unlock(&MYsync);

}
void* threadNo2(void* argc) {
pthread_mutex_lock(&MYsync);
int brother = 5000;
int oldBalanceOfUser = 100;
int OldBalance = CurrentBalance;
int NewBalance = OldBalance + brother;
CurrentBalance = NewBalance;

cout << "\n*\tBrother's Info\t*\n";


cout << "Already existing Amount in Account = " << oldBalanceOfUser;
cout << "\nNew Amount Deposited = " << brother ;
cout << "\nUpdated Balance in Account = " << CurrentBalance<<"\n";
pthread_mutex_unlock(&MYsync);

}
void* threadNo3(void* argc) {
pthread_mutex_lock(&MYsync);
int cousin = 5500;
int oldBalanceOfUser = 100;
int OldBalance = CurrentBalance;
int NewBalance = OldBalance + cousin;
CurrentBalance = NewBalance;

cout << "\n*\tCousin's Info\t*\n";


cout << "Already existing Amount in Account = " << oldBalanceOfUser;
cout << "\nNew Amount Deposited = " << cousin ;
cout << "\nUpdated Balance in Account = " << CurrentBalance<<"\n";
pthread_mutex_unlock(&MYsync);

int main()
{
pthread_t fist;
pthread_t second;
pthread_t third;
pthread_attr_t attr;
pthread_mutex_init(&MYsync, NULL);

pthread_attr_init(&attr);

pthread_create(&fist, &attr, threadNo1, NULL);


pthread_create(&second, &attr, threadNo2, NULL);
pthread_create(&third, &attr, threadNo3, NULL);
pthread_join(fist, NULL);
pthread_join(second, NULL);
pthread_join(third, NULL);
cout <<"\n";
pthread_mutex_destroy(&MYsync);
pthread_exit(NULL);
return 0;
}

You might also like