Midterm ReviewC BasicsThe “&” Operator: Reads “Address of”PointersThe “*” Operator Reads “Variable pointed to by”ArraysStrings (Null-terminated Arrays of Char)String OperationsMath: Increment and Decrement OperatorsMath: Increment and Decrement Operators on PointersSystem CallsExamples of System CallsSystem Calls for I/OI/O Library CallsProcessesProcess IdentificationCreating a Process – fork()Chain and FanProcess Terminationwait() FunctionZombie RemovalOrphansexec() FunctionThreadsThreads: Lightweight ProcessesTrade-offs?Hybrid Implementations (Solaris)SynchronizationCritical Region (Critical Section)Critical Region RequirementsCritical Region RequirementsSlide 32Lock VariablesTurn-based Mutual Exclusion with Strict AlternationOther Flag Mutual ExclusionTwo Flag Mutual ExclusionTwo Flag and Turn Mutual ExclusionSemaphoresExampleMutexPOSIX Mutex-related FunctionsSchedulingPreemptive vs. Non-preemptive schedulingScheduling ObjectivesPerformance CriteriaSimple Processor Scheduling AlgorithmsSignalsExamples of POSIX Required SignalsSlide 49Command Line Generates SignalsCommand Line Generates SignalsTimers Generate SIGALRM SignalsExamples: Programming SignalsProgramming SignalsSignal MasksSignal SetsSIGPROCMASKExample: Add SIGINT to Set of Blocked SignalsCatching and Ignoring Signals - SIGACTIONWaiting for SignalssigsuspendUse sigwaitTimersTiming a function (Example 1)TimePOSIX XSIRealtime clocksSleep Functions (Example 6)NanosleepPOSIX:XSI Interval TimersProducer-ConsumerProducer-Consumer ProblemDining PhilosophersDeadlockSlide 75Resource Allocation GraphStrategiesThe Default SolutionDeadlock PreventionDeadlock AvoidanceIs Allocation (1 0 2) to P1 Safe?Run Safety TestAllocate to P1, ThenRelease - P1 FinishesRelease - P3 FinishesRelease - P4 FinishesRelease - P2 FinishesSo P1 Allocation (1 0 2) Is SafeIs allocation (0 2 0) to P0 Safe?Slide 90So Unsafe State- Do Not EnterP0 Suspended Pending RequestDeadlock DetectionRecovery From DeadlockCopyright ©: Nahrstedt, Angrave, Abdelzaher 1Midterm ReviewTarek AbdelzaherCopyright ©: Nahrstedt, Angrave, Abdelzaher2C BasicsQuestions of the form:Is this code snippet correct?Find all the 5 bugs in the given code snippetWhat is the value of X, Y, Z at the end of the given code snippetetc.Copyright ©: Nahrstedt, Angrave, Abdelzaher310,00010,00210,00810,01010,012…The “&” Operator:Reads “Address of”Value1Value2Value3Value4Value5xyzpdNameValue&yCopyright ©: Nahrstedt, Angrave, Abdelzaher410,00010,00210,00810,01010,012…PointersValue1Value2Value310,002Value5xyzpdNameValueA pointer is a variable whose value is the address of anotherCopyright ©: Nahrstedt, Angrave, Abdelzaher510,00010,00210,00810,01010,012…The “*” OperatorReads “Variable pointed to by”Value1Value2Value310,002Value5xyzpdNameValueA pointer is a variable whose value is the address of another*PCopyright ©: Nahrstedt, Angrave, Abdelzaher6Arraysint p[5]; p[0]p[1]p[2]p[3]p[4]Name of array (is a pointer)pShorthand:*(p+1) is called p[1]*(p+2) is called p[2]etc..Copyright ©: Nahrstedt, Angrave, Abdelzaher7Strings (Null-terminated Arrays of Char)Examplechar s[5];Strings are arrays that contain the string characters followed by a “Null” character to indicate end of string.Do not forget to leave room for the null characters[0]s[1]s[2]s[3]s[4]sCopyright ©: Nahrstedt, Angrave, Abdelzaher8String OperationsstrcpystrlensrtcatstrcmpCopyright ©: Nahrstedt, Angrave, Abdelzaher9Math: Increment and Decrement OperatorsExample 1:int x, y, z, w;y=10; w=2;x=++y;z=--w;Example 2:int x, y;y=10; w=2;x=y++;z=w--;First increment/decrement then assign resultx is11, z is 1First assign then increment/decrementx is 10, z is 2Copyright ©: Nahrstedt, Angrave, Abdelzaher10Math: Increment and Decrement Operators on PointersExample 1:int a[2];int number1, number2, *p;a[0]=1; a[1]=10; a[2]=100; p=a;number1 = *p++;number2 = *p;What will number1 and number2 be at the end?Hint: ++ increments pointer p not variable *pCopyright ©: Nahrstedt, Angrave, Abdelzaher11System CallsfnCall()ProcessCaller and callee are in the sameProcess - Same user - Same “domain of trust”Function CallsysCall()ProcessSystem CallOS- OS is trusted; user is not.- OS has super-privileges; user does not- Must take measures to prevent abuseCopyright ©: Nahrstedt, Angrave, Abdelzaher12Examples of System CallsExample:getuid() //get the user IDfork() //create a child processexec() //executing a programDon’t mix system calls with standard library callsDifferences?Is printf() a system call?Is rand() a system call?Copyright ©: Nahrstedt, Angrave, Abdelzaher13System Calls for I/OThere are 5 basic system calls that Unix provides for file I/Oint open(char *path, int flags [ , int mode ] ); (check man –s 2 open)int close(int fd); int read(int fd, char *buf, int size); int write(int fd, char *buf, int size); off_t lseek(int fd, off_t offset, int whence); They look like regular procedure calls but are differentA system call makes a request to the operating system. A procedure call just jumps to a procedure defined elsewhere in your program. Some library calls may themselves make a system call(e.g. fopen() calls open())Copyright ©: Nahrstedt, Angrave, Abdelzaher14I/O Library CallsEach system call has analogous procedure calls from the standard I/O library:System Call Standard I/O callopen fopenclose fcloseread/write getchar/putchargetc/putcfgetc/fputcfread/fwritegets/putsfgets/fputsscanf/printffscanf/fprintflseek fseekCopyright ©: Nahrstedt, Angrave, Abdelzaher15ProcessesPossible process statesRunning (occupy CPU)BlockedReady (does not occupy CPU)Other states: suspended, terminatedQuestion: in a single processor machine, how many process can be in running state?Copyright ©: Nahrstedt, Angrave, Abdelzaher16Process IdentificationUNIX identifies processes via a unique Process ID Each process also knows its parent process ID since each process is created from a parent process. Root process is the ‘init’ process‘getpid’ and ‘getppid’ – functions to return process ID (PID) and parent process ID (PPID)Example 1#include <stdio.h>#include <unistd.h>int main (void) {printf(“I am process %ld\n”, (long)getpid());printf(“My parent id %ld\n”, (long)getppid());return 0; }Copyright ©: Nahrstedt, Angrave, Abdelzaher17Creating a
View Full Document