我如何检查我的包装功能的系统调用 - 阅读()是正确的? [英] How can I check if my wrapper function system call - read() is correct?

查看:146
本文介绍了我如何检查我的包装功能的系统调用 - 阅读()是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/10260178/inline-assembler-for-wrapper-function-doesnt-work-for-some-reason\">Inline汇编程序的包装功能不适用于某些原因

我要求写包装函数为读,写,关闭,开放和放大器;叉

我已经写了4包装功能读,写,关闭,打开

I've written 4 wrapper functions for read , write , close , open .

我的问题是:


  1. 我怎么能写包装函数为,使用,我写了4包装函数读,写,关闭和放大器;打开

  1. How can I write wrapper function for fork , using the 4 wrapper functions that I wrote for read , write , close & open ?

我如何检查,如果我写的包装是正确的?这里是code为的包装功能 - 名为 my_read

How can I check if the wrapper that I wrote is correct ? Here is the code for the wrapper function of read - called my_read :

ssize_t my_read(int fd, void *buf, size_t count)   
{    
      ssize_t res;

      __asm__ volatile(
        "int $0x80"        /* make the request to the OS */
        : "=a" (res),       /* return result in eax ("a") */
          "+b" (fd),     /* pass arg1 in ebx ("b") */
          "+c" (buf),     /* pass arg2 in ecx ("c") */
          "+d" (count)      /* pass arg3 in edx ("d") */
        : "a"  (5)          /* passing the system call for read to %eax , with call number 5  */
        : "memory", "cc"); 

      /* The operating system will return a negative value on error;
       * wrappers return -1 on error and set the errno global variable */

      if (-125 <= res && res < 0)
      {
        errno = -res;
        res   = -1;
      }

      return res;
}

备注:我不能直接使用打开,关闭,读,写和放大器;叉命令。

Remarks : I'm not allowed to use directly the open ,close ,read , write & fork commands .

我可以附加其他3包装的code的休息,如果需要的话。以上是为包装

I can attach the rest of the code of the other 3 wrappers , if it is needed . The above is the wrapper for read.

问候

罗恩

推荐答案

叉应该是系统调用2,使

Fork should be system call 2, so

    __asm__ volatile ("int $0x80" : "=a" (res) : "0" (2)); 

应该工作。请记住,fork函数返回两次,用 RES 作为子进程的PID(父)和0(在孩子)。

should work. Remember, fork returns twice, with res being the pid of the child(in the parent) and 0(in the child).

这篇关于我如何检查我的包装功能的系统调用 - 阅读()是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆