UTM
UNIVERSITI TEKNOLOGI MALAYSIA
jBACI - Ben-Ari
Concurrency Interpreter
Dr. Dayang Norhayati Abang Jawawi
&
Assoc. Prof. Dr. Rosbi bin Mamat
4. CP with BACI C--
• BACI C-- is a small subset of C++ with
concurrency constructs add-on not
originally available in C/C++.
• Only int & char data types & array of them
are supported. Refer to BACI C– compiler
handout for other limitations.
• Concurrent processes/threads are created &
executed using cobegin keyword.
2
4. CP with BACI C--
• BACI/jBACI (Ben-Ari Concurrency Interpreter)
is a simulation environment for learning
concurrent prog.
• BACI/jBACI is an IDE with editor, C-- &
pascal compiler, PCODE & concurrency
interpreter with RTSS.
• Refer to BACI/jBACI handouts for
installation & use. Requires Java (SDK or
JRE) to run !
3
4. CP with BACI C--
• Process sync provided with Semaphores &
Monitor:
• Semaphores keywords: semaphore, binarysem
– initialsem() initialize a semaphore at run-time.
– wait(semaphore& ) to enter critical region.
– signal(semaphore& ) to leave critical region.
• Monitor kw: monitor, condition
– waitc(condition, prio) to enter critical region.
– signalc(condition ) to leave critical region.
4
4. CP with BACI C--
• Example: interference in concurrent processes.
int sum = 0; // global shared var
void add10() { // task
int i; int local; // local vars
for (i = 1; i <= 10;i++) {
local = sum;
sum = local + 1;
}
}
void main() {
cobegin {
add10(); // create & run 2 tasks
add10();
}
cout << "Sum = " << sum << endl;
} 5
4. CP with BACI C--
• Solution: using semaphore.
int sum = 0; // global shared var
binarysem s = 1; // semaphore for sync
void add10() { // task
int i; int local; // local vars
for (i = 1; i <= 10;i++) {
wait(s); // enter critical region
local = sum; // do the job
sum = local + 1;
signal(s); // exit critical region
}
}
6
JBACI IDE: Lab Exercise 1
1. Open and compile concurrent program
(CP) C:\jbaci\examples\add.cm
2. Run the CP using Run & Go button. Run
the CP 5 times. Identify the value of
SUM for the 5 execution.
3. Run the CP step-by-step using Run &
Step Source or Step PCode. Explain the
problem occurred in this CP program.
Semaphore: Lab Exercise 2
1. Open and compile concurrent program
(CP) C:\jbaci\examples\addsem.cm.
2. Observe how the problem in Lab
Exercise 1 is solved here using step-by-
step using execution of the CP program.
Concurrent Programming
Problem Solving 5
Answer Problem Solving 5– question 1 & 2
Implement add10() using Java Concurrent Programming. Part of the
solution in Question 1.
9
another mechanism to support
concurrency
REAL-TIME OPERATING SYSTEMS