UNIX Process Execution Environment in Computer Systems II

csc 2405 computer systems ii n.w
1 / 24
Embed
Share

Explore the execution environment for Unix processes, including code vs. executable vs. process concepts, memory layout, program arguments, environment variables, and how to view variables in the environment. Enhance your knowledge of Unix process execution from a system perspective.

  • UNIX
  • Processes
  • Execution Environment
  • Memory Layout
  • Program Arguments

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSC 2405: Computer Systems II Execution Environment for Unix Processes

  2. Recap: Code vs. Executable vs. Process C source code C statements organized into functions Stored as a collection of files (.c and .h) C source code Executable module Binary image generated by compiler Stored as a file (e.g., a.out) compiling Executable Process Instance of a program that is executing Managed by the operating system running Process

  3. Recap: Memory Layout of a Unix Process 0 (machine code) Text (initialized global variables) Data (uninitialized global variables) BSS (dynamically allocated memory) Heap Stack (local variables) High Memory Address

  4. Recap: Program Arguments The main function is the entry point for C program execution int main( int argc, char * argv[]) myprog dog cat frog Example command line: What is argc? What is argv[0]? What is argv[1]?

  5. Program Execution Environment When a UNIX process begins, it receives two pieces of data: The arguments The environment Text Data BSS Heap Used to provide information to the running process In C, both are arrays of strings last array entry is a NULL pointer Stack Command-Line Arguments and Environment Variables Memory Layout of a C Program

  6. Environment Variables Environment variable are, in short, variables that describe the environment in which a process runs They have a name and a value. Example: HOME = /home/michael Used by processes to answer questions like What is the name of the user account? What is my current working directory? Where are the temporary files stored on this machine? Where can I find an executable? Simple way to share configuration settings between multiple processes

  7. Try This Now At the shell prompt type in the Unix command printenv You will see the names and values of the variables in the environment, with one name/value pair per line: SHELL=/bin/bash TERM=xterm-256color LOGNAME=michael HOME=/home/michael PWD=/home/michael/systems etc

  8. The PATH Environment Variable One important environment variable is PATH: PATH=/home/michael/bin:/home/michael/.local /bin:/usr/local/sbin:/usr/local/bin:/usr/sb in:/usr/bin:/sbin:/bin:/usr/games:/usr/loca l/games:/snap/bin This variable has a value consisting of many user directories, separated by colon: these directories are set by the different programs installed on the computer, and they vary widely from system to system

  9. How the shell uses PATH PATH=/home/michael/bin:/home/michael/.local/b in:/usr/local/sbin:/usr/local/bin:/usr/sbin:/ usr/bin:/sbin:/bin:/usr/games:/usr/local/game s:/snap/bin When you type in a command at the shell prompt, the shell looks in the directories listed in PATH in order: /home/michael/bin first /home/michael/.local/bin next and so on, until either the command is found or the search paths are exhausted If the command is found, the shell executes it. If not found, it displays the message "Command not found"

  10. Environment Variables: User and System There are two types of environment variables: user variables, specific to each user account system variables that apply to all user accounts User environment variables Can be edited by the account user (not by other users) Defined in ~/.profile in most Linux distributions System environment variables These are global and cannot be edited by the user Defined in /etc/environment and /etc/profile in most Linux distributions

  11. Do This Now Add the current directory to your PATH environment: Open ~/.profile with gedit or your favorite editor Add this line at the end of the file PATH=".:$PATH Save the file, then re-evaluate ~/.profileusing the shell command source ~/.profile You only need to do this ONCE. From this point on, the shell looks in your current directory first to find a command So you won t need to type in ./in front of your executable name

  12. Reading Environment Variables From C Programs

  13. Process Execution Environment When a UNIX process begins, it receives two pieces of data: The arguments The environment Text Data BSS Heap Used to provide information to the running process In C, both are arrays of strings last array entry is a NULL pointer Stack Command-Line Args and Environment Variables Memory Layout of a C Program

  14. Global Variable environ The global variable environ points to the array of environment strings (last entry is NULL) extern char **environ; /* environment array */

  15. Reading the environ Variable One way to access the environment strings is to use the environ variable directly Each environment string is of the form name=value #include <stdio.h> extern char **environ; int main(int argc, char *argv[]) { int i; for (i=0; environ[i] != NULL; i++) printf("%s\n", environ[i]); return 0; }

  16. Sample Output SHELL=/bin/bash TERM=xterm-256color LOGNAME=michael HOME=/home/michael PWD=/home/michael/systems PATH=/home/michael/bin:/home/michael/.local /bin:/usr/local/sbin:/usr/local/bin:/usr/sb in:/usr/bin:/sbin:/bin:/usr/games:/usr/loca l/games:/snap/bin etc

  17. Some Environment Variable Definitions Name Description HOME Your home directory Your login ID LOGNAME PWD The current working directory PATH A list of directories in which to look for executable commands. When you issue a command to the shell, the shell searches sequentially through each directory in the PATH list until it finds an executable program with the name you typed

  18. Environment Example PWD=/mnt/a/michael/systems/shell TZ=US/Eastern HOSTNAME=felix USER=michael MACHTYPE=sparc-sun-solaris2.9 OLDPWD=/mnt/a/michael/Systems LOGNAME=michael SHLVL=1 SHELL=/bin/bash HOSTTYPE=sparc OSTYPE=solaris2.9 HOME=/mnt/a/michael TERM=vt100 PATH=/usr/ccs/bin:/home2/jdk1.5.0/bin:/usr/sbin:/opt /gnu/bin:/usr/bin:/opt/local/bin:/usr/local/bin:.:/u sr/ccs/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/gnu/bi n:/usr2/local/bin:/mnt/local/bin:/usr/ucb SSH_TTY=/dev/pts/6

  19. getenv Call Listing an entire environment is an unusual requirement Typically, the value of a single environment variable is needed Can retrieve the value of a single variable using the getenv call Syntax is (use man getenv): #include <stdlib.h> char *getenv(const char * var); var is the variable to find returns value of variable, or NULL if not found

  20. man Command: Your Best Friend Syntax is man command Shows information about the command Usually it takes multiple pages, so hit Space or Enter to move down, and b (backwards) to move up (one page) When done, hit q to quit Try this now: man getenv What header file is needed for getenv?

  21. getenv Example #include <stdio.h> #include ??? /* what header file is needed? */ int main(void) { char *val; val = getenv("LOGNAME"); if (val == NULL) printf("variable not found\n"); else printf( LOGNAME = \"%s\"\n", val); return 0; }

  22. Some Environment Variables Name Description HOME your home directory your login ID USER HOSTNAME PWD the name of the host computer the current working directory a list of directories in which to look for executable commands PATH

  23. Hands On In your systems directory, create another directory called environ In your directory ~/systems/environ, write a program called env.c that retrieves and prints out the values of the HOME, USER, PWD and PATH environment variables Sample execution on Ubuntu: HOME = /home/michael LOGNAME = michael PWD = /home/michael/systems/environ PATH =/home/michael/bin:/home/michael/.local/bin:/usr/ local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /bin:/usr/games:/usr/local/games:/snap/bin

  24. Recap Environment variables describe the environment in which a process runs Can be system (global) or user (account-specific) When a process begins executing, it receives two arrays of strings: Command-line arguments in the main function Environment values in the environ global variable A process can access environ directly, or can retrieve single environment values using the getenv system call

More Related Content