Skip to content

Commit

Permalink
feat(os): ostep intro: cpu.c mem.c
Browse files Browse the repository at this point in the history
  • Loading branch information
bluewaitor committed Mar 30, 2022
1 parent 7c94b28 commit 6726047
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ node_modules
yarn-error.log
.history
.vscode
.DS_Store
.DS_Store

nohup.out
2 changes: 1 addition & 1 deletion c++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ cd到当前目录

执行

docker run -it --rm --entrypoint /bin/sh -v `pwd`:/code bluewaitor/linuxc
docker run -it --rm --entrypoint /bin/sh -v $(pwd):/code bluewaitor/linuxc
30 changes: 30 additions & 0 deletions os/ostep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Operating Systems: Three Easy Pieces

[网址](https://pages.cs.wisc.edu/~remzi/OSTEP/)

[代码地址](https://github.com/remzi-arpacidusseau/ostep-code)

## 使用docker

```shell
docker run -it --rm --entrypoint /bin/sh -v $(pwd):/code bluewaitor/linuxc
```

## Intro

```shell
gcc -o cpu cpu.c -Wall
./cpu A

./cpu A & ./cpu B & ./cpu C & ./cpu D & # 后台运行

# 结束方法
# ps 查看进程
kill -9 PID

# fg 将进程放到前台
# ctrl + c 结束进程

# 关掉整个terminal

```
21 changes: 21 additions & 0 deletions os/ostep/intro/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __COMMON_H__
#define __COMMON_H__

#include <sys/time.h>
#include <assert.h>

double GetTime() {
struct timeval t;
int rc = gettimeofday(&t, NULL);
assert(rc == 0);
return (double) t.tv_sec + (double) t.tv_usec / 1e6;
}

void Spin(int howlong) {
double t = GetTime();
while (GetTime() - t < (double) howlong)
; // do nothing in loop
}


#endif
Binary file added os/ostep/intro/cpu
Binary file not shown.
25 changes: 25 additions & 0 deletions os/ostep/intro/cpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

int main(int argc, char* argv[]) {
setbuf(stdout, NULL); // 使用nohup的时候,需要设置这个,否则会出现输出不到文件的情况

if (argc != 2) {
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
exit(1);
}
for (int i = 0; i < argc; i ++) {
printf("argv[%d] = %s\n", i, argv[i]);
}

char *str = argv[1];

while (1)
{
Spin(1);
printf("%s\n", str);
}

return 0;
}
Binary file added os/ostep/intro/mem
Binary file not shown.
19 changes: 19 additions & 0 deletions os/ostep/intro/mem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include "common.h"

int main(int argc, char *argv[]) {
int *p = malloc(sizeof(int));
assert(p != NULL);
printf("(%d) address pointed to by p: %p\n", getpid(), p);
*p = 0;
while (1)
{
Spin(1);
*p = *p + 1;
printf("(%d) p: %d\n", getpid(), *p);
}
return 0;
}

0 comments on commit 6726047

Please sign in to comment.