Week 4 - Wednesday CS222.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Guide To UNIX Using Linux Third Edition
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Week 4 - Wednesday.  What did we talk about last time?  Evil: break, continue, goto  System calls  Functions.
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
Introduction to Shell Script Programming
Linux in More Detail Shirley Moore CPS5401 August 29,
CSC 322 Operating Systems Concepts Lecture - 4: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Guide to Linux Installation and Administration, 2e1 Chapter 7 The Role of the System Administrator.
Linux Operations and Administration
Chapter 2: Linux & POSIX “She sells bash shells by the C shore”
Users Greg Porter V1.0, 26 Jan 09. What is a user? Users “own” files and directories Permission based on “ownership” Every user has a User ID (UID) 
ITR3 lecture 6: intoduction to UNIX Thomas Krichel
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
CS 346 – Chapter 2 OS services –OS user interface –System calls –System programs How to make an OS –Implementation –Structure –Virtual machines Commitment.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Company LOGO Security in Linux PhiHDN - VuongNQ. Contents Introduction 1 Fundamental Concepts 2 Security System Calls in Linux 3 Implementation of Security.
Chapter 1: Introduction to Computers and Programming
History of C and basics of Programming
Memory Management.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Protecting Memory What is there to protect in memory?
Week 4 - Friday CS222.
Chapter Objectives In this chapter, you will learn:
Department of Computer Engineering
Operating Systems Moti Geva
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Week 5 - Monday CS222.
UNIX System Overview.
The Linux Operating System
Processes in Unix, Linux, and Windows
Week 4 - Monday CS222.
Programmazione I a.a. 2017/2018.
Exceptions and files Taken from notes by Dr. Neil Moore
Chapter 1: Introduction to Computers and Programming
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
An Introduction to UNIX System --- Cosc513 Presentation
Chapter 2: System Structures
Processes in Unix, Linux, and Windows
Exceptions and files Taken from notes by Dr. Neil Moore
Memory Management Tasks
Dr. Bhargavi Dept of CS CHRIST
Scope Rules and Storage Types
Focus of the Course Object-Oriented Software Development
Week 1 – Lesson 2: Creating Shell Scripts, Linux Commands
Processes in Unix, Linux, and Windows
Chapter 2: Operating-System Structures
CSE 153 Design of Operating Systems Winter 2019
Variables in C Topics Naming Variables Declaring Variables
Compiler vs linker The compiler translates one .c file into a .o file
The Three Attributes of an Identifier
SPL – PS1 Introduction to C++.
Week 5 - Wednesday CS222.
Presentation transcript:

Week 4 - Wednesday CS222

Last time What did we talk about last time? Selection Loops Evil: break, continue, goto

Questions?

Project 2

Quotes Unix is user-friendly. It just isn't promiscuous about which users it's friendly with. Steven King (Not Stephen King)

Systems Programming

Kernel When people say OS, they might mean: The whole thing, including GUI managers, utilities, command line tools, editors and so on Only the central software that manages and allocates resources like the CPU, RAM, and devices For clarity, people use the term kernel for the second meaning Modern CPUs often operate in kernel mode and user mode Certain kinds of hardware access or other instructions can only be executed in kernel mode

What does the kernel do? Manages processes Manages memory Creating Killing Scheduling Manages memory Usually including extensive virtual memory systems File system activities (creation, deletion, reading, writing, etc.) Access to hardware devices Networking Provides a set of system calls that allow processes to use these facilities

System calls A system call is a way to ask the kernel to do something Since a lot of interesting things can only be done by the kernel, system calls must be provided to programmers via an API When making a system call, the processor changes from user mode to kernel mode There is a fixed number of system calls defined for a given system

glibc The most common implementation of the Standard C Library is the GNU C Library or glibc Some of the functions in the glibc perform systems calls and some do not There are slight differences between the versions of the glibc Microsoft also has an implementation of the Standard C Library that doesn't always behave the same

Handling system errors There are no exceptions in C Instead, when a system call fails, it usually returns -1 To find out why the system call failed First, make sure you #include <errno.h> Then check the value of the integer errno in your program after the system call fails Use the man pages to determine what a given value of errno means The perror() function is often used to print errors instead of printf() It sends the output to stderr instead of stdout

Error handling example #include <stdio.h> #include <fcntl.h> #include <errno.h> int main(){ int fd = open("eggplant.txt", O_WRONLY | O_CREAT | O_EXCL); if (fd == -1) { perror("Failure to create file: "); if( errno == EACCES ) perror("Insufficient privileges\n"); else if( errno == EEXIST ) perror("File already exists\n"); else perror("Unknown error\n"); exit(EXIT_FAILURE); } return 0;

System types C has a feature called typedef which allows a user to give a new name to a type System types are often created so that code is portable across different systems The most common example is size_t, which is the type that specifies length It's usually the same as unsigned int There are named types for process IDs (pid_t), group IDs (gid_t), user IDs (uid_t), time (time_t), and many others

Functions

Anatomy of a function definition type name( arguments ) { statements }

Differences from Java methods You don't have to specify a return type But you should int will be assumed if you don't If you start calling a function before it has been defined, it will assume it has return type int and won't bother checking its parameters

Prototypes Because the C language is older, its compiler processes source code in a simpler way It does no reasonable typechecking if a function is called before it is defined To have appropriate typechecking for functions, create a prototype for it Prototypes are like declarations for functions They usually come in a block at the top of your source file

Prototype example #include <stdio.h> int root(int value); //integer square root int main() { int output = root(19); printf("Value: %d\n", output); return 0; } int root(int value) { int i = 0; while( i*i <= value ) i++; return i – 1; Parameter names in the prototype are optional (and don't have to match) Both of the following work: int root(int); int root(int blah); You can also declare a prototype locally (inside a function), but there isn't a good reason to do so

Insanity If your method takes nothing, you should put void in the argument list of the prototype Otherwise, type checking is turned off for the arguments double stuff(); int main() { double output = stuff(6.4, "bang"); //legal return 0; } double stuff(void); int main() { double output = stuff(6.4, "bang"); //error return 0; }

Return values C does not force you to return a value in all cases The compiler may warn you, but it isn't an error Your function can "fall off the end" Sometimes it works, other times you get garbage int sum(int a, int b) { int result = a + b; return result; } int sum(int a, int b) { int result = a + b; }

Programming practice Let's write a function that: Takes an unsigned integer as a parameter Returns the location of the highest 1 bit in the integer (0-31) or -1 if the integer is 0 Parameter Return Value -1 2 1 3 2000 10 4294967295 31

Examples of functions doing bad things Functions without return types Functions with return types but no return statements Functions that take in arbitrary parameters

More Systems Programming Stuff

Shells A shell is a program written to take commands and execute them Sometimes called a command interpreter This is the program that manages input and output redirection By default, one of the shells is your login shell, the one that automatically pops up when you log in (or open a terminal) It's a program like any other and people have written different ones with features they like: sh The original Bourne shell csh C shell ksh Korn shell bash Bourne again shell, the standard shell on Linux

Users and groups On Linux, every user has a unique login name (user name) and a corresponding numerical ID (UID) A file (/etc/passwd) contains the following for all users: Group ID: first group of which the user is a member Home directory: starting directory when the user logs in Login shell Groups of users exist for administrative purposes and are defined in the /etc/group file

Superusers The superuser account has complete control over everything This account is allowed to do anything, access any file On Unix systems, the superuser account is usually called root If you are a system administrator, it is recommended that you do not stay logged in as root If you ever get a virus, it can destroy everything Instead, administrators should log in to a normal account and periodically issue commands with elevated permission (often by using sudo)

Single file system In Windows, each drive has its own directory hierarchy C: etc. In Linux, the top of the file system is the root directory / Everything (including drives, usually mounted in /mnt) is under the top directory /bin is for programs /etc is for configuration /usr is for user programs /boot is for boot information /dev is for devices /home is for user home directories

Files There are regular files in Linux which you can further break down into data files and executables (although Linux treats them the same) A directory is a special kind of file that lists other files Links in Linux are kind of like shortcuts in Windows There are hard links and soft links (or symbolic links) File names can be up to 255 characters long Can contain any ASCII characters except / and the null character \0 For readability and compatibility, they should only use letters, digits, the hyphen, underscore, and dot Pathnames describe a location of a file They can start with / making them absolute paths Or they are relative paths with respect to the current working directory

File permissions Every file has a UID and GID specifying the user who owns the file and the group the file belongs to For each file, permissions are set that specify: Whether the owner can read, write, or execute it Whether other members of the group can read, write, or execute it Whether anyone else on the system can read, write, or execute it The chmod command changes these settings (u is for owner, g is for group, and o is everyone else)

File I/O All I/O operations in Linux are treated like file I/O Printing to the screen is writing to a special file called stdout Reading from the keyboard is reading from a special file called stdin When we get the basic functions needed to open, read, and write files, we'll be able to do almost any kind of I/O

Processes A process is a program that is currently executing In memory, processes have the following segments: Text The executable code Data Static variables Heap Dynamically allocated variables Stack Area that grows and shrinks with function calls A segmentation fault is when your code tries to access a segment it's not supposed to A process generally executes with the same privileges as the user who started it

Scope

Scope The scope of a name is the part of the program where that name is visible In Java, scope could get complex Local variables, class variables, member variables, Inner classes Static vs. non-static Visibility issues with public, private, protected, and default C is simpler Local variables Global variables

Local scope Local variables and function arguments are in scope for the life of the function call They are also called automatic variables They come into existence on the stack on a function call Then disappear when the function returns Local variables can hide global variables

Global scope Variables declared outside of any function are global variables They exist for the life of the program You can keep data inside global variables between function calls They are similar to static members in Java int value; void change() { value = 7; } int main() value = 5; change(); printf("Value: %d\n", value); return 0;

Use of global variables Global variables should rarely be used Multiple functions can write to them, allowing inconsistent values Local variables can hide global variables, leading programmers to think they are changing a variable other than the one they are Code is much easier to understand if it is based on input values going into a function and output values getting returned

Hiding If there are multiple variables with the same name, the one declared in the current block will be used If there is no such variable declared in the current block, the compiler will look outward one block at a time until it finds it Multiple variables can have the same name if they are declared at different scope levels When an inner variable is used instead of an outer variable with the same name, it hides or shadows the outer variable Global variables are used only when nothing else matches Minimize variable hiding to avoid confusion

extern declarations What if you want to use a global variable declared in another file? No problem, just put extern before the variable declaration in your file There should only be one true declaration, but there can be many extern declarations referencing it Function prototypes are implicitly extern file2.c int count; extern int count; file1.c file3.c program.c

static declarations The static keyword causes confusion in Java because it means a couple of different (but related) things In C, the static keyword is used differently, but also for two confusing things Global static declarations Local static declarations

Global static variables When the static modifier is applied to a global variable, that variable cannot be accessed in other files A global static variable cannot be referred to as an extern in some other file If multiple files use the same global variable, each variable must be static or an extern referring to a single real variable Otherwise, the linker will complain that it's got variables with the same name

Local static variables You can also declare a static variable local to a function These variables exist for the lifetime of the program, but are only visible inside the method Some people use these for bizarre tricks in recursive functions Try not to use them! Like all global variables, they make code harder to reason about They are not thread safe

Local static example #include <stdio.h> void unexpected() { static int count = 0; count++; printf("Count: %d", count); } int main() unexpected(); //Count: 1 unexpected(); //Count: 2 unexpected(); //Count: 3 return 0;

The register modifier register int value; You can also use the register keyword when declaring a local variable It is a sign to the compiler that you think this variable will be used a lot and should be kept in a register It's only a suggestion You can not use the reference operator (which we haven't talked about yet) to retrieve the address of a register variable Modern compilers are better at register allocation than humans usually are register int value;

Compiling multiple files All real programs are written in multiple files To compile such files, do the following Create a header file (with a .h extension) for every file that contains prototypes for functions you're going to use in other files #include those header files in every file that uses them When you run gcc, put all the .c files needed on the line at the same time

Multiple compilation example Your main() function and core program is in a file called program.c You have files called networking.c and graphics.c that have networking and graphics functions that your program uses You should have headers called networking.h and graphics.h (names don't have to match, but it is better if they do) At the top of program.c should be: To run gcc you type: #include "networking.h" #include "graphics.h" gcc program.c networking.c graphics.c -o program

Or you can use compile but not link You can compile a file into object code without linking it into an executable Produces a .o file That way, you can compile all the pieces separately and then link them later This can be more efficient if you are updating some of the code but not all of it To compile but not link, use gcc -c We could compile the previous example as follows gcc –c program.c gcc –c networking.c gcc –c graphics.c gcc program.o networking.o graphics.o -o program

Makefiles to the rescue Now that we're talking about compiling multiple files, a makefile really makes (ha, ha) sense all: program program: program.o networking.o graphics.o gcc program.o networking.o graphics.o -o program program.o: program.c networking.h graphics.h gcc –c program.c networking.o: networking.c gcc –c networking.c graphics.o: graphics.c gcc –c graphics.c clean: rm -f *.o program

Quiz

Upcoming

Next time… Users, groups, and files Scope Compiling multiple files with makefiles Processes Lab 4

Reminders Read LPI chapter 6 Keep working on Project 2