问题
Possible Duplicate:
Inline Assembler for wrapper function doesn’t work for some reason
I'm requested to write wrapper function for read , write , close , open & fork
.
I've written 4 wrapper functions for read , write , close , open
.
My questions are :
How can I write wrapper function for
fork
, using the 4 wrapper functions that I wrote forread , write , close & open
?How can I check if the wrapper that I wrote is correct ? Here is the code for the wrapper function of
read
- calledmy_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 .
I can attach the rest of the code of the other 3 wrappers , if it is needed . The above is the wrapper for read
.
Regards
Ron
回答1:
Fork should be system call 2, so
__asm__ volatile ("int $0x80" : "=a" (res) : "0" (2));
should work. Remember, fork returns twice, with res
being the pid of the child(in the parent) and 0(in the child).
来源:https://stackoverflow.com/questions/10252848/how-can-i-check-if-my-wrapper-function-system-call-read-is-correct