Unix System Programming - Chapter 2, part a

Similar documents
Unix System Programming - Chapter 8

Parallel Programming Languages COMP360

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Sharing Objects Ch. 3

Assignment 2. Purpose. Points. Part I : Command Line parsing. Description. The purpose of this assignment is two-fold

CS 153 Design of Operating Systems Winter 2016

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Preview. The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

D Programming Language

Java Threads and intrinsic locks

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Suggested Solutions (Midterm Exam October 27, 2005)

SmartHeap for Multi-Core

Concurrent Server Design Multiple- vs. Single-Thread

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

Programming Languages

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Guidelines for Writing C Code

Threads. lightweight processes

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

CSE 153 Design of Operating Systems Fall 2018

CS 450 Exam 2 Mon. 11/7/2016

CSE 153 Design of Operating Systems

Concurrency, Thread. Dongkun Shin, SKKU

THREADS: (abstract CPUs)

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

CSci 4061 Introduction to Operating Systems. (Thread-Basics)

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

CS4411 Intro. to Operating Systems Exam 1 Fall points 9 pages

CSE 333 SECTION 3. POSIX I/O Functions

High Performance Computing Course Notes Shared Memory Parallel Programming

Midterm Exam Solutions and Grading Guidelines March 3, 1999 CS162 Operating Systems

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

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Lecture 4: Memory Management & The Programming Interface

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Vector and Free Store (Pointers and Memory Allocation)

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Operating Systems. Sina Meraji U of T

CS510 Operating System Foundations. Jonathan Walpole

CS 5460/6460 Operating Systems

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

CS516 Programming Languages and Compilers II

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Last class: Today: Thread Background. Thread Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

IT 540 Operating Systems ECE519 Advanced Operating Systems

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

CSE 153 Design of Operating Systems Fall 18

Operating System Structure

Process Synchronization and Communication

Programming with Shared Memory. Nguyễn Quang Hùng

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

pthread Tutorial August 23, Creating and Destroying Threads Creating Threads Returning Results from Threads...

Intro to POSIX Threads with FLTK

CSE 374 Programming Concepts & Tools

Programming in Parallel COMP755

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Parallel Programming using OpenMP

Experience with Processes and Monitors in Mesa. Arvind Krishnamurthy

Parallel Programming using OpenMP

Concurrent Programming Lecture 10

Lecture 8: September 30

CMPS 111 Spring 2013 Prof. Scott A. Brandt Midterm Examination May 6, Name: ID:

Contents of Lecture 3

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

CS-537: Midterm Exam (Spring 2001)

Lecture 3: C Programm

Operating Systems (1DT020 & 1TT802)

IT 540 Operating Systems ECE519 Advanced Operating Systems

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Introduction to OS Synchronization MOS 2.3

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

AC59/AT59/AC110/AT110 OPERATING SYSTEMS & SYSTEMS SOFTWARE DEC 2015

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

POSIX / System Programming

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

OS Extensibility: SPIN and Exokernels. Robert Grimm New York University

CS 3733 Operating Systems

CS533 Concepts of Operating Systems. Jonathan Walpole

POSIX Threads: a first step toward parallel programming. George Bosilca

Transcription:

Unix System Programming - Chapter 2, part a Neal Nelson The Evergreen State College Mar 23, 2010

USP Chapter 2.1 to 2.6 Processes and Threads Program Storage and Linkage Library Function Calls Error Handling Robbins Rules for Functions Argument Arrays Assignment for Week 1, part a, makeargv function

Processes and Threads Process State of Execution Address Space Resources (open files, etc). Thread State of Execution (including stack) Threads of a Process Share address space (stacks not shared) Share resources Need explicit synchronization and mutual exclusion

Threads in USP Here s where threads show up in USP POSIX pthreads in Chapter 12 Signal handlers We want thread-safe code when threads are possible

Program Image Program Image CommandLineArgs EnironmentVariables Stack highaddress Heap UninitializedStaticData InitializedStaticData ProgramText lowaddress

Program Image Initialized Static Data - saved and loaded with program text i n t myarray [ 5 0 0 0 0 ] = { 1, 2, 3, 4 } ; i n t main ( void ) { myarray [ 0 ] = 3 ; return 0 ; } Uninitialized Static Data - allocated upon load i n t myarray [ 5 0 0 0 0 ] ; i n t main ( void ) { myarray [ 0 ] = 3 ; return 0 ; } The first program image on disk is 200,000 bytes larger with initialized static data

Storage Classes in C - Object Lifetimes allocated - programmer controlled malloc and free allocated in heap space static - duration of program Variables declared outside of blocks With or without the static keyword! automatic - duration of activation record Variables declared within blocks Includes value parameters and stack return values Pointers may be automatic, but what they point to usually isn t

Linkage Classes in C - Visibility Rules external linkage Public Globally shared with other files internal linkage Private to file Shared within file no linkage Private (local) to block Not shared

The static keyword and its dual meaning static keyword can 1. Change storage class (object lifetime) 2. Change linkage class (visibility) static on block-local variables changes lifetime to program static on outside-block variables changes visibility to file private static on functions changes visibility to file private

Storage and Linkage Summary (USP Apx A.5 p814) Object declared outside block 1. storage class is static 2. static keyword changes linkage class Object declared inside block 1. Linkage class is none 2. static keyword changes storage class

Static Storage Class and Threads We want thread-safe code Static storage objects are not thread-safe You must enforce mutual exclusion using concurrency tools Use Locks, critical regions, semaphores, etc.

Signal Handler Variables We worry in USP about thread-safe code even without explicit threads Signal handlers are concurrent threads possible in all programs Only local variables (automatic storage class) are intrinsically safe Non-local variables are not safe Allocated storage class (programmer allocated by malloc/free). Static storage class Carefully analyze all non-local variables for thread safety

Using Library Function Calls Man pages are in the book for convenience (Apx A.1) Always consult the man pages on your system Use man -k blah to search man pages Man pages tell you the include files you need Include files are not shown in the listings in this presentation Look ahead to the USP POSIX threads Chapter 12 pp431-432 for important notes about thread safety of lib calls

Library Function Call Errors You must always check and handle error returns from library calls Traditional Unix 1 on return indicates error (sometimes Null). Traditional Unix sets global variable errno (USP p432) Traditional Unix errno may not be thread-safe (consult man errno and again USP pp432) New POSIX library functions return an error number. There is an error string associated with each errno Names for error numbers are given in man errno errno is wiped out by the very next error.

Handling library function errors with perror Don t forget the proper include files There s a string associated with each errno that perror uses i n t f i l d e s ; i f ( c l o s e ( f i l d e s ) == 1) p e r r o r ( F a i l e d to c l o s e the f i l e ) ; Returns, eg, F a i l e d to c l o s e the f i l e : i n v a l i d f i l e d e s c r i p t o after the colon is the errno string

Handling library function errors with strerror strerror returns the errno string more elaborate error messages can be printed function calls like strerror and other lib calls (or your own functions) can wipe out errno i n t f i l d e s ; i f ( c l o s e ( f i l d e s ) == 1) { e r r o r = e r r n o ; / s a v e e r r n o p r i v a t e l y / f p r i n t f ( s t d e r r, F a i l e d to c l o s e f i l e d e s c r i p t o r %d : %s \n, f i l d e s, s t r e r r o r ( e r r n o ) ) ; e r r n o = e r r o r ; / r e s t o r e e r r n o / }

Signal interrupts and library function errors Signal interrupts can cause library EINTR errno, but they aren t really errors Sample code for library call restart on USP p29 Uses while loop until no error, or an error other than EINTR The book provides a restart library for convenience, r close

USP error handling guidelines (p29 bottom) All your functions must check and handle library call errors Functions should not exit on their own (except main) Return errors to caller (ie, propogate errors to caller) Don t change process state outside your function, eg, if you block signals, then unblock them. Release all resources that you allocate, eg, if you malloc, then you free Be sure you release resources at all error returns as well as normal returns.

Robbins rules for writing functions Make a checklist of the guidelines for writing functions in Robbins and Robbins USP pp30-31. Check off each of the guideline items for each of your functions.

A few Robbins rules we haven t mentioned Do not make unnecessary assumptions about sizes of buffers. (This is often hard to implement.) When it is necessary to use limits, use standard system-defined limits rather than arbitrary constants. Do not modify input parameter values unless it makes sense to do so. Do not use static variables or dynamic memory allocation if automatic allocation will do just as well. Consider whether a function is ever called recursively or from a signal handler or from a thread. Functions with variables of static storage class may not behave in the desired way. (The error number can cause a big problem here.) Analyze the consequences of interruption by signals. Carefully consider how the entire program terminates.

Argument Arrays Some sample makeargv code Textbook makeargv code (USP p37) Sherri s makeargv code Neal s makeargv code Your makeargv code Assignment 1, part a, makeargv function 1. Analyze your makeargv code from fall quarter against the Robbins checklist. Analyze Neal s if you can t find yours. Specifically list which numbered items in the checklist are violated. 2. Choose one of the makeargv functions and modify it to work with USP Program 2.1 p34. 3. Assignment details listed on the USP assignments page.