2012-04-20 36 views
3

可能重复:
Inline Assembler for wrapper function doesn’t work for some reason如何检查我的包装函数系统调用 - read()是否正确?

我要求写的包装功能read , write , close , open & fork。我写了4个包装函数read , write , close , open

我的问题是:

  1. 我怎么能写fork包装功能,使用我写的read , write , close & open 4包装的功能呢?

  2. 如何检查我写的包装是否正确?下面是的read包装函数的代码 - 名为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; 
} 

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

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

问候

罗恩

回答

0

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

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

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