实验一 经典软件体系结构风格(一)
一、实验目的
(1)理解主程序-子程序软件体系结构、面向对象软件体系结构、管道-过滤器软件体系结构的原理
(2)掌握主程序-子程序软件体系结构、面向对象软件体系结构、管道-过滤器软件体系结构的实例
(3)主程序-子程序软件体系结构、面向对象软件体系结构、管道-过滤器软件体系结构的编程实现
二、实验内容
1.主程序-子程序软件体系结构
运行下面的程序,理解主程序-子程序软件体系结构
#include <stdio.h>
int max(int x,int y) {
int z;
z = x > y ? x : y;
return( z );
}
void main(){
int a,b,c ;
scanf(“%d,%d”,&a,&b);
c=max(a,b);
printf(“The max is %d”, c);
}
输入 1, 2
输出 2
2.数据抽象和面向对象软件体系结构
运行下面的程序,理解数据抽象和面向对象软件体系结构
class Spot{
private int x,y;
Spot(int u, int v){
setX(u);
setY(v);
}
void setX(int x1){
x=x1;
}
void setY(int y1){
y=y1;
}
int getX(){
return x;
}
int getY(){
return y;
}
}
class Trans{
void move(Spot p, int h, int k){
p.setX(p.getX()+h);
p.setY(p.getY()+k);
}
}
class Test{
public static void main(String args[]){
Spot s = new Spot(2,3);
System.out.println("s点的坐标:" + s.getX() + "," + s.getY());
Trans ts = new Trans();
ts.move(s,4,5);
System.out.println("s点的坐标:" + s.getX() + "," + s.getY());
}
}
3.管道-过滤器软件体系结构
(1)Java I/O流中的管道流类PipedInputStream和PipedOutputStream可以方便地实现管道-过滤器体系结构,这两个类的实例对象要通过connect方法连接。
下面程序的功能是sender发送“Hello,receiver! I`m sender”给receiver,然后receiver接受后显示出来并且在前面加上“the following is from sender”的信息。管道流内部在实现时还有大量的对同步数据的处理,管道输出流和管道输入流执行时不能互相阻塞,所以一般要开启独立线程分别执行,顺便复习了多线程操作。
import java.io.*;
import java.util.*;
public class TestPiped{
public static void main(String [] args){
sender s = new sender();
receiver r = new receiver();
PipedOutputStream out = s.getOut();
PipedInputStream in = r.getIn();
try{
in.connect(out);
s.start();
r.start();
}catch(Exception e){
e.printStackTrace();
}
}
}
class sender extends Thread {
PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOut(){
return out;
}
public void run() {
String str = "Hello,receiver ! I`m sender\n";
try {
out.write(str.getBytes());
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
class receiver extends Thread {
PipedInputStream in = new PipedInputStream();
public PipedInputStream getIn() {
return in;
}
public void run(){
byte [] buf = new byte[1024];
try {
int len = in.read(buf);
System.out.println("the following is from sender:\n"+new String(buf,0,len));
in.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
程序的执行结果:
the following is from sender:
Hello,receiver ! I`m sender
(2)编制一段程序,实现Linux进程的管道通信。使用系统调用pipe()建立一条管道线。两个子进程p1和p2分别向通道个写一句话:
//child1 process is sending message!
//child2 process is sending message!
//而父进程则从管道中读出来自两个进程的信息,显示在屏幕上。
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int pid1,pid2;
main( ){
int fd[2];
char outpipe[100],inpipe[100];
pipe(fd); /*创建一个管道*/
while ((pid1=fork( ))==-1);
if(pid1==0){
lockf(fd[1],1,0);
sprintf(outpipe,"child 1 process is sending message!");
/*把串放入数组outpipe中*/
write(fd[1],outpipe,50); /*向管道写长为50字节的串*/
sleep(5); /*自我阻塞5秒*/
lockf(fd[1],0,0);
exit(0);
}else{
while((pid2=fork( ))==-1);
if(pid2==0){
lockf(fd[1],1,0); /*互斥*/
sprintf(outpipe,"child 2 process is sending message!");
write(fd[1],outpipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}else{
wait(0); /*同步*/
read(fd[0],inpipe,50); /*从管道中读长为50字节的串*/
printf("%s\n",inpipe);
wait(0);
read(fd[0],inpipe,50);
printf("%s\n",inpipe);
exit(0);
}
}
}
运行结果:延迟5秒后显示: child1 process is sending message!
再延迟5秒: child2 process is sending message!