The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
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
© 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
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
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.
C Programs Preprocessor commands –#ifdef –#include –#define Type and macro definitions Variable and function declarations Exactly one main function Function.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
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.
Run-time Environment and Program Organization
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Advanced Programming in the UNIX Environment Hop Lee.
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.
System Calls 1.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
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.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
10/29/2015 Asst.Prof.Muhammed Cinsdikici 1 Network Programming UBI 510 Chapter 1.
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.
CE Operating Systems Lecture 10 Processes and process management in Linux.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
System calls for Process management
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
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,
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
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.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Tutorial 4. In this tutorial session we’ll see Threads.
Process Tables; Threads
Chapter 3: Process Concept
Process Memory COMP 40: Machine Structure and
Protection of System Resources
Linux Processes & Threads
Using Processes.
Programmazione I a.a. 2017/2018.
Lecture 5: Process Creation
Unix Process Course code: 10CS62 Prepared by : Department of CSE.
Chapter 14 - Advanced C Topics
More examples How many processes does this piece of code create?
Processes in Unix, Linux, and Windows
System Structure and Process Model
Process Tables; Threads
Dynamic Memory Allocation
Memory Allocation CS 217.
The Environment of Unix Process
Lecture 6: Multiprogramming and Context Switching
Processes in Unix and Windows
CS510 Operating System Foundations
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Presentation transcript:

The Environment of a UNIX Process

Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables Process termination

Process A Process is program (eg., a.out) under execution in a UNIX or POSIX system. For Example: a UNIX shell is a process that is created when a user logs on to a system. More over, when a user enters a command cat foo to a shell prompt, the shell creates a new process. In UNIX terminology, this is a child process, which executes the cat command on behalf of user. When a process creates a child process, it becomes the parent process of the child. The child process inherits many attributes from its parent process, and it is scheduled by the UNIX kernel to run independently from its parent.

Process (Cont…) It’s also possible to create multiple processes that run concurrently, an Operating system can serve multiple users and perform multiple tasks concurrently. Thus the process creation and management are the concern stone of a multi-user and multitasking operating system like UNIX.

main Function int main(int argc, char *argv[]) argc = no. of command line arguments argv[] = array of pointers to the arguments

main Function (Cont…) C program started by kernel (by one of the exec functions) Kernel executes a special START-UP routine before main() The executable program file specifies this start-up routine as the starting address for the program-this is set up by the link editor when it is invoked by the C compiler, usually cc Startup-routine – –Takes values from the kernel (command-line arguments and environment – –Start-up routine sets things up before main() is called: stack, heap, etc.

Process Termination 5 ways Normal termination: Normal termination: –return from main() –calling exit() –calling _exit() Abnormal termination Abnormal termination –calling abort() –terminated by a signal

exit() and _exit() #include #include void exit(int status); #include #include void _exit(int status); These two functions terminate a program normally : _exit, which returns to the kernel immediately. _exit, which returns to the kernel immediately. exit, which performs certain cleanup processing and the returns to the kernel exit, which performs certain cleanup processing and the returns to the kernel fclose() all open streams fflush() returns to kernel immediately

exit() and _exit() (cont..) Both the exit and _exit functions expect a single integer argument, which we call the exit status. Both the exit and _exit functions expect a single integer argument, which we call the exit status. Most UNIX shells provide a way to examine the exit status of a program, if Most UNIX shells provide a way to examine the exit status of a program, if –A) either of these functions is called without an exit status –B) main does a return without a return value –C) main “falls off the end”(an implicit return), the exit status of the process is undefined. This means that the classic example #include<stdio.h>main(){ printf(“Hello World !”); printf(“Hello World !”);} –Is incomplete, since the main function falls off the end, returning to the c start-up routine, but without returning a value(the exit status). Adding either return(0); or exit(0); –Provides an exit status 0 to the process that executed this program(often a shell). –int main(void)

atexit(): Exit Handler SYNOPSIS SYNOPSIS #include #include int atexit( void (*function)(void)); int atexit( void (*function)(void)); DESCRIPTION DESCRIPTION –The atexit() function registers the given function to be called at normal process termination, either via exit(0) or via return from the program's main(). exit(0) –Functions so registered are called in the reverse order of their registration; no arguments are passed. –The same function may be registered multiple times: it is called once for each registration. –Can register up to 32 functions.

atexit(): Exit Handler (Cont..) RETURN VALUE RETURN VALUE –The atexit() function returns the value 0 if successful; otherwise it returns a non- zero value. With ANSI C and POSIX.1, exit first calls the exit handlers and then fcloses all open streams. With ANSI C and POSIX.1, exit first calls the exit handlers and then fcloses all open streams.

Now lets see How a C program is started and how it terminates

– When a C program is started by the kernel (by one of the exec functions), a special start-up routine is called before the main function is called. – The executable program files specifies this start-up routine as the starting address for the program-that is when it is invoked by the C compiler, usually CC. – This start-up routine takes values from the kernel(the command- line arguments and the environment) and sets things up so that the main function is called.

Program 7.1: Exit Handlers #include "ourhdr.h" static voidmy_exit1(void), my_exit2(void); int main(void) { if (atexit(my_exit2) != 0) err_sys("can't register my_exit2"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); printf("main is done\n"); return(0);}

Environment List Each program is also passed an environment List. Each program is also passed an environment List. Like the argument list, the environment list is an array of character pointers. Like the argument list, the environment list is an array of character pointers. With each pointer containing the address of null terminated C string. With each pointer containing the address of null terminated C string. The Address of the array of pointers is contained in the global variables environ. The Address of the array of pointers is contained in the global variables environ. extern char **environ;

Environment List (Cont…) For example, if the environment list consisted of five strings it could look like this. For example, if the environment list consisted of five strings it could look like this.

Memory Layout of C Program Text segment: Machine instructions executed by CPU. (read-only, sharable) Initialized data segment: e.g. int maxcount = 99; (initialized!) Uninitialized data segment: This segment is often called the “bss” segment. (bss: block started by symbol) e.g. long sum[1000]; Stack: automatic variables, function calling information, context-switch information, (recursive functions) Heap: dynamic memory allocation

Memory Layout of C Program (Cont..)

size $ size /bin/cc /bin/sh text data bss dec hex /bin/cc a000 /bin/sh The fourth and fifth columns are the total of the sizes in decimal and hexadecimal

Shared Libraries Shared libraries remove the Common library routines from executable files. Shared libraries remove the Common library routines from executable files. Maintain the Single copy of library routines in the memory that all processes reference. Maintain the Single copy of library routines in the memory that all processes reference. This reduces the size of each executable file but many add some run-time overhead, either when the program is first executed, or the first time each shared library function is called. This reduces the size of each executable file but many add some run-time overhead, either when the program is first executed, or the first time each shared library function is called. Another advantage of shared libraries is that library functions can be replaced with new versions without having to re-link edit every program that uses the library. Another advantage of shared libraries is that library functions can be replaced with new versions without having to re-link edit every program that uses the library.

Memory Allocation There are three functions specified by ANSI C for memory allocation. There are three functions specified by ANSI C for memory allocation. –malloc() - allocates a specified number of bytes of memory. The initial value of the memory is undetermined. –calloc() – allocates apace for a specified number of objects of a specified size. The space is initialized to all 0 bits. –realloc() – Changes the size of a previously allocated area( increases or decreases). Initial value of any additional space is indeterminate. All three return pointer suitably aligned for any kind of data.