ksp0422
ksp0422

Reputation: 349

could anyone explain to me about system calls in programming languages?

I've started studying OS concepts a few days ago and I'm already having some problems. mainly I'm very curious about system calls. I have learned that each operating system provides each of it's own APIs (such as Windows for Windows API, Linux for libc etc.)

what I'm starting to get confused is wrapper functions. for example Linux has a fork() wrapper function. does this mean the algorithm inside this function execute a system call routine based on the OS' system call table? and I don't get what it means that it's written in C. Does it mean it's using a C standard library? or the just the C compiler? Additionally, how come C compilers compile it's standard library even though there are different kinds of compilers like GCC windows C compiler etc.? What I'm curious is that, C standard library functions also call a system call right? but it might have to call different system calls in order to get the same output in different OS right? Does this mean, even though it's the same standard C function, the system call implemented inside differs by each OS?

I really want a good concept of system call plz :)

Upvotes: 3

Views: 4287

Answers (3)

Srinivas Desai
Srinivas Desai

Reputation: 21

System calls and wrapper functions are two different concepts.

  1. System Calls: These are normal functions in kernel space. They cannot be accessed directly by user programs, i.e. functions written by people like you and me. System calls should only be accessed by kernel code. These functions will interact with devices or with main memory to provide the requested service to the user program.

  2. Wrapper functions: When you want to use a system call in your user code, you cannot call it directly in the way you could with functions in user space. Your processor needs to execute a trap machine instruction (interrupt) to switch into kernel mode. Once you switch to kernel mode to execute system functions, there should be no errors in the arguments that you passed to that function. A system call does not check its arguments for correctness, so if anything is wrong with the arguments it may lead to a kernel fault. If there is a fault in your user space program the kernel will just kill your process, but when there is a fault in the kernel your entire kernel goes down, which is unacceptable. Wrapper functions ensure that all the arguments are valid. They also set up the base to trap to the kernel if everything is fine.

Now to answer your questions:

  1. Wrapper function do not execute system call functions, they only check the correctness of arguments and set up the trap to kernel mode by passing the appropriate system call number. Your kernel contains a table of system calls by number and executes the requested function. Once the trap is invoked by the wrapper function, its job is over.

  2. Wrapper functions are regular user level programs which are written in C and partly in assembly. Yes, wrapper functions may call standard library functions.

  3. Standard library functions are written in C89, C99 versions and also older versions of C. It does not matter because newer versions of C are backwards compatible with older versions. So yes, library functions compile just as normal user code without problems with any version of C compilers.

  4. Most of the standard library functions are the same across multiple operating systems and compilers, but yes, there are some differences as well. The fork() wrapper function will not work in Visual Studio since Windows has a different approach.

Edit: fork() is not a system call, it is just a library function which internally traps to kernel

Upvotes: 0

Sean
Sean

Reputation: 62472

System calls are functionality exposed by the underlying operting system. There's usually a well defined way for for a language to issue a system call. For example, Intel x86 processors have a syscall instruction that is used, but you can also use other means, such as issuing an interrupt instruction.

When a system call is made there is a transition from user space to kernel space, which runs at a more privileged level. Arguments to the system call are copied into the kernel space and validated to ensure they don't compromise the kernel. However, it's highly likely that the parameters were validates in user space as well. In some cases the user space functions will be thin wrappers over the kernel functions, in other times they'll provide additional features on top.

C and Unix has a tight heritage, which is why a lot of the system call names are the same as their C versions (eg fork, execXXX). However, this is just a convention, and isn't the case everywhere. On Windows the name of system calls are completely different to their Linux counterparts. However, the underlying C runtime implementation on the platform hides this from you, and makes the correct system calls to achieve the functionality offered my the C function.

Typically higher level languages like Ruby don't issue system calls directly. Instead they're using libraries written in a lower level language (eg C or C++) that translate the Ruby call into calls to the underlying system functions.

Upvotes: 3

Jeff Li
Jeff Li

Reputation: 1120

I only know a little about Linux kernel, so what I will just take Linux kernel as an example in my answer. To understand system call you must understand the concept of kernel space and user space. System calls are the interfaces that user space applications communicate with the kernel. So implementation of system calls is part of the kernel.

First, Linux kernel is written mostly with C and partly with assembly language. But Linux kernel never uses any C standard library function. So in Linux, it is impossible to implement the system calls with C standard library function.

Second, Linux provides several hundred system calls. Some of them are written with C but some of them have to be implemented with assembly language. Not only programs written with C can make system call, most of other language such as Ruby, Golang can also do that. IMHO, it is not correct to say that system calls is written in C.

Third, different OSes provide different set of system calls. Even the same OS could run in different architectures such as x86, arm, etc. Different architecture means different instruction set. Thus even the same OS could also implement the same system call in different way. So I think your understanding about that is right.

Upvotes: 0

Related Questions