CSCI 311 Test 1 Spring 2018


  1. Environment Variables
    1. [2 points] What is an environment variable?

      Environment variables are an array of strings in the form varname=value. These variables are passed to a program by the program's parent and are used to help determine how the program will function.

      Since you know the details of how this is implemented, simply stating "a variable that controls the environment" seems unsatisfactory as an answer.

    2. [2 points] Give an example of a "standard" environment variable and describe how this environment variable is used.

      PATH is an example of a common environment variable. This variable is used by the execp* family of commands to search for a requested executable file.

    3. [6 points] A c++ programmer wishes to check to value of an environment variable. Describe two ways this can be done. (I expect code or pseudo-code)

      1. envp: char * envp[] is a standard argument to main. It is the null terminated list of environment variables.
        
        int main(int argc, char * argv[], char * envp[]) {
           string varName = "variable name to find"
           i = 0;
           string env;
           found = false;
           while (envp[i] != nullptr and not found) {
               env = envp[i];
               pos = env.find("=");
               if (env.substr(0,pos) == varName) {
                   // code to take action here.
           	found = true;
               }
               i++;
           }
        }
            
      2. getenv: getenv(char name) is a system call that returns a char* pointing to the name or null if not found.
        
        string varName = "variable name to find"
        char * value;
        
        value = getenv(varName.c_str());
        if (value!= nullptr) {
               // code to take action here.
        }
            
      3. environ: extern char *environ[]; is a global variable available to all c/c++ programs. It behaves much like envp[] and code similar to that given in answer 1 will do.

      I would not expect the detailed code given here, but I would expect basic types, and some attempt to provide code to answer the question.

  2. Program Execution
    1. [3 points] What does the fork system call do? What are the return values?

      The fork command creates a nearly identical copy of the current process. The new process has a copy of all memory, file descriptors and most other resources. A significant difference is the processid and the return value. The original process has the process id of the child returned while the child process receives the return value of 0.

    2. [3 points] Describe the operation of the waitpid system call.
      
      pid_t waitpid(pid_t pid, int *wstatus, int options);
      

      waitpid suspends the calling process until a process matching the pid exits.If a process matching this description has already exited, or the calling process has no children, the call returns immediately. The wstatus variable describes the conditions under which the child process exited. The behavior of this call can be altered with the options. The return status is the process id of the exiting process on success and -1 on failure.

    3. [2 points] What does the option WNOHANG do when passed to waitpid.

      WNOHANG allows the calling process to return instantly if no child process has exited. The return status is 0 if no process has exited. This allows for a non-blocking check of the status of the child process(s).

    4. [2 points] What happens to a process which has exited but the parent does not call wait, waitpid or handle a SIGCHLD signal?

      The child process becomes a zombie. Most of the memory is freed, but the process still holds a process id and is consuming some resources.

  3. Pipes
    1. [2 points] What is a pipe?

      A pipe is a one way communication mechanism which allows data transfer between two processes.

      I realize that you know what a pipe is at the shell level, well sort of know. The instructions above state that "Your answers should reflect time spent studying the subject." and we have spent very little time talking about pipes at the shell level. We have, however, spent quite a bit of time studying pipes at the program level. In fact, you have a program that should have used pipes due the day you took this test.

    2. [2 points] Describe the conditions required for two processes to use a pipe for communications.

      The two processes must share a common answer (parent child, or two children for example)

    3. [6 points] Provide a code segment which uses a pipe to communicate between two processes. The first process should send the numbers 1 through 10 to the pipe. The second process should read these numbers and print them out to the standard output. This segment should begin with creation of the pipe.
      
      int fd[2];
      pipe(fd);
      if (0==fork()) {
        // writer
        close(fd[read])
        for(1;i<=10;i++) {
           write(fd[write], &i, sizeof(i);
        }
        exit(0);
      } else  {
        close(fd[write])
        while (read(fd[read],&num, sizeof(num)) != 0) {
           cout << num << endl; 
        }
        exit(0);
      }
      
      I was looking for something like
      
      pipe
      fork
      if child
         write
      if parent
         read
      
  4. Errors
    1. [3 points] Describe how a programmer can detect that an error has occurred during a system call.

      All system calls have a return value. By checking this return value, the programmer can determine if an error has occurred.

    2. [3 points] Describe how a programmer can determine which error has occurred during a system call.

      All system calls set the global variable errno with an appropriate value indicating what error has occurred. By checking this variable, the programmer can determine what caused the error.

      perror does not inform the programmer of anything. I just prints a message. It does not allow recovery.

      The return value does not indicate WHAT error occurred, only that an error occurred.

  5. Misc
    1. [6 points] Name and describe three tasks performed by the shell.

      User Interface, Globbing, tilda expansion, I/O redirection, manage environment variables, command execution, job control...

    2. [6 points] Describe a situation where a programmer might choose to implement a program in BASH, Python and C++? Justify your selection. (There should be three answers like: BASH would be good for xxx because yyy).
    3. [2 points] Name and describe one task performed by process-id 1.
      • Spawns all initial processes
      • Respawns processes as needed
      • Parent of all orphan processes