操作系统概念第七版答案(含编程代码)(8)

2019-01-07 14:17

Operating system concepts(Seventh edition) 2008.3

(3) Posix program that outputs prime numbers #include #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 #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;


操作系统概念第七版答案(含编程代码)(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:信息碎片化降低人们认知水平

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: