Friday, 19 August 2022

Displaying output using printf() and format() in java

To format and display the output, printf() method is available in PrintStream class. This method works similar to printf() function in C. We know that System.out returns PrintStream class object, so to call the printf() method, we can use: System.out.printf(). It uses different format specifiers similar to C language.

If we want only a string that consist of formatted output, then we can take the help of format() of string class. The format characters supported by System.out.printf() are also usable with format() method.

The following program show how these methods works.
class OutputDemo 
{
	public static void main(String[] args) 
	{
		int id=10;
		String name="Raju";
		char ch ='D';
		System.out.printf("id=%d%nname=%s%ninitial=%c",id,name,ch);
		System.out.println("\n Formatted output");
		String str = String.format("id=%d%nname=%s%ninitial=%c",id,name,ch);
		System.out.println(str);
	}
}


Output:



Thursday, 18 August 2022

Accepting Input from the Keyboard in Java

 Input represents data given to a program. There are so many classes in java for reading input into program. Here I will discuss to most widely used to classes.

A stream is required to accept input from the keyboard. A stream represents flow of data from one place to another place. Basically there are two types of streams in java: input stream and output stream.

All streams are represented by classes in java.io package. Keyboard is represented by a filed 'in' in the System class. When we write System.in, we are representing a standard input device i.e, keyboard, by default. System class is found in java.lang package and has three fields as shown below. All these fields represent some type of stream.

  1. System.in: This represents InputStream object, which  by default represents standard input device like keyboard.
  2. System.out: This represents PrintStream object, which by default represents standard output device like monitor.
  3. System.error: This filed also represents PrintStream object, which by default represents standard output device like monitor. But it used to display error message.

To accept data from the keyboard i.e, System.in, we need to connect it to an input stream as some input stream is need to read data.

Connect the keyboard to an input stream object. Here, we can use InputStreamReader that can read data from the keyboard like

                     InputStreamReader obj = new InputStreamReader (System.in);

In this statement, we are creating InputStreamReader object and connecting the keyboard to it.

In order to read different types of data from stream, we need to connect InputStreamReader to BufferedReader as it has got methods for this purpose.

                   BufferedReader br = new BufferedReader(obj);

Now using different methods of BufferedReader class we read data from stream. The two most widely used method for reading are: 

read(): Used to read single character from stream.

readLine(): Used to read entire line of text from stream.

Reading input with java.util.Scanner class

We can use Scanner class of java.util package to read input from the keyboard or a text file. When the Scanner class receives input, it breaks the input into several pieces, called tokens. These  tokens can be retrieved from the Scanner object using the following methods:

  •  next() - to read a string 
  • nextByte() - to read byte value
  • nextInt() - to read an integer value.
  • nextFloat() - to read float value
  • nextLong() - to read long value
  • nextDouble() - to  read double value.

To read input from keyboard, we can use Scanner class as:

Scanner scr = new Scanner(System.in);

Now, using this 'scr' object and above methods one can read different types of data from keyboard.

Example:

import java.util.Scanner;
class InputDemo 
{
	public static void main(String[] args) 
	{
		Scanner scr = new Scanner(System.in);

		System.out.println("Enter employee Id");
		int id = scr.nextInt();

		System.out.println("Enter employee Name");
		String name=scr.next();

		System.out.println("Enter employee salary");
		float salary = scr.nextFloat();

		System.out.println("Employee Id=" + id);
        System.out.println("Employee Name=" + name);
        System.out.println("Employee salary=" + salary);

	}
}

Output:


It is good idea to use Scanner class instead of all other input class.


Tuesday, 2 August 2022

Process Control & Real time Systems

Describe the concept of process control in Operating systems?

Process Management

Multiprogramming systems explicitly allow multiple processes to exist at any given time, where only one is using the CPU at any given moment, while the remaining processes are performing I/O or are waiting.

The process manager is of the four major parts of the operating system. It implements the process abstraction. It does this by creating a model for the way the process uses CPU and any system resources. Much of the complexity of the operating system stems from the need for multiple processes to share the hardware at the same time. As a conseuence of this goal, the process manager implements CPU sharing ( called scheduling ), process synchronization mechanisms, and a deadlock strategy. In addition, the process manager implements part of the operating system's protection and security.
Process States

During the lifespan of a process, its execution status may be in one of four states: (associated with each state is usually a queue on which the process resides)

  1. Executing: the process is currently running and has control of a CPU
  2. Waiting: the process is currently able to run, but must wait until a CPU becomes available
  3. Blocked: the process is currently waiting on I/O, either for input to arrive or output to be sent
  4. Suspended: the process is currently able to run, but for some reason the OS has not placed the process on the ready queue
  5. Ready: the process is in memory, will execute given CPU time

Process Control Block (PCB)

If the OS supports multiprogramming, then it needs to keep track of all the processes. For each process, its process control block PCB is used to track the process's execution status, including the following:

  • Its current processor register contents.
  • Its processor state (if it is blocked or ready).
  • Its memory state.
  • A pointer to its stack.
  • Which resources have been allocated to it.
  • Which resources it needs 

Real time Systems /RTOS

Real-Time systems span several domains of computer science. They are defense and space systems, networked multimedia systems, embedded automative electronics etc. In a real-time system the correctness of the system behavior depends not only the logical results of the computations, but also on the physical instant at which these results are produced. A real-time system changes its state as a function of physical time, e.g., a chemical reaction continues to change its state even after its controlling computer system has stopped. Based on this a real-time system can be decomposed into a set of subsystems i.e., the controlled object, the real-time computer system and the human operator. A real-time computer system must react to stimuli from the controlled object (or the operator) within time intervals dictated by its environment. The instant at which a result is produced is called a deadline. If the result has utility even after the deadline has passed, the deadline is classified as soft, otherwise it is firm. If a catastrophe could result if a firm deadline is missed, the deadline is hard. Commands and Control systems, Air traffic control systems are examples for hard real-time systems. On-line transaction systems, airline reservation systems are soft real-time systems. 

Classification Of Real-Time Systems

Real-Time systems can be classified [Kopetz97] from different perspectives. The first two classifications, hard real-time versus soft real-time, and fail-safe versus fail-operational, depend on the characteristics of the application, i.e., on factors outside the computer system. The second three classifications, guaranteed-timeliness versus best-effort, resource-adequate versus resource-inadequate, and event-triggered versus time-triggered, depend on the design and implementation, i.e., on factors inside the computer system. However this paper focuses on the differences between hard and soft real-time classification.

Hard Real-Time versus Soft Real-Time

Tabel 1 shows the major differences between hard and soft real-time systems. The response time requirements of hard real-time systems are in the order of milliseconds or less and can result in a catastrophe if not met. In contrast, the response time requirements of soft real-time systems are higher and not very stringent. In a hard real-time system, the peak-load performance must be predictable and should not violate the predefined deadlines. In a soft real-time system, a degraded operation in a rarely occurring peak load can be tolerated. A hard real-time system must remain synchronous with the state of the environment in all cases. On the otherhand soft real-time systems will slow down their response time if the load is very high. Hard real-time systems are often safety critical. Hard real-time systems have small data files and real-time databases. Temporal accuracy is often the concern here. Soft real-time systems for example, on-line reservation systems have larger databases and require long-term integrity of real-time systems. If an error occurs in a soft real-time system, the computation is rolled back to a previously established checkpoint to initiate a recovery action. In hard real-time systems, roll-back/recovery is of limited use.

Source:  https://users.ece.cmu.edu/~koopman/des_s99/real_time/

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top