ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022-2-22 《操作系统导论》的代码 —— 第2章操作系统介绍

2022-02-22 15:34:52  阅读:143  来源: 互联网

标签:file NULL 操作系统 22 int 2022 pthread include cpu


体会 CPU 虚拟化,同时运行许多程序
cpu.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<assert.h>
#include <unistd.h>
int main(int argc,char* argv[]){
    if(argc != 2){
        fprintf(stderr,"usage:cpu<string>\n");
    }
    char *str = argv[1];
    while (1)
    {
        sleep(1);
        printf("%s\n",str);
    }
    return 0;
}

程序的单个实例
在这里插入图片描述
进程的多个实例

./cpu A & ./cpu B & ./cpu C & ./cpu D & 

在这里插入图片描述
在这里插入图片描述

运行多个实例的时候,无法通过 ctrl + c 使得进程停止。只能在另外的终端通过 kill -9 (pid) 终止进程。

mem.c

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<assert.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 long)p);
    //printf("(%d) memory address of p:%08x\n",getpid(),(unsigned)p);
    //cast from ‘int*’ to ‘unsigned int’ loses precision [-fpermissive]
    //主要是 8 字节的指针转化为 unsigned(4字节),不符合转换的精度,
    //应该转化为 unsigned long
    //由此可以得知,原来的程序是在 32位的电脑上测试
    *p = 0;
    while (1)
    {
        sleep(1);
        *p = *p + 1;
        printf("(%d) p: %d\n",getpid(),*p);
    }
    return 0;
}

运行程序的一个实例
在这里插入图片描述
运行程序的多个实例
在这里插入图片描述
和书本上的结果不同,两个进程并没有表现出相同的地址

threads.c

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
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){
        fprintf(stderr,"usage:threads<value>\n");
        exit(1);
    }
    loops = atoi(argv[1]);
    pthread_t p1,p2;
    int ret = pthread_create(&p1,NULL,worker,NULL);
    if(ret == -1){
        perror("pthread_create");
        exit(1);
    }
    ret = pthread_create(&p2,NULL,worker,NULL);
    if(ret == -1){
        perror("pthread_create");
        exit(1);
    }
    pthread_join(p1,NULL);
    pthread_join(p2,NULL);
    printf("Final Value     : %d\n",counter);
    return 0;
}

每次输出得到的结果都不一样。
在这里插入图片描述
顺便复习一下 pthread_join()
pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。

io.c

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main(void){
   // int fd = open("/tmp/file",O_WRONLY | O_CREAT | O_TRUNC,S_IRWXU);
   int fd = open("file.txt",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;
}

在这里插入图片描述
程序里面的补充知识

 The following symbolic constants are provided for mode:

              S_IRWXU  00700 user (file owner) has read,  write,  and  execute
                       permission

 O_TRUNC
              If  the file already exists and is a regular file and the access
              mode allows writing (i.e., is O_RDWR or  O_WRONLY)  it  will  be
              truncated to length 0.  If the file is a FIFO or terminal device
              file, the O_TRUNC flag is ignored.   Otherwise,  the  effect  of
              O_TRUNC is unspecified.
//好像是如果原来文件存在,其长度就会被截断成 0

标签:file,NULL,操作系统,22,int,2022,pthread,include,cpu
来源: https://blog.csdn.net/weixin_51187533/article/details/123066325

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有