Kernel manages operating system resources. User program can only access to those resources by making system calls to the kernel. System call is similar to an API of kernel, which in term, runs kernel tasks your program needs.
str = "something" // run on user space
x = x + 1 // run on user space
file.write(str) // switch to kernel space
y = x + 4 // switch back to user space