18

Let's say there is a simple program like:

#include<stdio.h>

void main() 
{ 
    int x;
    printf("Cool");
    fd = open("/tmp/cool.txt", O_READONLY)
}

The open is a system call here. I suppose when the shell runs it, it makes some hundred other system calls to implement it? How about a declaration like int x - at some point should it have some additional system calls in the backdrop to get the memory from the computer?

I am not sure what is the boundary between a system call and a normal stuff ... everything, in the end, needs the operating system's help right?!

Or is it like the C generates an executable (code) which can be run on the processor and need no OS assistance is needed until a system call is reached - at which point it has to do something to load the OS instructions etc ...

A bit vague :) Please clarify.

5
  • Idk if this will help at all, but: "Software <-> (e.g. system calls) OS/kernel <- Drivers -> Hardware". Sometime the lines are blurred between OS (e.g. does it include UI, standard support tools?) and kernel ("the heart" of the OS) and the Drivers (e.g. are drivers part of the kernel?). The parts leftward need the parts rightward. E.g. use C API -> perform action on Filesystem -> talk to IDE device, ...
    – user166390
    Apr 6, 2011 at 18:06
  • Thanks . My question is to reduce abstractions of kernel etc as much as possible . So lets say there is a Executable - called new ( created using gcc new.c new) . I use a tool like hexdump new which gives like 0000 01E5 A000 etc , is this the INTEL CPU code ?
    – Nishant
    Apr 6, 2011 at 18:19
  • "Maybe". C and C++ (typically) are compiled to machine code. However, the exact data being looked at in hexdump might not be related to a CPU instruction at all -- for instance, it could be other non-executed data in the executable file such as the COFF header or string data. However, this code will use the C run-time and make numerous system calls "just to run". That is, the CPU can execute the machine code in the executable -- but only in the context it was compiled for.
    – user166390
    Apr 6, 2011 at 23:38
  • Some embedded/micro C compilers (such as RabbitC or NesC or Charm++) build a single executable that is run directly (no "OS" to speak of and any require run-time is build into the resultant executable). A micro/specialized OS/library may be bundled inside the executable, but it is not a requirement. (In these environments, many of the C "standard" library functions are often omitted or replaced.)
    – user166390
    Apr 6, 2011 at 23:44
  • As far as "how" a system call is often made, see Stackoverflow: Internals of a Linux system call and Wikipedia: System Call
    – user166390
    Apr 6, 2011 at 23:53

1 Answer 1

37

I'm not answering the questions in order, so I'm prefixing my answers with the questions. I've taken the liberty of editing them a bit. You didn't specify the processor architecture, but I'm assuming you want to know about x86, so the processor-level details will pertain to x86. Other architectures can behave differently (memory management, how system calls are made, etc.). I'm also using Linux for examples.

Does the c compiler generate executable code that can be run straight on the processor without need for OS assistance until a system call is reached, at which point it has to do something to load the OS instructions?

Yes, that is correct. The compiler generates native machine code that can be run straight on the processor. The executable files that you get from the compiler, however, contain both the code and other needed data, for example, instructions on where to load the code in the memory. On Linux the ELF format is typically used for executables.

If the process is completely loaded into memory and has sufficient stack space, it will not need further OS assistance before it wants to make a system call. When you make a system call, it is just an instruction in the machine code that calls the OS. The program itself does not need to "load the OS instructions" in any way. The processor handles transferring execution to the OS code.

With Linux on the x86 architecture, one way for the machine code to make a system call is to use the software interrupt vector 128 to transfer execution to the operating system. In x86 assembly (Intel syntax), that is expressed as int 0x80. Linux will then perform tasks based on the values that the calling program placed into processor registers before making the system call: the system call number is found in the eax processor register and the system call parameters are found in other processor registers. After the OS is done, it will return a result in the eax register, and has possibly modified buffers pointed to by the system call parameters etc. Note however, that this is not the only way to make a system call.

However, if the process is not entirely in memory, and execution moves to a part of the code that is not in memory at the moment, the processor causes a page fault, which moves execution to the operating system, which then loads the required part of the process into memory and transfers execution back to the process, which can then continue execution normally, without even noticing that anything happened.

I'm not entirely sure on the next point, so take it with a grain of salt. The Wikipedia article on stack overflow (the computer error, not this site :) seems to indicate that stacks are usually of fixed size, so int x; should not cause the OS to run, unless that part of the stack is not in the memory (see previous paragraph). If you had a system with dynamic stack size (if it is even possible, but as far as I can see, it is), int x; could also cause a page fault when the stack space is used up, prompting the operating system to allocate more stack space for the process.

Page faults cause the execution to move to the operating system, but are not system calls in the usual sense of the word. System calls are explicit calls to the OS when you want it to perform some work for you. Page faults and other such events are implicit. Hardware interrupts continuously transfer the execution from your process to the OS so that it can react to them. After that it transfers the execution back to your process, or some other process.

On a multitasking OS, you can run many programs at once even if you have only one processor/core. This is accomplished by running only one program at a time, but switching between programs quickly. The hardware timer interrupt makes sure that control is transferred back to the OS in a timely fashion, so that one process can't hog the CPU all for itself. When control is passed to the OS and it has done what it needs to, it may always start a different process from the one that was interrupted. The OS handles all this totally transparently, so you don't have to think about it, and your process won't notice it. From the viewpoint of your process, it is executing continuously.

In short: Your program executes system calls only when you explicitly ask it to. The operating system may also swap parts of your process in and out of the memory when it wants to, and generally does things related and unrelated to your process in the background, but you don't normally need to think about that at all. (You can reduce the amount of page faults, though, by keeping your program as small as possible, and things like that)

In this case open() is an explicit system call, but I suppose when the shell runs it, it makes some hundred other system calls to implement it.

No, the shell has got nothing to do with an open() call in your c program. Your program makes that one system call, and shell doesn't come into the picture at all.

The shell will only affect your program when it starts it. When you start your program with the shell, the shell does a fork system call to fork off a second process, which then does an execve system call to replace itself with your program. After that, your program is in control. Before the control gets to your main() function though, it executes some initialization code, that was put there by the compiler. If you want to see what system calls a process makes, on Linux you can use strace to view them. Just say strace ls, for example, to see what system calls ls makes during its execution. If you compile a c program with just a main() function that returns immediately, you can see with strace what system calls the initialization code makes.

How does the process get its memory from the computer etc.? It has to involve some system calls again right? I am not sure what is the boundary between a system call and normal stuff. Everything in the end needs the OS help, right?

Yep, system calls. When your program is loaded into memory with the execve system call, it takes care of getting enough memory for your process. When you need more memory and call malloc(), it will make a brk system call to grow the data segment of your process if it has run out of internally cached memory to give you.

Not everything needs explicit help from the OS. If you have enough memory, have all your input in memory, and you write your output data to memory, you won't need the OS at all. That is, as long as you only do calculations on data you already have in memory, don't need more memory, and don't need to communicate with the outside world, you don't need the OS. On the other hand, a program that does not communicate with the outside world at all is a pretty useless one, because it can't get any input, and cannot give any output. Even if you calculate the millionth decimal of pi, it doesn't matter at all if you don't output it to the user.

This answer got quite big, so in case I missed something or didn't explain something clearly enough, please leave me a comment and I'll try to elaborate. If anyone spots any mistakes, be sure to point them out also.

4
  • Thanks for the great explanation . I will read it up and post commnents , if I need more clarity . Picturing the whole thing is always difficult for me but will try to get some idea and ask if something still remains unclear .
    – Nishant
    Apr 10, 2011 at 8:42
  • Nice reading your answer.. But " When you need more memory and call malloc(), it will make a brk system call to grow the data segment" .. Shouldnt that be heap segment?
    – Laz
    Mar 23, 2013 at 14:40
  • @RamBhat: Well, no, there really isn't such a thing as "heap segment", at least on x86. The heap lives in the data segment, though, and malloc() normally allocates memory from the heap, growing the data segment (and the heap) when necessary, so in that sense you are correct. (Also, sorry it took me so long to get back to you) Feb 12, 2014 at 21:40
  • Just nitpicking here: Depending on the malloc() implementation, it won't be calling brk() at all, it'll rather be calling mmap() instead. mmap() is just way more flexible. When I write an allocator, I just completely forget about brk()'s existence. Nov 4, 2017 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.