免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2091 | 回复: 7
打印 上一主题 下一主题

这段话,我不太理解,希望各位提醒一二 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-24 14:06 |只看该作者 |倒序浏览
"早期的系统调用都是用汇编语言提供的,只有在哟个汇编语言书写的程序中,才能直接使用系统调用;但在高级语言以及c语言中,往往提供了与各系统调用一一对应的库函数,这样应用程序便可通过调用对应的库函数来使用系统调用。但在后来推出的操作系统中,起系统调用本身本身已经采用c语言编写,并以函数调用的形式提供,故在c语言编制程序中,可直接使用系统调用"



我想问的问题是 :“什么是直接使用系统调用?”

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
2 [报告]
发表于 2010-09-24 14:54 |只看该作者
但在后来推出的操作系统中,起系统调用本身本身已经采用c语言编写,并以函数调用的形式提供,
[red]故在c语言编制程序中,可直接使用系统调用[/red]"
还是要通过C函数封装的,这句话不对。

对于你的问题:
直接使用系统调用

系统调用是通过Syscall指令实现的,X86系统使用INT指令“客串”Syscall指令。
比如,一个系统调用是这样的:
  movl 0,%eax
  int    0x40         //假设系统用40h实现系统调用

则相应的C的封装可能是:

  1. int syscall40(int function){
  2.      REGISTER r;
  3.      r.eax=0;
  4.      int86(40);
  5.      return r.eax;
  6. }
复制代码

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
3 [报告]
发表于 2010-09-24 19:18 |只看该作者
这段话本身就有问题,所谓的伪经典。C库中的系统调用只是个接口。系统调用本身并不在C库中实现,而在系统内核中。不管用什么语言实现系统调用,C语言都有能力来调用,只要系统权限允许。

论坛徽章:
0
4 [报告]
发表于 2010-09-24 22:47 |只看该作者
_SYSCALL(2)                   Linux Programmer's Manual                   _SYSCALL(2)

NAME         top

       _syscall - invoking a system call without library support (OBSOLETE)

SYNOPSIS         top

       #include <linux/unistd.h>

       A _syscall macro

       desired system call

DESCRIPTION         top

       The important thing to know about a system call is its prototype.  You need to
       know how many arguments, their types, and the function return type.  There are
       seven macros that make the actual call into the system easier.  They have the
       form:

              _syscallX(type,name,type1,arg1,type2,arg2,...)

       where

              X is 0-6, which are the number of arguments taken by the system call

              type is the return type of the system call

              name is the name of the system call

              typeN is the Nth argument's type

              argN is the name of the Nth argument

       These macros create a function called name with the arguments you specify.
       Once you include the _syscall() in your source file, you call the system call
       by name.

FILES         top

       /usr/include/linux/unistd.h

CONFORMING TO         top

       The use of these macros is Linux-specific, and deprecated.

NOTES         top

       Starting around kernel 2.6.18, the _syscall macros were removed from header
       files supplied to user space.  Use syscall(2) instead.  (Some architectures,
       notably ia64, never provided the _syscall macros; on those architectures,
       syscall(2) was always required.)

       The _syscall() macros do not produce a prototype.  You may have to create one,
       especially for C++ users.

       System calls are not required to return only positive or negative error codes.
       You need to read the source to be sure how it will return errors.  Usually, it
       is the negative of a standard error code, for example, -EPERM.  The _syscall()
       macros will return the result r of the system call when r is nonnegative, but
       will return -1 and set the variable errno to -r when r is negative.  For the
       error codes, see errno(3).

       When defining a system call, the argument types must be passed by-value or by-
       pointer (for aggregates like structs).

EXAMPLE         top

       #include <stdio.h>
       #include <stdlib.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);
           exit(EXIT_SUCCESS);
       }

Sample Output

       code error = 0
       uptime = 502034s
       Load: 1 min 13376 / 5 min 5504 / 15 min 1152
       RAM: total 15343616 / free 827392 / shared 8237056
       Memory in buffers = 5066752
       Swap: total 27881472 / free 24698880
       Number of processes = 40

SEE ALSO         top

       intro(2), syscall(2), errno(3)

COLOPHON         top

       This page is part of release 3.27 of the Linux man-pages project.  A
       description of the project, and information about reporting bugs, can be found
       at http://www.kernel.org/doc/man-pages/.


我觉得应该是指这个把

论坛徽章:
89
水瓶座
日期:2014-04-01 08:53:31天蝎座
日期:2014-04-01 08:53:53天秤座
日期:2014-04-01 08:54:02射手座
日期:2014-04-01 08:54:15子鼠
日期:2014-04-01 08:55:35辰龙
日期:2014-04-01 08:56:36未羊
日期:2014-04-01 08:56:27戌狗
日期:2014-04-01 08:56:13亥猪
日期:2014-04-01 08:56:02亥猪
日期:2014-04-08 08:38:58程序设计版块每日发帖之星
日期:2016-01-05 06:20:00程序设计版块每日发帖之星
日期:2016-01-07 06:20:00
5 [报告]
发表于 2010-09-24 23:37 |只看该作者
本帖最后由 fender0107401 于 2010-09-24 23:38 编辑
"早期的系统调用都是用汇编语言提供的,只有在哟个汇编语言书写的程序中,才能直接使用系统调用;但在高级语 ...
河边一支柳 发表于 2010-09-24 14:06



    哪本书写的?

论坛徽章:
0
6 [报告]
发表于 2010-09-25 08:36 |只看该作者
就直接使用函数呗

论坛徽章:
0
7 [报告]
发表于 2010-09-26 13:59 |只看该作者
谢谢楼上个位的热心解答,我很有收获,也看了一些资料。

论坛徽章:
0
8 [报告]
发表于 2010-09-26 23:42 |只看该作者
学究派的问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP