Unix System Overview ( Programmer’s Perspective) Chien-Chung Shen CIS/UD

Slides:



Advertisements
Similar presentations
CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CSCC69: Operating Systems
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
CSE 451 Section 4 Project 2 Design Considerations.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Unix System Overview Programmer’s Perspective Chien-Chung Shen CIS, UD
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
System Calls 1.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 6 System Calls OS System.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
The Structure of Processes (Chap 6 in the book “The Design of the UNIX Operating System”)
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Processes: program + execution state
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
System calls for Process management
Navigating Directories
Λειτουργικά Συστήματα - Lab1 Γιάννης Πετράκης. The Operating System  Unix is a layered operating system  The innermost layer is the hardware that provides.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Advanced Programming in the UNIX Environment Hop Lee.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Chapter 1 Introduction  What is an operating system  History of operating systems  The operating system zoo  Computer hardware review  Operating system.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
System calls for Process management Process creation, termination, waiting.
1 CSE 451 Section 2: Processes, the shell, and system calls.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Exceptional Control Flow
Implementation of a simple shell, xssh (Section 1 version)
Protection of System Resources
UNIX System Overview.
CS 3305 System Calls Lecture 7.
Processes in Unix, Linux, and Windows
Structure of Processes
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
System Structure B. Ramamurthy.
Processes in Unix, Linux, and Windows
System Structure and Process Model
System Calls David Ferry CSCI 3500 – Operating Systems
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Presentation transcript:

Unix System Overview ( Programmer’s Perspective) Chien-Chung Shen CIS/UD

Introduction to Operating Systems Why: it’s a ugly piece of hardware (Liskov's Turning Award lecture: The Power of Abstraction )Liskov's Turning Award lecture: The Power of Abstraction How: system calls and kernel implementations What: provide services for programs they run –Typical services: execute a new program open/read a file allocate memory obtain current time of day etc. –Programmer ’ s perspective Use Unix as an example

Unix Architecture OS is the software that (1) controls the hardware resources of the computer and (2) provides an environment under which program can run A.k.a. kernel (residing at the core) System calls: a layer of software providing the interface to the kernel Library functions, shells, application programs, etc.

Shells After login, a shell starts running A command-line interpreter that reads user input and executes commands –input from terminal: interactive shell –input from file: shell script On MLB (Linux on Intel processor) Name Path Bourne shell /usr/bin/sh Bourne-again shell /usr/local/gnu/bin/bash Cshell /usr/bin/csh Korn shell /usr/bin/ksh TENEX C shell /usr/bin/tcsh

Files and Directories UNIX file system is a hierarchical arrangement of directories and files starting from the root “ / ” A directory is a file that contains “directory entries” [files or (sub)-directories] Each directory entry has name and attributes: size, type, owner, permissions, etc. – obtained via system calls stat() / fstat(). and.. in each directory Relative and absolute pathnames –Relative to the current directory –Begin with “ / ” Home and (current) working directory

List All File Names in a Directory #include int main(int argc, char *argv[]) { DIR *dp; struct dirent *dirp; if (argc != 2) err_quit("usage: ls directory_name"); if ((dp = opendir(argv[1])) == NULL) err_sys("can ’ t open %s", argv[1]); while ((dirp = readdir(dp)) != NULL) printf("%s\n", dirp->d_name); closedir(dp); exit(0); } Bare-bone implementation of the ls(1) command Fig. 1.3 of Stevens’ book opendir(), readdir(), and closedir() are standard C library functions $ man 1 ls $ man –s 1 ls

Input and Output When opening an existing file or creating a new file, kernel returns a file descriptor (a non-negative integer) for reading/writing the file When executing a new program, shell opens three (3) standard file descriptors: stdin, stdout, stderr Normally, they are all connected to the “terminal” Redirection: –ls > file.list –a.out myOutput STDIN_FILENO (0), STDOUT_FILENO (1), STDERR_FILENO (2) are defined in /usr/include/unistd.h and used via #include Figure 1.4 (end-of-file == ^D ) – e.g.,./mycat > data –[ read() & write() (unbuffered I/O) are system calls]

Standard I/O Standard I/O functions provide buffered interface to unbuffered I/O functions ( read(), write(), etc.) Can deal with lines of input, and not worry about BUFFERSIZE #include e.g., fgets() reads an entire line printf() Figure 1.5 with getc() and putc()

Programs and Processes Program: an executable file residing on disk in a directory A program is read into memory and is executed by the kernel as a result of one of the six (6) exec() system calls Process: an executing instance of a program Every process has a unique identifier called process ID Figure 1.6: getpid()

Process Control Three primary functions: fork(), exec(), waitpid() Figure 1.7 End-of-file ( ^D ) causes fget() to return a null pointer What kind of program is Figure 1.7?

Unix Process Management Parent Child Set up context [priority and I/O] for the program that is about to be started (e.g., close some files, open others, reduce priority, etc.) Bring the new executable image into memory and start it running

Error Handling When an error occurs from executing a Unix system function, either -1 or null pointer is returned, and errno is set a value that tells why File defines symbol errno and assumed constants (error constants are listed in errno(3) manual page) Standard C library functions for printing error messages: strerror() and perror() Figure 1.8 Usefulness of argv[0] in $ prog1 output ( tell which of the 3 programs generates the error message) Rules of using errno –its value is never cleared by a routine if an error does not occur. Therefore, we should examine its value only when the return value from a function indicates that an error occurred –the value of errno is never set to 0 by any of the functions, and none of the constants defined in has a value of 0

Signals “Software interrupts” to notify events –ignore –default action: e.g., process terminates –“catching” signals via user-defined functions Control-C, kill, kill() Figure 1.10

Time Calendar time: number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), January 1, 1970, not counting leap seconds: C type time_t –e.g., used to record when a file was last modified –$ date +%s Process (CPU) time: measure the CPU usage in clock ticks (50, 60, or 100 ticks per second, found out via sysconf() ): C type clock_t The time(1) command $ cd /usr/include $ time grep _POSIX_SOURCE */*.h > /dev/null

Why System Calls Look Like Function Calls? You may wonder why a call to a system call, such as open() or read(), looks exactly like a typical function call in C; that is, if it looks just like a function call, how does the system know it’s a system call, and do all the right stuff?

System Calls and Library Functions “All operating systems provide service points through which programs request services from the kernel. All implementations of the UNIX System provide a well- defined, limited number of entry points directly into the kernel called system calls. “The system call interface has always been documented in Section 2 of the UNIX Programmer’s Manual. Its definition is in the C language, regardless of the actual implementation technique used on any given system to invoke a system call. This differs from many older operating systems, which traditionally defined the kernel entry points in the assembler language of the machine.” “man page” on Wikipedia

System Calls and Library Functions “The technique used on UNIX systems is for each system call to have a function of the same name in the standard C library. The user process calls this function, using the standard C calling sequence. This function then invokes the appropriate kernel service, using whatever technique is required on the system. For example, the function may put one or more of the C arguments into general registers and then execute some machine instruction [trap] that generates a software interrupt in the kernel. For our purposes, we can consider the system calls to be C functions.”

System Calls and Library Functions “Section 3 of the UNIX Programmer ’s Manual defines the general-purpose functions available to programmers. These functions aren’t entry points into the kernel, although they may invoke one or more of the kernel’s system calls. For example, the printf function may use the write system call to output a string, but the strcpy (copy a string) and atoi (convert ASCII to integer) functions don’t involve the kernel at all.”

System Calls and Library Functions “From an implementor’s point of view, the distinction between a system call and a library function is fundamental. But from a user’s perspective, the difference is not as critical. From our perspective in this text, both system calls and library functions appear as normal C functions. Both exist to provide services for application programs. We should realize, however, that we can replace the library functions, if desired, whereas the system calls usually cannot be replaced.”

How about malloc() ? Which one? A.Unix system call B.Standard C library function $ man malloc

malloc() and sbrk() malloc() : standard C library function sbrk() : system call

Why System Calls Look Like Function Calls? System call is a C function call, but hidden inside that function call is the trap instruction. More specifically, when you call open() (for instance), you are executing a function call into the C library. Therein, whether for open() or any of the other system calls provided, the library uses an agreed-upon calling convention with the kernel to put the arguments to open() in well-known locations (e.g., on the stack, or in specific registers), puts the system-call number into a well-known location as well (again, onto the stack or a register), and then executes the aforementioned trap instruction. The code in the library after the trap unpacks return values and returns control to the program that issued the system call. Thus, the parts of the C library that make system calls are hand-coded in assembly, as they need to carefully follow convention in order to process arguments and return values correctly, as well as execute the hardware-specific trap instruction. And now you know why you personally don’t have to write assembly code to trap into an OS; somebody has already written that assembly for you.