CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Similar documents
A Unix Process. Joseph Cordina

The Environment of a Unix Process

UNIT 4 UNIX PROCESSES

o Code, executable, and process o Main memory vs. virtual memory

Memory (Stack and Heap)

Dynamic memory allocation

CS631 - Advanced Programming in the UNIX Environment. Process Environment, Process Control

CS631 - Advanced Programming in the UNIX Environment. Process Environment, Process Control

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Lecture 8 Dynamic Memory Allocation

Memory Management I. two kinds of memory: stack and heap

CSC 1600 Memory Layout for Unix Processes"

Process Environment E. Im 1

Advanced Unix Programming Module 06 Raju Alluri spurthi.com

Secure Software Programming and Vulnerability Analysis

Programmation Systèmes Cours 2 Introduction to Process Management

Processes. Johan Montelius KTH

Memory Management. CSC215 Lecture

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

A process. the stack

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic memory. EECS 211 Winter 2019

Programmation Systèmes Cours 2 Process Management Basics

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

POINTER AND ARRAY SUNU WIBIRAMA

Heap Arrays. Steven R. Bagley

Understanding Pointers

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

OS Interaction and Processes

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

14. Memory API. Operating System: Three Easy Pieces

In Java we have the keyword null, which is the value of an uninitialized reference type

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

CSE 509: Computer Security

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

Dynamic memory allocation (malloc)

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

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Operating System Structure

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Arrays and Memory Management

Memory Management. CS449 Fall 2017

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

Carnegie Mellon. C Boot Camp. Oct 6th, Jack Biggs Raghav Goyal Nikhil Jog

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Dynamic Memory Allocation

Exercise Session 3 Systems Programming and Computer Architecture

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

C Structures & Dynamic Memory Management

Common Misunderstandings from Exam 1 Material

211: Computer Architecture Summer 2016

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Advanced practical Programming for Scientists

Carnegie Mellon. C Boot Camp. September 30, 2018

Processes: Introduction. CS 241 February 13, 2012

Dynamic Allocation in C

Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

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

edunepal_info

COMP 2355 Introduction to Systems Programming

Base Component. Chapter 1. *Memory Management. Memory management Errors Exception Handling Messages Debug code Options Basic data types Multithreading

C Programming Basics II

Dynamic Memory Allocation I Nov 5, 2002

Systems Programming and Computer Architecture ( )

TI2725-C, C programming lab, course

CSE 12 Spring 2016 Week One, Lecture Two

Dynamic Memory Management

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

My malloc: mylloc and mhysa. Johan Montelius HT2016

Agenda. Dynamic Memory Management. Robert C. Seacord. Secure Coding in C and C++

Reminder: compiling & linking

Memory and C/C++ modules

Dynamic Allocation of Memory

Dynamic Memory Allocation I

Exceptional Control Flow: Exceptions and Processes

Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

Array Initialization

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

CS201- Introduction to Programming Current Quizzes

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

ME964 High Performance Computing for Engineering Applications

Project 3a: Malloc and Free

A Quick Introduction to C Programming

COSC Software Engineering. Lecture 16: Managing Memory Managers

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Quick review pointer basics (KR ch )

CSCI 4061: Virtual Memory

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

ANITA S SUPER AWESOME RECITATION SLIDES

LSN 3 C Concepts for OS Programming

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

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

Computer Programming Practice (2008 Winter) Practice 10 C Scoping & Pointer

Transcription:

+ CMPS 105 Systems Programming Prof. Darrell Long E2.371 darrell@ucsc.edu

+ Chapter 7: The Environment of a UNIX process

+ Introduction

+ The main() fuction n int main(int argc, char* argv[]); n argc = number of command-line arguments n argv = array of pointers to the (string) arguments n main() is the first thing called in the program n A special start-up routine is called first n This is what sets up the parameters to main

+ Process Termination n Five ways to terminate a process n Normal termination n Return from main() n Call exit() n call _exit() n Abnormal termination n Call abort() n Terminate by a signal

+ exit() and _exit() n exit() Performs a clean shutdown of the standard I/O library n void exit(int status); n _exit() Same as exit except doesn t call atexit() n void _exit(int status); n No cleanup, returns to kernel immediately n Exit status undefined if not specified

+

+ atexit() n atexit() Calls funct without args when program terminates n int atexit(void (*func)(void)); n func is a pointer to a function that takes no parameters n ANSI C: A process can register up to 32 handler functions to execute when the program exits

+

+ Command-line arguments n Programs can pass command-line parameters

+ Environment List n Each program is passed an environment list n Extern char** environ; n Each environment string consists of name=value n Most names are uppercase n Usually ignored, but can be useful n Why? n Historically environment was input to main n int main(int argc, char* argv[], char* envp[]);

+ Environment structure

+ Memory Layout of a C Program n Text segment n The machine instructions of the program n Usually sharable and read-only n Data segment (initialized data) n Global variables that are initialized in the program n BSS (uninitialized data) n Global variables that are not initialized in the program n Initialized to zero or null pointers

+ C program layout cont. n Stack (automatic variables) n Function return information n Local variables n Heap n Dynamic memory allocation

+ Shared Libraries n Single shared copy of common library routines n Instead of each one being copied in each program n Big space savings n 805,175 vs. 1,696 for hello world

+ Memory Allocation n malloc() Allocates the specified number of bytes n void* malloc(size_t size); n Uninitialized n calloc() Allocates space for the specified number of objects n void* calloc(size_t nobj, size_t size); n Initialized to all 0 s n realloc() Changes the size of a previously allocated area n void* realloc(void* ptr, size_t newsize); n May move to a new location (and copy old contents) n New area is uninitialized

+ Memory Allocation n Memory is over-allocated n Additional space at the end of segment for record keeping n Writing past the end or before the start can overwrite the record

+ Freeing memory n free() Frees allocated space n void* free(void* ptr); n Mistakes with free n Call free twice on same pointer n Call free on pointer not obtained from alloc function n Not calling it causes leaks

+ alloca n Allocates memory from stack n Doesn t have to be freed n Doesn t live past the return from the calling function

+ Environment variables n Used by applications only n Not the kernel n Name= value n Common variables: HOME, USER, PRINTER, etc n getenv() gets environment variables n char* getenv(const char* name); n Returns null if not found

+

+ Other environment functions n putenv() creates (or overwrites) environment variable n int putenv(const char* str); n setenv()- same as putenv except does nothing if rewrite = 0 and old value exists n int setenv(const char* name, const char* value, int rewrite); n unsetenv() Clears an environment variable n int unsetenv(const char* name);

+

+ setjmp() and longjmp() n Allow goto s from lower in a call stack to higher in a call stack n setjmp() sets up location to jump to n int setjmp(jmp_buf env) n longjmp() jumps to location set by setjmp() n int longjmp(jmp_buf env, int val); n Parameter contains the environment of the function that will be jumped to n Spaghetti is good (delicious), but not for coding

+

+

+

+ getrlimit() and setrlimit() n getrlimit() get resource limit n int getrlimit(int resource, struct rlimit* rlptr); n setrlimit() set resource limit n int setrlimit(int resource, const struct rlimit* rlptr); struct rlimit{ rlim_t rlim_cur; /* Soft limit */ rlim_t rlim_max; /* hard limit */ };

+