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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Multitasking →← ProcessesNext lesson: Multitasking →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What is a system call?
Is printf a system call?
What is a file descriptor?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →