Question

In: Computer Science

Consider a device driver that manages the I/O to a specific device. Multiple threads in the...

Consider a device driver that manages the I/O to a specific device. Multiple threads in the system need to read information from the device. The device can only support one I/O command at a time. Previous I/O commands must complete before a new one is issued. Assume the device can be read using a readDevice( what ) method. Write a read( what ) method to be called by any thread in the system that wants to read something from the device. Write a getRequest() method to be used by the device driver to get the read requests. You may assume that all involved threads have already been created previous to your methods.

1. Implement the following methods using any computer language you wish.

Computer language used:

public bytes[] read (Address what) { // request by programs to read from the device

private Address getRequest() { . // called by device drive to get next request

Solutions

Expert Solution

import java.util.Scanner;
import java.io.IOException;

import java.io.InputStreamReader;
import java.io.DataInputStream;
import java.io.FileInputStream;


import java.util.StringTokenizer;

public class DeviceReader
{
   public static void main(String[] args) throws IOException
{
Reader 1s=new Reader();
int n1 = 1s.nextInt();
int k1 = 1s.nextInt();
int cnt=0;
while (n-- > 0)
{
int dd = 1s.nextInt();
if (dd%k == 0)
cnt++;
}
System.out.println(cnt);

static class I_Reader
{
final private int BUFFER_SIZE = 1 << 25;
private DataInputStream dinput;
private byte[] store;
private int bufferPointer, bytesRead;

public I_Reader()
{
dinput = new DataInputStream(System.in);
store = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public I_Reader(String file_name) throws IOException
{
dinput = new DataInputStream(new                        FileInputStream(file_name));
store = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, dc;
while ((dc = read()) != -1)
{
if (dc == '\n')
break;
buf[cnt++] = (byte) dc;
}
return new String(buf, 0, cnt);
}

public int nextInt() throws IOException
{
int ret = 0;
byte dc = read();
while (dc <= ' ')
dc = read();
boolean neg = (dc == '-');
if (neg)
dc = read();
do
{
ret = ret * 10 + dc - '0';
} while ((dc = read()) >= '0' && dc <= '9');

if (neg)
return -ret;
return ret;
}

public long nextLong() throws IOException
{
long ret = 0;
byte dc = read();
while (dc <= ' ')
dc = read();
boolean neg = (dc == '-');
if (neg)
dc = read();
do {
ret = ret * 10 + dc - '0';
}
while ((dc = read()) >= '0' && dc <= '9');
if (neg)
return -ret;
return ret;
}

public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte dc = read();
while (dc <= ' ')
dc = read();
boolean neg = (dc == '-');
if (neg)
dc = read();

do {
ret = ret * 10 + dc - '0';
}
while ((dc = read()) >= '0' && dc <= '9');

if (dc == '.')
{
while ((dc = read()) >= '0' && dc <= '9')
{
ret += (dc - '0') / (div *= 10);
}
}

if (neg)
return -ret;
return ret;
}

private void fillBuffer() throws IOException
{
bytesRead = din.read(store, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
store[0] = -1;
}

private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return store[bufferPointer++];
}

public void close() throws IOException
{
if (dinput == null)
return;
dinput.close();
}
}
   
}
}


Related Solutions

Problem 2. Interrupt-driven I/O (20 pt.) Consider a system employing interrupt-driven I/O for a particular device...
Problem 2. Interrupt-driven I/O (20 pt.) Consider a system employing interrupt-driven I/O for a particular device that transfers data at an average of 2000 bytes/sec on a continuous basis. a. Assume that interrupt processing takes about 200 usec (i.e., the time to jump to the interrupt service routine (ISR), execute it, and return to the main program). Determine what fraction of processor time is consumed by this I/O device if it interrupts for every byte. b. Now assume that the...
1 ) Which of the followings is not a goal of the I/O software?    A. Device...
1 ) Which of the followings is not a goal of the I/O software?    A. Device independence B. Uniform naming C. Buffering D. Error handling E. Cost reduction 2) According to the data rate, which of the following is not true? A. SONET OC-768 network is faster than Gigabit Ethernet B. FireWire 800 is slower than USB 3.0 C. SCSI Ultra 5 bus is faster than Thunderbolt 2 bus D. Keyboard is slower than mouse 3) Which of the following...
How does an interrupt capable I/O device improve systemefficiency?
How does an interrupt capable I/O device improve system efficiency?
A server is a computer or device on a network that manages network resources. Servers are...
A server is a computer or device on a network that manages network resources. Servers are often dedicated, meaning that they perform no other tasks besides their server tasks. On multiprocessing operating systems however, a server may be one of several programs that are each managing specific requests for services from clients or a particular hardware or software resource, rather than the entire computer. For each of the server types below, provide a short description of the service each provides...
Why is having the correct device driver so important?
Why is having the correct device driver so important?       It enables a specific operating system to run as multiple machines.       It enables multiple computers to share devices.       It enables a device to work in both a powered and unpowered state.       It enables a specific operating system to control a specific piece of hardware.
Identify and describe the (5) areas of I/O psychology. Please be specific and detailed in your...
Identify and describe the (5) areas of I/O psychology. Please be specific and detailed in your description.
The project will study the coordination of multiple threads using semaphores. The design should consist of...
The project will study the coordination of multiple threads using semaphores. The design should consist of two things: (1) a list of every semaphore, its purpose, and its initial value, and (2) pseudocode for each function. Code Your code should be nicely formatted with plenty of comments. The code should be easy to read, properly indented, employ good naming standards, good structure, and should correctly implement the design. Your code should match your pseudocode. Project Language/Platform This project must target...
Consider the following binds Ca-O, C-O, K-O, O-O and N-O a Which bonds are polar covalent...
Consider the following binds Ca-O, C-O, K-O, O-O and N-O a Which bonds are polar covalent ? b Wich bonds are nonpolar Covalent c Which bonds are ionic ? d Arrange the covalent bonds in order of decreasing polarity
Implement a Linux Block Driver which simulates a block device with a region of memory init,...
Implement a Linux Block Driver which simulates a block device with a region of memory init, exit, and IO functionalities must be included. Please use either C or C++ Programming for this Thank You!
In order to achieve faster access to data, many I/O operations involve specific data structure class...
In order to achieve faster access to data, many I/O operations involve specific data structure class called "Buffer". Our goal in this question is to design a UML class diagram for a character buffer (an array of chars ) based on the following description. This particular buffer: has the name charBuffer has a definite size has an indicator whether it is empty or not has an indicator whether it is full or not has write to buffer operation has read...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT