简体   繁体   English

使用C库进行系统调用

[英]system calls using c library

Generally, systems provide a library or API that sits between normal programs and the operating system. 通常,系统提供位于普通程序和操作系统之间的库或API。 On Unix-like systems, that API is usually part of an implementation of the C library (libc), such as glibc, that provides wrapper functions for the system calls.Functions like write(),read(),open().. are used to make system calls from a C program.Does it mean that if a java program has to make a system call then at the lowest level it has to call these C library functions ?If so, then how..??? 在类似Unix的系统上,该API通常是C库(libc)的实现的一部分,例如glibc,该库为系统调用提供包装函数。例如write(),read(),open()之类的函数。用于从C程序进行系统调用。这是否意味着如果Java程序必须进行系统调用,则必须在最低级别上调用这些C库函数?

If you really need to you can do that with the Java Native Interface (JNI). 如果确实需要,可以使用Java本机接口(JNI)来实现。

Beware it is not for the feint of heart; 当心,这不是为了假冒。 the JVM usually can do what you want. JVM通常可以执行您想要的操作。

One use-case of JNI is using OpenSSL for crypto in Java; JNI的一个用例是使用OpenSSL进行Java加密。 this used to be substantially faster when I tested it (it was nearly 10 years ago). 当我对其进行测试时(过去将近10年前),它的速度通常要快得多。

http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

I assume you don't want to know how to do it in Java since the question is not tagged as a java question. 我假设您不想知道如何在Java中执行此操作,因为该问题未标记为Java问题。

So the libc is the right way to do it but, for information purposes, in linux, you can use the syscall function. 因此,libc是执行此操作的正确方法,但出于参考目的,在linux中,可以使用syscall函数。 The syscall function can use an Int 0x80 signal, a sysenter instruction or something else depending on the platform. syscall函数可以使用Int 0x80信号,sysenter指令或其他东西,具体取决于平台。

Introduction to System Calls in Linux Linux中系统调用简介

In Java, this is done using native Methods . 在Java中,这是使用本机方法完成的。 These are methods declared with the native modifier and (similarly to abstract methods) no body, whose real implementation is written in a platform-dependent language, usually C. 这些是用本native修饰符声明的方法,(与抽象方法类似)没有主体,其实际实现是使用依赖于平台的语言(通常是C)编写的。

Additional native method implementations may be runtime loaded using System.loadLibrary(String libraryName) . 可以使用System.loadLibrary(String libraryName)在运行时加载其他本机方法实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM