Today s class. Review of more C Operating system overview. Informationsteknologi

Similar documents
Operating System Overview. Chapter 2

C programming basics T3-1 -

Operating System. Operating System Overview. Structure of a Computer System. Structure of a Computer System. Structure of a Computer System

A process. the stack

Processes. Johan Montelius KTH

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

211: Computer Architecture Summer 2016

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Lecture 03 Bits, Bytes and Data Types

PROGRAMMAZIONE I A.A. 2017/2018

Operating System Overview. Chapter 2

Fundamental of Programming (C)

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Contents. A Review of C language. Visual C Visual C++ 6.0

Course organization. Course introduction ( Week 1)

Arrays and Pointers in C. Alan L. Cox

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Today s class. Finish review of C Process description and control. Informationsteknologi. Tuesday, September 18, 2007

Operating System. Operating System Overview. Layers of Computer System. Operating System Objectives. Services Provided by the Operating System

Operating System Overview. Operating System

File IO and command line input CSE 2451

Organization of a file

PRINCIPLES OF OPERATING SYSTEMS

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Arrays, Strings, & Pointers

CSE 303: Concepts and Tools for Software Development

POINTER & REFERENCE VARIABLES

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Today s Learning Objectives

Today s class. Finish computer system overview Review of more C. Informationsteknologi. Thursday, September 6, 2007

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Dynamic memory allocation

Pointers and File Handling

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSE 333 SECTION 3. POSIX I/O Functions

Operating Systems: Overview and Introduction

Programming & Data Structure

25.2 Opening and Closing a File

File Handling. 21 July 2009 Programming and Data Structure 1

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

CSCI 171 Chapter Outlines

C++ for Java Programmers

Why learn C (after Java)? C for Java programmers. What does this C program do? Simple Example. Goals of this tutorial

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Introduction to Operating. Chapter Chapter

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Lecture 07 Debugging Programs with GDB

Lecture 7: file I/O, more Unix

DECLARAING AND INITIALIZING POINTERS

Introduction to Operating Systems. Chapter Chapter

Understanding Pointers

Class Information ANNOUCEMENTS

PROGRAMMAZIONE I A.A. 2017/2018

Lecture 04 Introduction to pointers

File I/O BJ Furman 23OCT2010

ENG120. Misc. Topics

Software Development With Emacs: The Edit-Compile-Debug Cycle

Systems Programming and Computer Architecture ( )

ECE264 Spring 2013 Exam 1, February 14, 2013

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Introduction to file management

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Example: Pointer Basics

CS1003: Intro to CS, Summer 2008

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Language comparison. C has pointers. Java has references. C++ has pointers and references

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

just a ((somewhat) safer) dialect.

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Chapter 2 Operating System Overview

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Programming in C. Pointers and Arrays

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Function a block of statements grouped together, to perform some specific task

Kurt Schmidt. October 30, 2018

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

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

First of all, it is a variable, just like other variables you studied

CSE 333 Lecture 2 Memory

1. Introduction to Operating Systems

Multiple Choice Questions ( 1 mark)

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Arrays and Pointers (part 2) Be extra careful with pointers!

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Arrays and Pointers (part 2) Be extra careful with pointers!

OPERATING SYSTEM SUPPORT (Part 1)

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Transcription:

Today s class Review of more C Operating system overview Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 1

Review of more C

File handling Open a file using fopen Returns a file pointer which is used to access the file Modes Read(r) error if file does not already exist. File pointer at the beginning of file. Write(w) create a new file (overwrite old one). File pointer at the beginning of file. Append(a) create a new file if file does not exist. Preserve the contents if file does exist and pointer at the end of the file. fprintf, fscanf, fclose Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 3

Example 8 #include <stdio.h> int main(int argc, char *argv[]) { FILE *infile=null; /* Declare a file pointer */ infile = fopen( test.txt, w ); /* open file for writing*/ if(infile == NULL){ /* need to do explicit ERROR CHECKING */ exit(1); } /* write some data into the file */ fprintf(infile, Hello there ); /* don t forget to release file pointer */ fclose(infile); } return 0; Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 4

Reading until end of file int feof(file *) The function is defined in stdio.h Returns a non-zero value if end of file has been reached, and zero otherwise. Sample code: fscanf(infile, "%d", &int1); // Try to read while (feof(infile) == 0 ){ //If there is data, enter loop printf("%d \n", int1); //Do something with the data fscanf(infile, "%d", &int1); //Try reading again } //go back to while to test if data was read Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 5

Functions why and how If a problem is large Modularization easier to code debug Code reuse Passing arguments to functions By value By reference Returning values from functions By value By reference Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 6

Functions basic example Example 9 #include <stdio.h> int sum(int a, int b); /* function prototype at start of file */ int main(int argc, char *argv[]) { int total = sum(4,5); /* call to the function */ } printf( The sum of 4 and 5 is %d\n, total); int sum(int a, int b){ /* the function itself - arguments passed by value*/ return (a+b); /* return by value */ } Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 7

Memory layout and addresses int x = 5, y = 10; float f = 12.5, g = 9.8; char c = r, d = s ; x y f g c d 5 10 12.5 9. 8 r s 4300 4304 4308 4312 4316 4317 Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 8

Pointers made easy float f; // data variable - holds a float float *f_addr; // pointer variable holds an address to f? 4300 // a float f_addr? 4304 NULL f_addr = &f; // & = address operator f f_addr? 4300 4300 4304 Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 9

*f_addr = 3.2; // indirection operator or dereferencing float g=*f_addr; // indirection: g is now 3.2 f f_addr 3.2 4300 3.2 4300 4304 4308 f = 1.3; f f_addr 1.3 4300 4300 4304 g Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 10

Pointer operations Creation int *ptr; Pointer assignment/initialization ptr = &i; (where i is an int and &i is the address of i) ptr = iptr; (where iptr is a pointer to an int) Pointer indirection or dereferencing i = *ptr; (i is an int and *ptr is the int value pointed to by ptr) Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 11

Example 10 #include <stdio.h> int main(int argc, char *argv[]) { int j; int *ptr; ptr=&j; /* initialize ptr before using it */ /* *ptr=4 does NOT initialize ptr */ *ptr=4; /* j <- 4 */ j=*ptr+1; /* j <-??? */ } return 0; Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 12

Pointers and arrays int p[10], *ptr* ptr; ; // Both p and ptr are pointers // i.e. can hold addresses. // p is already pointing to a // fixed location and cannot // be changed. ptr is still // to be initialized. p[i] ] is an int value. p, &p[i& p[i] ] and (p+i( p+i) ) are addresses or pointers. *p is the same as p[0] (They are both int values) *(p+i p+i) ) is the same as p[i] ] (They are both int values) Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 13

Pointer arithmetic ptr = p; // or ptr = &p[0] ptr +=2; // ptr = ptr + 2 * sizeof(int) = ptr+8 bytes // ptr = 3000 + 8 = 3008 => ptr = &(p[2]); ERROR: p = ptr; because p is a constant address, points to the beginning of a static array. Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 14

Operating system overview

Operating System A program that controls the execution of application programs An interface between applications and hardware Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 16

Operating System Objectives Convenience Makes the computer more convenient to use Efficiency Allows computer system resources to be used in an efficient manner Ability to evolve Permit effective development, testing, and introduction of new system functions without interfering with service Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 17

Layers of Computer System Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 18

Services Provided by the Operating System Program development Editors and debuggers Program execution Access to I/O devices Controlled access to files System access Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 19

Services Provided by the Operating System Error detection and response Internal and external hardware errors Memory error Device failure Software errors Arithmetic overflow Access forbidden memory locations Operating system cannot grant request of application Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 20

Services Provided by the Operating System Accounting Collect usage statistics Monitor performance Used to anticipate future enhancements Used for billing purposes Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 21

Operating System Responsible for managing resources Functions same way as ordinary computer software It is a program that is executed Operating system relinquishes control of the processor for other software to run and depends on the processor to allow it to regain control Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 22

Memory Operating System Software Computer System I/O Controller I/O Controller I/O Devices Printers, keyboards, digital camera, etc. Programs and Data Processor I/O Controller Processor Storage OS Programs Data Figure 2.2 The Operating System as Resource Manager Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 23

Kernel Portion of operating system that is in main memory Contains most frequently used functions Also called the nucleus Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 24

Evolution of an Operating System Hardware upgrades plus new types of hardware New services Fixes Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 25

Memory Protection User program executes in user mode Certain instructions may not be executed Monitor executes in system, or kernel, mode Privileged instructions are executed Protected areas of memory may be accessed Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 26

I/O Devices Slow Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 27

Multiprogramming When one job needs to wait for I/O, the processor can switch to the other job Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 28

Time Sharing Using multiprogramming to handle multiple interactive jobs Processor s time is shared among multiple users Multiple users simultaneously access the system through terminals Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 29