资源描述
Linux操作系统课程设计
班级:计算机13-2 学号: 姓名:谢甲山 成绩:
一、利用Linux相关系统调用函数编写一个简单文件管理工具,要求实现以下功效(可在任意目录下操作)。
功效
说明(提醒)
1.创建新文件
open(),close()
2.写文件
open(),write()
3.读文件
read()
4.复制文件
read(),write()
5.查看文件权限
需使用execv()函数实施”ls -l”命令实现
6.修改文件权限
chmod()
7.创建目录
mkdir()
8.查看目前路径和目录
类同5
9.删除目录
rmdir()
10.切换目录
chdir()
11.建立文件链接
link()
0.退出
exit()
二、经过访问/proc文件系统来获取系统目前信息,包含:
(1)进程信息。包含:进程名称、运行状态、PID、优先级、内存使用量。可结束任一进程。
(2)系统信息。包含:处理器信息(CPU名称、CPU频率、CPU类型、缓存大小),操作系统信息(系统类型、系统版本、GCC编译版本)。
(3)内存资源。包含:内存和缓冲区(内核使用情况(已用、剩下、中共)、交换区使用情况(已用、剩下、中共)),CPU使用率(各个核使用率)。
(4)模块信息。包含:模块名称、内存使用、使用次数。可卸载任一模块。
一、利用Linux相关系统调用函数编写一个简单文件管理工具
程序代码:
#filehandler.h
#include <iostream>
#include <string.h>
#include <fstream>
#include <unistd.h>
using namespace std;
int showmenu() //显示菜单,在主函数中循环调用。返回用户选择选项。
{
int option;
cout<<"1.创建新文件\n";
cout<<"2.写文件\n";
cout<<"3.读文件\n";
cout<<"4.复制文件\n";
cout<<"5.查看文件权限\n";
cout<<"6.修改文件权限\n";
cout<<"7.创建目录\n";
cout<<"8.查看目前路径和目录\n";
cout<<"9.删除目录\n10.切换目录\n";
cout<<"11.建立文件链接\n0.退出\n";
cin>>option;
return option;
}
void createfile() //以用户输入文件名创建新文件
{
string filename;
cout<<"input the filename\n";
cin>>filename;
ofstream of;
of.open(filename.c_str ());
if (!of)
{
cerr<<"open fail"<<endl;
}
of.close();
}
void insert() //写入用户指定内容到指定文件
{
string filename, msg;
cout<<"input the filename\n";
cin>>filename;
cout<<"input something you want to insert\n";
cin>>msg;
ofstream out;
out.open(filename.c_str ());
if (!out)
{
cerr<<"open fail"<<endl;
}
out<<msg.c_str();
out.close();
}
void readfile() //读取文件内容并显示
{
string filename;
cout<<"input the filename\n";
cin>>filename;
ifstream in;
in.open(filename.c_str ());
if (!in)
{
cerr<<"open fail"<<endl;
}
char buffer[1024];
while (!in.eof())
{
in.getline(buffer,100);
}
cout<<"===>"<<buffer<<endl;
in.close();
}
void copyfile() //复制文件
{
string ifilename;
cout<<"input the filename of old file\n";
cin>>ifilename;
/*将文件内容读取到buffer中*/
ifstream in;
in.open(ifilename.c_str ());
if (!in)
{
cerr<<"open fail"<<endl;
}
char buffer[1024];
while (!in.eof())
{
in.getline(buffer,100);
}
in.close();
/*将buffer中内容写入新文件*/
string ofilename, msg;
cout<<"input the filename of new file \n";
cin>>ofilename;
ofstream out;
out.open(ofilename.c_str ());
if (!out)
{
cerr<<"open fail"<<endl;
}
out<<buffer;
out.close();
}
void executecommand(const char * command, char * const* argv) //在子进程中实施路径为//command程序,参数在argv中
{
int pid = fork();
if (pid == 0)
{
if (execv(command, argv) == -1)
{
cout<<"===>error\n";
}
}
else
sleep(1); //等候子进程实施完成
}
#filehanlder.cpp
#include <iostream>
#include "filehandler.h"
using namespace std;
int main()
{
while (true) //keeping showing the menu
{
int option;
option = showmenu();
switch(option)
{
case 1: //创建新文件
createfile();
break;
case 2: //写入
insert();
break;
case 3: //读取
readfile();
break;
case 4: //复制
copyfile();
break;
case 5: //查看权限
{
char * argv[] = {"ls","-l",NULL};
char * path = "/bin/ls";
executecommand(path, argv);
break;
}
case 6: //修改权限
{
string filename;
string mod;
cout<<"input the filename\n";
cin>>filename;
cout<<"input the mode, r=4,w=2,x=1。 example:777 is rwxrwxrwx\n";
cin>>mod;
char f[20],m[10];
char * argv[] = { "chmod",
strcpy(m, mod.c_str()),
strcpy(f, filename.c_str()),
NULL};
char * path = "/bin/chmod";
executecommand(path, argv);
break;
}
case 7: //创建目录
{
string foldername;
cout<<"input the foldername\n";
cin>>foldername;
char f[20];
char * argv[] = {"mkdir",
strcpy(f, foldername.c_str()),
NULL};
char * path = "/bin/mkdir";
executecommand(path, argv);
break;
}
case 8: //查看目前路径
{
char * argv[] = {"pwd", NULL};
char * path = "/bin/pwd";
executecommand(path, argv);
break;
}
case 9: //切换目录
{
string foldername;
cout<<"input the foldername\n";
cin>>foldername;
char f[20];
char * argv[] = {"rm",
strcpy(f, foldername.c_str()),
"-r",
NULL};
char * path = "/bin/rm";
executecommand(path, argv);
break;
}
case 10: //切换目录
{
string dir;
cout<<"input the path you want to be \n";
cin>>dir;
char p[30];
if (chdir(strcpy(p, dir.c_str())) == -1)
{cout<<"fail to change dir"<<endl;}
break;
}
case 11: //建立文件连接
{
string oldpath,newpath;
cout<<"input old path \n";
cin>>oldpath;
cout<<"input new path\n";
cin>>newpath;
char np[30],op[34];
if (link(strcpy(op, oldpath.c_str()), strcpy(np, newpath.c_str())) == -1)
{cout<<"fail to change dir"<<endl;}
break;
}
case 0:
return 0;
default:
cout << "请选择0到11" << endl;
break;
}
cout<<"\n";
}
}
程序分为filehandler.h和filehandler.cpp两部分,关键功效保留在filehandler,h中,由showmenu()函数显示菜单,createfile()函数创建新文件,insert()函数写文件,readfile()函数读文件,copyfile()函数复制文件,executecommand()函数实施命令。filehandler.pp文件中,主函数为一个死循环,调用showmenu()函数显示菜单、获取用户选择选项,以后经过switch匹配对应函数。
试验截图:
以后运行程序,显示菜单,接着我们一个一个功效测试
选择功效1并输入文件名后,能够看到文件夹中确实创建了新文件
选择功效2以后、输入要写入文件文件名,再输入要写入内容
选择功效3以后,输入要读取文件文件名,在提醒符” ===》”以后是文件内容
选择功效4,依次输入旧文件名,新文件名
选择功效5,调用命令ls查看权限
选择功效6,输入文件名xiexie,并输入777以后再选择功效5,能够看到test2权限变成了rwxrwxrwx
选择功效7,输入新目录名字
选择功效8,查看目前目录
选择功效9,输入刚才创建目录目录名,确实删除了
选择功效10,切换到/home目录
经过功效10切换会之前shiyan6目录以后,选择功效11,创建filehandler.h文件连接
二、经过访问/proc文件系统来获取系统目前信息
程序代码:
//File.c
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<syslog.h>
#include<string.h>
#include<stdlib.h>
#include <wchar.h>
void menu(void);
void PIF(void);
void SIF(void);
void MIF(void);
void BIF(void);
int main() {
int choose;
menu();
scanf("%d",&choose);
while(choose!=0) {
switch(choose) {
case 1:PIF();
break;
case 2:SIF();
break;
case 3:MIF();
break;
case 4:BIF();
break;
default:printf("**************没有该选项,请重新输入**************\n");
}
menu();
scanf("%d",&choose);
}
return 0;
}
void menu(void) {
printf("*************************************** \n");
printf("*********亲爱用户请输出你需要操作********* \n");
printf("**************1.查看进程信息***************** \n");
printf("**************2.查看系统信息*************** \n");
printf("**************3.查看内存资源************** \n");
printf("**************4.查看模块信息************** \n");
printf("**************0.退出该系统************** \n");
printf("*************************************** \n");
printf("**************请输入1-4*************\n");
}
void PIF(void) {
char *pa = "/usr/bin/X11/top";
char *arg[4] = {"top", NULL};
if(fork()==0) {
printf("*****现在正在进入进程信息界面****** \n");
execv(pa,arg);
printf("ps:在该界面你能够输入k进行杀死进程,输入k以后再输入进程编号(PID)号\n");
printf("ps:假如你想要退出该界面,能够输入q进行退出 \n");
}else{
wait(0);
}
}
void SIF(void) {
printf("------------------------------------- \n");
printf("*** 系统信息以下 *** \n");
printf("------------------------------------- \n");
if(fork()==0) {
execlp("/bin/cat","cat","/proc/version",NULL);
}else{
wait(0);
}
printf("------------------------------------- \n");
printf("*** 处理器信息以下*** *** \n");
printf("------------------------------------- \n");
if(fork()==0) {
execlp("/bin/cat","cat","/proc/cpuinfo",NULL);
}else{
wait(0);
}
}
void MIF(void) {
printf("------------------------------------- \n");
printf("*** 相关内存信息以下*** \n");
printf("------------------------------------- \n");
if(fork()==0) {
execlp("/bin/cat","cat","/proc/meminfo",NULL);
}else{
wait(0);
}
}
void BIF(void){
printf("------------------------------------- \n");
printf("*** 全部模块信息以下(谨慎卸载)*** \n");
printf("------------------------------------- \n");
char *path = "/bin/lsmod";
char *argv[4] = {"lsmod", NULL};
if(fork()==0) {
execv(path,argv);
}else{
wait(0);
}
printf("------------------------------------- \n");
char name[1024];
printf("***请输入你所需要卸载模块名:*** \n");
scanf("%s", name);
char *pa="/sbin/rmmod";
char *ar[4]={"rmmod",name,NULL};
if(fork()==0) {
execv(pa,ar);
}else{
wait(0);
printf("卸载成功!\n");
}
}
试验截图:
进程信息:
杀死进程:
系统信息:
内存模块:
模块信息:
卸载模块:
卸载成功:
试验总结:
经过这次课程设计,让我明白怎样利用Linux相关系统调用函数编写一个简单文件管理工具,怎样写程序经过访问/proc文件系统来获取系统目前信息,即使大一只学了C++,不过c和C++有很多相同之处,经过查看资料还是能搞明白。
展开阅读全文