linux sync函数,linux sync命令到底做了什么

很久前保存的一片文章, 不清楚具体出处了.

/* sync - update the super block

Copyright (C) 1994-2004 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; either version 2, or (at your option)

any later version.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

You should have received a copy of the GNU General Public License

along with this program; if not, write to the Free Software Foundation,

Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */

/* Written by Jim Meyering */

#include

#include

#include

#include

#include "system.h"

#include "error.h"

#include "long-options.h"

/* The official name of this program (e.g., no `g' prefix). */

#define PROGRAM_NAME "sync"

#define AUTHORS "Jim Meyering"

/* The name this program was run with. */

char *program_name;

void

usage (int status)

{

if (status != EXIT_SUCCESS) //EXIT_SUCCESS是个宏定义,在linux系统中被定义成0,代表成功退出状态

fprintf (stderr, _("Try `%s --help' for more information.\n"),

program_name);//fprintf接受的第一个参数是输出的目的地,这是个FILE * 类型的流

//后面的参数是格式化输出,类似printf了,不知道这个加了下划线和括号的语法是何物?

//第三个参数是对应%s的

else //如果usage接收到的整型类型形参status等于宏EXIT_SUCCESS,则执行下面的语句块

{

//这段语句块也是一系列的输出,解释一下fputs吧,第一个参数是指向字符类型的指针,第二个参数是目的地流

printf (_("Usage: %s [OPTION]\n"), program_name);//输出类似: Usage: sync [OPTION]

fputs (_("\

Force changed blocks to disk, update the super block.\n\

\n\

"), stdout);//向标准输出,输出一串字符串

fputs (HELP_OPTION_DESCRIPTION, stdout);//是在System.h中定义的宏,其值为:

// _(" --help display this help and exit\n")

fputs (VERSION_OPTION_DESCRIPTION, stdout);//同上,也是个宏

printf (_("\nReport bugs to .\n"), PACKAGE_BUGREPORT);//PACKAGE_BUGREPORT这个宏定义没有找到,估计是在lib下面的头文件里定义的。

}

exit (status); //最后函数的返回值是传入的形参status的值

}

int

main (int argc, char **argv) //标准的main函数

{

initialize_main (&argc, &argv);//应该是为了兼容posix标准而调的在System.h里面定义的一个宏,我查看了预编译后的实际代码,

//结果是个空语句,说明其实没有什么用处,估计开源的程序都这样写,而在非linux平台,像System.h里面的initialize_main宏,应该有定义。

program_name = argv[0];//给全局字符指针变量program_name赋值,我看很多程序都这样做,估计这应该是标准做法

setlocale (LC_ALL, "");//意思是将整个locale设置为实现相关的本地环境,有点拗口,我理解就是恢复locale为本地的默认环境

bindtextdomain (PACKAGE, LOCALEDIR);

textdomain (PACKAGE);//这两句也是在System.h里面定义的宏,预编译后也是空语句,标准做法

atexit (close_stdout);//当这个程序(main函数)正常结束后,close_stdout被调用,看看man,对atexit等函数解释的相当到位了,不过我没有找到close_stdout的定义

parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,

usage, AUTHORS, (char const *) NULL);

/*

parse_long_options函数在lib目录下的long-options.h头文件中被定义,估计这也是个标准的做法

*/

if (getopt_long (argc, argv, "", NULL, NULL) != -1)

/*

getopt_long函数是在头文件getopt.h中定义的,如果没有参数,则函数返回-1

*/

usage (EXIT_FAILURE);//没有参数,打印帮助,提示看--help

if (optind < argc)//optind是传递给main函数的argv里的第一个不是选项参数的下标,如果这个下标比命令行参数个数还小

error (0, 0, _("ignoring all arguments"));//则调用error函数,传递了3个参数

sync ();//调用函数sync

/*

看到这里,说实话挺失望的,本来以为能看到具体怎么把块进行刷新到磁盘上的呐,结果这个程序

竟然是调用是系统调用sync,查也证实了这一点,apue的第62页写道:

命令sync(1)也调用sync函数。

值得注意的是sync函数只是将所有修改过的块缓冲区排入写队列,然后它就返回,它并不等待实际写

磁盘操作结束,幸运的是,通常成为update的系统守护进程会周期(30s)调用sync函数,这就保证了

定期冲洗内核的块缓冲区,所以我们在linux上更新一个文件后,不要着急重启服务器,最好等待

实际的磁盘写操作完成,避免数据丢失。

*/

exit (EXIT_SUCCESS);//最后返回成功执行的状态码。

}

从21点开始看,用了竟然是1个半小时才看完这100行的程序,查阅了《c和指针》《apue》和google,查setlocale、sync系统调用,

EXIT_SUCCESS宏等,量虽然少了点,总算是看了一个我经常使用的命令的源码了,呵呵。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值