The Environment of a Unix Process

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

A Unix Process. Joseph Cordina

UNIT 4 UNIX PROCESSES

Process Environment E. Im 1

Memory (Stack and Heap)

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

Lecture 8 Dynamic Memory Allocation

Processes. Johan Montelius KTH

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

A process. the stack

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

Secure Software Programming and Vulnerability Analysis

Dynamic memory allocation

Understanding Pointers

Process life and death. Tuesday, September 28, 2010

CSC 1600 Memory Layout for Unix Processes"

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

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

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

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

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

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

14. Memory API. Operating System: Three Easy Pieces

Netric Security Team - Instruction pointer schurken :-) -- By gloomy & The Itch. -- "Radical Environmentalists" Preface

Dynamic Memory Allocation

Memory management. Johan Montelius KTH

Dynamic memory allocation (malloc)

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Memory Management. CSC215 Lecture

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

Programmation Systèmes Cours 2 Process Management Basics

Chapter 14 - Advanced C Topics

Dynamic Memory Allocation I Nov 5, 2002

Advanced Unix Programming Module 06 Raju Alluri spurthi.com

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Memory Management. CS449 Fall 2017

Programmation Systèmes Cours 2 Introduction to Process Management

Dynamic Memory Allocation I

Introduction Programmer Interface User Interface Process Management Memory Management File System I/O System Interprocess Communication

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

CSE 509: Computer Security

Operating System Structure

Advanced practical Programming for Scientists

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

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

Dynamic Memory Allocation: Basic Concepts

CSCI 171 Chapter Outlines

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

Project 3a: Malloc and Free

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Memory Allocation in C

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

POINTER AND ARRAY SUNU WIBIRAMA

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

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Operating systems. Lecture 9

Dynamic memory. EECS 211 Winter 2019

PROCESS MANAGEMENT Operating Systems Design Euiseong Seo

CSE 153 Design of Operating Systems Fall 2018

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.


Tutorial 1 C Tutorial: Pointers, Strings, Exec

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Memory and C/C++ modules

C Legacy Code Topics. Objectives. In this appendix you ll:

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

211: Computer Architecture Summer 2016

edunepal_info

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Fall 2015 COMP Operating Systems. Lab #3

Project 2 Overview: Part A: User space memory allocation

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

CSC 2405: Computer Systems II

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

pthreads CS449 Fall 2017

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

Computer Systems Assignment 2: Fork and Threads Package

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

Memory Organization. The machine code and data associated with it are in the code segment

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

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

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Carnegie Mellon. C Boot Camp. September 30, 2018

Scientific Programming in C IV. Pointers

High Performance Programming Programming in C part 1

PROGRAMMAZIONE I A.A. 2017/2018

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

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

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

Quick review pointer basics (KR ch )

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Dynamic Memory: Alignment and Fragmentation

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

CptS 360 (System Programming) Unit 4: Debugging

Transcription:

Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library 5. Files and Directories 6. System Data Files and Information 7. Environment of a Unix Process 8. Process Control 9. Signals 10.Inter-process Communication The Environment of a Unix Process Objective: How a process is executed and terminates? What the typical memory layout is? Related functions and resource limits. int main(int argc, char *argv[]) A special start-up routine is called to set things up first before call main() Set up by the link editor (invoked by the compiler)

How a C program is started and terminated. _exit _exit user functions return main function return C start-up routine call call (does not return) exit exit (does not return) exit exit function _exit return (does not return)call return call return call exit handler exit handler standard I/O cleanup user process exec kernel Process Termination Five ways to terminate: Normal termination Return from main(). exit(main(argc, argv)); Call exit(). Call _exit() Abnormal termination Call abort() Be terminated by a signal.

Process Termination #include <stdlib.h> void exit(int status); ANSI C Perform cleanup processing Close and flush I/O streams (fclose) #include <unistd.h> void _exit(int status); POSIX.1 Undefined exit status: Exit/return without status. Main falls off the end. #include <stdio.h> main() { printf( hello world\n ); } Process Termination #include <stdlib.h> int atexit(void (*func)(void)); Up to 32 functions called by exit ANSI C, supported by SVR4&4.3+BSD The same exit functions can be registered for several times. Exit functions will be called in reverse order of their registration. Program 7.1 Page 165 Exit handlers

Command-Line Arguments & Environment Variables Command-Line Arguments argv[argc] is NULL under POSIX.1 & ANSI C Program 7.2 Page 166 Environment Variables int main(int agrc, char **argv, char **envp); environment extern char **environ; list getenv/putenv NULL environment strings HOME=/home/stevens\0 PATH=:/bin:/usr/bin\0 SHELL=/bin/sh\0 USER=stevens\0 LOGNAME=stevens\0 Memory Layout Read from program file by exec Pieces of a process Text Segment Read-only usually, sharable Initialized Data Segment int maxcount = 10; Uninitialized Data Segment bss (Block Started by Symbol) Initialized to 0 by exec long sum[1000]; Stack return addr, automatic var, etc. Heap dynamic memory allocation

Memory Layout text structure 4.3BSD per-process kernel stack.u argv, argc, user stack Red Zone 0x7fffffff sp x_caddr proc[i] entry p_textp p_addr p_p0br page table u_proc heap Uninitialized Data (bss) Initialized Data Code Segment PC 0x00000000 Memory Layout proc[i] entry 4.4BSD process grp file descrptors VM space p_addr p_p0br region lists page table per-process kernel stack.u argv, argc, user stack heap Uninitialized Data (bss) Initialized Data Code Segment u_proc

Memory Layout > size ls1 hole ls1: 6971 + 876 + 364 = 8211 hole: 6995 + 896 + 368 = 8259 text data bss Shared Library Why a shared library? Remove common library routines from executable files. Have an easy way for upgrading Problems More overheads Remark: compiling options gcc Supported by many Unix systems

Memory Allocation Three ANSI C Functions: malloc allocate a specified number of bytes of memory. Initial values are indeterminate. calloc allocate a specified number of objects of a specified size. The space is initialized to all 0 bits. realloc change the size of a previously allocated area. The initial value of increased space is indeterminate. #include <stdio.h> #include <stdlib.h> main() { char *ptr; } Memory Allocation ptr = malloc(100); free(ptr); free(ptr); ptr[0] = 'a'; ptr[1]=0; printf("%s - Done\n", ptr); #include <stdlib.h> void *malloc(size_t size); void *calloc(size_t nobj, size_t size); void *realloc(void *ptr, size_t newsize); Suitable alignments for any data obj Generic void * pointer free(void *ptr) to release space to a pool. Leaking problem Free already-freed blocks or blocks not from alloc(). mallopt(m_grainset, value), mallinfo

Memory Allocation Remark realloc() could trigger moving of data avoid pointers to that area! prt == NULL malloc() sbrk() is used to expand or contract the heap of a process a malloc pool Record-keeping info is also reserved for memory allocation do not move data inside. alloca() allocates space from the stack frame of the current function! No needs for free with potential portability problems Environment Variables Name=value Interpretation is up to applications. Setup automatically or manually E.g., HOME, USER, MAILPATH, etc. setenv FONTPATH $X11R6HOME/lib/X11/fonts\:$OPENWINHOME/lib/fonts #include <stdlib.h> char *getenv(const char *name); Figure 7.4 environment variables ANSI C function, but no ANSI C environment variable.

Environment Variables #include <stdlib.h> int putenv(const char *name-value); Remove old definitions if they exist. int setenv(const char *name, const char *value, int rewrite); rewrite = 0 no removing of existing names. int unsetenv(const char *name); Remove any def of the name No error msg if no such def exists. Environment Variables Adding/Deleting/Modifying of Existing Strings Modifying The size of a new value <= the size of the existing value overwriting Otherwise; malloc() & redirect the ptr Adding The first time we add a new name malloc of pointer-list s room & update envron Otherwise; copying. realloc() if needed. The heap segment could be involved.

setjmp and longjmp stack frame for main stack frame for do_line stack frame for cmd_add Objective: goto to escape from a deeply nested function call! Program 7.3 Program Skeleton line cmd token What if cmd_add() suffers a fatal error? How to return to main() to get the next line? Note: Automatic variables are allocated within the stack frames! setjmp and longjmp #include <setjmp.h> int setjmp(jmp_buf env); int longjmp(jmp_buf env, int val); Return 0 if called directly; otherwise, it could return a value val from longjmp(). env tends to be a global variable. longjmp() unwinds the stack and affect some variables. Program 7.4 Page 178 setjmp and longjmp

setjmp and longjmp Automatic, Register, and Volatile Variables Compiler optimization Register variables could be in memory. Values are often indeterminate Normally no roll back on automatic and register variables Shown in Program 7.5 later Global and static variables are left alone when longjmp is executed. Portability Issues! setjmp and longjmp Program 7.5 Page 179 Effects of longjmp Variables stored in memory have their values unchanged no optimization Potential Problems with Automatic Variables Program 7.6 (Page 180) Never be referenced after their stack frames are released.

getrlimit and setrlimit #include <sys/time.h> #include <sys/resource.h> int getrlimitint resource, struct rlimit *rlptr); int setrlimitint resource, const struct rlimit *rlptr); Not POSIX.1, but supported by SVR4 and 4.3+BSD Rules: Soft limit <= hard limit, the lowering of hard limits is irreversible. Only a superuser can raise a hard limit. struct rlimit { rlim_t rlim_cur; /* soft limit */ rlim_t rlim_max; /* hard limit */ } getrlimit and setrlimit Resources (SVR4&4.3BSD) RLIMIT_CORE (both), RLIMIT_CPU (both, SIGXCPU), RLIMIT_DATA (both), RLIMIT_FSIZE (both, SIGXFSZ), RLIMIT_MEMLOCK (4.3+BSD, no implementation), RLIMIT_NOFILE (SVR4, _SC_OPEN_MAX), RLIMIT_NPROC (4.3+BSD, _SC_CHILD_MAX), RLIMIT_OFILE (4.3+BSD=RLIMIT_NOFILE), RLIMIT_RSS (4.3+BSD, max memory resident size), RLIMIT_STACK (both), RLIMIT_VMEM (SVR4)

getrlimit and setrlimit Resource Limits inheritance by processes Built-in commands in shells umask, chdir, limit (C shall), ulimit H and S (Bourne shell and KornShell) Program 7.7 Page 183 Resource limits #define RLIM_NLIMITS 7 doit(rlimit_core) = pr_limits( RLIMIT_CORE, RLIMIT_CORE) #define doit(name) pr_limits(#name, name)