Unformatted text preview:

ConcurrencySafe FunctionsCPU Events Relative to Real TimeInterruptsSignalsUNIX StandardsFunction Errorsperrorperror Exampleerrnoperror/errno Examplestrerrorstrerror ExampleSuggestionsargv array for mine –c 10 2.0makeargv, first halfmakeargv, second halfargtestmakeargv data structuresThread Safe FunctionsAsync-Signal Safe FunctionsReentrant FunctionsPOSIX.1strtokreadSolutions to read ProblemCh1 ExercisesConcurrency•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 devicesSafe 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 TimeItem TimeScaled Time in Human Terms(100 million times slower)Prrocessor CycleCache AccessMemory AccessContext SwitchDisk AccessTime Quantum 10ns (100MHZ) 30ns 200ns 10,000ns (100s) 10,000,000ns (10ms)100,000,000ns (100ms)1 second3 seconds20 seconds166 minutes11 days116 daysInterrupts•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 timerSignals•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 signalUNIX 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 generatedperror•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<stdio.h>void perror(const char *s)ISO C, POSIX.1, Spec1170perror Example#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>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 lockedperror/errno Example#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>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 errnostrerror Example#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#include <string.h>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 componentargv array for mine –c 10 2.0Argv[][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 <string.h>#include <stdlib.h>/* * 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 numtokensargtest/* Program 1.1 */ #include <stdio.h> #include <stdlib.h> 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’NULLargvptThread Safe FunctionsA function is “thread safe” if it can be safely invoked by multiple threadsAsync-Signal


View Full Document

Chico CSCI 372 - Concurrency

Download Concurrency
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Concurrency and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Concurrency 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?