CS241

Lab 3 - Processes

Processes!

Address Space

What is Copied Over?

Forkbomb

Process Flowchart

Utilities Unleashed

What are you going to learn?

All Fork-Exec-Wait Code

pid_t pid = fork();
if(pid == -1){
    //fork failed
}else if(pid == 0){
    //I am the child
    exec(...)
}else{
    //I Am the parent
    wait(pid);
}

Exec Family

  • There are three modifiers/mnemonic
    • Either past a list or argv (null terminated array) of arguments
    • Search for the executable either in the current directory or in the path
    • Use the parent’s environment variables or use an environment setting
  • Here are the list exec
    • execl( path, arg, … ),execute the file in current directory
    • execlp( file, arg, … ), executes a file only searching in the path
    • execle( path, arg, …, envp[]), execute the file in path + environment settings
  • Pass an array of string as arguments
    • execv( path, argv[]), execute the file in current directory
    • execvp( file, argv[]), execute the file in the path only
    • execvpe( file, argv[]), envp[]) // environment setting

time

time Workflow

Helper Functions

  • struct timespec
    • time_t tv_sec;
    • long tv_nsec;
    • tv_sec = 10, tv_nsec = 992300000 -> 10.9923 sec
  • int clock_gettime(clockid_t, timespec *);
    • clockid_t: should use CLOCK_MONOTONIC in this lab
  • return 0 when success, -1 otherwise

env

env Workflow

Helper Functions

  • int setenv(const char* name, const char* value,
    int overwrite)
  • char *getenv(const char *name)
  • Write a split function that can split string based on ,
  • Write a function that can find all %notation in a string
  • Extend that function so that you can replace variables
    with environment variables
  • Use getenv to get environment variables
  • Be familiar with: return array of strings, clear an array
    of strings-> camelCasers

Authors: Steve & Bhuvan