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

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

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

Operating system concepts(Seventh edition) 2008.3

public class FortuneServer {

private static final String[] fortunes = { \ \

\

\ \

\ };

public static void main(String[] args) throws IOException { Socket client = null; ServerSocket sock = null;

try {

sock = new ServerSocket(6013); // now listen for connections while (true) {

client = sock.accept();

System.out.println(\ System.out.println(\

// we have a connection

PrintWriter pout = new PrintWriter(client.getOutputStream(), true);

// write the Date to the socket

pout.println(fortunes[(int)(java.lang.Math.random() * fortunes.length)] );

pout.close(); client.close(); } }

catch (IOException ioe) {

System.err.println(ioe); }

finally {

if (sock != null) sock.close(); if (client != null) client.close(); } } }

Operating system concepts(Seventh edition) 2008.3

3.9 An echo server is a server that echoes back whatever it receives from a client. For example, if a client sends the server the string Hello there! the server will respond with the exact data it received from the client — that is, Hello there!

Write an echo server using the Java networking API described in Section 3.6.1. This server will wait for a client connection using the accept() method. When a client connection is received, the server will loop, performing the following steps:

? Read data from the socket into a buffer.

? Write the contents of the buffer back to the client.

The server will break out of the loop only when it has determined that the client has closed the connection.

The date server shown in Figure 3.19 uses the java.io.BufferedReader class. BufferedReader extends the java.io.Reader class, which is used for reading character streams. However, the echo server cannot guarantee that it will read characters from clients; it may receive binary data as well. The class java.io.InputStream deals with data at the byte level rather than the character level. Thus, this echo server must use an object that extends java.io.InputStream. The read() method in the java.io.InputStream class returns -1 when the client has closed its end of the socket connection.

Answer:

// An echo server listening on port 6007.

// This server reads from the client and echoes back the result.

// When the client enters the character '.' – the server closes the connection. // This conforms to RFC 862 for echo servers.

import java.net.*; import java.io.*;

public class EchoServer {

public static final int DEFAULT_PORT = 6007; public static final int BUFFER_SIZE = 256;

public static void main(String[] args) throws IOException { ServerSocket sock = null;

byte[] buffer = new byte[BUFFER_SIZE]; InputStream fromClient = null; OutputStream toClient = null;

try {

Operating system concepts(Seventh edition) 2008.3

// establish the socket

sock = new ServerSocket(DEFAULT_PORT);

while (true) { /**

* now listen for connections */

Socket client = sock.accept();

/**

* get the input and output streams associated with the socket. */

fromClient = new BufferedInputStream(client.getInputStream()); toClient = new BufferedOutputStream(client.getOutputStream()); int numBytes;

/** continually loop until the client closes the connection */

while ( (numBytes = fromClient.read(buffer)) != -1) { toClient.write(buffer,0,numBytes); toClient.flush(); }

fromClient.close(); toClient.close(); client.close(); } }

catch (IOException ioe) { } finally {

if (sock != null) sock.close(); } } }

3.10 In Exercise 3.6, the child process must output the Fibonacci sequence, since the parent and child have their own copies of the data. Another approach to designing this program is to establish a shared-memory segment between the parent and child processes. This technique allows the child to write the contents of the Fibonacci sequence to the shared- memory segment and has the parent output the sequence when the child completes. Because the memory is shared, any changes the child makes to

Operating system concepts(Seventh edition) 2008.3

the shared memory will be reflected in the parent process as well.

This program will be structured using POSIX shared memory as described in Section 3.5.1. The program first requires creating the data structure for the shared-memory segment. This is most easily accom- plished using a struct. This data structure will contain two items: (1) a fixed-sized array of size MAX SEQUENCE that will hold the Fibonacci values; and (2) the size of the sequence the child process is to generate-sequence_size where sequence_size ≤ MAX SEQUENCE. These items can be represented in a struct as follows: #define MAX SEQUENCE 10 typedef struct {

long fib sequence[MAX SEQUENCE]; int sequence size; } shared data;

The parent process will progress through the following steps:

a. Accept the parameter passed on the command line and perform error checking to ensure that the parameter is ≤ MAX SEQUENCE.

b. Create a shared-memory segment of size shared data. c. Attach the shared-memory segment to its address space.

d. Set the value of sequence size to the parameter on the command line.

e. Fork the child process and invoke the wait() system call to wait for the child to finish.

f. Output the value of the Fibonacci sequence in the shared-memory segment. g. Detach and remove the shared-memory segment.

Because the child process is a copy of the parent, the shared-memory region will be attached to the child’s address space as well. The child process will then write the Fibonacci sequence to shared memory and finally will detach the segment. One issue of concern with cooperating processes involves synchronization issues. In this exercise, the parent and child processes must be synchronized so that the parent does not output the Fibonacci sequence until the child finishes generating the sequence. These two processes will be synchronized using the wait() system call; the parent process will invoke wait(), which will cause it to be suspended until the child process exits.

Answer: /* Example shared memory program. */

#include #include #include #include #include

#define PERMS (S_IRUSR | S_IWUSR) #define MAX_SEQUENCE 10

Operating system concepts(Seventh edition) 2008.3

typedef struct {

long fib_sequence[MAX_SEQUENCE]; int sequence_size; } shared_data;

int main(int argc, char *argv[]) {

int i, seq_size;

/* the process identifier */ pid_t pid;

/* the id of the shared memory segment */ int segment_id;

/* a pointer to the shared memory segment */ shared_data* shared_memory;

/* do some error checking to ensure the parameter was passed */ if (argc != 2) {

fprintf(stderr,\ return -1; }

seq_size = atoi(argv[1]);

if (seq_size > MAX_SEQUENCE) {

fprintf(stderr,\ return -1; }

/* allocate a shared memory segment */

if ( (segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), PERMS)) == -1) { fprintf(stderr,\ return 1; }

printf(\

/* now attach the shared memory segment at the specified address */ if ( (shared_memory = (shared_data *) shmat(segment_id, 0, 0)) == (shared_data *)-1) {

fprintf(stderr,\ return 0; }

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

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