《操作系统导论》(OSTEP)阅读笔记

前言

最近在看OSTEP,这里主要写一些阅读时遇到的问题与解决办法,以及阅读时手敲的代码,作备忘用。

开发环境

最开始我是用的VBox上虚拟的Ubuntu,感觉还是太麻烦了,后来就换成了Visual Studio 2022 + WSL2 + Makefile,感觉还是挺爽的。

大概这么个意思

第二章

#makefile

.PHONY: clean run

CC = gcc
CFLAGS = -W -Wall -pthread
RM = -rm -rf

object = threads.c			#根据具体文件名更改
BIN_NAME = $(subst .c,,$(object))	#可执行文件的名称

$(BIN_NAME): $(object)
	$(CC) $(CFLAGS) -o $@ $(object)
%.o: %.cc
	$(CC) $(CFLAGS) -c $< -o $@

run: 
	./$(BIN_NAME)

clean:
	$(RM) *.o $(BIN_NAME)

2.1展示了这样一段代码
在这里插入图片描述

//cpu.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.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;
}

首先sys/time.h头文件显示了这是个运行在Linux上的代码,然后我把这段代码CV过去,一编译,缺少头文件= =,然后又是Spin函数未定义,查了下资料,原来是我不知道看漏了哪里,这里应该有个common.h的!!!代码如下:

#ifndef __common_h__
#define __common_h__

#include <sys/time.h>
#include <assert.h>
#include <pthread.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
}


void
Pthread_create(pthread_t *t, const pthread_attr_t *attr,
        void *(*start_routine)(void *), void* arg) {
    int rc = pthread_create(t, attr, start_routine, arg);
    assert(rc == 0);
}

void
Pthread_join(pthread_t thread, void **value_ptr) {
    int rc = pthread_join(thread, value_ptr);
    assert(rc == 0);
}

void
Pthread_mutex_lock(pthread_mutex_t *mutex) {
    int rc = pthread_mutex_lock(mutex);
    assert(rc == 0);
}

void
Pthread_mutex_unlock(pthread_mutex_t *mutex) {
    int rc = pthread_mutex_unlock(mutex);
    assert(rc == 0);
}

void
Pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
    int rc = pthread_mutex_init(mutex, attr);
    assert(rc == 0);
}

#endif // __common_h__

我粘贴进去之后再编译,发现报了这个错:

/usr/bin/ld: /tmp/ccjTj0Sq.o: in function Pthread_create': cpu.c:(.text+0x101): undefined reference topthread_create’
/usr/bin/ld: /tmp/ccjTj0Sq.o: in function Pthread_join': cpu.c:(.text+0x153): undefined reference topthread_join’
collect2: error: ld returned 1 exit status

在这里插入图片描述
再查了一下资料后发现原来需要给编译选项加一个-pthread
这下编译通过了:
在这里插入图片描述
同时如预期运行了:
在这里插入图片描述
紧接着我把中文版的图2.2的命令
在这里插入图片描述

敲了进去,发现:

bash: 未预期的符号“;”附近有语法错误

在这里插入图片描述
然后我顺便看了一下原文:
在这里插入图片描述
看书下面说这个分号是有用的,可能是因为系统版本原因吧?
改成./cpu A & ./cpu B & ./cpu C & ./cpu D &后顺利运行:在这里插入图片描述

//mem.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

int main(int argc, char* argv[])
{
	int* p = malloc(sizeof(int));
	assert(p != NULL);
	printf("(%d) memory address of p: %08x\n",
		getpid(), (unsigned)p);
	*p = 0;
	while (1)
	{
		Spin(1);
		*p = *p + 1;
		printf("(%d) p: %d\n", getpid(), *p);
	}

	return 0;
}

//threads.c
#include <stdio.h>
#include <stdlib.h>
#include "common.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 <value>\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;
}

第五章

//p1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
	printf("hello world (pid:%d)\n", (int)getpid());
	int rc = fork();
	if (rc < 0) {
		fprintf(stderr, "fork failed\n");
		exit(1);
	}
	else if (rc == 0) {
		printf("hello, I am child (pid:%d)\n", (int)getpid());
	}
	else {
		printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
	}
	return 0;
}
请根据我给的格式,使用markdown格式生成一篇文章,用于发布在csdn上面,主要目的是为了获取更多粉丝和浏览量,要求内容详细,易懂,并提供示例,回答不能太简单,示例如下:第 1 章 计算机系统概述 1.1 操作系统的基本概念 1.1.1 操作系统的概念、功能和目标(系统资源的管理者、提供接口、作为扩充机器、虚拟机) 1.1.2 操作系统的特征(并发、共享、虚拟、异步) 1.2 操作系统的发展和分类 1.2.1 操作系统的发展和分类(手工、单道/多道批处理、分时、实时、网络、分布式、嵌入式、个人计算机) 1.3 操作系统的运行机制和体系结构 1.3.1 操作系统的运行机制和体系结构(大内核、小内核) 1.3.2 中断和异常(内中断和外中断、中断处理过程) 1.3.3 系统调用(执行过程、访管指令、库函数与系统调用) 1.0.0 第一章操作系统概述错题整理 第 2 章 进程管理 2.1 进程与线程 2.1.1 进程的定义、特征、组成、组织 2.1.2 进程的状态(运行、就绪、阻塞、创建、终止)及转换(就绪->运行、运行->就绪、运行->阻塞、阻塞->就绪) 2.1.3 原语实现对进程的控制 2.1.4 进程之间的通信(共享通信、消息传递、管道通信) 2.1.5 线程概念与多线程模型 2.2 处理机的调度 2.2.1 处理机调度的概念及层次 2.2.2 进程调度的时机(主动放弃与被动放弃)、切换与过程(广义与狭义)、方式(非剥夺与剥夺) 2.2.3 度算法的评价指标(cpu利用率、系统吞吐量、周转时间、等待时间、响应时间) 2.2.4 作业/进程调度算法(FCFS先来先服务、SJF短作业优先、HRRN高响应比优先) 2.2.5 作业/进程调度算法(时间片轮转调度算法、优先级调度算法、多级反馈队列调度算法) 2.3 进程的同步与互斥 2.3.1 进程的同步与互斥 2.3.2 实现临界区进程互斥的软件实现方法 2.3.3 实现临界区进程互斥的硬件实现方法 2.3.4 信号量机制(整型信号量、记录型信号量P、V) 2.3.5 信号量机制实现进程的互斥、同步与前驱关系 2.3.6 进程同步与互斥经典问题(生产者-消费者问题、多生产者-多消费者问题、吸烟者问题、读者-写者问题、哲学家进餐问题) 2.3.7 管程和java中实现管程的机制 2.4 死锁 2.4.1 死锁详解(预防、避免、检测、解除)
03-11
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值