Difference between revisions of "Operating Systems 2019F Lecture 3"

From Soma-notes
Jump to: navigation, search
(Created page with "==Video== Livestream is on [https://www.twitch.tv/anilsoma Twitch]. ==Topics== * supervisor and user mode * system calls * process abstraction")
 
(Code)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
==Video==
 
==Video==
  
Livestream is on [https://www.twitch.tv/anilsoma Twitch].
+
Video from the lecture given on Sept. 11, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019f/lectures/comp3000-2019f-lec03-20190911.m4v is now available].
  
 
==Topics==
 
==Topics==
Line 8: Line 8:
 
* system calls
 
* system calls
 
* process abstraction
 
* process abstraction
 +
 +
==Notes==
 +
 +
<pre>
 +
Lecture 3
 +
---------
 +
 +
To understand system calls, we have to be able to differentiate between function calls and system calls.
 +
 +
Function call
 +
* saver current instruction pointer to stack
 +
* jump to a memory location (load into current instruction register)
 +
 +
Function call return
 +
* pop instruction pointer from stack
 +
* jump to value (load it into current instruction register)
 +
 +
 +
CPUs have registers
 +
* mostly used for manipulating data
 +
* but some store special values
 +
 +
(some) Special registers
 +
* the instruction pointer, which contains the address of the currently executing instruction
 +
 +
key operating system code *cannot* be seen by regular programs
 +
- they can't even get a pointer to that code
 +
 +
this key part of the operating system is called the "kernel"
 +
 +
My apologies for the lack of sound!
 +
 +
the kernel has control over all hardware, all resources
 +
- processes have to ask the kernel for access to resources
 +
 +
And a process can't ask using a function call, because it can't
 +
jump to the code in the kernel
 +
 +
So we have to execute a special instruction that causes the kernel to be invoked - without this, the process code is completely isolated
 +
- can't access files, network, keyboard, etc
 +
 +
Those special instructions cause the CPU to do some interesting things, which we will talk about later
 +
 +
Key system calls
 +
----------------
 +
 +
The big ones are the ones for files
 +
* open
 +
* close
 +
* read
 +
* write
 +
...and a few others
 +
 +
From your program's perspective, the world is the process: that's your "cpu", that's your memory for storing all code and data
 +
system calls are special instructions
 +
 +
* note you can't make system calls with pure C, has to use assembly at some point
 +
 +
System calls for creating a process:
 +
* fork (clone)
 +
 +
But all fork does is duplicate the current process
 +
 +
System call for loading a program binary:
 +
* execve
 +
 +
Execve replaces the code in the process with the code from the specified file (throwing out all the old code and data, and only loading the data passed as command-line arguments and environment variables).
 +
</pre>
 +
 +
==Code==
 +
 +
===fork-demo.c===
 +
<source lang="c" line>
 +
#include <unistd.h>
 +
#include <stdio.h>
 +
 +
int main(int argc, char *argv) {
 +
        pid_t pid;
 +
 +
        pid = fork();
 +
 +
        if (pid == 0) {
 +
                printf("First branch, pid = %d\n", (int) pid);
 +
        } else {
 +
                printf("Second branch, pid = %d\n", (int) pid);
 +
        }
 +
 +
        printf("%d: After the check\n", getpid());
 +
 +
        return 0;
 +
}
 +
</source>
 +
 +
===execve-demo.c===
 +
 +
<source lang="c" line>
 +
#include <unistd.h>
 +
#include <stdio.h>
 +
 +
int main(int argc, char *argv[], char *envp[]) {
 +
        execve("/bin/ls", argv, envp);
 +
        printf("All done!\n");
 +
        return 0;
 +
}
 +
</source>
 +
 +
===fork-execve-demo.c===
 +
 +
<source lang="c" line>
 +
#include <unistd.h>
 +
#include <stdio.h>
 +
#include <sys/wait.h>
 +
 +
int main(int argc, char *argv[], char *envp[]) {
 +
        pid_t pid;
 +
 +
        pid = fork();
 +
 +
        if (pid == 0) {
 +
                printf("Child, pid = %d, doing execve\n", getpid());
 +
                execve("/bin/ls", argv, envp);
 +
        } else {
 +
                printf("Parent, pid = %d, will wait for child.\n",
 +
                      getpid());
 +
                /* wait(NULL); */
 +
                sleep(600);
 +
                printf("%d: Child %d finished!\n",
 +
                      getpid(), (int) pid);
 +
        }
 +
 +
        printf("Both parent and child could have run this "
 +
              "but it was run by %d\n", getpid());
 +
 +
        return 0;
 +
}
 +
</source>

Latest revision as of 22:06, 11 September 2019

Video

Video from the lecture given on Sept. 11, 2019 is now available.

Topics

  • supervisor and user mode
  • system calls
  • process abstraction

Notes

Lecture 3
---------

To understand system calls, we have to be able to differentiate between function calls and system calls.

Function call
* saver current instruction pointer to stack
* jump to a memory location (load into current instruction register)

Function call return
* pop instruction pointer from stack
* jump to value (load it into current instruction register)


CPUs have registers
 * mostly used for manipulating data
 * but some store special values

(some) Special registers
 * the instruction pointer, which contains the address of the currently executing instruction

key operating system code *cannot* be seen by regular programs
 - they can't even get a pointer to that code

this key part of the operating system is called the "kernel"

My apologies for the lack of sound!

the kernel has control over all hardware, all resources
 - processes have to ask the kernel for access to resources

And a process can't ask using a function call, because it can't
jump to the code in the kernel

So we have to execute a special instruction that causes the kernel to be invoked - without this, the process code is completely isolated
 - can't access files, network, keyboard, etc

Those special instructions cause the CPU to do some interesting things, which we will talk about later

Key system calls
----------------

The big ones are the ones for files
* open
* close
* read
* write
...and a few others

From your program's perspective, the world is the process: that's your "cpu", that's your memory for storing all code and data
system calls are special instructions

 * note you can't make system calls with pure C, has to use assembly at some point

System calls for creating a process:
 * fork (clone)

But all fork does is duplicate the current process

System call for loading a program binary:
 * execve

Execve replaces the code in the process with the code from the specified file (throwing out all the old code and data, and only loading the data passed as command-line arguments and environment variables).

Code

fork-demo.c

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 
 4 int main(int argc, char *argv) {
 5         pid_t pid;
 6 
 7         pid = fork();
 8 
 9         if (pid == 0) {
10                 printf("First branch, pid = %d\n", (int) pid);
11         } else {
12                 printf("Second branch, pid = %d\n", (int) pid);
13         }
14 
15         printf("%d: After the check\n", getpid());
16 
17         return 0;
18 }

execve-demo.c

1 #include <unistd.h>
2 #include <stdio.h>
3 
4 int main(int argc, char *argv[], char *envp[]) {
5         execve("/bin/ls", argv, envp);
6         printf("All done!\n");
7         return 0;
8 }

fork-execve-demo.c

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 #include <sys/wait.h>
 4 
 5 int main(int argc, char *argv[], char *envp[]) {
 6         pid_t pid;
 7 
 8         pid = fork();
 9 
10         if (pid == 0) {
11                 printf("Child, pid = %d, doing execve\n", getpid());
12                 execve("/bin/ls", argv, envp);
13         } else {
14                 printf("Parent, pid = %d, will wait for child.\n",
15                        getpid());
16                 /* wait(NULL); */
17                 sleep(600);
18                 printf("%d: Child %d finished!\n",
19                        getpid(), (int) pid);
20         }
21 
22         printf("Both parent and child could have run this "
23                "but it was run by %d\n", getpid());
24 
25         return 0;
26 }