CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
1 Introduction to UNIX 2 Ke Liu
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 Tuesday, June 13, Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man.
Advanced Programming in the UNIX Environment Hop Lee.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Lecture Topics: 11/3 Address spaces Memory protection Creating a process –NT style –Unix style.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
1 Logging in to a UNIX System init ( Process ID 1 created by the kernel at bootstrap ) spawns getty for every terminal device invokes our login shell terminal.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
Creating and Executing Processes
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
CSC 360- Instructor: K. Wu Interfaces to OS Services.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
Byoung-Jo CHOI Fall 2007 SW Project II Advanced Linux Programming.
Project 2: User Programs Abdelmounaam Rezgui Acknowledgment: The content of some slides is partly taken from Josh Wiseman’s presentation.
CSC552 Fall 2015 week 1 outline Parson, UNIX, and so forth
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
CSCI 330 UNIX and Network Programming
CSC Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
OS Labs 2/25/08 Frans Kaashoek MIT
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
CSCI 4061 Recitation 2 1.
Using Processes.
CSC 382: Computer Security
Programming Assignment 1
Lecture 5: Process Creation
Project1: Unix Shell using Multi-Processing
Fork and Exec Unix Model
Introduction to Linux/UNIX
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Tutorial 3 Tutorial 3.
Tutorial: The Programming Interface
Jonathan Walpole Computer Science Portland State University
Lecture 6: Multiprogramming and Context Switching
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
CSC Java Programming, Fall, 2008
Presentation transcript:

CSC Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Who am I? Dr. Dale E. Parson (a.k.a. Professor Parson), In a previous life I was an AT&T technician and then a Bell Labs software engineer for over 25 years. Also, I consult as Amplified Computing. I have taught at Albright College, Millersville University and Lehigh University.

Who are you? Excited Kutztown University computer science grad students who want to become or may already be Unix System software engineers! You have already taken CSC 352, UNIX: Systems Programming and Administration, or you have permission of the instructor. This course deals with the study of the UNIX operating system, particularly systems programming and administration. Under the former, such topics as UNIX commands, filters, shell scripts, system security, user accounts, system backup and rebooting, and associated utilities are studied. In addition, software procurement, using the WWW, and installation will be illustrated. Under the latter, such topics as file primitives and directory access, system utilities, processes, signals and signal handling, inter-process communication, semaphores, file and record-locking, sockets, and terminal manipulation are studied. Meaningful applications which illustrate the topics will be examined. (Ah, this is the fine print for CSC 352!)

What are we planning to do? We will build multi-process, multi-threaded and distributed software systems in Unix. We will use C++ interface and module construction techniques. The underlying building blocks are mostly C language system calls and library functions. man –s2 write (or read, fork, exec, dup, open, close, pipe) man –s3c fprintf (or fgets, fopen, fclose, popen) We will not use C++ iostream in this course.

References We start with the First Day Handout (syllabus).First Day Handout gcc and g GNU make Invoke gmake on SunOS systems. gdb - Solaris - Use man command for section 1 utilities such as ls and ps. /export/home/faculty/parson/UnixSysProg on bill.kutztown.edu

An simple example makefile makefile in UnixSysProg/lecture1 all: build TARGET = printdemo DEBUG = 1 include $(HOME)/makelib build: $(TARGET) $(TARGET): $(OBJFILES) $(CPPCC) $(OBJFILES) -o clean: subclean /bin/rm -f $(TARGET) $(TARGET).out $(TARGET).dif test: $(TARGET)./$(TARGET) a b c d e > $(TARGET).out diff $(TARGET).out $(TARGET).ref > $(TARGET).dif

makelib in my $HOME/ makelib CPPCC= g++ CXXFILES := $(wildcard *.cxx) OBJFILES := $(subst.cxx,.o,$(CXXFILES)) DEFFLAGS = ifeq ($(DEBUG),1) DEBUGFLAG = -g else DEBUGFLAG = endif INCFLAGS = -I. CPPFLAGS= $(DEFFLAGS) $(INCFLAGS) $(DEBUGFLAG) %.o : %.cxx $(CPPCC) -c $(CPPFLAGS) $< subclean: /bin/rm -f *.o

Some other files in my $HOME/.bash_profile -bash-3.00$ cat.bash_profile export EDITOR=/usr/local/bin/vim export VISUAL=/usr/local/bin/vim alias vi=vim alias make=gmake.vimrc -bash-3.00$ cat.vimrc set ai set ts=4 set sw=4 set expandtab set sta

Interface definitions split source along module / driver boundaries. printargs.h defines the interface to its module #ifndef PRINTARGS_H #define PRINTARGS_H #include /* Function: printargs Prints an array of strings to an output file. Parameters: outfile: output file for writing. stringv: array of \0-terminated strings to be printed. stringc: numbers of strings in stringv Return value: none Side Effects: none Preconditions: The outfile must be a non-NULL, open FILE. stringc must equal the number of strings in stringv. Each string in stringv must be terminated with a \0 character. Postconditions: none (no return values from this function). Invariants: none */ void printargs(FILE *outfile, const char **stringv, const int stringc); #endif

Implement the interface in a.cxx file. printargs.cxx implements the module of printargs.h /* printargs.cxx -- Function implementations for printargs.h. CSC552, Fall, 2008, Dr. Dale Parson, Class 1 example. This is a simple demo library module that prints an array of strings to an output file. */ #include "printargs.h" void printargs(FILE *outfile, const char **stringv, const int stringc) { int i ; for (i = 0 ; i < stringc ; i++) { fputs(stringv[i], outfile); fputs("\n", outfile); }

Invoke the interface from a client module or test driver. printmain.cxx is the test driver /* printmain.cxx -- main function that invokes printargs. CSC552, Fall, 2008, Dr. Dale Parson, Class 1 example. This is a simple driver for a demo library module that prints an array of strings to an output file. */ #include "printargs.h" int main(int argc, const char *argv[]) { printargs(stdout, argv, argc); return 0 ; }

Makefile-driven testing makefile is structured for testing -bash-3.00$ make clean test /bin/rm -f *.o /bin/rm -f printdemo printdemo.out printdemo.dif g++ -c -I. -g printargs.cxx g++ -c -I. -g printmain.cxx g++ printargs.o printmain.o -o printdemo./printdemo a b c d e > printdemo.out diff printdemo.out printdemo.ref > printdemo.dif

GDB for debugging -bash-3.00$ gdb --args printdemo a b c (gdb) break main Breakpoint 1 at 0x107f4: file printmain.cxx, line 13. (gdb) break printargs Breakpoint 2 at 0x10780: file printargs.cxx, line 14. (gdb) run Starting program: /export/home/faculty/parson/UnixSysProg/lecture1/printdemo a b c Breakpoint 1, main (argc=4, argv=0xffbffc54) at printmain.cxx:13 13printargs(stdout, argv, argc); 14(gdb) p argc 15$1 = 4 16(gdb) p &argc 17$2 = (int *) 0xffbffc34

GDB continued (gdb) cont Continuing. Breakpoint 2, printargs (outfile=0x20b20, stringv=0xffbffc54, stringc=4) at printargs.cxx:14 14 for (i = 0 ; i < stringc ; i++) { (gdb) step 15 fputs(stringv[i], outfile); (gdb) next /export/home/faculty/parson/UnixSysProg/lecture1/printdemo 16 fputs("\n", outfile); (gdb) next 14 for (i = 0 ; i < stringc ; i++) { (gdb) p i $3 = 0 (gdb) p &i $4 = (int *) 0xffbffb6c

Program Image, Sample Layout Figure 2.1. How can we verify? gdb and /proc! high addressargc/argv, env stackactivation recs. return addr., params, saved registers, autos heapmalloc or new uninitialized static data initialized static data low addressprogram text

Looking inside a process Use nm to get static layout information. Supply C address-of operator to gdb. /proc utilities examine process layout pstack [ -F ] PID examines stack pfiles [ -F ] PID examines open files pldd [ -F ] PID examines dynamic libraries pmap [ –xlF ] PID examines memory layout

Process status gdb is debugging printdemo -bash-3.00$ ps -al SUID PID PPIDTIMECMD O :00ps S :01gdb T :00printdemo nm -psC printdemo | sort | more This shows symbol layout in executable printdemo.

C++ interfaces and dynamic loading ~parson/UnixSysProg/lecture1/oo_variant We will walk through code and test execution in class. Important points C++ abstract classes are used as interfaces. Derived C++ concrete classes provide implementation. An abstract factory method uses dynamic loading and run-time type checking (RTTI) to load a concrete derived class and its concrete factory method. An factory method runs a constructor. See man pages for dlopen(), dlsym() and dynamic_cast.

fork / exec / wait example code ~parson/UnixSysProg/lecture1/forkexec We will walk through code and test execution in class. Important points argc / argv command parameter usage Use C string library to parse a command parameter. fork()’s return value indicates parent or child PID. getpid() returns child PID to child; getppid() gets parent. exec() returns only on failure. Use pmap to examine both processes.

Programming practices Always check and handle library return codes! malloc() and new are exceptions that we will discuss. Error logs, exit codes and C++ exceptions are useful. Buffer overruns are annoying and dangerous See man page for gets(), for example. Make malloc/free or new/delete symmetrical. Always use { curly braces } for control blocks. Use both healthy and degenerate tests. » Ignoring these rules will cost you points.