Operating Systems 2017F Lecture 5

From Soma-notes

Video

Video from the lecture given on September 21, 2017 is now available.

Code

Code and files from the lecture (captured as they were at the end) are available here.


Notes

IO Redirection

  • The > operator output to a location
    • Eg. Ls > foo.txt
  • The | operator connect the output of the 1st program to the input of the 2nd program
    • Eg. Shuf foo.txt | shuf > shuf2.txt (pipe operater connect output of shuf foo.txt to input of shuf then output to shuf2.txt)
  • File usually comes with standard input (0), standard output(1),standard error(error message, port 2). This defined when program started
    • Ls foo > test.txt 2 >error.txt: redirect error message (port 2) of foo to error.txt
  • Ls foo>& all.txt: redirect everything to output file
    • Bc -l: This command bring up the calculater
  • The convention is 3 file descriptors are available once the system boots. They can be accessed by a shell and used to send data in both directions.
    • file descriptor 0 is stdin
    • file descriptor 1 is stdout
    • file descriptor 2 is stderr
  • redirections is accomplished using the > (redirect to) or < (redirect from)
    • example: ls > foo.txt creates a file called foo.txt and puts the result of ls in it
    • example: ls foo > test.txt 2> errors.txt will generate errors because ls won't be able to find foo. In this case nothing will be put into test.txt instead the resulting error will be sent to errors.txt. 2> is the convention for selecting file descriptor 2.
  • using >& will merge stdout and stderr
  • The pipe | operator
    • This operator allows us to join output from one process sending the data to another process.
    • example: seq 1 100 num.txt | shuf > shuf2.txt
      • The result is the sequence of numbers from 1 to 100 are put in num.txt then sent to shuf to be "shuffled" and finally redirected to shuf2.txt

Manipulating environment variables

int main(int argc, char *argv[], char *envp[]){
While (envp[i] != NULL) {
     If (strcmp(User=, envp[i], 5){
         printf(%s\n, envp[i]);
	 char *username = envp[i] +5;  //pointer arithmetic 
     }
     I++;
}
printf (%s\n, username);
return 0;
}
  • Getopt: a better way to find environmental variable


Signals

  • Kill: send a signal to destroy a program
    • Kill -stop 5617(This stop a progam) and Kill -cont 5617(resume a program)
    • Sigkill, sigterm (ctrl+c), sigstop (ctrl+z), sigpipe (when | died), sighup (when close terminal window)
    • Sigchild(when a child process terminated),