---
# System prepended metadata

title: system programming

---

[TOC]
# Intro to Operating system
### why learn system programming
1. #### os hide hardware and provide programming API to control hardware
* disk => file + directory
* CPU => process + thread
2. #### os provide virturalization
* communication between process , internet and copy-paste
* every process has virtual memory
3. #### os provide virturalization
* multicore program,how to get shared data
* IO optimized library
### definition of os
 A body of software, in fact, that is responsible for making it easy to run programs (even allowing you to seemingly run many at the same time), allowing programs to share memory, enabling programs to interact with devices, and other fun stuff like that. (OSTEP)
### virtualization
** what does program do?
A:fetch->decode->execute
Operating system takes physical resource and transform it into a more general ,powerful and easy-to-use virtual form of itself.
physical resource->virtual form
#### virtualizing cpu
如何virtualize cpu？OS：明明自己只有一個核心，但卻假裝自己有很多核心
OS會給你一個CPU的使用權，但不一定真的給你CPU

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <assert.h>
#include "common.h"
int main(int argc, char *argv[]){
if (argc != 2) {
    fprintf(stderr, "usage: cpu <string>\n");
    exit(1);
    }
    char *str = argv[1];
    while (1) {
    Spin(1);
    printf("%s\n", str);
    }
    return 0;
}
```
```terminal
 $ ./cpu A & ./cpu B & ./cpu C & ./cpu D &
```
#### virtualizing memory
memory is a array of bytes
read & write:must specify address
If we run multiple instance of this program,we will found that although we got same memory address but the results will be independent.
why?because each running program has its own private memory.
OS will virtualize memory.
* Each process access its own private virtual address space.
每個process會有自己的定址空間
* physical memory is a shared resource managed by operating system.
```cpp=
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

int main(int argc, char *argv[]) {
    if (argc != 2) { 
	    fprintf(stderr, "usage: mem <value>\n"); 
	    exit(1); 
    } 
    int *p; 
    p = malloc(sizeof(int));
    assert(p != NULL);
    printf("(%d) addr pointed to by p: %p\n", (int) getpid(), p);
    *p = atoi(argv[1]); // assign value to addr stored in p
    while (1) {
	    Spin(1);
	    *p = *p + 1;
	    printf("(%d) value of p: %d\n", getpid(), *p);
    }
    return 0;
}
```
### Concurrency
problem of concurrency
* OS juggling many things at once
OS 一次隨意執行多個東西
* 

```clike
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "common_threads.h"

volatile int counter = 0; 
int loops;

void *worker(void *arg) {
    int i;
    for (i = 0; i < loops; i++) {
	counter++;
    }
    return NULL;
}

int main(int argc, char *argv[]) {
    if (argc != 2) { 
	fprintf(stderr, "usage: threads <loops>\n"); 
	exit(1); 
    } 
    loops = atoi(argv[1]);
    pthread_t p1, p2;
    printf("Initial value : %d\n", counter);
    Pthread_create(&p1, NULL, worker, NULL); 
    Pthread_create(&p2, NULL, worker, NULL);
    Pthread_join(p1, NULL);
    Pthread_join(p2, NULL);
    printf("Final value   : %d\n", counter);
    return 0;
}
````
執行結果在數字小的時候為2N但在數字增大後卻非預期。
->instructions didn't atomically execute
$ gcc -o thread thread.c -Wall -pthread
$ ./thread 1000
output:
Initial value : 0
Final value
***Core Problem:  How to build correct concurrent program***
-> When there are many concurrently executing threads within the same memory
* how to build a correctly working program?
* What primitives are needed from the OS?
* What mechannisms should be provided by hardware
### persistence
```cpp=
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
int main(int argc, char *argv[]) {
   int fd = open("/tmp/file", O_WRONLY|O_CREAT|O_TRUNC,
   S_IRWXU);
   assert(fd > -1);
   int rc = write(fd, "hello world\n", 13);
   assert(rc == 13);
   close(fd);
   return 0;
 }

```
life of data.
hard-drive for long-lived information.
file System responsible for storing any file user created.
how to accomplish task
program make system calls which will be routed to the part of the os which is called file system and return error code to user.
#### what os does to write to disk
1. where data reside
2. requre issuing I/O requests to the storage device to read/write them
3. OS access files through system calls.futhermore,they are routed to many parted to the part of the OS called file system.
file system->where handles the requests and returns some kind of error code to the user
device driver writer needs to know:deep knowledge of low-level device interface and exact semantics.

## File System
#### elements of file system
* data structure
* access methods
#### mental model of file system
* what on-disk structure stores data and meta-data
* what happen when a process open a file
* 