3

重複の可能性:
ラッパー関数のインライン アセンブラーが何らかの理由で機能しない

のラッパー関数を書いてほしいと頼まれましたread , write , close , open & fork

の 4 つのラッパー関数を作成しましたread , write , close , open

私の質問は次のとおりです。

  1. fork用に書いた 4 つのラッパー関数を使用して、用のラッパー関数を作成するにはどうすればよいread , write , close & openですか?

  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

よろしく

ロン

4

1 に答える 1

0

fork はシステムコール 2 である必要があるため、

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

動作するはずです。fork はres子 (親) の pid と 0 (子) の pid を 2 回返します。

于 2012-05-04T11:45:16.257 に答える