操作系统概述

OS概述

Q1:什么是操作系统(OS)?

操作系统是一类软件,什么软件呢?一类负责让程序运行的很容易(甚至能同时运行多个程序)的软件,它允许程序共享内存(share memmory),让程序与设备进行交互,以及类似的工作。

虚拟化

操作系统为了解决上述问题,使用了一种技术,虚拟化(virtualization)

虚拟化:就是指操作系统将物理资源(如处理器、内存、磁盘)等转换为更加通用、更强大且易于使用的虚拟形式,比如虚拟化CPU,虚拟化内存。故操作系统有时候被称为虚拟机(virtual machine),也就是提供虚拟化服务的机器啦(这话不准确,机器是硬件上的概念,操作系统是软件上的概念)。

1、虚拟化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 &

2、虚拟化内存

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "common.h"

int main(int argc, char **argv) {
    int *p = (int *)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;
}

出现的common.h文件为

#ifndef __common_h__
#define __common_h__

#include <sys/time.h>
#include <sys/stat.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 // __common_h__

并发

什么是**并发(concurrency)呢,简单点来说,就是有几个程序在一个处理机上运行,但是这里要注意就是所谓的并发,在某一个时刻点上,只有一个程序在运行中。这里与并行(Parallel)**区分,并行指的是在一个系统中有多个CPU的时候,一个CPU在跑一个线程,另一个CPU同时在跑另外一个线程,而且两者是在同一时间点的。

并发实例(多线程)

#include <stdio.h>
#include <stdlib.h>
#include "common_threads.h"

//volatile关键字将在另一篇文档中介绍
volatile int counter = 0;
int loops;

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

int main(int argc, char *argv[]) {
    if (argc != 2) {
        sprintf(stderr, "usage : threads <value>\n");
        exit(1);
    }
    loops = atoi(argv[1]);
    pthread_t p1, p2, p3;
    printf("Initial value : %d\n", counter);

    Pthread_create(&p1, NULL, worker, NULL);
    Pthread_create(&p2, NULL, worker, NULL);
    Pthread_create(&p3, NULL, worker, NULL);
    Pthread_join(p1, NULL);
    Pthread_join(p2, NULL);
    Pthread_join(p3, NULL);
    printf("Final value : %d\n", counter);

    return 0;
}

这里的common_threads.h其实就是在pthread.h提供的系统调用的基础上封装的,比如说pthread_create()创建线程、pthread_mutex_lock()线程加锁等,代码如下:

#ifndef __common_threads_h__
#define __common_threads_h__

#include <pthread.h>
#include <assert.h>
#include <sched.h>

#ifdef __linux__
#include <semaphore.h>
#endif

#define Pthread_create(thread, attr, start_routine, arg) assert(pthread_create(thread, attr, start_routine, arg) == 0);
#define Pthread_join(thread, value_ptr)                  assert(pthread_join(thread, value_ptr) == 0);

#define Pthread_mutex_lock(m)                            assert(pthread_mutex_lock(m) == 0);
#define Pthread_mutex_unlock(m)                          assert(pthread_mutex_unlock(m) == 0);
#define Pthread_cond_signal(cond)                        assert(pthread_cond_signal(cond) == 0);
#define Pthread_cond_wait(cond, mutex)                   assert(pthread_cond_wait(cond, mutex) == 0);

#define Mutex_init(m)                                    assert(pthread_mutex_init(m, NULL) == 0);
#define Mutex_lock(m)                                    assert(pthread_mutex_lock(m) == 0);
#define Mutex_unlock(m)                                  assert(pthread_mutex_unlock(m) == 0);
#define Cond_init(cond)                                  assert(pthread_cond_init(cond, NULL) == 0);
#define Cond_signal(cond)                                assert(pthread_cond_signal(cond) == 0);
#define Cond_wait(cond, mutex)                           assert(pthread_cond_wait(cond, mutex) == 0);

#ifdef __linux__
#define Sem_init(sem, value)                             assert(sem_init(sem, 0, value) == 0);
#define Sem_wait(sem)                                    assert(sem_wait(sem) == 0);
#define Sem_post(sem)                                    assert(sem_post(sem) == 0);
#endif // __linux__

#endif // __common_threads_h__

值得注意的时候,使用命令行终端编译的时候需要加上如下后缀-lpthread

gcc -o threads threads.c -Wall -lpthread

存在的问题:由于指令执行的顺序有问题,这就导致在线程并发执行的时候,执行顺序不同导致结果不同。

持久性

对于系统内存来说,数据容易丢失,常见的硬件设备诸如易失性内存和非易失性内存(NVMM)。对于易失性内存,系统一断电或者崩溃,数据就没了。因此需要软件和硬件来持久性(persistently)的存储数据。

操作系统中,用来管理磁盘的软件称之为文件系统。这里提一下,操作系统不仅管理文件,管理磁盘,管理内存、CPU等等各项资源,因此也被称之为资源管理器(resource manager)。对于文件系统来说,我们可以根据系统提供的系统调用来创建一个文件,得到文件的fd(file describer)文件描述符,然后就可以往文件描述符里面去写数据,最后还得把这个fd给关掉,防止造成内存泄漏。

I/O实例

#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>

int main(int argc, char **agrv) {
        char *pathname = "/tmp/file";
    int fd = open(pathname, 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;
}

其中对于程序中的诸如O_WRONLY , O_CREAT , O_TRUNC指的是文件权限,通过man手册可以看到详细介绍,使用方法如下:

man 3 open  //这里的3对应man手册里面的系统调用模块

OS设计目标

操作系统设计目标主要分为三个部分,分别是建立抽象(abstraction),提供高性能(performance)以及提供保护(protection)

抽象

抽象的目的是让系统便于使用,而不是难以理解的。比如说我们使用C语言这个高级语言就可以不用管汇编语言的的事情,使用汇编语言到时候可以不用考虑底层的逻辑,用逻辑电路构建处理器就不需要考虑晶体管。抽象,简化了系统,将系统划分成一个个易于理解的部分。

高性能

高性能简言之就是最小化系统开销,有两个主要的方面,分别是减少额外空间和额外时间的形式。与我们程序设计中时间复杂度和空间复杂度类似。操作系统寻求一个解决方案,平衡两者的开销,达到系统性能最优这样以一个状态。

提供保护

也就是提高系统的可靠性,或者对于那些恶意程序提供隔离,来达到保护操作系统这样一个目的。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值