Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Pointers.
Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Lecture 20 Arrays and Strings
MIPS Assembly Language Programming
The University of Adelaide, School of Computer Science
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
User-Level Memory Management in Linux Programming
System Files and Process Environment Password file Group file System identification Time Process environment.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
[Unix Programming] Process Basic Young-Ju, Han
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Advanced Programming in the UNIX Environment Hop Lee.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
System Calls 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
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.
The Structure of Processes (Chap 6 in the book “The Design of the UNIX Operating System”)
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
System calls for Process management
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Operating Systems Process Creation
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Today’s Material Strings Definition Representation Initialization
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
System calls for Process management Process creation, termination, waiting.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Linux Processes & Threads
Checking Memory Management
Programmazione I a.a. 2017/2018.
Unix Process Course code: 10CS62 Prepared by : Department of CSE.
Chapter 14 - Advanced C Topics
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Pointers.
The Environment of Unix Process
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Pointers.
Presentation transcript:

Unix Process Environment

main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int argc, char *argv[]); argc is the number of arguments and argv is an array of pointers to the arguments.

Process Termination There are five ways for a process to terminate. (1) Normal termination: (a) return from main (b) calling exit (performs certain cleanup processing and then return to the kernal) (c) calling_exit (returns to the kernel immediately) (2) Abnormal termination: (a) calling abort. (b) terminated by a signal.

Exit Function #include void exit (int status); _exit Function #include void_exit (int status);

Environment List An environment list is passed to each program. The environment list, like the argument list, is an array of character pointers, with each pointer containing the address of a null-terminated C string.

For example, if the environment consisted of five strings it could look like:

By convention the environment consists of name = value Memory Layout of a C Program Text Segment This is the machine instructions that are executed by the CPU.

The text segment is often read-only, to prevent a program from accidentally modifying its instructions. Initialized Data Segment It contains variables that are specifically initialized in the program. For example: the C declaration int maxcount = 99; appearing outside any function causes this variable to be stored in the initialized data segment with its initial value.

Uninitialized Data Segment Data in this segment is initialized by the kernel to arithmetic 0 or null pointers before the program starts executing. long sum [1000]; appearing outside any function causes this variable to be stored in the uninitialized data segment. Stack

This is where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to, and certain information about the caller’s environment is saved on the stack. Heap Dynamic memory allocation usually takes place on the heap.

The typical memory arrangement of the segments is as shown in the figure:

Memory Allocation There are three functions specified by ANSI C for memory allocation. (1) malloc: Allocates a specified number of bytes of memory. The initial value of the memory is indeterminate.

(2) calloc: Allocates space for a specified number of objects of a specified size. The space is initialized to all 0 bits. (3) realloc: Changes the size of a previously allocated area (increases or decreases)

Environment Variables ANSI C defines a function that we can use to fetch values from the environment. #include char *getenv(const char *name); Returns: pointer to value associated with name, NULL if not found

Using getenv to fetch a specific value from the environment, instead of accessing environ directly. The description of some of the environment variables and their standards, implementations etc. is shown in the table below : VariableDescription HOME LOGNAME home directory login name

We can also use the functions putenv, setenv and unsetenv to set environment variable VariableDescription PATH TERM TZ list of path searching for executable file terminal type time zone information

The operation of these three functions is as follows: #include int putenv (const char *str); int setenv (const char *name, const char *value, int rewrite); Both return: 0 if OK, nonzero on error void unsetenv (const char *name);

putenv takes a string of the form name = value and places it in the environment list. If the name already exists, its old definition is first removed. setenv sets name to value. If name already exists in the environment then (a) If rewrite is nonzero, the existing definition for name is first removed.

(b) if rewrite is 0, an existing definition for name is not removed (and name is not set to the new value, and no error occurs). unsetenv removes any definition of name. setjmp and longjmp Functions In C, we can’t goto a label that’s in another function. Instead we must use the setjmp and longjmp functions to perform this type of branching.