Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program Assignment 2: Thread Handling Due by Mar. 19th, 2017 15 credits Objectiv

ID: 3862015 • Letter: P

Question

Program Assignment 2: Thread Handling

Due by Mar. 19th, 2017

15 credits

Objective:

This program assignment is provided to let the students know how to handle threads and enhance system availability on a multiprocessor or multicore environment. A single process is supposed to create multiple threads with runner functions which include individual threads roles. Threads can be created by using Pthreads API, Win32 API, or Java API. on Unix/Linux or Windows platform environment.

Environment: Unix/Linux environment (VM Linux or Triton Server), Windows platform

Language: C, C++, Java

Requirements:

i. You have wide range of choices for this assignment. First, design your program to explain the basic concept of the process management in Unix Kernel. This main idea will be evolved to show your understanding on inter-process communication, file processing, etc.

ii. Refer to the following thread API:

- pthread_create(), pthread_join(), ..

- CreateThread(), CloseHandle(), ResumeThread(), ...

* http://www.relisoft.com/win32/active.html

  

- Thread handling with Java API

iii. The program should create at least two different threads and assign corresponding runner functions with the threads.

iv. The output should contain the screen capture of the execution procedure of the program.

v. Runner function may contain any logic you've designed, for example login procedure by opening session, collection of bank account information, catalog information display, audio/image data handling, etc.

vi. Result should be organized as a document which explains the overview of your program, code, execution results, and the conclusion including justification of your program, lessons you've learned, comments, etc.

Explanation / Answer

Process Management:

Process is one of the fundamental aspect of the operation system. It is a program undergo execution. Programatically process is way of executing a program i.e lines of code. They includes a application installing, signal processing and opening files or closing files. The way of managing process and threads that share the resources in the operating system.  The process management is way to allocate system generated resources to processes that request them in a thread safe manner.

Inter Process Communication

Inter-Process Communication and Inter Thread Communication is a kind of strategy assigned by many operating systems to allow communication between processes or threads. This can be achieved by message passing in linux or windows, the process will request to send a message to another process to the operating system which will send and queue the message to the other process. Inter Thread Communication can be invoked by distinct ways like files, pipes, sockets, message passing, signals, semaphores, shared memory, and memory mapped files. IPC is predominantly used in high end and monolithic kernel environment and is most prominent in micro kernel designs.

Example Program:

//Train.java

public class Train {
  
   public static void main(String[] args) {
  
       Reserve r=new Reserve(1); // Instantiating Reserve object
      
       Thread p1=new Thread(r); // First Thread
       Thread p2=new Thread(r); // Second Thread
      
       p1.setName("Kartheek"); // Setting Name for first Thread
       p2.setName("James"); // Setting Name for Second Thread
      
       p1.start(); // Thread-1 Started-It Calls run method
       p2.start(); // Thread-2 Started-It Calls run method
      
   }

}

// Reserve implements Runnable
class Reserve implements Runnable
{
   int avail=1; // Available berths are only 1
   int berth;
  
   public Reserve(int b) {
       berth=b; // Assigning berth as b
   }

   @Override
   public void run() { // Runner function
       synchronized(this) // Synchronizing the block to allow one thread into critical section
       {
       System.out.println("Available berths are:"+avail);
       if(avail>=berth) // Checking available with requested berths
       {
           System.out.println(avail+" berth allocated for"+" "+Thread.currentThread().getName());
           try {
               Thread.sleep(2000); // Making Thread to sleep for 2000ms
               avail=avail-berth; // Deducting the thread from available
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
       else
       {
           System.out.println("Sorry No Berths");
       }
       }
   }
      
}

Output:

javac Train.java

java Train

Available berths are:1
1 berth allocated for Kartheek
Available berths are:0
Sorry No Berths

Conclusion & Discussion:

We all are very much aware of reservation system in Train, Multiple passengers will try to reserve online for their berth reservation. So, each passenger should have chance to enter into critical section i.e reservation logic, so i synchronized that block inside run method to avoid race condition. Ofcourse we can also use Thread.join() method to make one thread to wait till other thread goes to dead state. As we all know thread states are:

1) NEW

2) RUNNABLE

3) TERMINATION

4) Waiting State

Whenever we call Thread.start() method, thread enters from NEW into Runnable state.

From the above example, Kartheek and James are 2 passengers trying for berth in the train, Since it has only one ticket, only one can allocate the ticket, So i synchronized the run method using synchornized(this) { }.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote