Introduction to UNIX (1)

22
Introduction to UNIX (1) • What is UNIX? • UNIX Philosophies • UNIX for Nonprogrammers

description

Introduction to UNIX (1). What is UNIX? UNIX Philosophies UNIX for Nonprogrammers. What is UNIX?. UNIX is a popular operating system (OS) in both engineering and business world OS is a “ super program ” that controls sharing of resources and communication between machines - PowerPoint PPT Presentation

Transcript of Introduction to UNIX (1)

Page 1: Introduction to UNIX (1)

Introduction to UNIX (1)

• What is UNIX? • UNIX Philosophies• UNIX for Nonprogrammers

Page 2: Introduction to UNIX (1)

What is UNIX?

• UNIX is a popular operating system (OS) in both engineering and business world– OS is a “super program” that controls sharing

of resources and communication between machines

– E.g., Windows, OS/2, VMS, MacOS, UNIX– UNIX is available for all platforms

Page 3: Introduction to UNIX (1)

UNIX Services

• UNIX facts about programs and files– A file is a collection of data stored on disk– A program is a collection of bytes representing

code and data that is stored in a file– When a program is started, it is loaded from disk

into RAM. A “running” program is called a process– Processes and files have an owner and a group,

and they protected from unauthorized access– UNIX supports a hierarchical directory structure– Files have a location within the directory hierarchy

Page 4: Introduction to UNIX (1)

UNIX Services

• Sharing resources– UNIX shares CPUs among processes by dividing

CPU time into slices (typically 0.1 second) and allocating time slices based on their priorities

– UNIX shares memory among processes by dividing RAM into equal-sized pages (e.g., 4K bytes) and allocating them; only those pages of a process that are needed in RAM are loaded from disk while pages of RAM that are not accessed are saved back to disk

– UNIX shares disk space among users by dividing disks into equal-sized blocks and allocating them

Page 5: Introduction to UNIX (1)

UNIX Services

• UNIX provides communication mechanism between processes and peripherals – Pipe is one-way medium-speed data channel that allows t

wo processes on the same machine to talk – Socket is two-way high-speed data chanel that allows two

processes on different machines to talk – UNIX allows server-client model of communication

• e.g., X Window systems

Page 6: Introduction to UNIX (1)

UNIX Services• Utilities

– UNIX has more than 200 small utility programs• Editors, compilers, GUI, Shells, text-processing tools

• Programmer Support– UNIX is an open (source) system, so its features are

available to C programmers through system calls

• Standards– UNIX includes two main versions:

• System V UNIX from Bell Labs• BSD UNIX from UC Berkeley • HP-UX, Solaris, IRIX, AIX are all based on System V but

includes all nice features of BSD as well

Page 7: Introduction to UNIX (1)

Philosophies of UNIX

• How UNIX utilities should be written– A UNIX utility should do one thing and do it

well, and complex tasks are done by combining these small utilities

– e.g., They made the pipe mechanism for combining (e.g., who | sort)

who sortdata data

terminal

Page 8: Introduction to UNIX (1)

Philosophies of UNIX

• Unix philosophy of solving problems– Solve it by combining multiple existing

utilities using pipe if possible; otherwise– Ask people on the network if they know how

to solve it. If they do, great; otherwise– Write some utilities that can aid you solve

the problem and add them into UNIX repertoire

– Write a C or C++ program to solve the problem

Page 9: Introduction to UNIX (1)

(Brief) History of UNIX

• K Thomson and D Ritchie at Bell Labs– Wrote first version in assembly then the next versi

on in C; the first “understandable” OS!

• UC Berkeley graduate students – Enhanced with memory management and networki

ng capabilities (BDS UNIX)

• UNIX Today: contain both features– Commercial UNIX– Linux

Page 10: Introduction to UNIX (1)

UNIX for Users

• Basic commands– man, ls, cat, more, page, head, tail, less– mv,cp,rm,ln– pwd,cd,mkdir,rmdir,file– chmod, groups, chgrp

• Editing utilities and terminal type– vi, emacs

Page 11: Introduction to UNIX (1)

Getting Started in UNIX• Obtain an account and logging-in

– passwd: utility for changing password• Shells

– A middleman program between you and UNIX OS that executes your commands• Shell commands vs. utilities vs. system calls

– Three popular shells: Bourne, Korn, C– Shell has a programming language that are tailored t

o manipulate processes and files; such program is called shell script

Page 12: Introduction to UNIX (1)

UNIX Utilities: man & ls • man

– on-line help for UNIX commandsman [-s section] word

man -k keyword

– Man pages are divided into 8 sections• 1. Commands & application programs• 2. System calls, 3. Library functions ...

• ls– list filesls [options] {fileName}*

Page 13: Introduction to UNIX (1)

UNIX Utilities• ls example

$ ls -alFstotal 151 drwxr-xr-x 2 jaemok mass 512 Mar 2 01:04 ./3 drwxr-xr-x 46 jaemok mass 3072 Mar 2 01:04 ../5 -rwxr-xr-x 1 jaemok mass 5091 Mar 2 01:04 a.out*5 -rwxr-xr-x 1 jaemok mass 5091 Mar 2 01:04 hello*1 -rw-r--r-- 1 jaemok mass 36 Mar 2 01:04 hello.c

5 size in blockdrwxr-xr-x type and file permission1 hard-link countjaemok ownermass group5091 size in bytesMar 2 01:04 modified timea.out filename

• Miscellaneous: date, clear

Page 14: Introduction to UNIX (1)

Utilities to List File Contents

• cat,more,page,head,tail,lesscat displays contents of files given on command

line (or from standard input) to standard output$ cat > heart

blah blah blah

^D

$ _

more and page displays contents of files(or input) fitted to rows of terminal, and allows you to scroll.

head and tail only displays given number of lines in the first part or the last part of files(or input).

less is similar to more, but has more functions

Page 15: Introduction to UNIX (1)

Utilities for Manipulating File • mv, cp, rm, ln

– move,copy,remove,make links to filesmv [-fi] soutce targetFile .. rename source filenamemv [-fi] soutceList targetDirectory .. move source files into target

directory

cp [-fip] soutceFile targetFile .. make copy of sourceFile named targetFile

cp [-fip] soutceFileList targetDirectory .. copy source files into target directory

cp -rR [-fip] soutceDirList targetDirectory .. copy source files into target directory, and copy the directory structure.

rm [-fi] fileListrm -rR [-fi] directoryList

ln [-fns] source [target]ln [-fns] sourceList targetDirectory .. make links to sour files into

target directory

• file - determine file type $ file hello.c hello.c: c program text

Page 16: Introduction to UNIX (1)

Utilities for Hanling Directory• pwd, cd, mkdir, rmdir

– print working directory– change directory

• cd, pushd, popd

– make directory– remove directory

Page 17: Introduction to UNIX (1)

File Owner and GroupEvery UNIX process has a owner which is

typically the user name who started it• My login shell’s owner is my user name

When a process creates a file, its owner is set to the process’ owner

UNIX represents the user name as user IDEvery UNIX user is a member of a group

and an UNIX process belongs to a group• My login shell’s group is my group name (ID)

Page 18: Introduction to UNIX (1)

File Permissions• File Permissions (ex: rw-r--r--)

– owner: rw-, group: r--, others: r--– r: read, w: write, x: execute

• When a process executes, it has four values related to file permission– a real user ID, an effective user ID– a real group ID, an effective group ID– When you login, your login shell process’ values

are your user ID and group ID

Page 19: Introduction to UNIX (1)

File Permission ApplicationWhen a process runs, file permission applies

If process’ effective user ID = file owner • Owner permission applies

If process’ effective group ID=file group ID• Group permission applies

Otherwise, others permission applies

Super user’s process automatically has all access rights

When creating a file, umask affect permission

Page 20: Introduction to UNIX (1)

Effective User and Group ID

• A process’ effective user ID – depends on who executes the process, not who owns

the executable– E.g., if you run passwd (owned by root), the effective u

ser ID is your ID, not root; then how can it update /etc/passwd file owned by root ?

– Two special file permissions• set user ID and set group ID • When an executable with set user ID permission is execute

d, the process’ effective user ID becomes that of executable; the real user ID is unaffected

• File permission of /bin/passwd is r-sr-sr-x

Page 21: Introduction to UNIX (1)

Changing File Permissions

• chmod– change the permission mode of fileschmod [-fR] <absolute-mode> fileList

chmod [-fR] <symbolic-mode-list> fileList

Page 22: Introduction to UNIX (1)

chmod

• absolute-mode is octal numberif you want a file to have permission setting: rwxr- x---

then, it is an octal number o750 (111 101 000)

$ chmod 750 myfile

• symbolic-mode-list is comma-separated list of symbolic expressions of form

[who] operator [permissions]who : characters u,g,o,a

operator : -,=.+

permissions : r,w,x,s

$ chmod g-rx myfile