Operating Systems 2020W Lecture 5

From Soma-notes
Revision as of 02:30, 20 March 2020 by Soma (talk | contribs) (Created page with "==Video== Video from the lecture given on January 22, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec05-20200122.m4v is now available]....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Video

Video from the lecture given on January 22, 2020 is now available.

Notes

Connecting to Openstack

To connect to an openstack instance off campus, either:

  • Connect to the Carleton VPN, then ssh to the instance, or
  • If you instance is at 134.117.30.30 and your username is carletonstudent, do the following (note that you can replace the 1200 with another number between 1024 and 65535):
 ssh -L 1200:134.117.30.30:22 carletonstudent@access.scs.carleton.ca
 [stay logged in, and in another window do the following]
 ssh -p 1200 student@localhost

Using gdb to view assembly code

To compile for debugging with gdb:

 gcc -O -g myprogram.c -o myprogram

The -O option is for optimization (could use -O2, -O3 for more optimization, but -O is sufficient normally). You want to optimize the code because otherwise the assembly code will be harder to follow.

To run a program under gdb:

gdb ./myprogram

The following commands in gdb should be useful:

  • break x: set a breakpoint at function x. Set a breakpoint for main if you want to see program execution starting with your code.
  • tui enable: enable display of source code
  • layout asm: switch the display to assembly code
  • layout split: switch display to assembly and source code
  • layout src: show source code
  • n or next: next source-level statement, staying within the current function
  • step: next source-level statement, going into called functions
  • nexti: next assembly-language statement, staying within the current function
  • stepi: next assembly-language statement, going into called functions

To repeat a command, just hit enter.

To see a system call be executed, single-step through a program until you see a "syscall" instruction (on x86-64). Note how even with a program that directly makes a system call such as syscall-hello.c, the program first makes a function call (using callq) to the syscall function, and that function makes the actual system call.

For more information on gdb in tui mode, see Chris Kauffman's UMN CSCI 2021 Quick guide to GDB.

In-Class Notes

Lecture 5
---------
Topics:
- Assignment 1
- syscall-hello.c
- gdb and assembly
- showing a system call instruction

- creating processes with fork
- running programs with execve
- returning values with exit

Process is a running program

Could have a system call to create a new process and run a program in it
 - this is how Windows does it with CreateProcess(), other calls?

But UNIX divides this into two steps
 - fork: make a copy of the current process
 - execve: run a binary in a process, replacing the currently running program

So normally, to run a program in a separate process
 - create a "child" process using fork
 - child process runs execve to load new program
 - parent process waits for child to finish (optional)

Basic flow of a UNIX shell (or anything that wants to run external programs)
 - setup execution context
 - fork
   - parent waits for child to finish, or calls wait when told
     the child has finished
   - child does an execve to load new program

Code

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
        pid_t fork_pid = -1;
        pid_t mypid;

        fork_pid = fork();
        mypid = getpid();
        printf("My pid is %d\n", mypid);
        
        if (fork_pid == 0) {
                printf("Fork done! I am the child.\n");
        } else {
                printf("I am the parent, my child's PID is %d\n", fork_pid);
        }
        
        return 0;
}