Operating Systems 2017F Lecture 5

From Soma-notes
Revision as of 00:28, 5 October 2017 by Ssarazin (talk | contribs) (→‎Signals)

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
  • How many are there?
    • arg c tells us how many are in argv[] not envp[]
    • we can look at all of the environment variables using env in the shell -- There are a lot!
    • Accessing argv[] and envp[] from a c programming can be accomplished using the unistd.h library
      • getopt for manipulating argv[]
      • getenv for manipulating envp[]

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),
  • Signals allow us to communicate with other processes through the kernel. The signal is a way to alert other processes that some event has happened giving the process some time to react.
    • for example the kill command sends a signal that can suspend or terminate a running process
      • kill -STOP pid or kill - CONT pid
    • use man 7 signal to see the list of available signals
      • SIGHUP: tells the process that the modem has just hung-up
      • SIGCHLD: is a signal that gets sent when a child process terminates
      • SIGUSR1: is a user defined signal
  • sigaction() is a programming tool that allows us to interact with signals
    • It is a structure that contains function pointers -- we have to define these functions in our code