Archive

Posts Tagged ‘APUE2e’

APUE2e Exercise3.2

September 11th, 2011 2 comments

My implementation for APUE2e Exercise3.2: implement my own dup2 function that performs the same service as the dup2 function. Based on a little testing, it seems to work and behave as the provided dup2 function. I used the dup function to implement mydup2 function. However, I didn’t work on the error handling part, just succeed or fail.
Read more…

Categories: 书山有路 Tags: , ,

APUE2e Exercise3.6

September 11th, 2011 No comments

I was working exercise3.6 of APUE2e and the code was simple. But when I tried to use fgets and fputs to print out the file content, I got problems. Then I tried to figure out the reason. It turned out to be the null or ‘’ byte of C string, which prevent fgets and fputs from behaving as I expected (fgets appends null byte to the end of input buffer, while fputs prints null-terminated string). I solved the problem by preventing write function to write the null or ‘‘ byte for a C string to the file. This was done by restricting the number of bytes to be writen (using sizeof(buffer)-1, rather than sizeof(buffer)). These findings reminded me of my careless reading, because the problem was caused by a really small mistake. Now I learned the lesson. I prefer reading to writing codes. But through writing and testing, I learn more than just reading. I think I must do more coding and don’t get satisfied just by reading the books and codes.

Attaching is the code I wrote for exercise3.6.
Read more…

0625之一周瞎扯

June 25th, 2011 12 comments

Research – CPSNET进展。上周把所有layer和component的具体功能和函数给列完了,这周开始实现具体的函数,完成那些和具体的应用无关的部分。我先从sensor layer开始写,因为它最简单,差不多快写完了。接下来写physical layer。computation和service两层太过复杂,留到后面。之前只是声明一下函数,定义一下类和各种数据结构,主要涉及的是C++的东西。现在做函数实现,就要用到OMNeT++的library了。去年看过OMNeT++的部分manual,但没记住多少。现在基本是边查边写边编译。为了不把系统整得过于繁琐复杂,我对其中一些功能进行了简化,先把简单的搭好了,有必要再修改。写代码的同时,我写了大量的注释,尽量具体,以利于以后的修改和改进。从进度上看,我已经落后了。每天我只花几个小时在这上面,写的比较慢。让我把其他事都丢下专门写这个,貌似有点做不到。我还是希望能并行着做一些事。It makes me feel better。

Reading。 还是读得那几本书。C++ Primer看了第13,15章。15章还没读完。最近没怎么看,被我扔一边了。暑假要把这本书读完,下学期再读一遍。 Linux Shell看到了第5章。6,7两章是关于Vim和Emacs的,打算先跳过,看第8章Bash shell。Vim和Emacs我倾向于先看Vim,这个貌似我稍微熟悉一些,对Emacs一无所知。APUE2e读到第7章。Exercise和代码才写到第4章,还有三章要赶,惭愧。 Unix OS看到第10章了,看了整书地四分之三左右,还剩一百来页,最近要把它完成了。明朝那些事儿看到第6部了,真佩服自己有耐性看。。。
Read more…

读书笔记 – APUE2e Chap4(2)

May 27th, 2011 2 comments

/////////////////////////////////////////////

stat, fstat, lstat

Specification:

#include<sys/stat.h>

int stat (const char *restrict pathname, struct stat *restrict buf);

int fstat(int filedes, struct stat *buf);

int lstat(const char *restrict pathname, struct stat *buf);

Return:

All three return: 0 if OK, –1 on error.

Comment:

Given a pathname, the stat function returns a structure of information about the named file.

The fstat function obtains information about the file that is already open on the descriptor filedes.

The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link.

The returned information is filled in the structure pointed by buf. See 读书笔记 – APUE2e Chap4(1) for detail about struct stat.

 

/////////////////////////////////////////////

access

Specification:

#include<unistd.h>

int access(const char *pathname, int mode);

Return:

0 if OK, –1 on error.

Comment:

When we open a file, the kernel performs its access tests based on the effective user and group IDs. access allows testing the file accessibility based on the real user and group IDs. Similar strategies is used in the testing (Four steps). See 读书笔记 – APUE2e Chap4(1) File Access Test (replace effective with real in these four steps.)

The mode is bitwise OR of any of the following constants:

  • R_OK: test for read permission
  • W_OK: test for write permission
  • X_OK: test for execute permission
  • F_OK: test for existence of file

Read more…

0522之一周瞎扯

May 22nd, 2011 2 comments

暑假生活的第一周。Jardine车少了,不用再烦找停车位的问题。学校里人少了很多,开车去系里的路上只见到若干人和几辆其他的车,或许我出门的时间太晚了。系里业几乎见不到其他学生,basement就更不用说了。我只在meeting的时候才能在main office见到些人,小蜜们。就这样,周遭顿时安静下来,只剩下夏天的风,摇摆着那些已经茂盛的树枝。去锻炼的路上,经过垃圾箱时,偶尔能看到胡乱摆放的各种家具,毕业的季节,又有多少人离开了。这段时间不是很热,正是享受Manhattan夏天的好时段。风,树,云,蓝天,这一切美得像画卷。虽已在这呆了三年,早已看惯这些景致,但我仍然很享受这一切,真如处于画中,而我俨然成了画中人,留个背影什么的。嗯,背影就好。

周二的时候出成绩了,CIS722得了A,总算没白辛苦。本来说要补完地project没补,虎头蛇尾的本性露出来了。也罢,现在确实提不起兴致倒回去折腾Minix。倒是在读Unix操作系统设计时,经常会回想起课上学过的东西,然后把Unix和Minix进行对照,似乎这样读起来更能记下东西,学过的东西的有用性就体现出来了。要是过了很久再回想学过的,八成都已经忘了,然后读Unix书的时候,也会想起Minix,但因为时间已久远,想不出个所以然来,最终导致更多的郁闷。所以,所以,一定要趁热打铁。

CPSNET,我还在Computation Layer上晃着,不断得细化每个component,定义新的data structure,message type,function。还要考虑好这些都在什么情况下会使用到。对于函数,往往是在自己的脑子里,从系统启动开始模拟一下可能的message flow,紧接着是function flow。越是深入,发现我要写的要考虑的东西越多。meeting时和老板说,要考虑的东西太多了,大脑不顶用,老板笑了笑。Anyway,现在要趁着有灵感有idea的时候,尽可能多地深入地考虑问题,涵盖各方面细节。这个framework,我从零开始,做完理论做实践,感觉还是不错的,有点像带孩子,虽然没带过孩子,囧。程序是用C++写的,每当遇到不熟悉的,就翻C++ Primer,再学习。感觉之前学的C++真是shit,就不评价本科学校里学的C++课了,人挫不能怪学校,自个的问题。此外,我也会花时间看C++ Primer,从头开始,看那些不熟悉的,一看到第7章。

Unix操作系统设计,已经开始看有关process的内容了,这周看了第六章《The Structure of Processes》。APUE2e这周看第4章《File and Directories》,因为之前看过一遍,现在看比较快,重点是把内容彻底弄懂。有时候会写一些读书笔记,把书上重要的内容摘抄下来,方便阅读和记忆。

最近彻底晚睡晚起了,都是凌晨三点左右才睡,早上睡到十一二点。我也不纠结了,保证睡眠长度就行。暑假有个目标,减肥。现在又130斤,我想减到125斤,最好能把肚子上的肉给减掉,我看着都烦,sigh。每周会去几次gym,打篮球,练器械。之前运动一次休息一周,而且运动后的第二天浑身酸疼。现在运动得频繁了,倒没有这样的感觉了,身体适应这样的节奏了。俺要再接再厉,减减减。

NBA季后赛继续着,已经到了东西部决赛了。偶支持的Heat很有希望。Go Heat!淘汰公牛。

周五傍晚去钓鱼了,蹲了几个小时,中间只有一次鱼上钩,在我往岸上拉的时候,鱼脱钩了,可惜。今年还没破零记录呢。最近鱼似乎也不多,看大家都没什么收获。不够给力啊,看别的地方都钓得很high。而这里,都夏天了还没大动静,更别说春季了。继续等。

Categories: 博士五年 Tags: , , , , ,

读书笔记 – APUE2e Chap4(1)

May 19th, 2011 No comments

STAT

struct stat{

mode_t st_mode;  //file types and mode (permissions)

ino_t st_ino;  //i-node number (serial number)

dev_t st_dev;   //device number (file system)

dev_t st_rdev;  //device number for special files

nlink_t st_nlink;  //number of links

uid_t st_uid;  //user id of owner

gid_t st_gid;  //group id of owner

off_t st_size;   //size in bytes, for regular files

time_t st_atime;  //time of last access

time_t st_mtime;  //time of last modification

time_t st_ctime  //time of last file status change

blksize_t st_blksize;  //best I/O block size

blkcnt_t st_blocks;   //number of disk blocks allocated

}

//////////////////////////////////////////////////////////////////

File Types

Regular File: The most common type of file, which contains data of some form. There is no distinction to the UNIX kernel whether this data is text or binary.

Directory File: A file that contains the names of other files and pointers to information on these files.

Block Special File: A type of file providing buffered I/O access in fixed size units to devices such as disk drives.

Character Special File: A type of file providing unbuffered I/O access in variable-sized units to devices. (All devices on a system are either block special files or character special files.)

FIFO: A type of file used for communication between processes. It is sometimes called a named pipe.

Socket: A type of file used for network communication between processes.

Symbolic Link: A type of file that points to another file.

Read more…

读书笔记 – APUE2e Chap3(2)

May 14th, 2011 No comments

PREAD

Header: #include <unistd.h>

Declaration: ssize_t pread(int filedes, void *buf, size_t nbytes, off_t offset);

Return: number of bytes read, 0 if end of file, –1 on error

Parameters:

filedes: file descriptor

buf: buffer to store the contents that have been read

nbytes: number of bytes to be read

offset: offset from the start of the file

Comments:

Equivalent to: lseek+read, except that pread is atomic.

Reads up to nbytes bytes from file descriptor filedes at offset offset (from the start of the file) into the buffer starting at buf. The file offset is not changed.

The file referenced by filedes must be capable of seeking.

Read more…

读书笔记 – APUE2e Chap3(1)

May 13th, 2011 No comments

**********************************************

Unbuffered I/O (File I/O): each read or write invokes a system call in the kernel. Part of POSIX.1 and the Single Unix Specification, but not part of ISO C. Including open, read, write, lseek, close. (Different from Standard I/O)

**********************************************

file descriptor (say, filedes): 0<= filedes <= OPEN_MAX

0 (STDIN_FILENO in POSIX): standard input

1 (STDOUT_FILENO in POSIX): standard output

2 (STDERR_FILENO in POSIX): standard error

Read more…

APUE2e编程环境搭建

April 15th, 2011 1 comment

APUE2e书于上周末看完了,不容易啊。这学期是狠了心看的,就怕一直拖着到毕业也看不完。算一下,断断续续,看了一年多的时间。不过我只是看,并没有把书上的代码都敲一遍,虽然代码都仔细读了,但还不够。于是想装个linux练一下,导致我这周基本上都在双系统安装上折腾。Ubuntu 10.10之前作为client os装在Win7上,像应用程序一样,没有问题。但这次我想单独安装,毕竟像应用程序一样安装在Windows上,一个不小心就可以把它卸载了,上面的东西一下就没了。单独安装,问题出现了一堆。Ubuntu 10.10在我的Thinkpad W500安装了三次,都以失败告终。这里的失败不是完全的失败,系统第一次可以启动,虽然会报错,但能进入图形界面。第二次就不行了,进了命令行模式。想着可能安装过程出了问题,于是重装。就这样装了三次,而且每次都会尝试着找些解决办法。试过安装图形界面包,试过修复所有的package,试过设置BIOS里的显卡,都不work。我记得之前也装过双系统的,也是Ubuntu,那时候都没有问题。也许是版本不行,于是找了10.04的安装,可以了。接着一番折腾,总算把系统基本设置好了。

回到正题。APUE2e的编译问题,我在之前的一篇文章说过。如果想自己练一遍所有代码,那得搭个环境,省去些麻烦。APUE2e的大部分程序里都用到了头文件apue.h,这是书上自带的,linux的库函数里没有这个。此外,还有两个关于错误输出的文件,error.c和errorlog.c,都是APUE2e自己定义的,也无法在linux的库函数中找到。这两个文件在APUE2e源码解压之后得到的文件夹的lib子文件夹里。error.c定义了“error functions that output to standard error”,代码在书地附录B的Figure B.3上; errorlog.c定义了“error functions for daemons”,代码在书地附录B的Figure B.4上。两个文件里定义的函数在apue.h中都声明过了,而error.c和errorlog.c都有#include “apue.h”语句。编写C程序时,对于指定的头文件(include语句),一般在project当前文件夹(#include “xx.h”)和/usr/include中查找(#include <xx.h>)。为了方便,我就把apue.h,error.c和errorlog.c都放到了/usr/include底下,但要做一定的修改。具体如下:
Read more…

Categories: 技术乱弹 Tags: ,