安工大Linux程序设计实验

Linux实验指导

实验一 Linux基本命令的使用

 

实验环境:fedora26  /bin/bash

 

1实验目的 

学习和掌握Linux的基本命令。

2、实验内容和步骤

步骤1以user_login用户身份并使用telnet登录Linux服务器,按照提示创建自己的账户和口令。

步骤 2使用新创建的用户账户和口令登录Linux系统,察看登录后的界面。

步骤3使用pwd命令察看当前的工作目录,然后用ls命令查看当前目录下的内容,尝试使用-a,-l,-F,-A,-lF等不同选项并比较不同之处。 

[yan@localhost ~]$ pwd

/home/yan

[yan@localhost ~]$ ls

公共 模板 视频 图片 文档 下载 音乐 桌面

[yan@localhost ~]$ ls-a

. .bash_profile .dbus .java .pki 模板 下载

.. .bashrc .eclipse .lesshst .sogouinput 视频 音乐

.bash_history .cache .esd_auth .local .swt 图片 桌面

.bash_logout .config .ICEauthority .mozilla 公共 文档

[yan@localhost ~]$ gnome-screenshot

[yan@localhost ~]$ gnome-screenshot -a

[yan@localhost ~]$clear


 

[yan@localhost ~]$ pwd

/home/yan

[yan@localhost ~]$ ls

公共 模板 视频 图片 文档 下载 音乐 桌面

[yan@localhost ~]$ ls-a

. .bash_profile .dbus .java .pki 模板 下载

.. .bashrc .eclipse .lesshst .sogouinput 视频 音乐

.bash_history .cache .esd_auth .local .swt 图片 桌面

.bash_logout .config .ICEauthority .mozilla 公共 文档

[yan@localhost ~]$ ls-l

总用量 32

drwxr-xr-x.2 yan yan 4096 6月 1121:27 公共

drwxr-xr-x.2 yan yan 4096 6月 1121:27 模板

drwxr-xr-x.2 yan yan 4096 6月 1121:27 视频

drwxr-xr-x.2 yan yan 4096 6月 1423:00 图片

drwxr-xr-x.3 yan yan 4096 6月 1300:15 文档

drwxr-xr-x.2 yan yan 4096 6月 1422:57 下载

drwxr-xr-x.2 yan yan 4096 6月 1121:27 音乐

drwxr-xr-x.2 yan yan 4096 6月 1121:27 桌面

[yan@localhost ~]$ ls-A

.bash_history .cache .esd_auth .local .swt 图片 桌面

.bash_logout .config .ICEauthority .mozilla 公共 文档

.bash_profile .dbus .java .pki 模板 下载

.bashrc .eclipse .lesshst .sogouinput 视频 音乐

[yan@localhost ~]$ ls-lF

总用量 32

drwxr-xr-x.2 yan yan 4096 6月 1121:27 公共/

drwxr-xr-x.2 yan yan 4096 6月 1121:27 模板/

drwxr-xr-x.2 yan yan 4096 6月 1121:27 视频/

drwxr-xr-x.2 yan yan 4096 6月 1423:00 图片/

drwxr-xr-x.3 yan yan 4096 6月 1300:15 文档/

drwxr-xr-x.2 yan yan 4096 6月 1422:57 下载/

drwxr-xr-x.2 yan yan 4096 6月 1121:27 音乐/

drwxr-xr-x.2 yan yan 4096 6月 1121:27 桌面/


 

步骤4在当前目录下建立一个名为test的新目录,然后将工作目录切换到test下,尝试将/etc目录下的文件passwd拷贝到该目录下(cp源文件 目的目录)。察看当前目录下的passwd文件的属主和文件权限。

[yan@localhost ~]$mkdir test

[yan@localhost ~]$ cdtest

[yan@localhost test]$cp -r /etc/passwd ./

[yan@localhost test]$ls

passwd

[yan@localhost test]$ls -l passwd

-rw-r--r--.1 yan yan 2385 6月 1423:09 passwd

步骤5尝试向当前目录下的passwd文件和/etc/passwd文件分别写入一些新内容(可使用echo字符串”>>文件的命令),看看操作能否成功,如果不能成功,请说明原因。用cat命令浏览文件password的内容,用more命令进行浏览翻页操作,再用less命令浏览文件的内容。比较这几个命令的不同之处

[yan@localhosttest]$ echo "你好123">> passwd

[yan@localhosttest]$ echo "你好123">> /etc/passwd

bash: /etc/passwd:Permission denied

当前passwd文件可写入,/etc/passwd文件不可写入

因为 当前passwd文件拥有写权限而当前用户对/etc/passwd没有写权限

[yan@localhosttest]$ cat passwd | more //分屏显示,空格可以切换下一页

[yan@localhosttest]$ cat passwd | less //less 在查看之前不会加载整个文件

步骤6ls命令查看test下文件的权限,用mv命令更改文件password的文件名为test.txt,尝试用chown和chgrp更改文件的属主为root、组为root,看看能否成功,不成功,请说明原因。尝试用chomd将文件权限为“-rw-------。看看能否成功,不成功,请说明原因。

[yan@localhost test]$ls -l passwd

-rw-r--r--.1 yan yan 2406 6月 1423:15 passwd

[yan@localhost test]$mv passwd test.txt

[yan@localhost test]$ls

test.txt

[yan@localhost test]$chown root test.txt

chown:正在更改'test.txt'的所有者:Operation not permitted

[yan@localhost test]$chgrp root test.txt

chgrp:正在更改'test.txt'的所属组:Operation not permitted

[yan@localhost test]$chmod -rw------ test.txt

[yan@localhost test]$ls -a test.txt

test.txt


 

步骤7rm命令删除test目录下的所有文件,再用rmdir命令删除test目录。 (想一想有没有一条命令将目录及目录下的所有文件删除,写出这条命令)

[yan@localhost ~]$ rm-f test/*

[yan@localhost ~]$rmdir test

[yan@localhost ~]$ rm-rf test

步骤8使用ps命令查看当前系统内的进程,并利用man命令获取ps命令的参数,写出获取当前终端进程执行情况的ps命令。

[yan@localhost ~]$ ps

PIDTTY TIME CMD

3878pts/0 00:00:00 bash

4029pts/0 00:00:00 less

4422pts/0 00:00:00 ps

[yan@localhost ~]$ ps-T

PID SPID TTY TIME CMD

3878 3878 pts/0 00:00:00 bash

4029 4029 pts/0 00:00:00 less

4482 4482 pts/0 00:00:00 ps

步骤9使用df命令查看当前系统已安装的文件系统的空间使用情况,记录结果。

[yan@localhost ~]$ df

文件系统 1K-块 已用 可用 已用%挂载点

devtmpfs 1946164 0 1946164 0% /dev

tmpfs 1959560 25940 1933620 2% /dev/shm

tmpfs 1959560 2036 1957524 1% /run

tmpfs 1959560 0 1959560 0% /sys/fs/cgroup

/dev/mapper/fedora-root47122644 8518380 36180868 20% /

tmpfs 1959560 64 1959496 1% /tmp

/dev/sda8 999320 147752 782756 16% /boot

/dev/sda1 98304 43284 55020 45% /boot/efi

tmpfs 391912 12 391900 1% /run/user/42

tmpfs 391912 40 391872 1% /run/user/1000

步骤10使用du命令查看用户的工作目录占用了多少空间,记录结果。

[yan@localhost ~]$ cdtest

[yan@localhost test]$du

4 .

步骤11使用free命令查看内存资源的使用情况,记录结果。

[yan@localhost ~]$free

total used free shared buff/cache available

Mem: 3919120 1707840 706012 341852 1505268 1785636

Swap: 4059132 0 4059132

步骤12使用man获取tar和gzip的帮助信息,尝试将test目录下的文件打包并压缩,然后到另外一目录tmp下解包,写出这几条命令。

tar:

[yan@localhost ~]$ tarcvf a.tar ./test

./test/

[yan@localhost ~]$ ls

a.tar more test 公共 模板 视频 图片 文档 下载 音乐 桌面

[yan@localhost ~]$ tarxvf a.tar

./test/

[yan@localhost ~]$ ls

a.tar more test 公共 模板 视频 图片 文档 下载 音乐 桌面

[yan@localhost tmp]$tar xvf ~/a.tar

./test/

gzip:

[yan@localhost test]$gzip test

[yan@localhost test]$ls

test.gz

[yan@localhost test]$gzip -d test.gz

[yan@localhost test]$ls

test

步骤13尝试执行“lsl> tmp,看看这条命令的执行会出现什么结果,解释一下这条命令。

[yan@localhost ~]$ ls-l > tmp

bash: tmp: Is adirectory

步骤14尝试执行find/usr/src -name *.c -print | xargs grep#include,看看这条命令的执行会出现什么结果,解释一下这条命令。

结果:打印出了/usr/src下到所有的头文件的包含行

解释:查询 /usr/src下到所有.c文件并筛选出带#include的行并打印

步骤15执行cal和date命令,说说这两条指令的用途。


 

[yan@localhost/]$ date

2018年06月15日星期五 00:16:59CST


 

步骤16执行命令clear和logout,退出系统。(想一想有没有其他的方法,写出这种方法)

exit

步骤17执行命令shutdown,关闭系统。(想一想有没有更简单的命令,写出这条命令)

1、halt  立刻关机

2、poweroff 立刻关机

3、shutdown-h now 立刻关机(root用户使用)

4、shutdown-h 10 10分钟后自动关机


 

3、实验结论

linux内置命令提供了管理系统的很多实用功能,通过使用这些命令可以完成对系统到管理和使用.

 

实验二:脚本程序设计

1.目的和要求

加强对脚本程序设计的理解。

2.实验内容

参数列表,循环。把下面程序的参数列表显示用for循环实现。

3.实验提示

salutation=”Hello”

echo$salutation

echo“The program $0 is now running”

echo“The second parameter was $2”

echo“The first parameter was $1”

echo“The parameter list was $*”

echo“The user’s home directory is $HOME”

echo“Please enter a new greeting”

readsalutation

echo$salutation

echo“The script is now complete”

exit0

 

4.实验运行结果

 

 

 

 

 

 

 

 

 

改写后:

#!/bin/sh

 

salutation=”Hello”

echo$salutation

count=0

str=""

forstr in $*

do

echo"the parameter $count is $str "

count=$(($count+1));

done

echo“The user’shome directory is $HOME”

echo“Please enter a new greeting”

readsalutation

echo$salutation

echo“The script is now complete”

exit0

 

 

 

 

 

 

实验三文件操作

1.目的和要求

熟悉文件操作的系统调用和库函数

2.实验内容

比较使用系统调用和库函数的文件拷贝程序的速度性能

3.实验提示//c编译时的错误:程序中有游离的‘\240’‘\302’‘\XXX 时解决方案

#include<unistd.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

intmain()

{

char c;

intin, out;

in= open(“file.in”, O_RDONLY);

out= open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

while(read(in,&c,1)== 1)

write(out,&c,1);

exit(0);

}

 

#include<stdio.h>

#include<stdlib.h>

intmain()

{

intc;

FILE*in, *out;

in= fopen(“file.in”,”r”);

out= fopen(“file.out”,”w”);

while((c= fgetc(in)) != EOF)

fputc(c,out);

exit(0);

}

 

4.实验运行结果

 

系统调用:

 

 

库函数:

 

 

 

 

结论:

系统调用比库函数慢,因为系统调用要进行用户态到内核态的上下文切换

 

实验四 创建子进程

1.目的和要求

熟悉创建子进程的系统调用fork()

2.实验内容

创建子进程并实现父子进程的同步

3.实验提示

#include<sys/types.h>

#include<sys/wait.h>

#include<unistd.h>

#include<stdio.h>

intmain()

{

pid_tpid;

char*message;

intn;

intexit_code;

printf(“forkprogram starting\n”);

pid= fork();

switch(pid)

{

case-1:

perror(“forkfailed”);

exit(1);

case0:

message= “This is the child”;

n= 5;

exit_code= 37;

break;

default:

message= “This is the parent”;

n= 3;

exit_code= 0;

break;

}

for(;n > 0; n--) {

puts(message);

sleep(1);

}

if(pid != 0) {

intstat_val;

pid_tchild_pid;

child_pid= wait(&stat_val);

printf(“Childhas finished: PID = %d\n”, child_pid);

if(WIFEXITED(stat_val))

printf(“Childexited with code %d\n”, WEXITSTATUS(stat_val));

else

printf(“Childterminated abnormally\n”);


}
exit(exit_code);

}

 

4.实验运行结果

 

 

 

 

实验五 SOCKET

1.目的和要求

理解socket的基本原理。

2.实验内容

利用socket完成本地通信

3.实验提示

服务端代码:

#include<sys/types.h>

#include<sys/socket.h>

#include<stdio.h>

#include<sys/un.h>

#include<unistd.h>

#include<stdlib.h>

 

intmain()

{

intserver_sockfd, client_sockfd;

intserver_len, client_len;

structsockaddr_un server_address;

structsockaddr_un client_address;

 

unlink(“server_socket”);

server_sockfd= socket(AF_UNIX, SOCK_STREAM, 0);

server_address.sun_family= AF_UNIX;

strcpy(server_address.sun_path,“server_socket”);

server_len= sizeof(server_address);

bind(server_sockfd,(struct sockaddr *)&server_address, server_len);

listen(server_sockfd,5);

while(1){

charch;

 

printf(“serverwaiting\n”);

 

 client_len= sizeof(client_address);

client_sockfd= accept(server_sockfd,

(structsockaddr *)&client_address, &client_len);

read(client_sockfd,&ch, 1);

ch++;

write(client_sockfd,&ch, 1);

close(client_sockfd);

}

}

客户端代码:

#include<sys/types.h>

#include<sys/socket.h>

#include<stdio.h>

#include<sys/un.h>

#include<unistd.h>

#include<stdlib.h>

 

intmain(){

intsockfd;

intlen;

structsockaddr_un address;

intresult;

charch='A';

sockfd=socket(AF_UNIX,SOCK_STREAM, 0);

address.sun_family=AF_UNIX;

strcpy(address.sun_path,"server_socket");

len=sizeof(address);

result=connect(sockfd,(struct sockaddr*) &address, len);

if(result==-1){

perror("oops:client1");

exit(1);

}

write(sockfd,&ch,1);

read(sockfd,&ch,1);

printf("charfrom server=%c\n",ch);

close(sockfd);

exit(0);

}

4.实验运行结果

 

  • 10
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值