Linux内核同步,进程,线程同步
包括我自己在内,很多人对内核,进程,线程同步都不是很清楚,下面稍微总结一下:
内核同步:
主要是防止多核处理器同时访问修改某段代码,或者在对设备驱动程序进行临界区保护。主要有一下几种方式:
1. Mutex(互斥)
头文件:
#include <linux/mutex.h>
初始化方法:
DEFINE_MUTEX(name);或者
void mutex_init(struct mutex *lock);
使用方法:
void mutex_lock (struct mutex *lock);
Tries to lock the mutex, sleeps otherwise.
Caution: can't be interrupted, resulting in processes you cannot kill!
int mutex_lock_interruptible (struct mutex *lock);
Same, but can be interrupted. If interrupted, returns a non zero value and doesn't hold the lock. Test the return value!!!
int mutex_trylock (struct mutex *lock);
Never waits. Returns a non zero value if the mutex is not available.int mutex_is_locked(struct mutex *lock);Just tells whether the mutex is locked or not.
void mutex_unlock (struct mutex *lock);
Releases the lock. Make sure you do it as quickly as possible!
2. Reader/writer semphopres 读写信号量
Allow shared access by unlimited readers, or by only 1 writer. Writers get priority.
允许有限数量的读访问,但是只能有一个写访问。
void init_rwsem (struct rw_semaphore *sem);
void down_read (struct rw_semaphore *sem);
int down_read_trylock (struct rw_semaphore *sem);
int up_read (struct rw_semaphore *sem);
void down_write (struct rw_semaphore *sem);
相关文档:
可以采用sysV的shmget + shmat 实现。
但是我更喜欢shm_open + mmap 更简单。
#---------------------writer.c----------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
......
第20章 Linux内核移植
软件移植的概念简单地说就是让一套软件在指定的硬件平台上正常运行。移植至少包括了两个不同的硬件或者软件平台。对于应用软件来说,移植主要考虑操作系统的差异,重点在修改系统调用。本章的重点是Linux内核移植,需要考虑硬件平台的差异,涉及较多知识。主要内容如下:
q ......
在Linux下产生并调试core文件
先看看我用的是个什么机器:
$ uname -a
Linux dev 2.4.21-9.30AXsmp #1 SMP Wed May 26 23:37:09 EDT 2004 i686 i686 i386 GNU/Linux
再看看默认的一些参数,注意core file size是个0,程序出错时不会产生core文件了。
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbyt ......
2.2 安装Linux至硬盘
准备工作完成之后,就可以着手将Linux安装到硬盘中了。如今Linux的安装过程已经非常“傻瓜”化,只需要轻点几下鼠标,就能够完成整个系统的安装。尽管如此,这里仍然详细地给出安装过程的每一步。同时,对于和Windows存在显著区别的地方,如硬盘分区的组织方式,本节将做详细的讨论。 ......