You are on page 1of 46

Ch1:Intro to Unix

1
Unix (officially trademarked as UNIX, sometimes
also written as Unix) is a multitasking, multi-
user computer operating system originally
developed in 1969 by a group of AT&T employees
at Bell Labs.
The Unix operating system was first developed
in assembly language, but by 1973 had been
almost entirely recoded in C.
Among all variants of Unix, Linux (1991) is the
most widely used, powering everything from huge
data centers to desktop systems to mobile phones
to embedded devices such as routers.


2
Unix shell is a command-line interpreter or shell
that provides a traditional user interface for the
Unix operating system and for Unix-like systems.
Users direct the operation of the computer by
entering commands as text for a command line
interpreter to execute or by creating text scripts
of one or more such commands.
The program is called a "shell" because it hides
the details of the underlying operating system
behind the shell's interface.
The shell manages the technical details of the
operating system kernel interface, which is the
lowest-level, or 'inner-most' component of an
operating system.

3
Kernel is the main component of most
computer operating systems.
It is a bridge between applications and the
hardware.
Kernel's responsibilities include managing the
system's resources (the communication
between hardware and software components)
especially processors and I/O devices that
application software must control to perform
its function.
It typically makes these facilities available
to application processes through inter-process
communication mechanisms and system calls.

4

5
System call is how a program requests a
service from an operating system's kernel. This
may include hardware related services (for
e.g. accessing the Hard Disk), creating and
executing new processes, and communicating
with kernel services(like scheduling). System
calls provide the interface between a process
and the operating system.

6
Systems Programming
Systems programming is an area of computer
engineering that deals with the development
of complex program systems and their
components.
Unix Standards
ANSI C
a language standard
includes standard C library


7
8
Basic modules of UNIX

User program

Library

Shell
User level
OS command

Library
System call interface
Kernel
level
Process management
&
memory management
File
Subsystem
Character block
I/O I/O
Device
drivers
Hardware control

Hardware
Hardware
level
9
In computing, a device driver is a computer program allowing
higher-level computer programs to interact with a hardware device.
A driver typically communicates with the device through
the computer bus to which the hardware connects Drivers are
hardware-dependent and operating-system-specific.
A device driver simplifies programming by acting as translator
between a hardware device and the applications or operating
systems that use it. Programmers can write the higher-level
application code independently of whatever specific hardware
device.
There are device drivers for printers, displays, CD-ROM readers,
diskette drives, and so on. When you buy an operating system,
many device drivers are built into the product. However, if you later
buy a new type of device that the operating system didn't
anticipate, you'll have to install the new device driver. A device
driver essentially converts the more general input/output
instructions of the operating system to messages that the device
type can understand.

10
An abstract view of UNIX architecture

hardware
Text editor Command-line interpreter (shell)
VI sh pico
a.out
open( ), read( ), close( )
w
r
i
t
e
(

)

f
o
r
k
(

)

UNIX kernel
who
cc
Application
programs
C compiler
System
calls
11
UNIX and its basic services


User Processes System Processes
System call
interface
Interface to
devices
U U U S S
Kernel
LAN
Devices

Modem
12
Basic Services
System initialization
Process management
Memory management
File system management
Input/output management
Communication facilities
Program interface (system calls)
UNIX - overview:
UNIX is a Multitasking, Multi-user OS, with a Virtual
Memory, Programmable Shell and Networking capability.
UNIX has access controls and security
UNIX environment is organized into three layers kernel -
programs - shell
UNIX has a hierarchical, universal file system
UNIX - set of processes
13
Multitasking Capability of UNIX
Process:
A single program in execution (e.g. running a C
program, reading I/O, sending an email are all kinds of
processes)
Each process in UNIX has a unique Process ID (PID)
Created by fork() systems calls
Process termination (return from main() or calling
exit())
Multiprocess application: a process wants to execute
another program
Process Synchronization: wait() system call is used
Process communication: local and remote IPC
mechanisms
14
Multitasking: is the ability to execute several
processes simultaneously.

Processes are not really executed at the same
time, but the operating system executes a piece
of one process and then switches to another (this
is called task switching).
This switching happens so fast (10x per seconds
or more) that it looks like the computer is doing
several things at a time.

At any given time, only one process is running.
For a certain period of time, all the processes
have made progress.

15

Process (task)
1
Process (task)
2
Process (task)
3
Context
switching
Time
16
Multiuser capability of UNIX

UNIX
host
Local Area Network (LAN)
UNIX
is here
No UNIX
here
(Windows
OS)
Users workstations (PCs)


UNIX
Shell commands
Shell
Each host runs a number of independent processes that executes in different protection domains (WSs).

17
Networking capability of UNIX
Electronic mail (e-mail)
File transfer (FTP) service
World wide web (WWW) service
Remote login (through Telnet)
Archie: is a tool for indexing FTP archives, allowing
people to find specific files.
Gopher: is a TCP/IP application layer protocol designed
for distributing, searching, and retrieving documents
over the Internet. Gopher protocol was a predecessor
of (and later, an alternative to) the World Wide Web.
The protocol imposes a much stronger hierarchy on
information stored on it. Its text menu interface is well-
suited to computing environments that rely heavily on
remote text-oriented computer terminals, which were
still common at the time of its creation in 1991.


18
A typical session in UNIX

Login: your identificator
Password: your password
%command 1
%
%logout

A few UNIX commands

ls or ls -l to list the current directory
cd <path> to change a directory
rm <file> to delete a file
mkdir <name> to create a directory
rmdir <name> to remove a directory
more <file> to show a text file

19
Standard Files on a UNIX System:
/bin - (binary files) contains executable
programs, UNIX commands
/dev - special files that represent the physical
devices
/lib - libraries of programs used by
programmers
/sys - system source files (system managers)
/tmp - temporary files

20
Commands:
The basic form of any Unix command is:
command_name options argument(s)
Most descriptions for commands such as
those given in the On-line Manual use a much
more precise syntax. For example:
cp [-iprR] filename ... directory
UNIX on-line help manual
Section
Contents
1 User commands (Shell commands)
2 OS services (system calls)
3 Library functions
4 Devices, networks, interfaces
21
5 System file formats
6 Demo programs
7 Miscellaneous (ASCII, etc )
8 System maintenance commands
Getting Specific Information
Once you know the names of the man page or pages you
need, you can display them on the screen by giving the man
command:
man [section] <NAME>
If you omit the section number, the man program searches
through each section in turn until it finds the named man
page.
Most manual sections have an ``intro'' man page, describing
that section in general. So to find out more about library
functions (section 3 of the man pages), type
%man 2 intro
%man 3 intro
%man 2 fork
%man 3 sin

22
Interrupts are a commonly used technique
for computer multitasking, especially in real-
time computing. Such a system is said to be
interrupt-driven. An act of interrupting is
referred to as an interrupt request (IRQ). The
part of a program that deals with the interrupt
is referred to as an interrupt service routine
(ISR) or interrupt handler.

23
Interruption handling in UNIX (simplified)


Interrupt
signal
Save the
interruption
No
New priority > current
priority?
Yes
Create a new context,
set current priority <-- new prior.
Handle the interruption
Restore old context
Can any saved interruption be
handled?
No
Return to normal
processing
Yes
Return to
normal
processing
24
UNIX kernel
Basic services:
System initialization (bootstrap facility).
Process management (creation, control, and
termination of process).
Memory management (using a virtual
memory).
File system management.
I/O management
Communication facilities (inter-process
communication, networks).
Program interface (system calls).

25

Kernel software
Machine dependent Machine independent
System initialization
Interrupt handling
Memory management
I/O device drivers
Etc.
System call interface (!)
Management of processes
Scheduling system
Etc.
26
The UNIX Operating System
Basic structure of the UNIX operating system is divided
into three parts:
kernel
schedules programs
manages data/file access and storage
enforces security mechanisms
performs all hardware access
shell
presents each user with a prompt
interprets commands types by a user
executes user commands
supports a custom environment for each user
utilities
file management (rm, cat, ls, rmdir, mkdir)
user management (passwd, chmod, chgrp)
process management (kill, ps)
printing (lp, troff, pr)
program development tools

27
Ch2: Preparing a C program and using
command line parameters in UNIX

28
How to prepare a C program in UNIX
Case A: Your program consists of one source file
After starting a text editor (for example PICO), create and save a
source text file (for example myprog.c).
Compile and link the program. The result is executable file (for
example, myprog).
Start your program.
%pico
%cc -o myprog myprog.c
%myprog or
%myprog param1,param2.



29
Case B: Your program consists of a few separate source
files (for example, module1.c, module2.c, module3.c)
1. Create in a text editor, and save each source file.
2. Compile separately each source file. The result is
object files (module1.o, module2.o, module3.o)
3. Link the object files. The result is executable file.
%cc -c module1.c
%cc -c module2.c
%cc -c module3.c
%cc -o myprog module1.o module2.o module3.o
%cc -o myprog module1.c module2.c module3.c


30
31
32
33
34
35
36
37
If you execute as day it will print usage: day <day> <year>.

38
39
40
41
42
43
44
To clarify array pointers,
45
46

You might also like