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)
** 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
如何virtualize cpu?OS:明明自己只有一個核心,但卻假裝自己有很多核心
OS會給你一個CPU的使用權,但不一定真的給你CPU
#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;
}
$ ./cpu A & ./cpu B & ./cpu C & ./cpu D &
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.
#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;
}
problem of concurrency
#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
#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.