Chapter 7 Process Environment Chien-Chung Shen CIS, UD

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7: User-Defined Functions II
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
© 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
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Reference: / MPI Program Structure.
Chapter 6 Limited Direct Execution
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
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.
C Programs Preprocessor commands –#ifdef –#include –#define Type and macro definitions Variable and function declarations Exactly one main function Function.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
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.
Guide To UNIX Using Linux Third Edition
Advanced Programming in the UNIX Environment Hop Lee.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
File IO and command line input CSE 2451 Rong Shi.
Chapter 14 Memory API Chien-Chung Shen CIS, UD
Chapter 2 Programs, Processes, and Threads Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
C/C++ Programming Environment
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
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,
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”
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Process Management Azzam Mourad COEN 346.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Chapter 7: User-Defined Functions II
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Protection of System Resources
Command Line Arguments
Command line arguments
Command Line Arguments
Threads in C Caryl Rahn.
Boost String API & Threads
Using Processes.
Programmazione I a.a. 2017/2018.
Command Line Arguments
Chapter 14 - Advanced C Topics
The Environment of Unix Process
Programming with Shared Memory
Programming with Shared Memory
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Functions Reasons Concepts Passing arguments to a function
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Presentation transcript:

Chapter 7 Process Environment Chien-Chung Shen CIS, UD

Introduction “Environment” of a process How the main() function is called when the corresponding program is executed How command-line arguments are passed to a program Process termination Memory layout of a process Memory allocation Environment variables

main() Function A C program starts execution from its main() function Prototype of main() int main(int argc, char *argv[]); – argc: # of command-line arguments – argv: an array of pointers to the arguments When a C program is executed by the kernel via exec(), a special start-up routine is called before main() is called

main() Function The executable file (e.g., a.out ) of the program specifies this start-up routine as the starting address for the program This is set up by link editor invoked by C compiler The start-up routine takes values from the kernel (command-line arguments and environment variables), sets things up, and calls main()

Process Termination Normal termination – Return from main() – Call exit() – Call _exit() or _Exit() – Return of the last thread from its start routine – Call pthread_exit() from the last thread Abnormal termination – Call abort() – Receive a signal – Response of the last thread to a cancellation request

Exit Functions #include // ISO C void exit(int status ); void _Exit(int status ); #include // POSIX.1 void _exit(int status ); exit() performs clean shutdown of standard I/0 library: call flcose() for all open streams causing all buffered output data to be flushed (written to files)

Exit Status If (a) exit function is called without a status, (b) main() does a return without a return value, or (c) main() is not declared to return an integer, exit status of the process is undefined If return type of main() is integer, and main() “falls off the end” (implicit return), exit status is 0 exit(0) == return(0) $ echo $? % print exit status

atexit() Function A process can “register” up to 32 functions (termed exit handlers) that are automatically called (in reverse order of registration) by exit() exit() calls exit handlers and then closes all open streams (via fclose() ) Figure 7.3

How C Program Starts and Terminates The only way a C program is executed by the kernel is when exec() is called The only way a process voluntarily terminates is when _exit() or _Exit() is called, either explicitly or implicitly (by calling exit() ) A process can be involuntarily terminated by signals

How C Program Starts and Terminates

Command-Line Arguments When a program is executed, the process that does the exec() can pass command-line arguments to the new program Code in Figure 7.4 argv[argc] is a null pointer for (i = 0; argv[i] != NULL; i++) for (i = 0; I < argc; i++)

Environment List Each program is passed an environment list extern char **environ; an array of character pointers, containing the address of a null-terminated C string

Environment List Each program is also passed an environment list (an array of character pointers, with each pointer containing the address of a null- terminated C string) The address of the array of pointers is contained in the global variable environ extern char **environ; Environment pointer ( environ), environment list, environment strings Each environment string is a name=value string getenv() / putenv()

Memory Layout of a C Program

Shared Library Remove common library routines from executable files – Maintain a single copy of the library routine in memory that all processes reference Reduce the size of each executable file, but add some runtime overhead gcc –static hello1.c // no SL gcc hello1.c // default with SL size a.out

Memory Allocation