CE 466 Operating System Programming
Introduction to UNIX System Programming
Computer Engineering Department Yarmouk University 9/28/2008
UNIX History Bell Labs Research (Ken Thompson) 1969 1978
Very simple, PDP-11
First edition
Seventh edition
By AT&T 1983
System V
1989
System V.4
“Standard” UNIX
By Berkeley Software Distribution (BSD)
BSD 4.1
1980
BSD 4.3
1987
MINIX
1990
LINUX
1991
UNIX Design Principles • Written mostly in C (little assembly which helped in portability). • Written by programmers for programmers • User interface is simple and replaceable. • Time sharing with simple priority-based CPU scheduling ¾ Supports multiple processes. ¾ A process can easily create a new process
• Demand paging to support memory management
Unix Design Principles • File system is multilevel tree ¾Allow users to create their own subdirectories. ¾A data file is a simple sequence of bytes.
• I/O devices are treated similar to data files. • Was simple and easy to understand ¾Algorithms were selected for simplicity ¾Small set of powerful facilities → system easily extended
Abstract View of UNIX Architecture Text editor
Command-line interpreter (shell)
VI
sh
pico
fork( )
a.out
write( )
open( ), read( ), close( ) hardware
who
UNIX kernel
System calls
cc Application programs
C compiler
Unix Interface • UNIX consists of kernel and system programs ¾Kernel provides the file system, CPU scheduling, and memory management through system calls. ¾System programs provide useful functions using kernel-supported system calls. e.g. compilation, file manipulation, …
• System calls define programmer interface to UNIX . • The set of system programs define the user interface.
System Calls • The set of extended instructions provided by the operating system and defines the interface it provides to user programs. • A system call is a request to the operating system for service that causes the normal CPU cycle to be interrupted and control to be given to the operating system. • They communicate with the OS to request its services ¾ Create, delete and use various "software objects " e.g. processes, files,…, managed by the OS
System Call Groups in UNIX
In Windows
System calls and C library functions Subject
Win32
UNIX
C library
Console I/O
ReadConsole
read
getc, scanf, gets
Console I/O
WriteConsole
write
putc, printf, puts
Comments
Directory Mgt
CreateDirectory
mkdir
N/A
Make a new directory
Directory Mgt
FindClose
closedir
N/A
Close a directory search handle
fclose
CloseHandle is not limited to files
File System
CloseHandle (file handle)
close
File System
CopyFile
open; read; write; close fopen; fread; fwrite; fclose Duplicate a file
Memory Mgt
HeapAlloc
sbrk, or C library
malloc, calloc
Memory Mgt
HeapFree
use C library
free
shmctl
N/A
Shared Memory CloseHandle (map handle)
Shared Memory CreateFileMapping, OpenFileMappingshmget
N/A
Process Mgt
CreateProcess
fork () then execl ()
N/A
Process Mgt
GetCurrentProcess
getpid
N/A
Process Mgt
GetCurrentProcessId
getpid
N/A
IPC
CreatePipe
pipe
popen
IPC
DuplicateHandle
dup or dup2 or fcntl
N/A
System Calls
There are 6 execxx functions
Or use file names CONIN$, CONOUT$
Layered Structure of UNIX User programs Shells, compilers, interpreters, libraries
kernel
System-call interface to kernel Signals terminal handling character I/O system terminal drivers
File system CPU scheduling Swapping Page replacement block I/O system Virtual memory Disk drivers
Kernel interface to the hardware Terminal controllers terminals
Device controllers disks
Memory controllers Physical memory
UNIX Modes of Operation • User: A number of operations (privileged operations) are forbidden • System (Kernel): All operations are allowed including privileged operations. ¾Privileged operations such as: Interrupt enabling and disabling Direct access to I/O devices
System vs. Library Calls • System calls run in kernel mode • Library calls run in user mode Libraries: sin(), cos(), atoi(),…
User program
System call interface System call procedures: write(), read(), fork(), . . .
System vs. Library Calls Library function call Interface to library functions Code of a function
User Program
Without trap to the kernel
Interface to kernel
System call Library functions With a trap to the kernel
Kernel
Codes for other system calls
Hardware
Main code of a system call
Example: getpid System Call
General Form of a System Call [Return Value] = System_Call_Name(params) non-negative
, if OK
-1
, not OK
Return Value =
• In case of error, an error code is placed in an external variable errno.
Handling a System Call
#include <stdio.h> extern int errno; void perror(const char *message);
Actions After a System Call
Let’s Put Things Together
Not checking may cause failure for no apparent cause!
UNIX On-Line Man Pages Section 1 2 3 4 5 6 7 8
Contents User commands (Shell commands) OS services (system calls) Library functions Devices, networks, interfaces System file formats Demo programs Miscellaneous (ASCII, etc ) System maintenance
Check the Man Pages (very helpful) • Each section has an introduction %man 2 intro %man 2 fork %man 3 sin
Exercise 1. Print all: ¾ Command line arguments ¾ Environment strings
2. Use the following system call to implement an " ls" command: int system(const char *string);
#include <sys/types.h> #include <sys/stat.h> #include #include <stdio.h> #include <errno.h> #define BUF_SIZE 256 int main (int argc, char *argv []) { int input_fd, output_fd; ssize_t bytes_in, bytes_out; char rec [BUF_SIZE]; if (argc != 3) { printf ("Usage: cp file1 file2\n"); return 1; } input_fd = open (argv [1], O_RDONLY); if (input_fd == -1) { perror (argv [1]); return 2; } output_fd = open (argv [2], O_WRONLY | O_CREAT, 0666); if (output_fd == -1) { perror (argv [2]); return 3; } /* Process the input file a record at a time. */ while ((bytes_in = read (input_fd, &rec, BUF_SIZE)) > 0) { bytes_out = write (output_fd, &rec, bytes_in); if (bytes_out != bytes_in) { perror ("Fatal write error."); return 4; } } close (input_fd); close (output_fd); return 0; }