如何检查我的包装函数系统调用 - read()是否正确?

时间:2012-04-20 20:06:45

标签: c linux system-calls

  

可能重复:
  Inline Assembler for wrapper function doesn’t work for some reason

我被要求为read , write , close , open & fork编写包装函数。

我为read , write , close , open编写了4个包装函数。

我的问题是:

  1. 如何使用我为fork编写的4个包装函数为read , write , close & open编写包装函数?

  2. 如何检查我写的包装是否正确?以下是read的包装函数的代码 - 名为my_read

  3. 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;
    }
    

    备注:我不允许直接使用open ,close ,read , write & fork命令。

    如果需要,我可以附加其他3个包装器的其余代码。以上是read的包装。

    问候

    罗恩

1 个答案:

答案 0 :(得分:0)

Fork应该是系统调用2,所以

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

应该有效。请记住,fork返回两次,res是孩子的pid(在父母中)和0(在孩子中)。

相关问题