每日一练(二十四)
12.21 TCP与UDP的区别
- TCP面向连接;UDP面向无连接
- TCP提供:可靠交付、无差错、不丢失、不重复、按序到达;UDP不提供可靠交付、不保证不丢失、不保证顺序到达
- TCP面向字节流的,发送的是一个流,无头无尾;UDP时面向数据报的,一个一个发送数据报
- TCP可以提供流量控制和拥塞控制,即防止对端被压垮,也防止网络被压垮
12.22 I/O重定向的使用技巧
通过IO重定向,可以将命令的输入来源以及输出地点重定向为某一个文件。
下面介绍一些IO重定向的小技巧:
-
> hh.txt
:可以创建一个文件 -
ls -l > hh.txt
:可以将标准输出重定向到hh.txt
文件中 -
> hh.txt
:可以擦除文件内容,因为重定向每次输入之前都会清除文件 -
2> hh.txt
:可以将标准错误重定向到文件 -
ls -l /feak &> hh.txt
:可以将标准输出和标准错误同时重定向到文件中 -
当不需要出错信息的时候,可以通过
> /dev/null
重定向错误到这个文件中(黑洞) -
通过cat命令向文件中写入数据:
cat >> hh.txt
,命令行输入之后以Ctrl+D进行写入- 通过管道来建立一个过滤器:
ls /bin /usr/bin | sort | less
,可以对两个目录下的所有文件进行排序
- 通过管道来建立一个过滤器:
12.23 栈的压入与弹出
6、若已知一个栈的入栈顺序为1、2、3、4,其出栈顺序为P1、P2、P3、P4,则P2、P4不可能是()。
2、1
4、3
2、4
1、4
答案:B
这里涉及到栈的压入和弹出,先压入、后弹出。每次压入后判断栈顶元素是否为要弹出的元素,如果是就弹出,再判断下一个,如果弹出后下一个不满足,则继续入栈。
力扣:点击跳转链接
12.24 memmove解决内存重叠问题
处理内存重叠问题就要涉及到两个函数了:memcpy()
和memmove()
,下面先介绍一下两个函数的函数原型
memcpy():
NAME
memcpy - copy memory area
SYNOPSIS
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
DESCRIPTION
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.
可以看出memcpy()
知识单纯的拷贝地址,并没有考虑地址重叠的问题,但是它推荐使用memmove()
来解决地址重叠问题,其函数原型如下:
NAME
memmove - copy memory area
SYNOPSIS
#include <string.h>
void *memmove(void *dest, const void *src, size_t n);
DESCRIPTION
The memmove() function copies n bytes from memory area src to memory area dest. The mem‐
ory areas may overlap: copying takes place as though the bytes in src are first copied
into a temporary array that does not overlap src or dest, and the bytes are then copied
from the temporary array to dest.
两个函数实现的对比:
void* memmove(void* dest, void* source, size_t count)
{
assert((dest!=NULL) && (source !=NULL));
void* ret = dest;
if (dest <= source || dest >= (source + count)) //无重叠
{
while (count --)
*dest++ = *source++;
}
else //重叠处理
{
dest += count - 1;
source += count - 1;
while (count--)
*dest-- = *source--;
}
return ret;
}
void* memcpy(void* dest, void* source, size_t count)
{
assert((dest!=NULL) && (source !=NULL));
void* ret = dest;
while (count--)
*dest++ = *source;
return ret;
}
12.25 Linux中常用查找命令
1、最强大的搜索命令:find 查找各种文件的命令
2、在文件资料中查找文件:locate
3、搜索命令所在的目录及别名信息:which
4、搜索命令所在的目录及帮助文档路径:whereis
5、在文件中搜寻字符串匹配的行并输出:grep
6、分页显示一个文件或任何输出结果:more
7、分页显示一个文件并且可以回头:less
8、指定显示前多少行文件内容:head
9、指定显示文件后多少行内容:tail
10、查看一个文件:cat
11、查看文件内容多少字符多少行多少字节:wc
12、排序文件内容:sort