Operating system concepts(Seventh edition) 2008.3
(3) Posix program that outputs prime numbers #include
/** we will only allow up to 256 prime numbers */ #define MAX_SIZE 256 int primes[MAX_SIZE];
void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) {
int i;
pthread_t tid; /* the thread identifier */
pthread_attr_t attr; /* set of attributes for the thread */
if (argc != 2) {
fprintf(stderr,\ return -1;
}
if (atoi(argv[1]) < 2) {
fprintf(stderr,\ return -1;
}
/* get the default attributes */ pthread_attr_init(&attr); /* create the thread */
pthread_create(&tid,&attr,runner,argv[1]); /* now wait for the thread to exit */ pthread_join(tid,NULL);
/** now output the prime numbers */ for (i = 1; i <= atoi(argv[1]); i++) if (primes[i] > 0) printf(\}
/* Generate primes using the Sieve of Eratosthenes. */ void *runner(void *param) {
int i, j;
int upper = atoi(param); primes[1] = 0;
for (i = 2; i <= upper; i++)
Operating system concepts(Seventh edition) 2008.3
primes[i] = 1;
for (i = 2; i <= upper/2; i++) for (j = 2; j <= upper/i; j++) primes[i*j] = 0; pthread_exit(0); }
4.10 Modify the socket-based date server (Figure 3.19) in Chapter 3 so that the server services each client request in a separate thread. Answer:
/*Server.java
This is the date server where each client is serviced in a separate thread. The server listens on port 6013. */ import java.net.*; import java.io.*;
public class Connection implements Runnable {
private Socket outputLine;
public Connection(Socket s) { outputLine = s; }
public void run() {
// getOutputStream returns an OutputStream object // allowing ordinary file IO over the socket. try {
// create a new PrintWriter with automatic flushing
PrintWriter pout=new PrintWriter(outputLine.getOutputStream(),true); // now send the current date to the client pout.println(new java.util.Date() ); // now close the socket outputLine.close(); }
catch (java.io.IOException e) { System.out.println(e); } } }
/*Connection.java
This is the separate thread that services each request. This reads a random fortune from the list of fortunes. */
Operating system concepts(Seventh edition) 2008.3
import java.net.*; import java.io.*;
public class Connection implements Runnable {
private Socket outputLine;
public Connection(Socket s) { outputLine = s; }
public void run() {
// getOutputStream returns an OutputStream object // allowing ordinary file IO over the socket. try {
// create a new PrintWriter with automatic flushing
PrintWriter pout = new PrintWriter(outputLine.getOutputStream(), true); // now send the current date to the client pout.println(new java.util.Date() ); // now close the socket outputLine.close(); }
catch (java.io.IOException e) { System.out.println(e); } } }
4.11 The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally,it can be expressed as:
fib0 = 0 fib1 = 1
fibn = fibn?1 + fibn?2 Write a multithreaded program that generates the Fibonacci series using either the Java, Pthreads, or Win32 thread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in data that is shared by the threads (an array is probably the most convenient data structure).When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread to finish using the techniques described in Section 4.3.
Operating system concepts(Seventh edition) 2008.3
Answer:
(1) Java program
// Generates the Fibonacci sequence in a separate thread. class FibThread implements Runnable {
private int[] fibNums;
public FibThread(int[] fibNums) { this.fibNums = fibNums; }
public void run() {
int length = fibNums.length; if (length == 0) return;
else if (length == 1) fibNums[0] = 0; else if (length == 2) { fibNums[0] = 0; fibNums[1] = 1; }
else { // length > 2 fibNums[0] = 0; fibNums[1] = 1;
for (int i = 2; i < length; i++)
fibNums[i] = fibNums[i-1] + fibNums[i-2]; } } }
public class Fib {
public static void main(String args[]) { if (args.length == 0) {
System.out.println(\ System.exit(0); }
else if (Integer.parseInt(args[0]) < 0) {
System.out.println(\ System.exit(0); }
else {
int[] sequence = new int[Integer.parseInt(args[0])]; Thread worker = new Thread(new FibThread(sequence)); worker.start();
Operating system concepts(Seventh edition) 2008.3
try {
worker.join();
} catch (InterruptedException ie) { } for (int i = 0; i < sequence.length; i++) System.out.println(sequence[i]); } } }
(2)Windows program #include
/** we will only allow up to 256 Fibonacci numbers */ #define MAX_SIZE 256
int fibs[MAX_SIZE];
/* the thread runs in this separate function */ DWORD WINAPI Summation(PVOID Param) {
DWORD upper = *(DWORD *)Param; int i;
if (upper== 0) return 0;
else if (upper == 1) fibs[0] = 0;
else if (upper== 2) { fibs[0] = 0; fibs[1] = 1; }
else { // sequence > 2 fibs[0] = 0; fibs[1] = 1;
for (i = 2; i < upper; i++)
fibs[i] = fibs[i-1] + fibs[i-2]; }
return 0; }
int main(int argc, char *argv[]) {
DWORD ThreadId;
HANDLE ThreadHandle;