System Call Programming & Debugging: Week 7
System Call Programming & Debugging: Week 7
Debugging
Week 7
Processor Modes
Operating modes that place restrictions on the
type of operations that can be performed by
running processes
User mode: restricted access to system resources
Kernel/Supervisor mode: unrestricted access
System resources?
Memory
I/O Devices
CPU
Supervisor/kernel mode
CPU is unrestricted, can use all instructions, access all
areas of memory and take over the CPU anytime
Fairness
Make sure processes have a fair use of devices and the
CPU
Memory Protection
Prevent processes from accessing illegal memory and
modifying kernel code and data structures
CPU Protection
Prevent a process from using the CPU for too long
System Calls
Special type of function that:
System Calls
When a system call is made, the program
being executed is interrupted and control is
passed to the kernel
If operation is valid the kernel performs it
Library Functions
Functions that are a part of standard C library
To avoid system call overhead use equivalent
library functions
getchar, putchar vs. read, write (for standard I/O)
fopen, fclose vs. open, close (for file I/O), etc.
Buffered
collect as many bytes as possible (in a buffer) and
read more than a single byte (into buffer) at a
time and use one system call for a block of bytes
Buffered I/O
Writing to a file:
fwrite() copies outgoing data to a local buffer as
long as its not full and returns to the caller
immediately
When buffer space is running out, fwrite() calls
the write() system call to flush the buffer to make
room
Reading from a file:
fread() copies requested data from the local buffer
to user's process as long as the buffer contains
enough data
When the buffer is empty, fread() calls the read()
system call to fill the buffer with data and then
copies data from the buffer
Lab 7
Write 2 versions of the cat program
Version 1 (catb.c)
Library functions: getchar and putchar for copying
Version 2 (catu.c)
System calls: read and write for copying
Homework 7
Rewrite srot13 using system calls (srot13u)
srot13u should behave like srot13 except:
If stdin is a regular file, it should initially allocate enough
memory to hold all data in the file all at once
It outputs a line with the number of comparisons
performed