计算机操作系统

更新时间:2023-03-08 17:51:30 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

计算机操作系统

实验名称:熟悉Linux操作系统(进程观测)

一 实验目的

(1)了解在Linux操作系统中进程的特点和表现形式 (2)掌握Linux查看进程的方式与方法 (3)在一个进程中创建另一个进程的方法

(4)掌握父进程和子进程的关系和fork的用法 二 实验内容 二、实验内容

(1)编写一个简单的程序,使用ps或top工具观察该进程的的ID号,并使用kill工具终止进程运行。

(2)编写一个程序,使用fork函数生成一个子进程,并使用相关工具观察进程状态。 三、实验步骤 (1)

#include int main() { //设计一个循环,使其反复运行,方便观察 while(1) { printf(\ } return 0; }

文件名命名为process1.c,使用gcc process1.c -o process编译该程序。运行该程序,打开其它一个终端窗口,输入命令top,观察名称为process1的进程,记录各项数据(包括进程号)。使用\进程号\直接杀死该进程。观察进程是否消失?

需要记录的数据:进程状态中的id,内存使用和CPU占有率。由于该进程一直处于循环中,思考id、内存使用和cpu占有率哪一个因素和循环

关系密切?如何避免,请给出合理的建议。这是否说明CPU也是操作系统中的一个重要资源? (2)

#include #include void main() {

int i;

if ( fork() == 0 ) {

/* 子进程程序 */

for ( i = 1; i <1000; i ++ )

printf(\ } else {

/* 父进程程序*/

for ( i = 1; i <1000; i ++ )

printf(\ } }

请保存为process2.c,编译运行,写出你观察到的输出结果,能否对输出的现象做一个合理的解释?

若将上述实例(2),改写为: #include #include void main() {

int i;

if ( fork() == 0 ) {

/* 子进程程序 */ for ( ;; )

printf(\ } else {

/* 父进程程序*/

for ( i = 1; i <1000; i ++ )

printf(\ } }

请保存为process2.c,编译运行,写出你观察到的输出结果,能否对输出的现象做一个合理的解释?

若将将实例(2)改为: #include #include void main() {

int i;

if ( fork() == 0 ) {

/* 子进程程序 */ for ( ;; )

printf(\ } else {

/* 父进程程序*/ for (;;)

printf(\ } }

请保存为process3.c,编译运行,写出你观察到的输出结果,能否对输出的现象做一个合理的解释?

三 实验步骤与调试 实验步骤:

1.文件名命名为process1.c,使用gcc process1.c -o process编译该程序。

2.运行该程序,打开其它一个终端窗口,输入命令top,观察名称为process1的进程,记录各项数据(包括进程号)。

3.使用\进程号\直接杀死该进程。

4.新建一个process2.c使用fork()生成子进程。 5.运行process2.c观察其输出结果。

四 实验结果 实验结果:

1.process1.c:程序无限次输出 I am the first process!直到输入“kill 进程号”结束 2.process2.c:父进程、子进程各输出1000,且输出次序不一

3.父进程子进程打印顺序不确定,可见父进程和子进程各独立执行。父进程子进程均无限次输出,知道输入“kill -9 进程号”终止.

五 疑难小节 疑难:

1.最后一个程序peocess3.c,运行后,kill 进程号无法结束进程,只有kill 进程号-9后才能终止进程

2.父子进程打印次序无规律,说明父子进程的进行是无序的随机的。

六 主要算法 1.process.c

#include int main() {

//设计一个循环,使其反复运行,方便观察 while(1) {

printf(\ }

return 0; }

2.process.c

#include #include #include int main() {

int i;

if ( fork() == 0 ) {

/* 子进程程序 */

for ( i = 1; i <1000; i ++ )

printf(\ } else {

/* 父进程程序*/

for ( i = 1; i <1000; i ++ )

printf(\ } }

实验名称:进程的控制

一 目的

通过进程的创建、撤消和运行加深对进程概念和进程并发执行的理解,明确进程与程序之间的区别。

二、实验内容及步骤

(1) 了解系统调用fork()、execvp()和wait()的功能和实现过程。

(2) 编写一段程序,使用系统调用fork()来创建两个子进程,并由父进程重复显示字符串“parent:”和自己的标识数,而子进程则重复显示字符串“child:”和自己的标识数。

(3) 编写一段程序,使用系统调用fork()来创建一个子进程。子进程通过系统调用execvp()更换自己的执行代码,新的代码显示“new program.”后结束进程。而父进程则调用wait()等待子进程结束,并在子进程结束后显示子进程的标识符,然后正常结束。 三、思考

(1) 系统调用fork()是如何创建进程的?

(2) 当首次将CPU 调度给子进程时,其入口在哪里? (3) 系统调用execvp()是如何更换进程的可执行代码的?

(4) 对一个应用,如果用多个进程的并发执行来实现,与单个进程来实现有什么不同?

三 调试

1.打开实验需要的平台,用mkdir xz 创建一个空的文件夹,在xz里面用vim编辑器 创建一个new.c的文件,

2.在new.c文件中,写入一个用whil循环控制的语句,退出vim编辑器,保存。 3.用gcc编译源文件(gcc new.c -o new),然后运行(./new)。

4.再打开两个终端,用top命名查看进程,一个用kill杀死进程,防止运行的程序 无限循环下去

5.进程无限循环。 四

如下,可以查看附件文档 实验一:

[root@localhost xz]# gcc test.c -o test [root@localhost xz]# ./test This xingzheng program!

实验二:

Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!! Child Programmer!!!!

五 疑难:

1.通过本次实验了解了系统调用fork()函数和创建进程的具体过程 2.系统通过调用execvp()函数,调用进程 六 算法: 程序一:

#include #include #include int main() {

package org.tony.hdfs;

import java.io.IOException; import java.net.URI;

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path;

import org.apache.hadoop.hdfs.DistributedFileSystem; import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

public class HDFSTest { public static void main(String[] args) throws Exception { //uploadLocalFile2HDFS(\ //createNewHDFSFile(\ //String str = new String(readHDFSFile(\ //System.out.println(str); //mkdir(\ //deleteDir(\ //listAll(\

//getDateNodeHost(); //testRename(); testUpload(); } //???HDFS????????н???????? public static void getDateNodeHost() throws IOException{ Configuration conf = getConf();

FileSystem fs=FileSystem.get(conf);

DistributedFileSystem hdfs = (DistributedFileSystem)fs; DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); for(int i=0;i

System.out.println(\ } }

/* * upload the local file to the hds * ·?????·?? */ /*public static void uploadLocalFile2HDFS(String s, String d) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); Path src = new Path(s); Path dst = new Path(d); hdfs.copyFromLocalFile(src, dst); hdfs.close(); }*/ /* * create a new file in the hdfs. * notice that the toCreateFilePath is the full path * and write the content to the hdfs file. */ public static void createNewHDFSFile(String toCreateFilePath, String content) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); FSDataOutputStream os = hdfs.create(new Path(toCreateFilePath)); os.write(content.getBytes(\ os.close(); hdfs.close(); } /* * delete the hdfs file * notice that the dst is the full path name */ public static boolean deleteHDFSFile(String dst) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); Path path = new Path(dst); boolean isDeleted = hdfs.delete(path); hdfs.close(); return isDeleted;

} /*

* read the hdfs file content

* notice that the dst is the full path name */

public static byte[] readHDFSFile(String dst) throws Exception { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); // check if the file exists Path path = new Path(dst); if ( fs.exists(path) ) { FSDataInputStream is = fs.open(path); // get the file info to create the buffer FileStatus stat = fs.getFileStatus(path); // create the buffer byte[] buffer = new byte[Integer.parseInt(String.valueOf(stat.getLen()))]; is.readFully(0, buffer); is.close(); fs.close(); return buffer; } else { throw new Exception(\ } } /*

* make a new dir in the hdfs * the dir may like '/tmp/testdir' */

public static void mkdir(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); fs.mkdirs(new Path(dir));

fs.close(); } /*

* delete a dir in the hdfs * dir may like '/tmp/testdir' */

public static void deleteDir(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); fs.delete(new Path(dir)); fs.close(); }

//?????????? hdfs?????????

private static Configuration getConf(){ Configuration conf = new Configuration(); // ??仰??????Щ???????hadoop????????е???? conf.set(\ conf.set(\ return conf; } /**

* @Title: listAll

* @Description: ?г???????????? * @return void ???????? * @throws */

public static void listAll(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); FileStatus[] stats = fs.listStatus(new Path(dir)); for(int i = 0; i < stats.length; ++i) { if (!stats[i].isDir()) { // regular file System.out.println(stats[i].getPath().toString()); } else {

// dir System.out.println(stats[i].getPath().toString()); } // else if(stats[i].()) // { // // is s symlink in linux // System.out.println(stats[i].getPath().toString()); // } } fs.close(); } public static void testRename() throws Exception{ Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf);

Path dst = new Path(\

Path frpath = new Path(\

Path topath = new Path(\

fs.rename(frpath, topath);

FileStatus files[] = fs.listStatus(dst);

for(FileStatus file : files){

System.out.println(file.getPath());

} } public static void testUpload() throws Exception{

Configuration conf = getConf(); FileSystem fs = FileSystem.get(URI.create(\

Path src = new Path(\

Path dst = new Path(\

fs.copyFromLocalFile(src, dst);

System.out.println(\

FileStatus files[] = fs.listStatus(dst);

for(FileStatus file : files){

System.out.println(file.getPath());

} } }

实验名称:HBase环境搭建、sehll操作及Java API编程

一 目的

1.掌握Hbase在Hadoop集群体系结构中发挥的作用和使过程。 2.掌握安装和配置HBase基本方法。 3.掌握HBase shell的常用命令。

4.使用HBase shell命令进行表的创建,增加删除修改操作。 5.使用Java API进行表的创建,增加删除修改操作。 二 内容

1.完成Zookeeper和HBase的搭建。

2.使用HBase shell命令进行表的创建,增加删除修改操作。 3.使用Java API进行表的创建,增加删除修改操作。

三 步骤 步骤:

1.完成Zookeeper和HBase的搭建。

2.使用HBase shell命令进行表的创建,增加删除修改操作。 3.使用Java API进行表的创建,增加删除修改操作。 基本调试:

1.掌握Hbase在Hadoop集群体系结构中发挥的作用和使过程。 2.掌握安装和配置HBase基本方法。

3.掌握HBase shell的常用命令。

4.使用HBase shell命令进行表的创建,增加删除修改操作。 5.使用Java API进行表的创建,增加删除修改操作。

四 结果

结果成功,详见上传文档截图 五 疑难

对于HBase的命令练习还不够,经常有点提笔忘字的感觉。 还有就是java api的使用,感觉很乱

六 算法

package com.wujintao.hbase.test;

import java.io.IOException; import java.util.ArrayList; import java.util.List;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue;

import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.HTablePool; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList;

import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.util.Bytes;

public class JinTaoTest {

public static Configuration configuration; static {

configuration = HBaseConfiguration.create();

configuration.set(\ configuration.set(\ configuration.set(\ }

public static void main(String[] args) { // createTable(\ // insertData(\ // QueryAll(\

// QueryByCondition1(\ // QueryByCondition2(\ //QueryByCondition3(\ //deleteRow(\

deleteByCondition(\ }

/**

* 创建表

* @param tableName */

public static void createTable(String tableName) { System.out.println(\ try {

HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);

if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建

hBaseAdmin.disableTable(tableName); hBaseAdmin.deleteTable(tableName);

System.out.println(tableName + \ }

HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); tableDescriptor.addFamily(new HColumnDescriptor(\ tableDescriptor.addFamily(new HColumnDescriptor(\ tableDescriptor.addFamily(new HColumnDescriptor(\ hBaseAdmin.createTable(tableDescriptor); } catch (MasterNotRunningException e) { e.printStackTrace();

} catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) {

e.printStackTrace(); }

System.out.println(\ }

/**

* 插入数据

* @param tableName */

public static void insertData(String tableName) { System.out.println(\

HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName);

Put put = new Put(\一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值

put.add(\本行数据的第一列 put.add(\本行数据的第三列 put.add(\本行数据的第三列 try {

table.put(put); } catch (IOException e) { e.printStackTrace(); }

System.out.println(\ }

/**

* 删除一张表

* @param tableName */

public static void dropTable(String tableName) { try {

HBaseAdmin admin = new HBaseAdmin(configuration); admin.disableTable(tableName); admin.deleteTable(tableName); } catch (MasterNotRunningException e) { e.printStackTrace();

} catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }

} /**

* 根据 rowkey删除一条记录 * @param tablename * @param rowkey */

public static void deleteRow(String tablename, String rowkey) { try {

HTable table = new HTable(configuration, tablename); List list = new ArrayList();

Delete d1 = new Delete(rowkey.getBytes()); list.add(d1);

table.delete(list);

System.out.println(\删除行成功!\

} catch (IOException e) { e.printStackTrace(); }

}

/**

* 组合条件删除 * @param tablename * @param rowkey */

public static void deleteByCondition(String tablename, String rowkey) {

//目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作

}

/**

* 查询所有数据 * @param tableName */

public static void QueryAll(String tableName) {

HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); try {

ResultScanner rs = table.getScanner(new Scan());

for (Result r : rs) {

System.out.println(\获得到rowkey:\ for (KeyValue keyValue : r.raw()) {

System.out.println(\列:\ + \值:\ } }

} catch (IOException e) { e.printStackTrace(); } }

/**

* 单条件查询,根据rowkey查询唯一一条记录 * @param tableName */

public static void QueryByCondition1(String tableName) {

HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); try {

Get scan = new Get(\根据rowkey查询 Result r = table.get(scan);

System.out.println(\获得到rowkey:\ for (KeyValue keyValue : r.raw()) {

System.out.println(\列:\ + \值:\ }

} catch (IOException e) { e.printStackTrace(); } }

/**

* 单条件按查询,查询多条记录 * @param tableName */

public static void QueryByCondition2(String tableName) {

try {

HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); Filter filter = new SingleColumnValueFilter(Bytes

.toBytes(\

.toBytes(\当列column1的值为aaa时进行查询 Scan s = new Scan(); s.setFilter(filter);

ResultScanner rs = table.getScanner(s); for (Result r : rs) {

System.out.println(\获得到rowkey:\ for (KeyValue keyValue : r.raw()) {

System.out.println(\列:\ + \值:\ } }

} catch (Exception e) { e.printStackTrace(); }

}

/**

* 组合条件查询 * @param tableName */

public static void QueryByCondition3(String tableName) {

try {

HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName);

List filters = new ArrayList();

Filter filter1 = new SingleColumnValueFilter(Bytes

.toBytes(\ .toBytes(\ filters.add(filter1);

Filter filter2 = new SingleColumnValueFilter(Bytes

.toBytes(\ .toBytes(\ filters.add(filter2);

Filter filter3 = new SingleColumnValueFilter(Bytes

.toBytes(\ .toBytes(\ filters.add(filter3);

FilterList filterList1 = new FilterList(filters);

Scan scan = new Scan(); scan.setFilter(filterList1);

ResultScanner rs = table.getScanner(scan); for (Result r : rs) {

System.out.println(\获得到rowkey:\ for (KeyValue keyValue : r.raw()) {

System.out.println(\列:\ + \值:\ } }

rs.close();

} catch (Exception e) { e.printStackTrace(); }

} }

实验名称:大数据综合案例

一 目的

1.掌握Hadoop大数据基本框架。 2.掌握MR核心编程。

3.掌握Hadoop生态组件使用。

二 内容

使用Hadoop框架完成日志的数据清理,分析。

三 步骤

1、先在本地创建一个gd.txt文件,然后把数据导入进去;

2、在hdfs上创建一个demo文件夹,然后在demo文件夹中创建t1文件夹(t1文件夹也可以不创建,可以只有demo一个文件夹),然后把ubuntu中的gd.txt文件导入到hdfs中的demo文件夹中的t1文件夹中,然后使用命令查看是否导入进去

3、进入hive中,创建表t1(ip、年、月、日、网址),然后使用命令查看表t1中的数据; (1)分别统计30,31号总流

select count(*) from t1 where day=30 select count(*) from t1 where day=31;

(2)分别统计30,31号所有IP数(去重) select distinct ip from t1 where day=30;

select distinct ip from t1 where day=31

(3)统计30,31号IP访问数为1的。

create table t2(ip String,ipcount int) row format delimited fields terminated by '\\t';

insert overwrite table t2 select ip,count(ip) ipcount from t1 where day=30 group by ip;

select * from t2;

select ip,ipcount from t2 where ipcount=1;

create table t3(ip String,ipcount int) row format delimited fields terminated by '\\t'; OK

Time taken: 0.247 seconds

insert overwrite table t3 select ip,count(ip) ipcount from t1 where day=31 group by ip; hive> select * from t3; OK

211.97.15.179 4 27.19.74.143 7 8.35.201.161 1 8.35.201.163 1 8.35.201.164 2 8.35.201.165 4

Time taken: 0.089 seconds, Fetched: 6 row(s) hive> select ip,ipcount from t3 where ipcount=1; OK

8.35.201.161 1 8.35.201.163 1

Time taken: 0.101 seconds, Fetched: 2 row(s)

(4)统计30,31号IP访问最高的。

select ip,ipcount from t2 order by ipcount desc limit 1; select ip,ipcount from t3 order by

四 结果

1.成功搭建Hadoop生态环境,用于海量数据分析。 2.对某日志的hive数据清洗分析。

五 疑难

1.在最开始的不太懂MR的原理。 2.搭建hive的时候出现各种的错误。

六:算法

(1)分别统计30,31号总流

select count(*) from t1 where day=30 select count(*) from t1 where day=31;

(2)分别统计30,31号所有IP数(去重) select distinct ip from t1 where day=30; select distinct ip from t1 where day=31 (3)统计30,31号IP访问数为1的。

create table t2(ip String,ipcount int) row format delimited fields terminated by '\\t';

insert overwrite table t2 select ip,count(ip) ipcount from t1 where day=30 group by ip;

select * from t2;

select ip,ipcount from t2 where ipcount=1;

create table t3(ip String,ipcount int) row format delimited fields terminated by '\\t'; OK

Time taken: 0.247 seconds

insert overwrite table t3 select ip,count(ip) ipcount from t1 where day=31 group by ip; hive> select * from t3; OK

211.97.15.179 4 27.19.74.143 7 8.35.201.161 1 8.35.201.163 1 8.35.201.164 2 8.35.201.165 4

Time taken: 0.089 seconds, Fetched: 6 row(s) hive> select ip,ipcount from t3 where ipcount=1; OK

8.35.201.161 1 8.35.201.163 1

Time taken: 0.101 seconds, Fetched: 2 row(s) (4)统计30,31号IP访问最高的。

select ip,ipcount from t2 order by ipcount desc limit 1; select ip,ipcount from t3 order by

非关系数据库

实验名称:HBase的安装与配置

一 目的

1. 掌握HBase完全分布式的安装方法; 2. 验证HBase完全分布式的安装;

3. 打开Web UI管理界面验证HBase的安装; 4. 打开HBase Shell验证测试安装环境。

二 内容

本文来源:https://www.bwwdw.com/article/ovh6.html

Top