c++macosunixoperating-systemsystem-calls

How does a computer perform any instruction without making a system call


I am learning about Operating Systems and trying to get a deep understanding about the fundamentals about how a program works.

I have a simple C++ program

When I look at the basic architecture of Linux, it always shows User Space and Kernel space where the operating system accesses the hardware through device drivers.

So the way I see it any program i create needs to access the OS for every instruction if i am not mistaken especially when it is running in user space ?

But then I see this answer on another site: https://cseducators.stackexchange.com/a/3463/14110

which mentions things that are purely computational do not make system calls, but then how do these access CPU, RAM etc ??

My basic understanding tells me, everything should make a system call, but now I see enough info that not not everything is a system call, so how does basic arithmetic or any other instruction that isn't a system call work ?

I know i am not getting into too many lower level details. But in a nutshell, how does this work Please can someone explain?

Here is a diagram I created based on how I think it works, please feel free to correct:

My basic diagram of Linux architecture

Here is a simple C++ program that creates three variables in memory, takes two numbers as input, Adds the two numbers and places the value into the third and then displays the output:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char const *argv[])
{
    int x, y, z; // Create three variables and store them in memory, should make a system call
    printf("Enter first number: "); // Ask for User Input, I/O
    cin >> x; // I/O operation
    printf("Enter second number: ");
    cin >> y;
    z = x + y;
    printf("Total: %d \n", z);
    return 0;
}

Doesn't each of these instructions require system calls implicit or explicit ?

I read related questions,

The program works perfectly, but I want to know about system calls


Solution

  • The kernel allocates blocks (pages) of virtual memory for you (via a system call). Once the kernel has given you that page you are free to perform whatever operations you like within that page. Mathematical operations only need access to memory and registers to be performed, the kernel is not involved in user space code whilst it's just doing these kind of operations. As with the memory the program is free to use whatever instructions it needs (as long as it stays within its assigned memory pages).

    At some point the kernel will want another process to run, when it does it'll interrupt the process, save its registers and resume the other process. This is basically transparent to the user space code though, and its execution will continue where it left off when the kernel restores its registers and resumes execution.

    System calls are only required when accessing other hardware in the computer like the gpu, network card, storage etc.