Вы находитесь на странице: 1из 12

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) Very simple,
PDP-11

1969 First edition


By Berkeley
1978 Software
Seventh edition Distribution
(BSD)
By AT&T

1983 System V BSD 4.1 1980

BSD 4.3 1987


1989 System V.4

MINIX 1990
“Standard”
UNIX
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
open( ), read( ), close( )
a.out write( )

fork( )
hardware
who System
UNIX kernel calls

cc

Application C compiler
programs

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 Comments

Console I/O ReadConsole read getc, scanf, gets


Console I/O WriteConsole write putc, printf, puts
Directory Mgt CreateDirectory mkdir N/A Make a new directory
Directory Mgt FindClose closedir N/A Close a directory search handle
File System CloseHandle (file handle) close fclose CloseHandle is not limited to files
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
Shared Memory CloseHandle (map handle) shmctl N/A
Shared Memory CreateFileMapping, OpenFileMappingshmget N/A
Process Mgt CreateProcess fork () then execl () N/A There are 6 execxx functions
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 Or use file names CONIN$, CONOUT$

System Calls
Layered Structure of UNIX
User programs

Shells, compilers, interpreters, libraries


System-call interface to kernel
Signals File system CPU scheduling
terminal handling Swapping Page replacement
kernel

character I/O block I/O system Virtual memory


system
terminal drivers
Disk drivers
Kernel interface to the hardware

Terminal Device Memory


controllers controllers controllers
terminals disks 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
User Libraries:
program sin(), cos(),
atoi(),…

System call interface

System call procedures:


write(), read(),
fork(), . . .

System vs. Library Calls

Library System
User Program
function call call
Interface to
library
functions Library
Without functions
Code of a trap to the
function kernel With a trap to
Interface the kernel
to kernel

Kernel

Main code of a
Hardware system call
Codes for
other system
calls
Example: getpid System Call

General Form of a System Call


[Return Value] = System_Call_Name(params)

non-negative , if OK
Return Value =
-1 , not OK

• 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 Contents
1 User commands (Shell commands)
2 OS services (system calls)
3 Library functions
4 Devices, networks, interfaces
5 System file formats
6 Demo programs
7 Miscellaneous (ASCII, etc )
8 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 <fcntl.h>
#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;
}

Вам также может понравиться