Week 4 - Friday CS222.

Slides:



Advertisements
Similar presentations
CS 497C – Introduction to UNIX Lecture 11: - The File System Chin-Chih Chang
Advertisements

Road Map Introduction to object oriented programming. Classes
Linux+ Guide to Linux Certification, Second Edition
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Guide To UNIX Using Linux Third Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
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.
Introduction to Linux Installing Linux User accounts and management Linux’s file system.
Filesystem Hierarchy Standard (FHS) –Standard of outlining the location of set files and directories on a Linux system –Gives Linux software developers.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
1 Lecture 2 Working with Files and Directories COP 3344 Introduction to UNIX.
Unix Basics Chapter 4.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
CSC 322 Operating Systems Concepts Lecture - 4: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Linux+ Guide to Linux Certification, Second Edition
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) 
Chapter 3 & 6 Root Status and users File Ownership Every file has a owner and group –These give read,write, and execute priv’s to the owner, group, and.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
1 © 2001 John Urrutia. All rights reserved. Chapter 4 The LINUX Filesystem.
Linux Commands C151 Multi-User Operating Systems.
Slide: 1 UNIX FILE SYSTEM By:Qing Yang ID: Operating System Research Topic December, 2000.
1 Lecture 2 Working with Files and Directories COP 3353 Introduction to UNIX.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
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.
ORAFACT The Linux File System. ORAFACT Filesystem Support Support for dozens of filesystem types including: Minix, ext2, MS-DOS, UMSDOS, VFAT, NTFS, NFS,
Company LOGO Security in Linux PhiHDN - VuongNQ. Contents Introduction 1 Fundamental Concepts 2 Security System Calls in Linux 3 Implementation of Security.
Linux Filesystem Management
Week 4 - Wednesday CS222.
3 Introduction to Classes and Objects.
C Functions -Continue…-.
Linux file system "On a UNIX system, everything is a file;
Lecture 2 Working with Files and Directories
Week 5 - Monday CS222.
UNIX System Overview.
C++ Classes & Object Oriented Programming
Week 4 - Monday CS222.
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Exploring the UNIX File System and File Security
Processes in Unix, Linux, and Windows
Packages and Interfaces
Chapter 7 File and file System structure
PHP.
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Scope Rules and Storage Types
CISC/CMPE320 - Prof. McLeod
Processes in Unix, Linux, and Windows
Object Oriented Programming in java
Compiler vs linker The compiler translates one .c file into a .o file
Chapter 4: The Linux Filesystem
The Three Attributes of an Identifier
January 26th, 2004 Class Meeting 2
SPL – PS1 Introduction to C++.
Week 2 - Friday CS222.
Week 7 - Friday CS222.
Week 5 - Wednesday CS222.
Introduction to Classes and Objects
Presentation transcript:

Week 4 - Friday CS222

Last time What did we talk about last time? Functions Systems programming

Questions?

Project 2

Quotes Unix never says "please." It also never says: Rob Pike "Thank you" "You're welcome" "I'm sorry" "Are you sure you want to do that?"

More Systems Programming Stuff

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

Lab 4

Upcoming

Next time… Processes Arrays

Reminders Read K&R chapter 5 Keep working on Project 2