Lab. 2 (April 27th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.

Slides:



Advertisements
Similar presentations
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
02/05/2008CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying an earlier.
1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Jonathan Walpole Computer Science Portland State University
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
Tutorial 2 Adventures in Threading presented by: Antonio Maiorano Paul Di Marco.
Multithreading.
Project 2 Data Communication Spring 2010, ICE Stephen Kim, Ph.D.
Lecture 4 : JAVA Thread Programming
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CS333 Intro to Operating Systems Jonathan Walpole.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Exercise 1 : ex1.java class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread.
Multithreading in JAVA
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Software and Threading Geza Kovacs Maslab Abstract Design: State Machines By using state machine diagrams, you can find flaws in your behavior without.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Now that you know the pthread API…  How do you create threads?  How do you pass different values to them?  How do you return values from threads? 
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Computer Science 320 A First Program in Parallel Java.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
Component-Based Software Engineering Understanding Thread Safety Paul Krause.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java.
1 Programming with Shared Memory - 2 Issues with sharing data ITCS 4145 Parallel Programming B. Wilkinson Jan 22, _Prog_Shared_Memory_II.ppt.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Lab. 2 (April 15th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
CS399 New Beginnings Jonathan Walpole.
Lab. 3 (May 6st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
Lab. 2 Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use cygwin.
Operating Systems Lecture 13.
Lab. 2 (May 12th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
null, true, and false are also reserved.
Operating Systems Lecture 14.
Lab. 3 (May 11th) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute :
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Parallelism for summation
Lab. 2 (May 2nd) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use.
Jonathan Walpole Computer Science Portland State University
Programming with Shared Memory
Lab. 3 (May 1st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
Developing Java Applications with NetBeans
Programming with Shared Memory - 2 Issues with sharing data
Exercise 1 : ex1.java Thread Creation and Execution
Developing Java Applications with NetBeans
Exercise 1 : ex1.java Thread Creation and Execution (sleep() and join()) class C extends Thread {   int i;   C(int i) { this.i = i; }   public void run()
Computer Architecture Multi-threaded Matrix Multiply
Presentation transcript:

Lab. 2 (April 27th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use cygwin for programming You may use Text editor : nano or vim Compiling “> gcc ex1.c” will generate a.exe Execute : “> ./a.exe”

Exercise 1 : ex1.java Modify following JAVA code ex1.java to C code with pthread library for Thread Creation and Execution class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread " + i + " says bye"); } class ex1 { public static void main(String[] args) { System.out.println("main thread start!"); for(int i=1; i <= 5; ++i) { C c = new C(i); c.start(); System.out.println("main thread end!");

Exercise 1 : lab2_ex1.c Sample C code with pthread library #include <pthread.h> #include <stdio.h> void *c(void* i) { printf("Thread %d says hi\n",i); printf("Thread %d says bye\n",i); } int main (int argc, char *argv[]) { pthread_t threads[20]; int rc, t; printf("main thread start\n"); for(t=1; t<=20; t++){ rc = pthread_create(&threads[t], NULL, c, (void *)t); if (rc) { printf("ERROR code is %d\n", rc); exit(-1); printf("main thread end\n"); pthread_exit(NULL);

Exercise 2 : parallel summation Modify following multithreaded JAVA code ex2.java to C code with pthread library class SumThread extends Thread { int lo, hi; // fields for communicating inputs int[] arr; int ans = 0; // for communicating result SumThread(int[] a, int l, int h) { lo=l; hi=h; arr=a; } public void run() { // insert your code here class ex2 { private static final int NUM_END = 10000; private static final int NUM_THREAD = 4; // assume NUM_END is divisible by NUM_THREAD public static void main(String[] args) { int[] int_arr = new int [NUM_END]; int i,s; for (i=0;i<NUM_END;i++) int_arr[i]=i+1; s=sum(int_arr); System.out.println("sum=" + s) ; static int sum(int[] arr) {

Exercise 3 : parallel integration Modify your multithreaded JAVA code ex3.java to C code with pthread library

Exercise 4 : Prime numbers Modify your multithreaded JAVA code ex4.java to C code with pthread library class ex4_serial { private static final int NUM_END = 100000; public static void main(String[] args) { int counter=0; int i; long startTime = System.currentTimeMillis(); for (i=0;i<NUM_END;i++) { if (isPrime(i)) counter++; } long endTime = System.currentTimeMillis(); long timeDiff = endTime - startTime; System.out.println("Execution Time : "+timeDiff+"ms"); System.out.println("1..."+(NUM_END-1)+" prime# counter=" + counter +"\n"); private static boolean isPrime(int x) { if (x<=1) return false; for (i=2;i<x;i++) { if ((x%i == 0) && (i!=x)) return false; return true;

Exercise 5 : Garage Parking Simulation Modify multithreaded JAVA code (lecture4-1) to C code with pthread library The actual state of a parking garage is defined by the number of free parking places. Cars are modelled by thread whereby a car can enter or leave the parking garage each of these methods changes the actual state of the garage: When a car enters, the number of free places is decremented; leaving implies incrementing the free places. The number of free places can not be decremented, if the parking garage has become full (free places == 0) A parking garage can simultaneously be used by more than one car (each changing the state), therefore methods enter() and leave() have to be marked as synchronized.

Example Java Code class ParkingGarage { private int places; public ParkingGarage(int places) { if (places < 0) places = 0; this.places = places; } public synchronized void enter() { // enter parking garage while (places == 0) { try { wait(); } catch (InterruptedException e) {} places--; public synchronized void leave() { // leave parking garage places++; notify();

Example : Garage Parking class Car extends Thread { private ParkingGarage parkingGarage; public Car(String name, ParkingGarage p) { super(name); this.parkingGarage = p; start(); } public void run() { while (true) { try { sleep((int)(Math.random() * 10000)); // drive before parking } catch (InterruptedException e) {} parkingGarage.enter(); System.out.println(getName()+": entered"); sleep((int)(Math.random() * 20000)); // stay within the parking garage parkingGarage.leave(); System.out.println(getName()+": left");

Example : Garage Parking public class ParkingGarageOperation { public static void main(String[] args){ ParkingGarage parkingGarage = new ParkingGarage(10); for (int i=1; i<= 40; i++) { Car c = new Car("Car "+i, parkingGarage); }

Example : Garage Parking output $ java ParkingGarageOperation Car 38: entered Car 21: entered Car 12: entered Car 22: entered Car 23: left Car 5: entered Car 32: entered Car 28: entered Car 18: entered Car 5: left Car 37: entered Car 22: left Car 35: entered