【操作系统 实验三】---文件操作 myecho.c mycat.c mycp.c mysys.c sh1.c

本文档介绍了四个编程题目,分别涉及文件读写(myecho.c, mycat.c, mycp.c)和多进程(mysys.c),展示了如何使用C语言实现echo, cat, cp功能,以及自定义系统命令执行。后续的sh1.c程序则演示了如何通过mysys调用处理用户输入的命令。
摘要由CSDN通过智能技术生成

文件读写编程题目

job3/myecho.c

myecho.c的功能与系统echo程序相同
接受命令行参数,并将参数打印出来,例子如下:

$ ./myecho x
x
$ ./myecho a b c
a b c

【解】

mkdir job3  //新建文件夹
cd job3  //打开文件夹
touch myecho.c //新建文件
vim myecho.c //编辑文件
#include<stdlib.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
	int i;
	for(i=1;i<argc;i++)
	{
		printf("%s ",argv[i]);
	}
	printf("\n");
	return 0;
}

在这里插入图片描述
:wq 保存退出

cc myecho.c //编译

在这里插入图片描述

在这里插入图片描述

job3/mycat.c

mycat.c的功能与系统cat程序相同
mycat将指定的文件内容输出到屏幕,例子如下:
要求使用系统调用open/read/write/close实现

$ cat /etc/passwd 
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
$ ./mycat /etc/passwd 
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...

【解】

touch mycat.c //新建文件
vim mycat.c //编辑文件
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
	FILE *fp;
	int fsize;
	char *buffer;
	if(argc!=2)
	{
		printf("parameter error...\n");
		exit(0);
	}
	else
	{
		fp = fopen(argv[1],"r"); 
		if(!fp)
		{
			printf("can't open the file!\n");
			exit(0);
		}
		fseek(fp,0,SEEK_END);
		fsize = ftell(fp);
		rewind(fp);
		buffer=(char*)malloc((1+fsize)*sizeof(char));
		if(!buffer)
		{
			printf("malloc error...\n");
			exit(0);
		}
		fread(buffer,1,fsize,fp);
		printf("%s\n",buffer);
		fclose(fp);
		free(buffer);
		return 0;
	}
}

在这里插入图片描述
在这里插入图片描述
:wq 保存退出

cc mycat.c //编译

在这里插入图片描述

job3/mycp.c

mycp.c的功能与系统cp程序相同
将源文件复制到目标文件,例子如下:
要求使用系统调用open/read/write/close实现

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
$ ./mycp /etc/passwd passwd.bak 
$ cat passwd.bak
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

【解】

touch mycp.c //新建文件
vim mycp.c //编辑文件
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
	FILE *fp;
	int fsize;
	int fr;
	char *buffer;
	if(argc!=3)
	{
		printf("parameter error...\n");
		exit(0);
	}
	else
	{
		fp = fopen(argv[1],"r"); 
		if(!fp)
		{
			printf("can't open the file!\n");
			exit(0);
		}
		fseek(fp,0,SEEK_END);
		fsize = ftell(fp);
		rewind(fp);
		buffer=(char*)malloc((1+fsize)*sizeof(char));
		if(!buffer)
		{
			printf("malloc error...\n");
			exit(0);
		}
		fr = fread(buffer,1,fsize,fp);
		if(!fr)
		{
			printf("read file error...\n");
			exit(0);
		}
		fclose(fp);
		fp=fopen(argv[2],"w");
		if(!fp)
		{
		
			printf("open file error...\n");
			exit(0);
		}

		fwrite(buffer,1,fsize,fp);
		fclose(fp);
		free(buffer);
		return 0;
	}
}

在这里插入图片描述
在这里插入图片描述
:wq 保存退出

cc mycp.c //编译

在这里插入图片描述

多进程题目

job3/mysys.c

实现函数mysys,用于执行一个系统命令,要求如下
mysys的功能与系统函数system相同,要求用进程管理相关系统调用自己实现一遍
使用fork/exec/wait系统调用实现mysys
不能通过调用系统函数system实现mysys
在这里插入图片描述
在这里插入图片描述

【解】

touch mysys.c //新建文件
vim mysys.c //编辑文件
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<stdlib.h>
void mysys(char *str)
{
	pid_t pid;
	if(str==NULL)
	{
		printf("shell string error...\n");
		exit(0);
	}
	pid = fork();
	if(pid==0)
	{
		execl("/bin/sh","sh","-c",str,NULL);
	}
	wait(NULL);
}

int main()
{
	printf("----------------------------------------------------\n");
	mysys("echo a b c d");
	printf("----------------------------------------------------\n");
	mysys("ls /");
	printf("----------------------------------------------------\n");
	return 0;
}

在这里插入图片描述
:wq 保存退出

cc mysys.c //编译

在这里插入图片描述

int main()
{
	...
}

为测试程序,以下为mysys.c

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>

void mysys(char *str)
{
	pid_t pid;
	if(str==NULL)
	{
		printf("shell string error...\n");
		exit(0);
	}
	pid = fork();
	if(pid==0)
	{
		execl("/bin/sh","sh","-c",str,NULL);
	}
	wait(NULL);
}


job3/sh1.c

该程序读取用户输入的命令,调用函数mysys(上一个作业)执行用户的命令,示例如下
#编译sh1.c
$ cc -o sh1 sh1.c

执行sh1
$ ./sh

sh1打印提示符>,同时读取用户输入的命令echo,并执行输出结果

echo a b c
a b c

sh1打印提示符>,同时读取用户输入的命令cat,并执行输出结果

cat /etc/passwd
root: x :0:0:root:/root:/bin/bash
daemon: x :1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin: x :2:2:bin:/bin:/usr/sbin/nologin
实现内置命令cd、pwd、exit

【解】

vim mysys.c

在这里插入图片描述

vim sh1.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<string.h>

extern void mysys(char *str);

int main()
{
	while(1)
	{
		printf(">");
		char str[100]={0};
		char x;
		int i=0;
		do
		{
			x=getchar();
			str[i]=x;
			i++;
		}while(x!='\n');
		str[i-1]='\0';
		if(str[0]=='e'&&str[1]=='x'&&str[2]=='i'&&str[3]=='t')
		{
			exit(0);
		}
		else if(str[0]=='c'&&str[1]=='d'&&str[2]==' ')
		{
			char cd[100]={0};
			for(i=0;str[i+3]!='\0';i++)
			{
				cd[i]=str[i+3];
			}
			cd[i] = '\0';
			chdir(cd);
		//	if(chdir(cd))
		//	{
		//		printf("cd:%s:no such directory\n",cd);
		//	}
		}
		mysys(str);
	}
}


在这里插入图片描述
在这里插入图片描述
编译、链接

在这里插入图片描述
测试
在这里插入图片描述

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值