Computer Programming Lecture 2
이윤진 서울대학교 2007.12 .24 . Slide Credits y 엄현상 교수님 y 서울대학교 컴퓨터공학부 y Comppgg,uter Programming, 2007 봄학기 Unix/Linux (3) 순서 y File system y 지난 시간 정리 y Sample file system y Link, unlink, remove, and rename y File descriptors, file table, and inode table y Process control y Command in the shell y Process control primitives y Examples y Booting & logging-in y 환경변수 설정 File System File System 정리 y File system y Abstraction Used by the Kernel to Represent and Organize the System’s Storage Resources y File system의특징 y Hierarchical Structure y Consistent Treatment of File Data y Ability to Create and Delete Files y Dynamic Growth of Files y Protection of File Data y Treatment of Peripheral Devices as Files File System 정리 (계속) y Inode (Static on Disk) y Information about each file in a structure that the kernel maintains y Owner of the file y Size of the file y Device that the file is located on y Pointers to where the actual data blocks for the file are located on disk y Type of the file y Access permission of the file y Access times of the file y (Hard) Link Count/Number File Descriptors, File & Inode Tables y Kernel Data Structure for Open Files martini:~> cat file1.txt > file2.txt
Process Table File Table Inode Table fd file1.txt in-core inode 0 File Status Flags inode number 1 Current File Offset reference count: 1 2
3 file2.txt in-core inode
User File Descriptor Entry File System Layout y Boot Block: OS가 부팅될 수 있도록 하는 boot sector y Super Block: file system의크기, block 개수, inode 개수, 각종 날짜, file system magic number 등 file system을 인 식하는데 필요한 정보 저장
boot block(s) super block
File system i-list directory blocks and data blocks
i-node i-node … i-node File System Layout in More Detail
directory blocks and data blocks
data data directory data directory i-list block block block block block
i-node i-node i-node … i-node
i-node file name number
i-node file name number Sample File System
directory blocks and data blocks
directory directory i-list block block data block data block
i-node i-node i-node 0 1267 2549 2549 . 1267 . .
1267 . i-node . . number $ mkdir testdir 2549 testdir Link, Unlink, Remove and Rename y Link and Unlink int link(const char *existingpath, const char *newpath); /* create a new directory entry, newpath, that references the exiiisting file exiiistingpat h. */ int unlink(char *pathname); /* remove the directory entry and decrements the link count */ y Remove int remove(const char *pathname); y Rename int rename(const char *oldname, const char *newname); Unlink Code Example #include
User level Libraries Kernel level System call interface
Inter-process File subsystem communication Process control Scheduler subsystem Memory Device drivers management
Hardware control Kernel level Hardware level Hardware Command in the Shell y Issue of a Command at the Shell Level y Generate a Child Process (by Calling fork) y Repp()gglace the Current (Shell) Image with the Command Image (by Calling exec) y Execution of a Command via Process Creation
Getting a waiting in Having the Command Issued the Background Prompt Return Shell
fork exit, return, etc. Child Process
exec Process Control Primitives y fork y Only Way for an Existing Process to Create a New Process (Its Copy) y Return 0 in child y Return process ID in parent y Return -1 on error #include
y Text Segment Shared If It Is Read-Only y Copy-On-Write (()COW) y Parent’s Data Space, Stack, and Heap Shared Until Being Modified y Copied at the modification time Process Control Primitives (계속) y exec y Ways to Execute another Program y Return -1 on error y No return on success y Current Process Replaced by a New Program on Disk y Six Different Versions y execl, etc.
int execl(const char *pathname, const char *arg0, … /* (char *) 0 */ ); Process Control Primitives (계속) y wait y Way to Block the Caller Until a Child Process Terminates
#include
y exit y Ways for a Process to Terminate Normally y exit status y argument to exit or _ exit as the return value from main void exit(int status); Process Control Code Example
#include
y Result 123 martini:~> exec cat file1.txt > file2.txt y Result y ??? Process Control의 시작 y Booting (Bootstrapping) : Start Up a Computer y Load the Kernel into Memory y Perform Various Initialization Tasks y init process (/sbin/init) invoked at the end) y getty program executed per terminal y Logging in to the System : Invoke the login Process y Initialize the Environment y RdRead the Start-up Files y .bash_profile, .bash_login, & .bashrc or .login & .cshrc y Change/Add Some Environment Variables Booting & Logging-In Processes
process ID 1 init read /etc/ttys (BSD) or /etc/inittab (Linux) fork fork once per terminal; init create empty environment exec open terminal device getty (file descriptors 0,1,2); exec read user name; login initial environment set
shell fd 0, 1, 2 tilterminal didevice didriver
user at a terminal 환경변수 설정 y Environment Variables martini:~>env USER=net001 LOGNAME=net001 HOME=/csehome/net001 PATH=/usr/local/bin:/usr/bin:/bin SHELL= /bin/bash : (omitted) martini:~>expp$ort PATH=$PATH:. martini:~>set PATH = ($PATH .) csh y Result martini:~>env : PATH=/usr/local/bin:/usr/bin:/bin:. Unix/Linux (4) 순서 y Process Control y Interprocess Communication y Miscellaneous y Example Interprocess Communication (IPC) y Pipes y Mechanisms That Allow a Stream of Data to be Passed Between Two Related Processes y FIFOs y Mechanisms That Allow Data to be Exchanged by Unrelated Processes y Message Queues y Linke d List of Messages Store d within the Kerne l and Identi fie d by a Message Queue Identifier y Semaphores y Counters Used to Provide Access to a Shared Data for Multiple Processes y Shared Memory y Mechanism that Allows Multiple Processes to Share a Given Region of Memory y Sockets y Mechanism that Allows (TCP/IP) Network Communication between Processes Interprocess Communication (계속) y fork y Check for Available Kernel Resources y Free Process Table Entry y Copy Data from Parent Process Table Entry to New Child Entry y Increment Counts y Copy Parent Program in Memory y Return Child Process ID in Parent y Return 0 in Child y Return -1 on Error Pipe Code Example
#include
Pipe Miscellaneous y Signals y Technique Used to Notify a Process That Some Condition Has Occurred
#include
y Software Interrupts Miscellaneous (계속) y Interrupts y Signals That Get the Attention of the CPU y Usually Made By Devices y Serviced Between Instructions y Prioritized y Disk Usage martini:~>du . 20 ./Dir 120 . martini:~>quota FIFO & Signal Code Example #include
write(pid file)
read(pid file)
write(file) pause
SIGUSR1
read(file )
print IPC Code Example - Reader
#include
#include
martini:~> gcc –o reader reader.c martini:~ > gcc –o writer writer.c martini:~> reader & [1] 22246 martini:~> writer & [2] 22247 martini:~> Reader, Print This Message! [Enter] [1]- Done ./reader [2]+ Done ./writer martini:~> IPC Example (계속) reader writer
write(pid file)
read(pid file)
mkfifo(file) pause
open(file, O_WRONLY) open(file, O_RDONLY)
read(file) SIGUSR1 write(file)
print Deadlock! UNIX Kernel User programs
User level Libraries Kernel level System call interface
Inter-process File subsystem communication Process control Scheduler subsystem Memory Device drivers management
Hardware control Kernel level Hardware level Hardware Remaining Stuff y Process Control System y Scheduler y Memoryyg Management y File System y Buffer Cache y Miscellaneous y Error Handling y Time