操作系统自用3 进程管理和文件管理

OS Programme Lecture #3

DOS系统,DOS、Linux进程管理和文件系统

1. DOS commands

使用DOS terminal(cmd)

参考网址:https://home.csulb.edu/~murdock/dosindex.html

(1)dir

>dir /s (Ctrl+C停止执行)

>dir /p

>dir /? 查看dir的命令选择

>cd (cd DIRECTORY_NAME)

>cls

(2)第一个批处理脚本程序

myname2.bat

创建批处理.bat文件,写入以下代码:

@echo off

REM myname2.bat

REM Anything following the REM on a line is ignored by DOS

REM REMarks are used to comment or document your BATCH code.

REM "echo" prints string messages on the screen

echo mayname2.bat is now running

echo.

echo Hello %1!

REM Now DOS quits and provides you with another command prompt...

>myname2 Qinbing

(3)参数输入

通过以上例子,学习如何传入参数到批处理文件,创建另一个listdoc2.bat文件,列出输入文件夹的所有.pdf和.txt文件

参考代码:

echo listdoc2.bat is now running

echo.

DIR %1\*.txt

echo.

DIR %1\*.pdf

>listdoc2 D:\Lecture

(4)控制语句 (if, for)

if控制语句,创建testif2.bat文件,写入以下代码:

@echo off

REM testif.bat

REM Recognises user input and decides whether it is a 1 or 2.

IF %1==1 echo You Entered 1...

IF %1==2 echo You Entered 2...

IF NOT %1==3 echo It was not 3!!!

REM end of testif.bat

>testif2 1

>testif2 2

>testif2 3

可参考:https://ss64.com/nt/if.html

for控制语句,创建testfor2.bat文件,写入以下代码:

@echo off

REM testfor2.bat

REM A file to learn the for loop mechanisms

echo testfor.bat is now running

SET end=%1

FOR /L %%G IN (1,1,%1) DO echo repetition n. %%G

REM end of test_for2.bat

>testfor2 10

>testfor2 100

可参考:https://ss64.com/nt/for_l.html

我已介绍完三个代表性OS的基本脚本编程常用语法与方法(包括与内核交互),

接下来我们继续学习进程、内存、文件管理等内容。

(5)打印进程列表tasklist

创建listproc2.bat文件,写入以下代码:

@echo off

REM listproc2.bat

echo.

echo Processes with PID within a given range

echo.

@echo off

REM listproc2.bat

echo.

echo Processes with PID within a given range

echo.

IF "%1"=="" (tasklist)ELSE IF "%2"=="" (tasklist /FI "PID ge %1")ELSE (tasklist /FI "PID ge %1" /FI "PID le %2")

REM end of program

REM end of program

>listproc2 (打印所有进程)

>listproc2 100 (打印进程PID>=100的进程)

>listproc2 100 500 (打印PID在100到500之间的进程)

使用tasklist /fi "PID gt 1000"查看进程号大于1000的进程

使用tasklist /fi “USERNAME ne wuhsh” /fi “status eq running”命令查看wuhsh用户正在运行的进程

使用tasklist /s 192.168.0.9 /u wuhs /p password列出远程主机192.168.0.9的进程,使用账户wuhs,密码password登录

使用tasklist /m shell32.dll列出使用shell32.dll文件的所有进程

使用tasklist /svc /fi "imagename eq svchost.exe"列出svchost进程所运行的服务

2. BASH - list and read process information,一个较复杂脚本程序

保存当前电脑的进程使用情况到一个文本文件,在控制台写入以下命令:

$top -b > process.txt

任务 - 写一个脚本ProcessMemoryUsage.sh,以读取process.txt,在控制台输出内存使用大于0的进程

参考代码:

filename="process.txt"

echo "Reading $filename"

linecounter=0

while read -r line; do

line="$line"

let linecounter+=1

if [ $linecounter -gt 7 ]; then

column=0

memsize=""

for word in $line; do

if [ $column -eq 9 ]; then

memsize=$word

break;

fi

let column+=1

done

if [ $memsize != "0.0" ]; then

echo $line

fi

fi

done < $filename

4. 作业:

(1)编写脚本在DOS控制台中打印PID小于1000的进程,要求参数输入

(2)编写脚本在BASH中输出CPU使用率大于0.0%的进程

版权归广大操作系统实验课老师所有

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值