is a set of standards for physically connecting and transferring data between computer and peripheral devices.SCSI is most commonly used for hard disks and tape drives,but it can connect a wide range of other devices including scanners and CD drives.
ATA is short for AT COMPUTER attachment,means AT computer s attachment.You can easilly use ATA to connect harddisk on PC.
ATA is commonly consist of PATA(parallel ATA ) and SATA(serial ATA).
为了编译自己的内核,需要弄清楚各自的驱动。
为了知道自己的机器的具体硬件信息,请参考这篇文章:
迭代 查找单词collections.Counter()
for line in open('5.py'):
print(line.upper(), end='')
将5.py的每一行换成大写。
f = open('5.py')
while True:
line = f.readline()
if not line: break
print(line.upper(), end = ' ')
在交互模式下:
L = [1,2,3]
I = iter(L)
I.next()
请参阅这篇文章 here
for的使用主要注意, 后面的冒号。
在字典中的使用 :
#!/usr/bin/python
from __future__ import print_function
D = { 'a':1, 'b':2,'c':3}
for key in D:
print(key, '=>', D[key])
结果:
a => 1
c => 3
b => 2
关键注意字典的键和值。这一点在perl中也是很重要的。字典本身具有很多属性,稍微看一个。将上面的字典用items()打印出来就是
L = D.items()
print(L)
这是python 2.x中的用法,3.x直接调用: list(D.items())
for(key, value) in D.items():
print(key, '=>', value)
与上面实现了同样的方法。
这里的计数器类似于c语言中的for循环,典型的用法如下, from 0 to (i - 1):
for i in range(3):
print(i, 'pythons')
就会打印三个pythons.
处理字符串时利用 len()函数即可达到相同的作用。
X = 'spam'
for item in X:
print(item, end = ' ')
# 这两种循环都是一个作用。
i = 0
while i < len(X):
print(X[i], end = ' ')
i += 1
以上这两中并不是处理这类循环最佳的选择,下面的语句就可以简单的解决。
for item in X: print(x, end = ' ')
以下两种方法实现相同的功能:
S = 'abcdefghijk'
for i in range(0, len(S), 2): print(S[i], end = ' ')
print('\n')
# 使用切片技术
for c in S[::2]: print(c, end = ' ')
print('\n')
L = [1,2,4,5,6]
for i in range(len(L)):
L[i] += 1
print(L)
L1 = [1,2,4,5,6]
L2 = [2,3,5,6,7]
M = zip(L1,L2)
print(M)
for (x, y) in M:
print(x,y, '--', x + y)
zip可以接受任何类型的对象,下面这个例子。
L1 = [1,2,4,5,6]
L2 = [2,3,5,6,7]
L3 = [3,4,6,7,8]
print(list(zip(L1,L2,L3)))
打印的结果为:
[(1, 2, 3), (2, 3, 4), (4, 5, 6), (5, 6, 7), (6, 7, 8)]
zip函数以最短的对象为准。 map以None补全不够的那一部分。
S1 = 'abc'
S2 = 'hechun'
print(zip(S1, S2))
print(map(None, S1,S2))
可惜,3.0以上不支持这个函数了。
keys = ['spam', 'eggs', 'toast']
vals = [1, 2, 3]
print(list(zip(keys, vals)))
处理的结果是如下图所示:
[('spam', 1), ('eggs', 2), ('toast', 3)]
这样,我们并没有形成字典,正确的还需要多处理一步:
keys = ['spam', 'eggs', 'toast']
vals = [1, 2, 3]
print(list(zip(keys, vals)))
D2 = { }
for (k, v) in zip(keys, vals): D2[k] = v
print(D2)
其输出的结果如下所示:
[('spam', 1), ('eggs', 2), ('toast', 3)]
{'toast': 3, 'eggs': 2, 'spam': 1}
在2.x以后,可以直接使用dict函数。
D3 = dict(zip(keys, vals))
print(D3)
在python中,字符串的比较应该多留意下,不能直接比较,可以转化为浮点型进行比较
>>> a = '1.2'
>>> b = '100.9'
>>> c = '2.4'
>>> alist = [a, b, c]
>>> alist.sort()
>>> print(alist)
['1.2', '100.9', '2.4']
So , you would better to do it :
>>> a='1.2'
>>> b='100.9'
>>> c='2.4'
>>> alist=[float(a), float(b), float(c)]
>>> alist.sort()
>>> print(alist)
[1.2, 2.4, 100.9]
这个错误经常出现在当应用程序进行一些非阻塞(non-blocking)操作(对文件或socket)的时候。例如,以 O_NONBLOCK的标志打开文件/socket/FIFO,如果你连续做read操作而没有数据可读。此时程序不会阻塞起来等待数据准备就绪返回,read函数会返回一个错误EAGAIN,提示你的应用程序现在没有数据可读请稍后再试。
出现这个错误是因为函数没有任何数据被传送此时发生了中断,
如果出现:
error: implicit declaration of function ‘kmalloc’ [-Werror=implicit-function-declaration]
你应该添加上 #include
下面这些操作与模块有关。
这个函数是打印出module的属性
除此之外集合实例也可以查看其自己的所有使用方法。
导入一个模块文件
这个集合与数学中的集合类似,可以进行相关的‘与’、‘或’、‘非’等操作
>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
set(['a', 'c', 'b', 'e', 'd'])
>>> 'e' in x
True
>>> x | y
set(['a', 'c', 'b', 'e', 'd', 'y', 'x', 'z'])
>>> x & y
set(['b', 'd'])
>>> z = x.intersection(y)
>>> z
set(['b', 'd'])
# 以上两种都是求交集
>>> x - y
set(['a', 'c', 'e'])
>>> x ^y
set(['a', 'c', 'e', 'y', 'x', 'z'])
使用其他方式改变集合的样子
# 求交集
>>> z = x.intersection(y)
>>> z
set(['b', 'd'])
# add 插入一个项目
>>> z.add('spam')
>>> z
set(['b', 'd', 'spam'])
# update按位置求并集
>>> z.update(set(['x','y']))
>>> z
set(['y', 'x', 'b', 'd', 'spam'])
# remove 删除一个项目
>>> z.remove('b')
>>> z
set(['y', 'x', 'd', 'spam'])
第二种方式成为集合的方法,前一种限制了两边必须是集合,后一种可以针对任何可迭代的对象
>>> s = set([1,2,3])
>>> s | set([3,4])
set([1, 2, 3, 4])
>>> s | [3,4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'set' and 'list'
>>>
>>> s.union([3,4])
set([1, 2, 3, 4])
>>>
python3.0中以下形式是等价的:
set([1,2,3,4])
{1,2,3,4}
所谓的集合 解析就是将一个集合的每一个元素分离出来,在2.x中是下面这个样子的:
>>> {x ** 2 for x in [ 1,2,3,4]}
set([16, 1, 4, 9])
>>>
自己就生成了集合,在3.0中不会,直接就是{,,}的形式。
{ x for x in 'spam' }
set(['a', 'p', 's', 'm'])
>>>
x是循环变量在这个程序例子中。
>>> L = [1,2,3,4]
>>> set(L)
set([1, 2, 3, 4])
>>> L = list(set(L))
>>> L
[1, 2, 3, 4]
>>>
两种方法: int(N)和math.trunc(N)
>>> int(6.5)
6
>>> import math
>>> math.trunc(6.5)
6
两种方案: float(n)和混合运算
>>> float(6)
6.0
>>> 6 * 1.0
6.0
在2.x的情况下测试如下:
>>> hex(16)
'0x10'
>>> oct(765)
'01375'
>>> bin(16)
'0b10000'
使用int(S, base),其中base是进制的参数
>>> int('01375',8)
765
>>> int('0b10000',2)
16
好吧,开一系列介绍linux进程的文章,先从fork说起。
fork()函数就是复制当前的进程创造一个新的进程,当前的进程叫做叫做父进程,复制产生的进程叫做子进程。
子进程复制父进程的一切资源,除了:
1 子进程具有独一无二的进程 pid,
2 子进程有一个父进程,其ID和创造这个进程(子进程)的id相同
进程调用fork后,内核将控制权交给两个进程。
先看代码,这里有点意思,程序的输出结果如果你不仔细分辨的,真的会让你想不到。
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t fpid; //表示fork()返回的值
int count = 0;
fpid = fork();
if(fpid < 0)
printf("error in fork!");
else if (fpid == 0){
printf("I am the child process, my process id is %d\n", getpid());
printf("I am son of my father\n");
count ++;
}
else {
printf("I am the parent process, my process id is %d \n", getpid());
printf("I am father of my son\n");
count++;
}
printf(" as a result: %d\n", count);
return 0;
}
结果在我的机子上显示为;
I am the parent process, my process id is 10369
I am father of my son
as a result: 1
root@yubo-2:~/test/unix_linux# I am the child process, my process id is 10370
I am son of my father
as a result: 1
也就是说一个主程序在没有循环的情况下,居然有两个返回值,它是怎么做到的后来剖析,现在简单地分析下导致这种现象的原因.
1 在父进程中,fork返回新创建子进程的进程id
2 在子进程中,fork返回0
3 fork返回一个负值,意味着fork创建失败。
看到网上说,进程经过fork()后形成了进程链,父进程的fpid指向子进程的进程id,因为子进程没有子进程(至少在这个程序中),所以其fpid为0.
fork()出错的原因可能有几下几种:
1 当前的进程数已经达到了系统规定的上限,这是errno的值为EAGAIN.
2 系统内存不足,这时errno的值为ENOMEM
两个进程的执行没有先后顺序,根据调度算法而已.
#include<stdio.h>
int main()
{
int ret_from_fork, mypid;
mypid = getpid();
printf("Before: my pid is %d\n", mypid);
ret_from_fork = fork();
sleep(1);
printf("After, my pid is %d, fork() said %d\n", getpid(), ret_from_fork);
}
结果如下所示:
注意第一段所说,fork调用是将原来的进程复制到新的进程,包括数据和程序,但是,新的程序是运行到当前程序行,并不会从整个程序的开头从头运行。这个图片中,原进程21635运行Before:后运行fork,启动了一个子进程,随后,进程21635接着进行,打印了After:,当进程21635消亡后,才转移到子进程,注意,子进程并不是从头开始的,而是从fork开始,也就是说,fork的Before不会打印,只执行After的语句。
p21636
|------->
Before | After
=========|=======> p21635
fork
一个fork会产生2个进程,两个fork会产生4个进程,依次类推….
#include<stdio.h>
int main()
{
fork();
fork();
fork();
printf("my pid is %d \n", getpid());
}
yubo@debian:~/test/tmp/unix$
结果如下所示:
#include<stdio.h>
int main()
{
int fork_rv;
printf("Before: my pid is %d \n", getpid());
fork_rv = fork();
if (fork_rv == -1)
perror("Fork failed\n");
else if (fork_rv == 0)
printf("I am the child, my pid is %d\n", getpid());
else
printf("I am the parent, my pid is %d\n", getpid());
}
先上代码;
#include<stdio.h>
#include<unistd.h>
int main()
{
int i = 0;
printf("i son/pa ppid pid fpid\n");
/* ppid 是当前进程的父进程pid,
* pid 是当前进程的pid
* fpid 是fork返回给当前进程的值
*/
for(i = 0; i < 2; i++){
pid_t fpid = fork();
if(fpid == 0)
printf(" %d child %4d %4d %4d\n", i, getppid(), getpid(), fpid);
else
printf("%d parent %4d %4d %4d\n", i, getppid(), getpid(), fpid);
}
return 0;
}
这是在我的机子上的结果:
root@yubo-2:~/test/unix_linux# ./test
i son/pa ppid pid fpid
0 parent 11040 11508 11509
1 parent 11040 11508 11510
root@yubo-2:~/test/unix_linux# 1 child 1 11510 0
0 child 1 11509 0
1 parent 1 11509 11511
1 child 1 11511 0
仔细分析这个输入法,在 i = 0时,上面这个程序调用fork函数,系统中出现两个进程 p11508和p11509(p是进程的意思),且p11508是p11509的父进程,p11040是p11508的父进程。因为对应的函数为: getpid(), fpid = fork(), getppod().即进程链p11040->p11508->p11509.