Operating Systems 2018F Lecture 2: Difference between revisions

From Soma-notes
 
Line 10: Line 10:
'''Where do processes come from?'''
'''Where do processes come from?'''
* Can be visualized as a tree hierarchy
* Can be visualized as a tree hierarchy
* Every process has a parent and a child
* Every process has a parent
* In windows one process specifies the executable of another





Latest revision as of 15:26, 11 October 2018

Video

Video from the lecture given on September 7, 2018 is now available.

Code

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

Notes

Where do processes come from?

  • Can be visualized as a tree hierarchy
  • Every process has a parent


Orthogonal Design in API's: Unix Principle

  • Only one way of doing an operation
  • Complex operations are made by simple ones


Process Duplication

Cases for making a new process:

  • Run a new program
  • Duplicate an existing process


Why would you think of duplicating a process?

  • It's not the same as running a process a second time
    • Variables and state are carried over
  • The duplicate becomes the child and the original is the parent


Process Duplication and Unix

  • Useful operation for concurrency
  • Simple interface
  • Can be combined with other operations to run programs


Shell

  • These commands make up an important part of the unix shell:
    • Fork: The action to duplicate a process
    • Execve: Replaces the process running with something else
  • Most operations you perform are done by forking the current process and execve to run the commands binary
  • If you don't want to `fork` you can write `exec` in front of your command.
  • The result is your shell morphing into a command. Once your command ends, your terminal will close


Finding documentation from the shell

  • $ man 'name of command here'


Running LS from another program

_Execve is a suicide call_

  • in C:
    • argv and envp are params given to main
execve("bin/ls", argv, evnp);
printf("Hello");
  • printf will not run since program morphs into ls


  • What if we want hello to print?
    • If you just throw `fork()` in you will lose track of which process is the parent and both duplicates will do the same thing


Fork: The two faced method

  • Based on the return value you can identify the parent process:
  • Fork returns the PID of the child to the parent
  • Fork returns 0 to the child


  • In the following scenario, the main process terminates before `ls` runs resulting in the terminal re printing its prompt before `ls` outputs
    • C code example:
PID = fork();

if(!PID){
    execve("bin/ls",argv,envp);
}
else{
    printf("done");
    return(0);
}


  • In order to avoid this we need to add a `wait()` so that the parent process waits for its child to terminate before it does.
    • Cxample C code:
PID = fork();
if(!PID){
   execve("bin/ls",argv,envp);
}
else{
    wait(NULL)
    printf("done");
    return(0);
}


What happens to a child process when a parent dies?

  • Another process becomes the parent
  • Some process status code examples:
 S = Sleeping
 Z = Zombie (A process that died but is still there)
  • If nobody waits for a process that dies it becomes a zombie
    • "If you have a kid, wait for it to die"


What is kill?

  • Clicking X on a window is how we normally invoke the kill command.
    • This triggers a signal which is something used for processes to communicate between each other


  • `$ man 7 signal` is the command that shows the list of signals

examples:

 `kill -STOP *PID*`
 `kill -CONT *PID*`


  • Some processes can handle signals differently thanks to signal handlers
`$ kill -9` can avoid signal handlers and kill mercilessly
  • The `wait()` method responds to `sigchild` which gets the signal of a child dying


About `int main` parameters:

  • char* argv[] is an array of strings
    • Each string comes from input separated by whitespace when entering a command
argc:
  Count of how many strings are in argv

envp:
  evironment variables(state) was passed in