LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 528|回复: 0

能否给一个可以通过编译的使用系统调用函数的例子?

[复制链接]
发表于 2007-4-29 21:53:43 | 显示全部楼层 |阅读模式
man 2 intro中的程序无法通过编译。

它的代码是

#include <stdio.h>
#include <errno.h>
#include <linux/unistd.h>     /* for _syscallX macros/related stuff */
#include <linux/kernel.h>     /* for struct sysinfo */

_syscall1(int, sysinfo, struct sysinfo *, info)

/* Note: if you copy directly from the nroff source, remember to
*        REMOVE the extra backslashes in the printf statement. */

int main(void)
{
      struct sysinfo s_info;
      int error;

      error = sysinfo(&s_info);
      printf("code error = %d\n", error);
      printf("Uptime = %lds\nLoad: 1 min %lu / 5 min %lu / 15 min %lu\n"
             "RAM: total %lu / free %lu / shared %lu\n"
             "Memory in buffers = %lu\nSwap: total %lu / free %lu\n"
             "Number of processes = %d\n",
             s_info.uptime, s_info.loads[0],
             s_info.loads[1], s_info.loads[2],
             s_info.totalram, s_info.freeram,
             s_info.sharedram, s_info.bufferram,
             s_info.totalswap, s_info.freeswap,
             s_info.procs);
       return(0);
}

我执行的是:

# gcc a.c
得到:
a.c:6: error: expected declaration specifiers or '...' before 'sysinfo'
a.c:6: error: expected declaration specifiers or '...' before 'info'
a.c: In function '_syscall1':
a.c:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
a.c:6: error: parameter name omitted
a.c:6: error: parameter name omitted
a.c:29: error: expected '{' at end of input

不知道如何能写一个正确的程序呢?

我的gcc版本如下:

chenxing:~# gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-tune=i686 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表