University of Massachusetts Lowell. One University Avenue. Department of Computer Science Tel: (508)

Similar documents
Overview. Over the next four weeks, we will look at these topics: Building Blocks. Advanced Authentication Issues.

Building blocks for Unix power tools

Department of Computer Science and Technology, UTU 2014

4. System Functions and Subroutines

Noorul Islam College Of Engineering, Kumaracoil MCA Degree Model Examination (October 2007) 5 th Semester MC1642 UNIX Internals 2 mark Questions

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Design Overview of the FreeBSD Kernel CIS 657

CS2028 -UNIX INTERNALS

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

PESIT Bangalore South Campus

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

CSC209 Review. Yeah! We made it!

Module A: The FreeBSD System

Module A: The FreeBSD System

cs642 /operating system security computer security adam everspaugh

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Data Security and Privacy. Unix Discretionary Access Control

Advanced Programming in the UNIX Environment W. Richard Stevens

Appendix A: FreeBSD. Operating System Concepts 9 th Edition

Reconstructive Software

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Lecture Topics. Announcements. Today: Operating System Overview (Stallings, chapter , ) Next: Processes (Stallings, chapter

The UNIX File System

The UNIX Time- Sharing System

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Introduction to Operating Systems. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Linux System Administration

Linux Operating System

Linux is obsolete 2.0

Unix Basics. UNIX Introduction. Lecture 14

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

412 Notes: Filesystem

PROCESS CONTROL: PROCESS CREATION: UNIT-VI PROCESS CONTROL III-II R

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

The Classical OS Model in Unix

File Management 1/34

CIS 5373 Systems Security

CL020 - Advanced Linux and UNIX Programming

Babu Madhav Institute of Information Technology, UTU

Files and the Filesystems. Linux Files

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

The UNIX File System

Introduction to Operating Systems. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

System Programming. Introduction to Unix

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Operating system security models

Processes COMPSCI 386

Classic Systems: Unix and THE. Presented by Hakim Weatherspoon

UNIX Kernel. UNIX History

Reconstructive Software Archaeology Warren Toomey School of IT, Bond Uni

Unix Processes. What is a Process?

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

UNIT I Linux Utilities

Chapter 3: Process Concept

Chapter 3: Process Concept

File I/O and File Systems

CSE 153 Design of Operating Systems Fall 2018

Processes. CS3026 Operating Systems Lecture 05

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

When we start? 10/24/2013 Operating Systems, Beykent University 1

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Mon Sep 17, 2007 Lecture 3: Process Management

CSCI 4500/8506 Operating Systems Some UNIX System Calls, Library, and PThreads Functions

INTRODUCTION TO OPERATING SYSTEMS. Jo, Heeseung

Introduction to Linux

(32 KB) 216 * 215 = 231 = 2GB

Introduction to Computer Security

Introduction to Unix. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating Systems. Lecture 05

Operating System Design

Introduction to Operating Systems

Welcome to Linux. Lecture 1.1

Introduction to Operating Systems. Jo, Heeseung

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Unix System Architecture, File System, and Shell Commands

What is an Operating System? A Whirlwind Tour of Operating Systems. How did OS evolve? How did OS evolve?

Chapter 3: Process Concept

Input & Output 1: File systems

History. 5/1/2006 Computer System Software CS012 BE 7th Semester 2

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

les are not generally available by NFS or AFS, with a mechanism called \remote system calls". These are discussed in section 4.1. Our general method f

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Module 20: The UNIX System. History

Dr. Rafiq Zakaria Campus. Maulana Azad College of Arts, Science & Commerce, Aurangabad. Department of Computer Science. Academic Year

St. MARTIN S ENGINEERING COLLEGE Dhulapally,Secunderabad DEPARTMENT OF INFORMATION TECHNOLOGY Academic year

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

High Performance Computing Lecture 11. Matthew Jacob Indian Institute of Science

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Operating Systems. II. Processes

The Design and Implementation of the 4.4BSD Operating System. Marshall Kirk McKusick Keith Bostic Michael J. Karels John S.

CS510 Operating System Foundations. Jonathan Walpole

CS 333 Introduction to Operating Systems Class 2 OS-Related Hardware & Software The Process Concept

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

Transcription:

University of Massachusetts Lowell One University Avenue Lowell, MA 01854 College of Arts and Science Prof. William F Moloney, Executive Ocer Department of Computer Science Tel: (508) 934-3640 bill@cs.uml.edu UNIX Systems Programming 1

UNIX Systems Programming The topics covered in this course include the following: Generic UNIX organization System address space, process address space General C function calls, kernel access C binding format for system calls Process attributes and protection mechanisms Categories of system services I-O services File systems and INODES, internal organization Device independent I-O system calls Advanced I-O and terminal control Control operations on INODES Line driver discipline ioctl with terminal drivers Processes and process management Process states and transitions Managing process attributes Process scheduling algorithm Advanced process control 2

UNIX Systems Programming (Cont'd) Basic UNIX signal mechanisms Global jumps and state saving Basic interprocess communications Using named pipes (FIFOs) System V advanced IPC mechanisms Generalized message passing Sharing physical memory among processes Generalized semaphore facility The internetwork and TCP/IP Generalized connection mechanism Datagrams and unconnected sockets System V driver organization Block, character and streams overview Upper and lower half functionality Streams implementation issues 3

The UNIX System The UNIX operating system was created at Bell Laboratories in 1969 by Ken Thompson and Dennis Ritchie. Much oftheir work reected Bell's experience with Project Mac from 1965 to 1969. Project Mac was a joint eort by Bell Labs, General Electric and MIT to dene a time-sharing operating system. When Bell withdrew from the project in 1969, they were left without a system (they had been using a GE specially modied system) or an operating system. Later that year using a cast-o PDP-7, Thompson and Ritchie wrote UNIX. UNIX remained as a Bell internal software package through the rst 5 years of its life, but a number of signicant events occurred during the years from 1970 to 1975. The C language was constructed by Thompson and Ritchie of Bell Laboratories during the early 1970s in an eort to provide a portable language platform for their UNIX operating system. Both UNIX and C were implemented on the early PDP-11 series of systems from DEC, and, in 1975, Bell Laboratories decided to provide the software to non-prot organizations for a small media charge. 4

The UNIX System (Cont'd) When the rst DEC Vax machines showed up in 1977 UNIX was quickly ported and became the system of choice within Universities: Originally described in 1974 CACM as a general purpose, multi-user, interactive operating system The dispatch-able unit is the process, an asynchronous activity a compact kernel provides classic multiprocessing support including: { dispatching, context switching and state transition { interprocess synchronization and communication { memory management { a device independent i-o system { a simple hierarchical stream le system A small collection of kernel system calls provide support access directly to the kernel for application programs Interactive user interface to the kernel through a type of program called a shell (several exist) 5

The UNIX Philosophy The basic idea is that that power of a computing system is derived more from the relationship among the programs than the programs themselves To extract maximum utility from the UNIX environment you must understand not only how to use the programs provided, but how they t into the environment A UNIX system provides an environment to support the basic principles of software engineering and incremental development by supplying a collection of tailored tools The support of team development and resource sharing is also an integral part of the UNIX philosophy 6

The UNIX Environment UNIX is a classic multiprocessing system The existence of multiple processes supports: { Timesharing, or process multiplexing to share resources { Pipelining, or coroutine construction to use resources eciently { Concurrent Processing, or multiple threads of control for applications which need to synchronize on multiple external events 7

The Process Model Programs execute within a process. A process can best be thought of as a collection of system resources that the operating system views as a unit of management: Computational progress in the system can only be made by a process Kernel procedures must be driven by a process Such procedures are activated by a process when: { the process explicitly requests kernel support by making a system call { the process is coerced to execute kernel procedures as a result of an exception of some variety which occurs on the CPU that the process is currently running on 8

The Process Model (Cont'd) Among the resources of a process is an address space and a collection of contiguously addressed objects which occupy some part of this space. The objects typically found on a UNIX system include: A Text segment which contains machine instructions A Global Data segment which contains static and dynamic global memory allocations (the dynamic allocations are said to belong to the heap portion of this segment) One or more Stack segments (one per execution path/thread) to manage activation records Additional objects which may have been inherited from a parent or mapped dynamically by the process at run time, including: { Shared Memory regions { Memory Mapped le object regions { Shared Library regions { Dynamically Mapped Library regions 9

Typical Process Address Space 0 Text Segment Initialized Global Data Heap Space Stack 1 Stack 2 Shared Memory Memory Mapped File Shared Library 4 GB 10

The Process Model (Cont'd) A process also contains a system segment, but this segment lives in the kernel's address space. It is composed of two components known as the u.area and the proctab entry. While the process has direct address manipulation privileges on the text, code, etc. segments, the system segment is not directly part of the process space, and only part of it is accessible to the process, and then only by using system calls: The process accessible part of the system segment is called the u.area Among other pieces of process status which are kept here, the process I-O channel table resides in the u.area All I-O system calls require a channel number as a vector into this table to read or write (or otherwise access) an external le object When a process is associated with a control terminal (as a login shell process is) channel 0 (slot 0 in the table) is assigned as standard input and connected to the terminal keyboard while channel 1 is assigned as standard output and is assigned to the display 11

The Process System Segment 0 KV MEM u.area proctab u.area entry info ruid, rgid, euid, egid signal management proctab slot addr profiling information working directory root directory controlling tty open channel table etc... 4GB proctab table slot entry uid gid ppid u.area addr VM mapping info priority signals pending CPU usage etc... 12

The Process Model (Cont'd) At system boot time, the UNIX kernel may create one or more kernel processes (threads) to support the environment. Such processes are generally of little direct interest to a user of the system, but one of them, the so-called init process, will serve asthe ancestral forefather for all user processes which can ever exist in the system. The process which runs the init program is allocated the special process ID (PID )of1. This PID is treated in a unique way in all UNIX systems, and upon exit, will bring the system down. In USL System Five (SV) environments, the init process comes to life and looks for a le called /etc/inittab to determine how to set up the system for the boot level indicated by the system administrator upon startup. The inittab le generally directs the init process to create children to support various system needs such as user access (i.e. login), printer scheduling, network monitoring, etc. 13

The Process Hierarchy PID a PID 1 init UID=0 GID=1 PID h UID=0 GID=1 PID b ttymon sac UID=0 GID=1 UID=0 GID=1 cron PID j lpsched UID=0 GID=1 login: tty02 UID=0 GID=1 PID d PID e PID f login csh csh tty03 login: tom passwd: UID=125 GID=104 tty04 PID g ls login: don passwd: % UID=143 GID=104 PID k inetd UID=0 GID=1 tty05 login: kim passwd: % ls file1 file2 file3 root:-pw-:0:1: super user :/:/usr/bin/sh tom:-pw-:117:104: Tom Mix x234 :/usr/home/tom:/usr/bin/csh don:-pw-:125:104: Don Ho x567 :/usr/home/don:/usr/bin/csh kim:-pw-:143:104: Kim Novak x890 :/usr/home/kim:/usr/bin/csh 14

Objects and Protection Processes are the only active objects in the UNIX world. They may attempt to access other processes or one of the passive objects which include le objects, message queues, shared memory segments and semaphores: Processes File Objects { ordinary { directory { character device special { block device special { named pipes (FIFOs) { symbolic link { UNIX domain socket Message queues Shared memory segments Semaphores 15

Objects and Protection (Cont'd) All UNIX objects have an identity which consists of the user-id (UID) and group-id (GID) of the process (if the object is a process) or the UID and GID of the creating process if the object is one of the passive types. The passive objects can only exist if a process creates them, and at creation time their controlling structures are stamped with the creator's UID, GID pair. Access to a process requires that the initiator have aneective or real UID of the target process (or is running with EUID = 0). Sending a signal is an example of this type of access. Access to all other objects proceeds according to the protection domain which an accessing process occupies with respect to the target: Protection domains: {owner initiator UID matches target UID (no further checking done) { group initiator GID matches target GID i UIDs don't match { other neither a UID nor a GID match occurs 16

Objects and Protection (Cont'd) General read, write and execute access is maintained for objects with the obvious interpretation. For directories however, the execute privilege indicates whether a user can use the name in a pathname, and without this privilege, read and write are irrelevant. Read and execute on a directory allow the ls command, and write and execute allow the user to create or delete to/from the directory. The set UID and set GID pertain to ordinary binary or script executables and allow the executing process to change its eective identity as discussed: Access privileges: { read (r) { write (w) { execute (x) { set UID (s) { set GID (s) 17

Objects and Protection (Cont'd) Output from an ls -gl Command - rw- r-- r-- 2 bill staff 6326 Sep 26 09:56 fall94.sched - rw- --- --- 1 bill staff 18269 Oct 19 19:25 fall94.rooms - rw- --- --- 2 bill staff 1200 Feb 27 1994 gradeprint - rws r-x --x 1 bill staff 65541 Aug 19 19:30 rsched d rwx r-x --x 2 bill staff 24 Jul 19 19:27 subdir d rwx r-x --x 4 bill staff 24 Jul 19 19:27 subdir1 l rwx --x --x 1 bill staff 9 Oct 19 19:33 tape1 -> /dev/rmt8 +- FILE NAME +- LAST MODIFY DATE +- FILE SIZE IN BYTES +- GROUP AFFILIATION +- OWNER NAME +- NUMBER OF LINKS (other names to this object) +- ACCESS ALLOWED TO NON OWNER NON GROUP PROCESSES +- ACCESS ALLOWED TO NON OWNER BUT GROUP MEMBER PROCESSES +- ACCESS ALLOWED TO OWNER +- FILE TYPE AS SHOWN: - ordinary d directory l symbolic link p pipe s socket (UNIX domain) c character special (device) b block special (file system device) 18

Set UID and Set GID Execution PID x parent_program RUID=150 EUID=150 RGID=101 EGID=101 fork() PID y parent_program After fork() but before exec..() RUID=150 EUID=150 RGID=101 EGID=101 exec..() PID y After exec..() new_child_program RUID=150 EUID=0 RGID=101 EGID=101 19

The System Call Interface Most UNIX system calls return an integer, however there are some which return some other type (i.e. pointer type). When a program makes a system call it should check the return for an error. Regardless of the return type, when a UNIX system call errors it returns a value of -1 and sets the global variable errno to the error code for this error. UNIX system calls have the following format: { return = system call (arg1,...,argn ) The successful return type and value are specic to each call When a system call fails it will return a value of - 1 and will set an integer error code into the linker supplied global variable named errno Three linker supplied global variables are important to UNIX C programmers, they are: { extern int errno, sys nerr; { extern char *sys errlist[]; 20

A Useful Function For Error Returns extern int errno, sys_nerr; extern char *sys_errlist[]; void syserr(char *some_string) { printf("error number %d in %s is: ", errno, some_syscall); if(errno < sys_nerr){ /* THEN ERROR NUMBER HAS A CORRESPONDING TEXT */ printf("%s \n", sys_errlist[errno]); }else{ /* ELSE NO ERROR TEXT AVAILABLE FOR THIS ERROR */ printf("not documented \n"); } } Example Calling Sequence: int sys_call_ret, chan, numbytes; char buf[]; if( (sys_call_ret = read(chan, buf, numbytes)) == -1 ){ /* THEN AN ERROR HAS OCCURRED IN READ */ syserr("read myfile"); exit(n); } Possible Output: error number 9 in read myfile is: Bad file number 21

System Services Categories Basic I-O: { creat, unlink, open, close, read, write, lseek Advanced and Terminal I-O: { mknod, link, symlink, chmod, chdir, chown, fcntl, access, stat, fstat, select, mmap, ioctl Processes and Process Management: { exec family, fork, exit, getuid, getgid, geteuid, getegid, getpid, getpgrp, nice, setuid, setgid, setpgrp, getpriority, setpriority, signal, sigvec, sigaction, pause, sigprocmask, sigsuspend, sigsend, kill, wait, setjmp, longjmp Basic Pipe and Advanced IPC Mechanisms: { pipe, dup, dup2, FIFOs with mknod, msgops, shmops, semops Interhost IPC with Sockets: { socket, socketpair, bind, connect, listen, accept, gethostbyname, getsockname, recv, send Miscellaneous System Services: { umask, brk, sbrk, ptrace, prole, mount, umount 22

The UNIX Filesystem Abstraction While the UNIX lesystem is subject to implementation dierences among various vendor platforms, the abstract lesystem is essentially the same for all platforms. The abstract lesystem consists of a region of storage which supports block data transfer (e.g. a disk) and has a type b driver interface. A lesystem is formatted onto a collection of logically contiguous blocks using the mkfs maintenance command. The resulting format generally organizes the blocks into three regions: A superblock to maintain statistics for and pointers to the other two regions which contains a xed number of inodes used as le control blocks (FCBs), and allocated at the rate of exactly one per le object (setting a limit on the number of le objects in a lesystem) An I-list A collection of data/index blocks, used to carry the data and possible indirect index pointers of a le 23

Filesystem Layout /dev/sd0 /dev/sd3 /dev/sd5 /dev/sd7 /dev/sd0 2 3 4 5 X 0 1 2 N-1...... Superblock I-list Data/Index Blocks 24

UNIX inode Organization An inode is used as a le control block and each object in the le system requires one inode. An inode is logically divided into two parts, a collection of le state information and a set of 13 pointers used to point to the le's data/index blocks, it contains: The I-number of this node The type and mode of the le, where mode is the set of read-write-execute privileges and types are as follows: { ordinary (-) { directory (d) { character (c) { block (b) { pipe or FIFO (p) { symbolic link (l) { socket (s) 25

UNIX inode Organization (Cont'd) The number of links to the le The owner's user-id number (UID) The group-id (GID) of the le The number of bytes in the le The device codes of the inode's device The device codes of device inode points to The date and time last accessed The date and time last modied The date and time i-node last modied An array of 13 disk block addresses 26

I-node Layout inode inumber 250 xxxxsgtrwxrwxrwx OTHER STATE INFO DATA DATA DATA 0 1 2 3 4 5 6 7 8 9 10 11 12.. DATA DATA DATA DATA Assume data block is 1BK (2 10 ) and pointers are 4 bytes An INDEX block could then hold 256 pointers (1K/4) Direct - 10 ptrs * 1K = 10 KB 1st Level Indirect - 256 ptrs * 1K + = 266 KB 2nd Level Indirect - 256 ** 2 ptrs * 1K + = 64+ MB 3rd Level Indirect - 256 ** 3 ptrs * 1K + = 16+ GB 27