1

Generally, systems provide a library or API that sits between normal programs and the operating system.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. Now C programs can call these library functions as these library functions are written in C and make a system call. How does a language like Cobol or any other compiler based language will make a System call on linux ? These languages cannot call the API provided by the system.

saurav1405
  • 399
  • 3
  • 9
  • 2
    They are happy with just using libc. Other not-so-clever runtimes may go through their own kernel version support hell. – user3125367 Oct 17 '14 at 12:32
  • 1
    "These languages cannot call the API provided by the system." Why not? – glglgl Oct 17 '14 at 12:36
  • 1
    How do they call libc functions ?.. – saurav1405 Oct 17 '14 at 12:37
  • The API is wrtten in C, how can i call a C function from other languages? – saurav1405 Oct 17 '14 at 12:39
  • 1
    [Java Native Interface](http://en.wikipedia.org/wiki/Java_Native_Interface), [Calling Native Functions from Managed Code](http://msdn.microsoft.com/en-us/library/ms235282.aspx), insert appropriate solution for your language here – Philip Kendall Oct 17 '14 at 12:41
  • @ philip: I can call the C library functions that invoke the system calls using foreign function call interface. – saurav1405 Oct 17 '14 at 12:50
  • @saurav1405 Maybe you can't call other languages. But the people who wrote the implementation of a language (like Cobol) and the runtime/standard library for that language can - as they can go under the hood of the language and do stuff you can't do in the language itself. However, many languages do provide a way to call other languages. – nos Oct 17 '14 at 13:46
  • @nos Thanks. You cleared my doubts. – saurav1405 Oct 17 '14 at 14:12

2 Answers2

0

Compiled languages are translated (compiled) into machine language. The operating system, during execution, reads the machine language and makes appropriate system calls.

So, I wouldn't say it's C, or any compiled language for that matter, which makes the system call. The operating system executes the system call according to the machine language generated by your compiler.

  • It's not quite that simple - you have to understand the ABI in order to know *how* to make those calls. – Philip Kendall Oct 17 '14 at 12:40
  • The compiler must understand the ABI to do that ? – saurav1405 Oct 17 '14 at 12:45
  • 1
    @saurav1405 Yes. The people who wrote the compiler/interpreter and/or the runtime libraries must understand the system call ABI of the platform the compiler runs on and which system calls they want the language to call on a particular platform. – nos Oct 17 '14 at 13:50
0

An example might help: if I compile a simple Fortran program like this with gfortran:

  PROGRAM HELLO

  WRITE(*,*) "HELLO WORLD"

  END

I get code looking like this (on x86-64):

000000000040079d <MAIN__>:
  40079d:       55                      push   %rbp
  40079e:       48 89 e5                mov    %rsp,%rbp
  4007a1:       48 81 ec e0 01 00 00    sub    $0x1e0,%rsp
  4007a8:       48 c7 85 28 fe ff ff    movq   $0x4008e0,-0x1d8(%rbp)
  4007af:       e0 08 40 00 
  4007b3:       c7 85 30 fe ff ff 03    movl   $0x3,-0x1d0(%rbp)
  4007ba:       00 00 00 
  4007bd:       c7 85 20 fe ff ff 80    movl   $0x80,-0x1e0(%rbp)
  4007c4:       00 00 00 
  4007c7:       c7 85 24 fe ff ff 06    movl   $0x6,-0x1dc(%rbp)
  4007ce:       00 00 00 
  4007d1:       48 8d 85 20 fe ff ff    lea    -0x1e0(%rbp),%rax
  4007d8:       48 89 c7                mov    %rax,%rdi
  4007db:       e8 c0 fe ff ff          callq  4006a0 <_gfortran_st_write@plt>
  4007e0:       48 8d 85 20 fe ff ff    lea    -0x1e0(%rbp),%rax
  4007e7:       ba 0b 00 00 00          mov    $0xb,%edx
  4007ec:       be e7 08 40 00          mov    $0x4008e7,%esi
  4007f1:       48 89 c7                mov    %rax,%rdi
  4007f4:       e8 57 fe ff ff          callq  400650 <_gfortran_transfer_character_write@plt>
  4007f9:       48 8d 85 20 fe ff ff    lea    -0x1e0(%rbp),%rax
  400800:       48 89 c7                mov    %rax,%rdi
  400803:       e8 78 fe ff ff          callq  400680 <_gfortran_st_write_done@plt>
  400808:       c9                      leaveq 
  400809:       c3                      retq   

In there you can see three calls to gfortran library functions (e.g. gfortran_transfer_character_write). It looks like gfortran inserts these to implement the WRITE statement. The library functions are implemented in C and will eventually call the underlying system calls through the C library wrappers.

When the compiler constructs the calls to these functions, it has to follow the C calling conventions and perform any conversions to/from the calling language's data types. Other than that, there is no magic here. Once you're in the runtime library, you can call into other C libraries (like libc).

benj
  • 713
  • 6
  • 12
  • @ benj Thanks a lot. I understand how a language would make use of libc for invoking system calls but How can system calls be called directly i.e without using libc ? Do i need to write assembly code for that ? – saurav1405 Oct 17 '14 at 14:08
  • http://stackoverflow.com/questions/13219501/accessing-a-system-call-directly-from-user-program might help with that – benj Oct 20 '14 at 13:50