Concurrency Sharing of resources in the same time frame Apparent concurrency is sharing the same CPU, memory, or I/O device Real concurrency is sharing.

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSCC69: Operating Systems
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
CS 4284 Systems Capstone Project 2 Hints Slides created by Jaishankar Sundararaman.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Applications Complex applications must: run for weeks or months properly release resources to avoid waste cope with outrageously malicious user input recover.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
OS Spring’03 Introduction Operating Systems Spring 2003.
C Programs Preprocessor commands –#ifdef –#include –#define Type and macro definitions Variable and function declarations Exactly one main function Function.
1 I/O Management in Representative Operating Systems.
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
System Calls 1.
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.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
More on UART Interrupts; System Calls Reference on Interrupt Identification Register(IIR) slide 17 of
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
File IO and command line input CSE 2451 Rong Shi.
Chapter 2 Programs, Processes, and Threads Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Processes and Virtual Memory
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Barriers and Condition Variables
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
I/O Software CS 537 – Introduction to Operating Systems.
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Error handling I/O Man pages
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Process concept.
Sorting Tutorial Using C On Linux.
CS 6560: Operating Systems Design
Protection of System Resources
CS399 New Beginnings Jonathan Walpole.
More on UART Interrupts; System Calls
Project1: Unix Shell using Multi-Processing
System Structure and Process Model
Thread Implementations; MUTEX
Unix System Calls and Posix Threads
CSE 333 – Section 3 POSIX I/O Functions.
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Jonathan Walpole Computer Science Portland State University
Chapter 4 Threads, SMP, and Microkernels
Thread Implementations; MUTEX
CS510 Operating System Foundations
Process Description and Control in Unix
Process Description and Control in Unix
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Concurrency Sharing of resources in the same time frame Apparent concurrency is sharing the same CPU, memory, or I/O device Real concurrency is sharing the same program among several CPUs, memories, and/or I/O devices

Safe Functions Thread-Safe – Can be invoked concurrently or by multiple threads. Async-Signal-Safe – Can be called without restriction from a signal handler. These terms replace the older notion of reentrant function.

CPU Events Relative to Real Time ItemTime Scaled Time in Human Terms (100 million times slower) Prrocessor Cycle Cache Access Memory Access Context Switch Disk Access Time Quantum 10ns ( 100MHZ ) 30ns 200ns 10,000ns (100  s) 10,000,000ns (10ms) 100,000,000ns (100ms) 1 second 3 seconds 20 seconds 166 minutes 11 days 116 days

Interrupts Causes transfer of control to interrupt handling routine Synchronous interrupts are invoked by program system calls Asynchronous interrupts are invoked by external devices such as I/O or timer

Signals Notifies software of an event Signals are often invoked by interrupt handling routine A signal is caught if the process receiving the signal executes an interrupt handling routine (signal handler) for the signal

UNIX Standards ANSI C POSIX Spec 1170 ISO C (a subset of POSIX and Spec 1170)

Function Errors Many functions return –1 on error and set external parameter errno to appropriate error code Include errno.h in order to access the symbolic names associated with errno Use errno only immediately after an error is generated

perror Outputs message string to standard error followed by the error message from the last system or library call that produced an error Place perror immediately after the occurrence of an error SYNOPSIS #include void perror(const char *s) ISO C, POSIX.1, Spec1170

perror Example #include #include #include #include int fd; void main(void) { int fd; if ((fd = open("my.file", O_RDONLY)) == -1) perror("Unsuccessful open of my.file"); }

errno Test errno only if a function call returns an error errno can be set to various values depending on the function it is used with For example, when used with the function open, the value EAGAIN indicates the file is locked

perror/errno Example #include #include #include #include #include int fd; void main(void) { int fd; while (((fd = open("my.file", O_RDONLY)) == -1) && (errno == EAGAIN)) ; if (fd == -1) perror("Unsuccessful open of my.file"); }

strerror Use strerror instead of perror to format a message that contains variable values The message output depends on the value of errno

strerror Example #include #include #include #include #include #include void main(int argc, char *argv[]) { int fd; if ((fd = open(argv[1], O_RDONLY)) == -1) fprintf(stderr, "Could not open file %s: %s\n", argv[1], strerror(errno)); }

Suggestions Make use of return values Do not exit from functions – use error value from function instead Make functions general but usable (conflict?) Do not assume buffer sizes Use standard system defined limits rather than arbitrary constants Don’t re-invent the wheel Don’t modify input parameters unless necessary Don’t use static (global) variables if automatic (local) variables will do just as well Be sure to free memory allocated by malloc Consider recursive calls to functions Analyze consequences of interrupts Careful plan the termination of each program component

argv array for mine –c Argv[] [0] [1] [2] [3] [4]NULL ‘m’‘i’‘n’‘e’‘/0’ ‘-’‘c’‘/0’ ‘1’‘0’‘/0’ ‘2’‘.’‘0’‘/0’

makeargv, first half /* Program 1.2 */ #include #include /* * Make argv array (*arvp) for tokens in s which are separated by * delimiters. Return -1 on error or the number of tokens otherwise. */ int makeargv(char *s, char *delimiters, char ***argvp) { char *t; char *snew; int numtokens; int i; /* snew is real start of string after skipping leading delimiters */ snew = s + strspn(s, delimiters); /* create space for a copy of snew in t */ if ((t = calloc(strlen(snew) + 1, sizeof(char))) == NULL) { *argvp = NULL; numtokens = -1; ; }

makeargv, second half } else { /* count the number of tokens in snew */ strcpy(t, snew); if (strtok(t, delimiters) == NULL) numtokens = 0; else for (numtokens = 1; strtok(NULL, delimiters) != NULL; numtokens++) ; /* create an argument array to contain ptrs to tokens */ if ((*argvp = calloc(numtokens + 1, sizeof(char *))) == NULL) { free(t); numtokens = -1; } else { /* insert pointers to tokens into the array */ if (numtokens > 0) { strcpy(t, snew); **argvp = strtok(t, delimiters); for (i = 1; i < numtokens + 1; i++) *((*argvp) + i) = strtok(NULL, delimiters); } else { **argvp = NULL; free(t); } } } return numtokens

argtest /* Program 1.1 */ #include #include int makeargv(char *s, char *delimiters, char ***argvp); void main(int argc, char *argv[]) { char **myargv; char delim[] = " \t"; int i; int numtokens; if (argc != 2) { fprintf(stderr, "Usage: %s string\n", argv[0]); exit(1); } if ((numtokens = makeargv(argv[1], delim, &myargv)) < 0) { fprintf(stderr, "Could not construct argument array for %s\n", argv[1]); exit(1); } else { printf("The argument array contains:\n"); for (i = 0; i < numtokens; i++) printf("[%d]:%s\n", i, myargv[i]); } exit(0); }

makeargv data structures ‘m’‘i’‘n’‘e’‘\0’‘-’‘c’‘\0’‘1’‘0’‘\0’‘2’‘.’‘0’‘\0’ NULL argvp t

Thread Safe Functions A function is “thread safe” if it can be safely invoked by multiple threads

Async-Signal Safe Functions A function is async-signal safe if that function can be called without restriction from a signal handler

Reentrant Functions A function is reentrant if it is: Thread safe Async-signal safe

POSIX.1 Table 5.3 lists the functions that are guaranteed to be async-signal-safe according to the Posix.1 standard. However, conformance to Posix.1 standards is NOT guaranteed even for those OS that claim to conform, so be sure to check the man pages.

strtok strtok is not thread safe: strtok(NULL, delimiters); strtok_r IS thread safe. It has a user-provided parameter that stores the position of the next call to strtok_r.

read Not reentrant because error information is returned in external variable errno. If read returns –1, errno is set to the appropriate error.

Solutions to read Problem Have the read function return the error value as a function value Have the read function return the error value as a parameter. Implement errno as local to each thread (instead of as a global variable). Implement errno as a macro that invokes a function to get errno for the currently running thread. Change read so that it invokes a signal on error.

Ch1 Exercises Invoke argtest with a makefile. Write a freeargv function. Write a man page for makeargv. Write a new version of strtok called estrtok. /bin:/usr/bin:/usr/local/bin:: Write strtok with a flags parameter.