in the example client1.c,client_fifo.c(inside directory fs)

to run the program,need to associa both files,no main functions in 

client1.c,client_fifo.c uses client1.c to read and write.To run 

client_fifo.c,needs 2 files:fifo.1 and fifo.2,so create these two files 

before running,because then unlink,so two files are removed.

run server first,gcc -o server server_fifo.c server1.c err_sys.c to create these

two files

(Unlink:call the unlink function to remove the special file)

to delete a file( count =0)

use ls -l show hard link count only,

ln filename defaultly create hard link,to unlink is to unlink hard link.

ln -s is to create soft link.

no matter close file descriptor before unlink or not,ls -l it won't have entry of special files after unlink

The benefits of using hard links in this scenario are:

  • I can have a file in two places without having to duplicate data on disk

  • Each Hard Link does not increase the number of inodes, this will help if this system processes a large amount of files

  • I can use the stat or find commands to identify the current number of hard links to a file to identify which files have finished processing

How do I determine the block size of an ext3 partition on Linux?

# tune2fs -l /dev/sda1 | grep -i 'block size'
Block size:               1024

Replace /dev/sda1 with the partition you want to check.

What is  block size in Linux?

A block is a sequence of bit or Bytes with a fixed length ie 512 bytes, 4kB, 8kB, 16kB, 32kB etc.

blockdev --getbsz partition


# blockdev --getbsz /dev/sda1 4096

So the block size of this file system is 4kB.


how to sequentially read and process files/records in linux

ps -aef | while read f1 f2 f3; do echo $f1 $f2 $f3 ; done

disk inodes and in-core inodes

Internal representation of a file is called an "inode" (contraction of term - index node) which contains all the required information and description of the file data and its layout on disk.

Inodes resides on the disk and the kernel reads them into an into memory which we can call as in-core inodes. 


头文件:#include <stdio.h>

定义函数:int fseek(FILE * stream, long offset, int whence);


1、参数stream 为已打开的文件指针,
2、参数offset 为根据参数whence 来移动读写位置的位移数。参数 whence 为下列其中一种:
    SEEK_SET 从距文件开头offset 位移量为新的读写位置. SEEK_CUR 以目前的读写位置往后增加offset 个位移量.
    SEEK_END 将读写位置指向文件尾后再增加offset 个位移量. 当whence 值为SEEK_CUR 或
    SEEK_END 时, 参数offset 允许负值的出现.

1) 欲将读写位置移动到文件开头时:fseek(FILE *stream, 0, SEEK_SET);
2) 欲将读写位置移动到文件尾时:fseek(FILE *stream, 0, 0SEEK_END);

返回值:当调用成功时则返回0, 若有错误则返回-1, errno 会存放错误代码.

附加说明:fseek()不像lseek()会返回读写位置, 因此必须使用ftell()来取得目前读写的位置.

#include <stdio.h>
    FILE * stream;
    long offset;
    fpos_t pos;
    stream = fopen("/etc/passwd", "r");
    fseek(stream, 5, SEEK_SET);
    printf("offset = %d\n", ftell(stream));
    fgetpos(stream, &pos);
    printf("offset = %d\n", pos);
    pos = 10;
    fsetpos(stream, &pos);
    printf("offset = %d\n", ftell(stream));

offset = 5
offset = 0

offset = 10

#include <fcntl.h>

int creat(const char *pathname, mode_t mode);

General description

The function call: creat(pathname,mode) is equivalent to the call:

open(pathname, O_CREAT|O_WRONLY|O_TRUNC, mode);

Thus the file named by pathname is created, unless it already exists. The file is then opened for writing only, and is truncated to zero length. See open() — Open a file for further information.

The mode argument specifies the file permission bits to be used in creating the file

#include <fcntl.h>
           int fd;
           mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
           char *pathname = "/tmp/file";
           fd = creat(pathname, mode);
 是将当前磁盘根路径(和当前进程和它们的子进程)更改到另一个根目录。当你更改根路径到另一个目录下时,你不能在那个目录外存取文件和使用命令。这个目录叫作 chroot jail。切换根目录通常为了系统维护,例如重装引导程序或者重置遗忘的密码。
detailed explanation:

Why can't I mount the same filesystem at multiple points, and why can't a mount-point inode reference count be > 1?

This isn't a direct answer, but you can get behavior similar to mounting in two places by using mount --bind

If mount() fails, errno is set to one of the following values.

  • [EACCES] A component of the path prefix denies search permission.

  • [EBUSY] path is currently mounted on, is someone's current working directory, or is otherwise busy.

  • [EBUSY] The file system associated with fs is currently mounted.

You get the first EBUSY when your question (1) applies because:

  • if the directory is already a mount point, you lose access to the previously mounted directory, which makes the prior mount irrelevant.

  • if the directory (say /some/where) is some process's current directory, you have a process with a different view of the contents of /some/where; newcomers see what's on the mounted file system, but the old processes see what was in the mounted-upon directory.

You get the second EBUSY to answer your question (2) when the file system is already mounted - in other words, you can't mount it twice. This is a good thing - there would be a dreadful danger of confusion if two separate mount points both went around assuming they had exclusive access to the superblock etc when it was in fact shared. It would also be confusing if creating a file /some/where/newfile also simultaneously created /opt/other/newfile because the same device was mounted on both /some/where and /opt/other.