System Calls, Explained | Introduction to Operating Systems

UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 2.3 Free with a free account

System Calls

Introduction to Operating Systems · Chapter 2: Process Management: Scheduling, Multitasking, and System CallsSection 2.3 · lesson 12 of 837 min · free with a free account

← ProcessesNext lesson: Multitasking →

A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

In this lesson

How programs ask the kernel for help through user and kernel mode.

  • Distinguish user mode from kernel mode and explain why the CPU enforces the split
  • Define a system call and name its main families
  • Describe how a wrapper and a trap carry a request into the kernel and back
  • Use file descriptors with open, read, write, and close and check for errors

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (954 words). Read along, or search it.

Opening Question

How Does Save Reach the Disk

When you press Save, your spreadsheet program doesn't touch the disk. It can't. The disk is guarded by the operating system, and no ordinary program is allowed past the door. So how does anything ever get saved? The program asks, politely, using a fixed set of requests called system calls. Every file you've ever opened, and every message you've sent over the internet, started with one.

User and Kernel Mode

A Kitchen for Staff Only

Picture a restaurant. Customers sit in the dining room. The kitchen, with its sharp knives and hot ovens, is for staff only. A customer who wants soup asks a waiter. Customers stay safe from the kitchen, and the kitchen stays safe from customers. A CPU works the same way. In user mode, where every ordinary program runs, some instructions are forbidden: talking to the disk, changing the memory map, or switching off the clock. In kernel mode, where only the operating system's core, the kernel, runs, everything is allowed. The CPU itself enforces the rule, so a buggy or hostile program can't cross over just by wishing.

Mode Bit

One Switch Programs Cannot Flip

Inside the CPU, the current mode is held in one tiny switch, sometimes called the mode bit. Programs can't flip it themselves. Only a trap into the kernel, and the kernel's own return instruction, can change it. Why bother? If any program could write to the disk, a bug in a photo editor could scribble over someone's order file. If any program could read any memory, one could read another's passwords. Splitting the world in two means a crash in one program stays there. It can't become a crash for everyone.

Controlled Doorway

The Kernel Checks Every Request

And the kernel never hands that power out for free. It checks every request. Does this program own that file? Does the address it passed make sense? Only then does it act. That check is the reason system calls exist at all. They're a controlled doorway, not a hole in the wall.

Definition

Families of Requests to the Kernel

So a system call is a request from a program to the kernel, to do something the program isn't allowed to do itself. Each request has a number and a fixed set of inputs. Linux and macOS each have hundreds, in families you can guess. Process control, like fork and exit. Files: open, read, write and close. Devices. Information, such as the time or a process ID. And communication, such as pipes and sockets, which lessons 3.7 and 8.5 cover.

The Trap

Wrapper Functions and the Switch to Kernel Mode

In C, you rarely make the request by hand. A small library function, called a wrapper, loads the call's number and its arguments into registers. Then it runs a special instruction: the trap. The CPU stops the program, switches to kernel mode, and jumps to a fixed spot where the kernel's handler waits. The kernel checks the request and does the work. Then it switches the CPU back to user mode. And the program carries on at its next line, holding the result.

Common Mistake

Printf Sits on Top of Write

A common mistake is believing that printf is a system call. It isn't. printf is an ordinary library function. It collects text in a buffer, and eventually calls write to send it out. The system call is the write underneath. That's also why a buffer sometimes has to be emptied by hand, like the fflush in lesson 2.2.

Worked Example

File Descriptors Zero, One, and Two

Here's a real program that makes system calls directly, with no printf. It saves an order for Sam, who runs a small online bakery, then reads it back. Every file the kernel opens is named by a small number, called a file descriptor. Three exist from the start: zero for the keyboard, one for the screen, and two for errors. So write, with a one, asks the kernel to put these bytes on the screen.

Opening a File

Open, Write, and Close

Next, open asks the kernel for order dot t x t, for writing. Create it if it's missing, and empty it if it exists. The zero six four four sets who may read the file. Lesson 6.7 explains those digits. open returns a new descriptor. A result of minus one is how a system call says no, so the program checks before going on. Then write sends the order's bytes to that descriptor, and close hands the descriptor back.

Reading Back

Read Returns a Byte Count

Step two opens the same file again, this time for reading. read returns how many bytes it actually got, and the program keeps that count in n. The last writes print exactly those n bytes, then a closing line. Each of these lines is one trip into the kernel and back. Build it, and run it. The order went to the disk and came back, all through system calls. And the file really is there: cat prints it.

Quick Check

A Read-Only File Makes Open Fail

Quick check. Sam makes the saved file read-only, with chmod four four four, and then runs the very same program again. The program hasn't changed, and it's still Sam's own file. What does it print this time? Pause, and decide. The first line, then open failed. The file now says nobody may write to it, so when open asked for writing, the kernel refused. open returned minus one. The program's check caught it, wrote to descriptor two, and stopped with exit code one. Here are the two lines that decided it. open asked to write, and the kernel said no. All four lines is the tempting guess. But the program doesn't get to decide. The kernel checks every request, every time.

Key Takeaways

Requests Through a Guarded Door

To recap. Programs run in user mode; only the kernel runs in kernel mode, and the CPU enforces the split. A system call is a request to the kernel, which checks it before acting. A trap switches the CPU to kernel mode and back, and the program carries on with the result. And printf is a library function. The system call underneath is write.

About Introduction to Operating Systems at UPI Study

Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About System Calls

What is the difference between user mode and kernel mode?
User mode is where ordinary programs run with some instructions forbidden, such as talking to the disk or changing the memory map, while kernel mode is where the kernel runs with everything allowed. The CPU itself enforces the split through a mode bit that programs cannot flip directly. The CPU enters kernel mode only through controlled entry points, such as a system-call trap or a hardware interrupt, and the kernel's return instruction switches it back.
What is a system call?
A system call is a request from a program to the kernel to do something the program is not allowed to do itself, such as opening a file. Each call has a number and a fixed set of inputs, and the kernel checks every request before acting. Common families cover process control, files, devices, information such as the time, and communication.
Is printf a system call?
No, printf is an ordinary C library function, not a system call. It collects text in a buffer and eventually calls write, which is the real system call that asks the kernel to send the bytes out. Because of that buffer, output sometimes has to be emptied by hand, for example with fflush before calling fork.
What is a file descriptor?
A file descriptor is a small number the kernel uses to name each file a process has open. Three exist from the start: 0 for standard input, usually the keyboard, 1 for standard output, usually the screen, and 2 for errors. The open call returns a new descriptor, or -1 when the kernel refuses, so programs check the result before going on.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Introduction to Operating Systems.

83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Introduction to Operating Systems
3 credits · NCCRS
Enroll →