Page | 1
Iqra University Islamabad Campus
(Department of Computer Science)
Project Report
Of
Operating System
Presented By
Roll No Name
25552 SHAHBAZ LATIF
25397 BILAL IMRAN
25510 Maheen Ibrar
Under the guidance of
Sir Arslan Majid
Page | 2
Contents of Report:
● -Introduction
● Chapter-1 Synchronization
● Chapter-2 Code section
● Chapter-3 Screenshots
Page | 3
Introduction
This is a small scale project for operating system. The basic idea is
that to synchronize the equation because of avoiding the deadlock where
we implement the locks with in the thread implementation to make the
equation synchronize where they get the input from user and give the
solution related their inputs.
A thread is a single sequence stream within in a process. Because
threads have some of the properties of processes, they are sometimes
called lightweight processes. In a process, threads allow multiple
executions of streams. In many respect, threads are popular way to
improve application through parallelism. The CPU switches rapidly back
and forth among the threads giving illusion that the threads are running in
parallel. Like a traditional process i.e., process with one thread, a thread
can be in any of several states (Running, Blocked, Ready or terminated).
Each thread has its own stack. Since thread will generally call different
procedures and thus a different execution history. This is why thread
needs its own stack. An operating system that has thread facility, the
basic unit of CPU utilization is a thread. A thread has or consists of a
program counter (PC), a register set, and a stack space. Threads are not
independent of one other like processes as a result threads shares with
other threads their code section, data section, OS resources also known
as task, such as open files and signals.
Page | 4
A lock is an abstract concept. The basic premise is that a lock
protects access to some kind of shared resource. If you own a lock then
you can access the protected shared resource. If you do not own the lock
then you cannot access the shared resource.
CHAPTER-1
Synchronization
The correctness of a concurrent program should not depend on accidents of
timing.
Since race conditions caused by concurrent manipulation of shared mutable data
are disastrous bugs — hard to discover, hard to reproduce, hard to debug — we
need a way for concurrent modules that share memory to synchronize with each
other.
Locks are one synchronization technique. A lock is an abstraction that allows at
most one thread to own it at a time. Holding a lock is how one thread tells other
threads: “I’m working with this thing, don’t touch it right now.”
Locks have two operations:
Acquire allows a thread to take ownership of a lock. If a thread tries to acquire
a lock currently owned by another thread, it blocks until the other thread
releases the lock. At that point, it will contend with any other threads that are
trying to acquire the lock. At most one thread can own the lock at a time.
Release: relinquishes ownership of the lock, allowing another thread to take
ownership of it.
Page | 5
Using a lock also tells the compiler and processor that you’re using shared
memory concurrently, so that registers and caches will be flushed out to shared
storage. This avoids the problem of reordering, ensuring that the owner of a lock
is always looking at up-to-date data.
CHAPTER-2
CODE:
//(ab + ac +ad +ae)/b^2 - 4ac
//eq to solve
//if a,b,c,d,e = {2,6,4,2,2} answer should be 7
#include <iostream>
#include<pthread.h>
#include<sys/types.h>
#include <stdio.h>
#include <stdlib.h>
bool l_a=false,l_m=true,l_d=false;
int value[5]={0,0,0,0,0};
int p1,p2,p3,p4,s1,d1;
int result=-1;
using namespace std;
Page | 6
void *Division(void*)
cout<<"I am in div"<<endl;
if(l_d==true)
d1=(value[1]*value[1])-(4*p2);
result = s1/d1;
cout << "\nfinal result" << endl ;
cout << result<<endl;
l_m=false;
l_a=false;
l_d=false;
pthread_exit(NULL);
else
cout<<"Division cannot perform"<<endl<<endl;
return 0;
}
Page | 7
void *Multiplication(void*)
cout<<endl<<"I am in mult"<<endl;
if(l_m==true)
p1=value[0]*value[1];
p2=value[0]*value[2];
p3=value[0]*value[3];
p4=value[0]*value[4];
cout << "Multiplication result" << endl;
cout <<"a*b : "<< p1<<endl;
cout <<"a*c : " <<p2<<endl;
cout <<"a*d : " <<p3<<endl;
cout <<"a*e : " <<p4<<endl;
l_m=false;
l_a=true;
l_d=false;
pthread_exit(NULL);
else
Page | 8
cout<<"Multiplication cannot perform now"<<endl<<endl;
void *Addition(void*)
cout<<endl<<"I am in Addition section"<<endl;
if(l_a==true)
s1=p1+p2+p3+p4;
cout<<"output of add function"<<s1<<endl<<endl;
l_m=false;
l_a=false;
l_d=true;
pthread_exit(NULL);
else
cout<<"Addition cannot perform now"<<endl<<endl;
Page | 9
int main()
for(int k=0;k<5;k++)
cout<<"Kindly enter "<<k+1<<" value"<<endl;
cin >> value[k];
pthread_t mul;
pthread_t div;
pthread_t ad;
while(l_m||l_a||l_d)
cout<<"lm la ld"<<endl;
cout<<l_m<<" "<<l_a<<" "<<l_d<<endl;
pthread_create(&div,NULL,Division,NULL);
pthread_create(&ad,NULL,Addition,NULL);
pthread_create(&mul,NULL,Multiplication,NULL);
Page | 10
pthread_join(mul,NULL);
pthread_join(ad,NULL);
pthread_join(div,NULL);
};
return 0;
}
Page | 11
CHAPTER-3
Screenshots:
Page | 12