In: Computer Science
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
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();
}
}
}
}