77范文网 - 专业文章范例文档资料分享平台

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

来源:网络收集 时间:2019-01-07 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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;

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库操作系统概念第七版答案(含编程代码)(8)在线全文阅读。

操作系统概念第七版答案(含编程代码)(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/409896.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: