Operating Systems 2018F Lecture 5: Difference between revisions

From Soma-notes
Created page with "==Video== Video from the lecture given on September 19, 2018 [https://homeostasis.scs.carleton.ca/~soma/os-2018f/lectures/comp3000-2018f-lec05-20180919.m4a is now available]...."
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 8: Line 8:


==Notes==
==Notes==
===Notes on Tutorial 2===
'''Q6:''' Make the shell output "Ouch!" when you send it a SIGUSR1 signal.
*To test, send a signal to a process
*Use kill command
**kill -<SIGNALNAME> <PID>
**ex: kill -USR1 4094
'''Q8:'''  Replace the use of find_env() with getenv(). How do their interfaces differ?
*How are the parameters/arguments different
*Look at man pages for getenv()
*Different number of arguments
*Why does getenv() not need the environment to be passed in
** It has it already
**#man pages, see also, environ  is user environment global variable defined by standard library
**#Longer answer: we got the environment variable from main as an argument (*envp[])
'''Q9:''' Use putenv() to implement LASTCOMMAND from Tutorial 1.
*putenv() handles variables for you
*Allows you to define environment variables and add it
*Only changes for current process
**To change for another process, fork and use execve
===Notes on Assignment 1 ===
*Not finalized = “not completely debugged”
*Due Sept 26th 2:30pm
*Based on tutorials
*Question 9 looking at man page for execve can get an answer- not what he’s talking about, he wants experimental evidence
*Solutions will be posted and discussed in class Sept 26th
===Class Notes===
*Signals:
**is a message sent to a process
**some can be blocked until unblocked
**SIGKILL and SIGSTOP cannot be caught, blocked or ignored
**Handlers respond to specific signals
***Interrupts what’s currently happening
***Can cause jumps that happen randomly in your program
***You don’t know the state of the program when a signal handler executes
***Kernel calls a function
***Don’t try to handle a signal in a signal handler, generally block them in a handler
***If you don’t define a signal handler, there is a default handler that will run
**No signal handler for SIGKILL or SIGSTOP
**Can run strace on kill signal
***Does kill system call
***Takes a process id and a signal
*I/O redirection:
**changing which file things get written to
**ls > foo.txt
***foo.txt contains the contents that ls outputted
***'''>''' send output to foo.txt
***ls does not know about redirection
***ls should send it is output to foo.txt
***ls without redirection is already outputting to a file (standard output- file descriptor 1)
****standard out sends information to a file
****fprintf – goes to file descriptor 2
****printf – goes to file descriptor 1
**ps aux
***shows you what’s running and what terminal it is connected to (tty2, ?, pts/0)
****pts/0 – sudo terminal
****tty1 – used to run log in
***terminal usually referred to as tty (teletype, screen and keyboard, connects to another computer)
*File descriptors
**a number associated with an open file
**each process has its own file descriptor
**first file opened is assigned file descriptor 0
**stored on the kernel
**by default in linux there are file descriptors for every program
**the shell makes sure that file descriptors 0,1,2 are open and are going somewhere valid
===Useful Commands/examples===
*envp – came from main, can be changed by putenv
*ls /proc
**Tells you information on all the running processes
**Can cd into a process id directory
***fd – file descriptor directory
***ex: /proc/1973/fd
**echo $TERM
**tells you what terminal you are using 
**TERM is an environment variable
*cd /dev
**can see all of the special files
**b – block devices
**c – character devices
*echo hello > 0   
*echo hello > 1
**run in one terminal, can write to file displayed in another terminal
**numbers correspond to different terminals being used
**if you have permission you can write to them (other users would not be allowed)
*echo hello > test1
**to the shell, please open the file test1 for writing, open it on file descriptor 1, the process will then do an execve of echo
**cat test -> results in “hello”
*cat 333shell.c | sort
**vertical bar is a pipe
**whatever is written by cat will be read by sort
**pairing of file descriptors
**sorted all the lines of the program
*pstree
**displays the tree of all running processes

Latest revision as of 23:14, 1 October 2018

Video

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

Code

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

Notes

Notes on Tutorial 2

Q6: Make the shell output "Ouch!" when you send it a SIGUSR1 signal.

  • To test, send a signal to a process
  • Use kill command
    • kill -<SIGNALNAME> <PID>
    • ex: kill -USR1 4094

Q8: Replace the use of find_env() with getenv(). How do their interfaces differ?

  • How are the parameters/arguments different
  • Look at man pages for getenv()
  • Different number of arguments
  • Why does getenv() not need the environment to be passed in
    • It has it already
      1. man pages, see also, environ is user environment global variable defined by standard library
      2. Longer answer: we got the environment variable from main as an argument (*envp[])

Q9: Use putenv() to implement LASTCOMMAND from Tutorial 1.

  • putenv() handles variables for you
  • Allows you to define environment variables and add it
  • Only changes for current process
    • To change for another process, fork and use execve

Notes on Assignment 1

  • Not finalized = “not completely debugged”
  • Due Sept 26th 2:30pm
  • Based on tutorials
  • Question 9 looking at man page for execve can get an answer- not what he’s talking about, he wants experimental evidence
  • Solutions will be posted and discussed in class Sept 26th

Class Notes

  • Signals:
    • is a message sent to a process
    • some can be blocked until unblocked
    • SIGKILL and SIGSTOP cannot be caught, blocked or ignored
    • Handlers respond to specific signals
      • Interrupts what’s currently happening
      • Can cause jumps that happen randomly in your program
      • You don’t know the state of the program when a signal handler executes
      • Kernel calls a function
      • Don’t try to handle a signal in a signal handler, generally block them in a handler
      • If you don’t define a signal handler, there is a default handler that will run
    • No signal handler for SIGKILL or SIGSTOP
    • Can run strace on kill signal
      • Does kill system call
      • Takes a process id and a signal
  • I/O redirection:
    • changing which file things get written to
    • ls > foo.txt
      • foo.txt contains the contents that ls outputted
      • > send output to foo.txt
      • ls does not know about redirection
      • ls should send it is output to foo.txt
      • ls without redirection is already outputting to a file (standard output- file descriptor 1)
        • standard out sends information to a file
        • fprintf – goes to file descriptor 2
        • printf – goes to file descriptor 1
    • ps aux
      • shows you what’s running and what terminal it is connected to (tty2, ?, pts/0)
        • pts/0 – sudo terminal
        • tty1 – used to run log in
      • terminal usually referred to as tty (teletype, screen and keyboard, connects to another computer)
  • File descriptors
    • a number associated with an open file
    • each process has its own file descriptor
    • first file opened is assigned file descriptor 0
    • stored on the kernel
    • by default in linux there are file descriptors for every program
    • the shell makes sure that file descriptors 0,1,2 are open and are going somewhere valid

Useful Commands/examples

  • envp – came from main, can be changed by putenv
  • ls /proc
    • Tells you information on all the running processes
    • Can cd into a process id directory
      • fd – file descriptor directory
      • ex: /proc/1973/fd
    • echo $TERM
    • tells you what terminal you are using
    • TERM is an environment variable
  • cd /dev
    • can see all of the special files
    • b – block devices
    • c – character devices
  • echo hello > 0
  • echo hello > 1
    • run in one terminal, can write to file displayed in another terminal
    • numbers correspond to different terminals being used
    • if you have permission you can write to them (other users would not be allowed)
  • echo hello > test1
    • to the shell, please open the file test1 for writing, open it on file descriptor 1, the process will then do an execve of echo
    • cat test -> results in “hello”
  • cat 333shell.c | sort
    • vertical bar is a pipe
    • whatever is written by cat will be read by sort
    • pairing of file descriptors
    • sorted all the lines of the program
  • pstree
    • displays the tree of all running processes