0% found this document useful (0 votes)
27 views28 pages

04 Process Environment

Uploaded by

mandysorasora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views28 pages

04 Process Environment

Uploaded by

mandysorasora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

CSCI 4061: Processes and Environment

Chris Kauffman

Last Updated:
Mon Feb 8 03:54:59 PM CST 2021

1
Logistics

Reading Labs/HWs
▶ Stevens/Rago, Ch 7-8 ▶ Lab02 / HW02 due Mon
(Procs / Env) ▶ Lab03 on Mon, realloc()
▶ Stevens/Rago Ch 3, 4, 5, 6 function,
(I/O + Files) ▶ HW03: on Mon WNOHANG
and parents
Goals Today
▶ Process Lifecycle Project 1
▶ Killing programs ▶ Up now
▶ Process memory layout ▶ Due Mon 2/22 11:59pm
▶ Command Line Args ▶ Partners allowed
▶ Environment Variables ▶ Will create Piazza post for
▶ Start I/O discussion finding partners

2
Process: A “Running” Program

▶ Most OS’s provide a Process abstraction


▶ Hardware like the CPU just sees a stream of instructions, bits
stored, bytes on disk
▶ OS presents notion of
▶ “These instructions are for this running program”
▶ “This running program owns this part of memory”
▶ “This file was opened by this running program”
▶ One stored program can create many Processes
▶ OS is responsible for managing the lives of Processes with
fairness and security

3
Process Life Cycle

Source: Saverio Perugini, lecture notes

▶ Processes (running programs) can be in one of several states


▶ OS tracks these states and manages transitions between them
▶ OS uses some internal data structure to track process state,
can report states via utilities like top and ps
4
ps and top show running process status
These shell commands show a STAT or S columns corresponding
loosely to process states.
STAT Meaning
Common
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped, either by a job control signal or being traced.
Z defunct (“zombie”) process, terminated but not reaped by parent.
I idle (kernel process/thread only)
Less Common
D uninterruptible sleep (usually IO)
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Source: man page for ps
We’ll continue to discuss Specifics of Zombines and Orphans

5
Handy Commands
▶ top: interactively observe top running processes, usually
sorted by CPU usage
▶ ps: snapshot of running processes filtered on various criteria
▶ watch: repeatedly run a command showing its output on the
screen
Interactively observe all processes sorting by top CPU usage

> top

press q to quit
Watch processes with command name yes refreshing every 0.1
seconds showing u-ser relevant information on the processes

> watch -n 0.1 'ps u -C yes'

Press Ctrl-c to end the watch

6
Terminal: Foreground/Background Processes
▶ Type a program into the terminal, press enter
▶ Stars a process in the foreground of the terminal
▶ Input from user typing, output to terminal screen
▶ Jobs can be run in the background as well
▶ Usually input must come from somewhere aside from user
typing, output should go into a file or it will pollute the
terminal

Key/Cmd Effect
Ctrl-z Stop/Suspend foreground process, gets prompt back
Ctrl-c Terminate foreground process (usually)
ls & Run program in background, gets prompt immediately
bg %2 Moves stopped Job 2 to background and continues it
fg %4 Moves background Job 4 to foreground
jobs List jobs under the control of the terminal
kill %3 End job 3 nicely
kill -9 %3 End job 3 unequivocally
7
Exercise: Basic Job Control
Give a sequence of commands / keystrokes to…
Misbehaving
▶ Compile no_interruptions.c to a program named
invincible
▶ Run invincible
▶ Try to end the process by sending it the interrupt signal
▶ In a separate terminal, end the invicible program

Edit / Build Seq


▶ Edit a source file like collatz_funcs.c with vi
▶ Suspend vi (don’t quit it)
▶ Re-build program and run automated tests
▶ Terminate before completing tests
▶ Bring back vi to edit codes
8
Murdering Processes

Keystrokes to Remember
Ctrl-c Send the interrupt signal, kills most processes
Ctrl-z Send the stop signal, puts process to sleep

Easy to Kill Harder to Kill


▶ yes spits output to the ▶ Consider the program
screen continuously no_interruptions.c
▶ End it from the terminal it ▶ Ignores some common
started in signals
▶ Suspend it then, end it ▶ Need to use the big stick for
▶ Kill it from a different this one:
terminal kill -9 1234 OR
pkill -9 a.out

9
States of a Living Process
▶ Note inclusion of
Kernel/OS here
▶ Interrupt and Sys
Calls start running
code in the operating
system
▶ Interrupt/Signal can
come from software
or hardware
▶ Context switch
starts running another
process, only happens
when one process is
Source: Design of the Unix Operating System by Maurice Bach
safely tucked in and
put to sleep
10
Recall: Program Memory

▶ What are the 4 memory areas to a C program we’ve discussed


OR that you know from previous courses?
▶ Give an example of how one creates variables/values in each
area of memory

11
Answers: Program Memory
▶ What are the 4 memory areas to a C program we’ve discussed
OR that you know from previous courses?
1. Stack: automatic, push/pop with function calls
2. Heap: malloc() and free()
3. Global: variables outside functions, static vars
4. Text: Assembly instructions
▶ Give an example of how one creates variables/values in each
area of memory
1 #include <stdlib.h>
2 int glob1 = 2; // global var
3 int func(int *a){ // param stack var
4 int b = 2 * (*a); // local stack var
5 return b; // de-allocate locals in func()
6 }
7 int main(){ // main entry point
8 int x = 5; // local stack var
9 int c = func(&x); // local stack var
10 int *p = malloc(sizeof(int)); // local stack var that points into heap
11 *p = 10; // modify heap memory
12 glob1 = func(p); // allocate func() locals and run code
13 free(p); // deallocate heap mem pointed to p
14 return 0; // deallocate locals in main()
15 }
16 // all executable code is in the .text memory area as assmebly instructions
12
More Detailed Process Memory

Source: Unix Systems Programming, Robbins & Robbins

13
Yet more detailed view (Link)
A detailed picture of the virtual memory image, by Wolf Holzman
Memory Layout (Virtual address space of a C process)
System High memory Stack illustrated after the call
func(72,73) called from main(),
assuming func defined by:
env func(int x, int y) {

STACK
argv
argc int a;
mfp − frame pointer (for main) int b[3];
auto variables for
main() /* no other auto variables */
auto variables for Assumes int = long = char * of
func()
stack pointer size 4 and assumes stack at high
(grows downward if func() address and descending down.
calls another function)
available for Expanded view of the stack
stack growth
Stack

Offset from current main()


frame pointer (for auto Contents
func()) variables
MEMORY
SHARED

malloc.o (lib*.so) library functions if +12 73 y


dynamically linked +8 72 x
printf.o (lib*.so) (usual case) +4 ra return address
frame pointer 0 mfp caller’s frame pointer
available for points here −4 garbage a
heap growth −8 garbage b[2]
brk point −12 garbage b[1]
−16 garbage b[0]
Heap stack pointer
(malloc arena) (top of stack)
points here

All auto variables and parameters


DATA

global variables uninitialized data (bss) are referenced via offsets from the
frame pointer.

"...%d..." initialized data The frame pointer and stack pointer


are in registers (for fast access).
malloc.o (lib*.a) library functions if
compiled code (a.out)

statically linked When funct returns, the return value


TEXT

printf.o (lib*.a) (not usual case) is stored in a register. The stack pointer
file.o is move to the y location, the code
func(72,73) ra (return address) is jumped to the return address (ra),
main.o and the frame pointer is set to mfp
crt0.o (startup routine) (the stored value of the caller’s frame
Low memory pointer). The caller moves the return
value to the right place.
14
Unix Processes In Memory

▶ Separate Memory Image for


Each Process
▶ OS + Hardware keeps
processes inside their own
address space
▶ Consequence for program
dynamic memory allocation?
▶ Problems with running
system calls?
Source: Tutorials Point

This picture should bother you


Shows a gross simplification but will suffice until later when we
discuss Virtual Memory system which is maintained by the OS
15
Exercise: Memory Problems in C Programs
What you’re up against
▶ Stack problems: References to stack variables that go away
▶ Segmentation Faults: Access memory out of bounds for whole program,
via heap or via stack
▶ Null pointers dereference: Often results in a segfault as NULL translates to
address 0x0000 which is off limits
▶ Use of uninitialized: variables don’t have values by default, assign or get
something random
▶ Memory Leaks: malloc() memory that is not used but never free()’d,
program gobbles more and more memory
▶ Examine results of running overflow.c, EXPLAIN OUTPUT

Solutions
▶ Don’t program in C
▶ Use a tool to help identify and fix problems
▶ Valgrind → FREE for Linux Programs

16
Code for overflow.c
1 // overflow.c: program traverses memory that it really ought not to by
2 // walking off the end of an array into parts unknown.
3
4 #include <stdio.h>
5 int main(int argc, char *argv[]){
6 char a[3] = {'A','B','C'};
7 int i = 0;
8 while(1){
9 printf("%c",a[i]);
10 i++;
11 if(i%40 == 0){
12 printf("\n");
13 }
14 }
15 return 0;
16 }
17
18 // ## COMPILE AND RUN
19 // > gcc overflow.c
20 // > ./a.out
21 // ABC..^@....E.....*V^@^@ ...^?^@^@X.^?^@^@^@^@^@^@.
22 // ^@^@^@9..*V^@^@.....^?^@^@^@^@^@^@^@^@^@^@..K..|..
23 // V^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
24 // ......M.....

17
Valgrind: Memory Tool on Linux and Mac
▶ Valgrind catches most memory
errors
▶ Use of uninitialized memory
▶ Reading/writing memory after it
has been free’d
▶ Reading/writing off the end of
malloc’d blocks > gcc -g badmemory.c
▶ Memory leaks > ./a.out
-714833203
▶ Source line of problem happened 0
1
(but not cause) 4
9
▶ Super easy to use, installed on lab 16
machines 0
1
▶ Slows execution of program way ...
5
down 6
▶ Usually install on Linux via 7
8
> sudo apt install valgrind Segmentation fault (core dumped)
# what now??
18
Valgrind on Common Problems in badmemory.c
> valgrind ./a.out
==2913308== Memcheck, a memory error detector
==2913308== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2913308== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==2913308== Command: ./a.out
==2913308==
==2913308== Conditional jump or move depends on uninitialised value(s)
==2913308== at 0x109189: main (badmemory.c:6)
==2913308==
0
1
4
9
==2913308== Invalid write of size 4
==2913308== at 0x1091D2: main (badmemory.c:11)
==2913308== Address 0x4a43050 is 0 bytes after a block of size 16 alloc'd
==2913308== at 0x483877F: malloc (vg_replace_malloc.c:309)
==2913308== by 0x1091AA: main (badmemory.c:9)
...
8
==2913308== Invalid read of size 4
==2913308== at 0x10924E: main (badmemory.c:20)
==2913308== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==2913308== Process terminating with default action of signal 11 (SIGSEGV):
==2913308== dumping core
19
Debuggers

▶ There comes a day when printf just isn’t enough


▶ On that day you will start compiling with -g to turn on the
debugger
▶ Then you will run gdb myprog, set some breakpoints, and get
to the root of the problem
▶ Debuggers are covered in earlier CSCI courses (like CSCI
2021); refer to those materials to review / refresh
https://www-users.cs.umn.edu/~kauffman/2021/gdb

20
Communicating Information to Programs

▶ Often programs need info from the outside world


▶ What file to read/write, # of iterations to run, verbose/quiet
output, report immediately, shutdown gracefully etc.
▶ A variety of mechanisms exist to convey such info to a
program
1. Command Line Arguments
2. Environment Variables
3. Signals
4. Input/Output system calls and libraries
▶ Will now discuss 1 & 2 which are often used at program
startup
▶ Alluded to Signals (#3) earlier (SIGKILL, SIGSTOP);
Will discuss Signals in more detail later
▶ I/O calls (#4) will come soon (next lecture)

21
Exercise: Command Line Arguments
int main(int argc, char *argv[])
2-arg version of main() will be set up to have number of
arguments and array of strings in it by whatever started it

> cat print13.c


#include <stdio.h>
int main(int argc, char *argv[]){
printf("%s\n",argv[1]);
printf("%s\n",argv[3]);
}
> gcc -o mine print13.c
> ./mine -c 10 2.0
-c
2.0 argc is 4 in this case

Print Args
Write a quick C program which prints ALL of its argv elements as
strings. Print a special message if an arg is string --verbose
22
Answers: Command Line Arguments

File: 04-process-environment-code/print_args.c
1 // Print all the arguments in the argv array. Prints a special message
2 // if option is --verbose.
3
4 #include <stdio.h>
5 #include <string.h>
6
7 int main(int argc, char *argv[]){
8 printf("%d args received\n",argc);
9 for(int i=0; i<argc; i++){
10 printf("%d: %s\n",i,argv[i]);
11 if( strcmp(argv[i],"--verbose") == 0){
12 printf("Turning on VERBOSE output\n");
13 }
14 }
15 return 0;
16 }

23
Environment Variables

All programs can access environment variables, name/value pairs


used to communicate and alter behavior.
Shell show/set variables Shell env
Done with echo $VARNAME Show all environment

> echo $PAGER > env


less JAVA8_HOME=/usr/lib/jvm/java-8-openjdk
> PAGER=cat PAGER=less
> echo $PAGER PWD=/home/kauffman/4061-F2017/lectures/04-proces
cat HOME=/home/kauffman
> echo "'$PS1'" BROWSER=chromium
'>' COLUMNS=79
> PS1='wicked$ ' MAIL=/var/spool/mail/kauffman
wicked$ MANPATH=:/home/kauffman/local/man:/home/kauffman
> export x=1234 # in env PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/us
> y=5678 # not PS1=>
x=1234
...

24
C Programs and Environment Vars

▶ Global variable char **environ provides array of


environment variables in form VARNAME=VALUE, null
terminated
▶ NOT suggested to use environ directly,
▶ Instead use library functions getenv() / setenv() to
check/change
25
C Library for Environment Vars
The C Library Provides standard library functions for manipulating
environment variables.
#include <stdlib.h>

char *getenv(const char *name);


// returns pointer to value associated with name, NULL if not found
int setenv(const char *name, const char *value, int rewrite);
// 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; or (b) if rewrite is 0, an existing definition for
// name is not removed, name is not set to the new value,and no error
// occurs. return: 0 if OK, -1 on error

int unsetenv(const char *name);


// removes any definition of name. It is not an error if such a
// definition does not exist. return: 0 if OK, -1 on error
int putenv(char *str);
// str is of form NAME=VALUE, alters environment accordingly. If name
// already exists, its old definition is first removed. Don't use with
// stack strings. Returns: 0 if OK, nonzero on error.

26
Exercise: Manipulate Environment Vars
Note the use of export to ensure child
Write a short C program which processes see the environment variables
behaves as indicated in the demo
> unset ROCK
▶ Prints ROCK and VOLUME > unset VOLUME
environment variables > gcc environment_vars.c
> a.out
▶ If ROCK is set to anything, ROCK not set
change VOLUME to “11” VOLUME is not set
> export VOLUME=7
> a.out
Use these functions ROCK not set
VOLUME is 7
char *getenv(const char *name); > export ROCK=yes
// NULL if name not sot > a.out
// otherwise pointer to value ROCK is yes
Turning VOLUME to 11
int setenv(const char *name, VOLUME is 11
const char *value, > echo $VOLUME
int rewrite); 7
// Change name value pair,
// if rewrite is 1, Note also that the program does not
// overwrite previous definitions change the shell’s values for ROCK: no child
can change a parent’s values (or mind)
27
Answers: Manipulate Environment Vars
See 04-process-environment-code/environment_vars.c
1 // environment_vars.c: solution to in-class exercise showing how to
2 // check and set environment variables via the standard getenv() and
3 // setenv() functions.
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 int main(int argc, char *argv[]){
8
9 char *rock = getenv("ROCK");
10 if(rock == NULL){
11 printf("ROCK not set\n");
12 }
13 else{
14 printf("ROCK is %s\n",rock);
15 printf("Turning VOLUME to 11\n");
16 int fail = setenv("VOLUME","11",1);
17 if(fail){
18 printf("Couldn't change VOLUME\n");
19 }
20 }
21 char *volume = getenv("VOLUME");
22 if(volume == NULL){
23 volume = "not set";
24 }
25 printf("VOLUME is %s\n",volume);
26 return 0;
27 }
28

You might also like