操作系统之shell实现(上)

news2025/5/26 8:18:19

🌟 各位看官好,我是maomi_9526

🌍 种一棵树最好是十年前,其次是现在!

🚀 今天来学习C语言的相关知识。

👍 如果觉得这篇文章有帮助,欢迎您一键三连,分享给更多人哦

目录

1. 进程创建

1.1fork函数

​编辑

1.2写时拷贝(Copy-on-write)

1.3 虚拟地址存储 

2. 进程终止

2.1进程退出情况

2.2退出方式

2.3退出码

2.4exit函数VS_exit函数

2.4.1exit函数

 2.4.2_exit函数

​编辑 2.4.3eixt VS_exit​编辑

3. 进程等待

3.1进程等待的必要性

 3.2wait等待

3.3获取子进程status 

4.总结

1. 进程创建

1.1fork函数

fork() 是 Linux 中非常重要的函数,用来从父进程创建一个子进程。父进程返回子进程的PID,子进程返回0。通过 fork() 创建的父子进程可以独立执行各自的代码。

头文件:#include<unistd.h>

返回值:成功父进程返回孩子pid,子进程返回0。失败返回-1。

函数:pid_t frok(void); 

#include<stdio.h>
#include<unistd.h>

int main()
{
    printf("Befor pid is %d\n",getpid());
    pid_t id=fork();
    if(id==-1)
    {
        perror("fork()");
        exit(1);
    }
    printf("After:pid is %d,return id is %d \n",getpid(),id);
}

结果: 

1.2写时拷贝(Copy-on-write)

fork() 使父子进程共享相同的内存内容,直到其中一个进程对内存进行修改,这时才会进行拷贝,避免不必要的资源浪费。 

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
    int a=0;
    printf("Befor pid is %d\n",getpid());
    pid_t id=fork();
    if(id==-1)
    {
        perror("fork()");
        exit(1);
    }

      printf("After:pid is %d,return id is %d \n",getpid(),id);  

}

 此时三个地址:a取地址一样

1.3 虚拟地址存储 

尝试在子进程中修改a的地址后,查看a的地址。

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
    int a=0;
    printf("Befor pid is %d,path is %p\n",getpid(),&a);
    pid_t id=fork();
    if(id==-1)
    {
        perror("fork()");
        exit(1);
    }
    if(id==0) 
    {
      int a=15;
      printf("After:pid is %d,return id is %d ,path is %p\n",getpid(),id,&a);  
    }
    else
    printf("After:pid is %d,return id is %d ,path is %p\n",getpid(),id,&a);

}

 三个地址仍然相同?这是为什么呢?

两个虚拟地址相同的原因在于它们位于不同的虚拟页面上,但在各自的虚拟页面内,偏移量相同。因此,尽管它们属于不同的虚拟内存块,最终的虚拟地址值却相同。

2. 进程终止

2.1进程退出情况
  • 代码运行完毕,结果正常
  • 代码运行完毕,结果不正常
  • 进程异常终止
2.2退出方式

如果系统正常终止可以通过$?来查看进程退出码

  • 从main函数返回
  • 调用exit
  • 调用_exit
2.3退出码

每个进程都有一个退出码,0 表示正常退出,非 0 表示出错。

2.4exit函数VS_exit函数
2.4.1exit函数

#include <unistd.h>

#include<stdlib.h>
void exit(int status); 

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
  printf("exit");
  exit(1);

}

 2.4.2_exit函数

#include <unistd.h>

#include<stdlib.h>
void _exit(int status); 

#include<stdio.h>
#include<unistd.h>
int main()
{
  printf("_exit");
  _exit(1);

}
 2.4.3eixt VS_exit

3. 进程等待

3.1进程等待的必要性
  • 之前讲过,子进程退出,父进程如果不管不顾,就可能造成‘僵尸进程’的问题,进而造成内存
  • 泄漏。
  • 另外,进程一旦变成僵尸状态,那就刀枪不入,“杀人不眨眼”的kill -9 也无能为力,因为谁也没有办法杀死一个已经死去的进程。
  • 最后,父进程派给子进程的任务完成的如何,我们需要知道。如,子进程运行完成,结果对还是不对,或者是否正常退出。
  • 父进程通过进程等待的方式,回收子进程资源,获取子进程退出信息
 3.2wait等待

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int *status);

返回值:
        成功返回被等待进程pid,失败返回-1。
参数:
        输出型参数,获取子进程退出状态,不关心则可以设置成为NULL

pid_t waitpid(pid_t pid, int *status, int options);

返回值:
        当正常返回的时候waitpid返回收集到的子进程的进程ID;
        如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;
        如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;
参数:
        pid:
                Pid=-1,等待任一个子进程。与wait等效。
                Pid>0.等待其进程ID与pid相等的子进程。
        status:

                输出型参数
                        WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)
                        WEXITSTATUS(status): 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)
options:默认为0,表示阻塞等待

                        WNOHANG: 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等
待。若正常结束,则返回该子进程的ID。

int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);

 

3.3获取子进程status
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
  int id=0;
  int cur=5;
  id=fork();
  if(id==-1)
  {
    perror("fork");
  }
  if(id==0)
  {
    while(cur--)
    {
      printf("My pid is %d,My ppid is %d\n",getpid(),getppid());
    }
    sleep(1);
    exit(1);
  }
  int status=0;
  int idd=waitpid(id,&status,0);
  printf("idd is %d,status is %d\n",idd,status);

}

3.3.1退出码 

为什么退出码是256而不是1?

  printf("idd is %d,status is %d\n",idd,(status>>8));

3.3.2终止信号 

4.总结

 1.Linux下的错误码及描述

0->Success
1->Operation not permitted
2->No such file or directory
3->No such process
4->Interrupted system call
5->Input/output error
6->No such device or address
7->Argument list too long
8->Exec format error
9->Bad file descriptor
10->No child processes
11->Resource temporarily unavailable
12->Cannot allocate memory
13->Permission denied
14->Bad address
15->Block device required
16->Device or resource busy
17->File exists
18->Invalid cross-device link
19->No such device
20->Not a directory
21->Is a directory
22->Invalid argument
23->Too many open files in system
24->Too many open files
25->Inappropriate ioctl for device
26->Text file busy
27->File too large
28->No space left on device
29->Illegal seek
30->Read-only file system
31->Too many links
32->Broken pipe
33->Numerical argument out of domain
34->Numerical result out of range
35->Resource deadlock avoided
36->File name too long
37->No locks available
38->Function not implemented
39->Directory not empty
40->Too many levels of symbolic links
41->Unknown error 41
42->No message of desired type
43->Identifier removed
44->Channel number out of range
45->Level 2 not synchronized
46->Level 3 halted
47->Level 3 reset
48->Link number out of range
49->Protocol driver not attached
50->No CSI structure available
51->Level 2 halted
52->Invalid exchange
53->Invalid request descriptor
54->Exchange full
55->No anode
56->Invalid request code
57->Invalid slot
58->Unknown error 58
59->Bad font file format
60->Device not a stream
61->No data available
62->Timer expired
63->Out of streams resources
64->Machine is not on the network
65->Package not installed
66->Object is remote
67->Link has been severed
68->Advertise error
69->Srmount error
70->Communication error on send
71->Protocol error
72->Multihop attempted
73->RFS specific error
74->Bad message
75->Value too large for defined data type
76->Name not unique on network
77->File descriptor in bad state
78->Remote address changed
79->Can not access a needed shared library
80->Accessing a corrupted shared library
81->.lib section in a.out corrupted
82->Attempting to link in too many shared libraries
83->Cannot exec a shared library directly
84->Invalid or incomplete multibyte or wide character
85->Interrupted system call should be restarted
86->Streams pipe error
87->Too many users
88->Socket operation on non-socket
89->Destination address required
90->Message too long
91->Protocol wrong type for socket
92->Protocol not available
93->Protocol not supported
94->Socket type not supported
95->Operation not supported
96->Protocol family not supported
97->Address family not supported by protocol
98->Address already in use
99->Cannot assign requested address
100->Network is down
101->Network is unreachable
102->Network dropped connection on reset
103->Software caused connection abort
104->Connection reset by peer
105->No buffer space available
106->Transport endpoint is already connected
107->Transport endpoint is not connected
108->Cannot send after transport endpoint shutdown
109->Too many references: cannot splice
110->Connection timed out
111->Connection refused
112->Host is down
113->No route to host
114->Operation already in progress
115->Operation now in progress
116->Stale file handle
117->Structure needs cleaning
118->Not a XENIX named type file
119->No XENIX semaphores available
120->Is a named type file
121->Remote I/O error
122->Disk quota exceeded
123->No medium found
124->Wrong medium type
125->Operation canceled
126->Required key not available
127->Key has expired
128->Key has been revoked
129->Key was rejected by service
130->Owner died
131->State not recoverable
132->Operation not possible due to RF-kill
133->Memory page has hardware error

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2336392.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构与算法——链表OJ题详解(2)

文章目录 一、前言二、OJ续享2.1相交链表2.2环形链表12.2环形链表2 三、总结 一、前言 哦了兄弟们&#xff0c;咱们上次在详解链表OJ题的时候&#xff0c;有一部分OJ题呢up并没有整理完&#xff0c;这一个星期呢&#xff0c;up也是在不断的学习并且沉淀着&#xff0c;也是终于…

Linux 基础知识详解

Linux 基础知识详解 一、快照与克隆 1. &#x1f4f8;快照&#xff08;Snapshot&#xff09; 快照是虚拟机当前运行状态的一次“瞬间拷贝”&#xff0c;包括内存、磁盘、配置等信息。这使得管理员能够快速恢复到某个特定的时间点。 用途&#xff1a; 安全实验前保存状态&am…

centOs7配置有限网络

最简单快速的是使用nmtui命令&#xff0c;采用图形页面修改。 点击编辑连接并回车&#xff1a; 选中编辑然后回车&#xff1a; 千万记住DNS服务器就是子网掩码&#xff0c;不是常说的DNS域名。把地址&#xff0c;网关&#xff0c;子网掩码配置好。只要ip不冲突&#xff0c;网…

C语言 —— 指尖跃迁 刻印永恒 - 文件操作

目录 1. 什么是文件 1.1 程序文件 1.2 数据文件 1.3 文件名 2. 二进制文件和文本文件 3. 文件的打开与关闭 3.1 流和标准流 3.2 文件指针 3.3 文件的打开与关闭 fopen fclose 4. 文件的顺序读写 4.1 fgetc和fputc fgetc fputc 4.2 fgets和fputs fgets fputs…

网络安全与信息安全的区别​及共通

在数字化时代&#xff0c;网络安全与信息安全已成为保障个人、企业乃至国家正常运转的重要防线。尽管二者紧密相关且常被混为一谈&#xff0c;但实则存在显著差异。当然&#xff0c;它们也有一些相同点&#xff0c;比如都以保障数字环境下的安全为核心目标&#xff0c;均需要通…

【愚公系列】《Python网络爬虫从入门到精通》052-Scrapy 编写 Item Pipeline

&#x1f31f;【技术大咖愚公搬代码&#xff1a;全栈专家的成长之路&#xff0c;你关注的宝藏博主在这里&#xff01;】&#x1f31f; &#x1f4e3;开发者圈持续输出高质量干货的"愚公精神"践行者——全网百万开发者都在追更的顶级技术博主&#xff01; &#x1f…

【AI News | 20250416】每日AI进展

AI Repos 1、Tutorial-Codebase-Knowledge 自动分析 GitHub 仓库并生成适合初学者的通俗易懂教程&#xff0c;清晰解释代码如何运行&#xff0c;还能生成可视化内容来展示核心功能。爬取 GitHub 仓库并从代码中构建知识库&#xff1b;分析整个代码库以识别核心抽象概念及其交互…

GIS开发笔记(6)结合osg及osgEarth实现半球形区域绘制

一、实现效果 输入中心点坐标及半径&#xff0c;绘制半球形区域&#xff0c;地下部分不显示。 二、实现原理 根据中心点及半径绘制半球形区域&#xff0c;将其挂接到地球节点。 三、参考代码 void GlobeWidget::drawSphericalRegion(osg::Vec3d point,double radius) {// 使…

element-ui自定义主题

此处的element-ui为基于vue2.x的 由于https://element.eleme.cn/#/zh-CN/theme/preview&#xff08;element的主题&#xff09;报错503&#xff0c; 所以使用https://element.eleme.cn/#/zh-CN/component/custom-theme 自定义主题文档中&#xff0c;在项目中改变scss变量的方…

windows下使用nginx + waitress 部署django

架构介绍 linux一般采用nginx uwsgi部署django&#xff0c;在Windows下&#xff0c;可以取代uwsgi的选项包括Waitressa、Daphnea、Hypercoma和Gunicorna(通过WSLa 运行)。windows服务器一般采用nginx waitress 部署django&#xff0c;,他们的关系如下 django是WEB应用…

MySQL-多版本并发控制MVCC

文章目录 一、多版本并发控制MVCC二、undo log&#xff08;回滚日志&#xff09;二、已提交读三、可重复读总结 一、多版本并发控制MVCC MVCC是多版本并发控制&#xff08;Multi-Version Concurrency Control&#xff09;&#xff0c;是MySQL中基于乐观锁理论实现隔离级别的方…

目标检测与分割:深度学习在视觉中的应用

&#x1f50d; PART 1&#xff1a;目标检测&#xff08;Object Detection&#xff09; 1️⃣ 什么是目标检测&#xff1f; 目标检测是计算机视觉中的一个任务&#xff0c;目标是让模型“在图像中找到物体”&#xff0c;并且判断&#xff1a; 它是什么类别&#xff08;classif…

杰弗里·辛顿:深度学习教父

名人说&#xff1a;路漫漫其修远兮&#xff0c;吾将上下而求索。—— 屈原《离骚》 创作者&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 杰弗里辛顿&#xff1a;当坚持遇见突破&#xff0c;AI迎来新纪元 一、人物简介 杰弗…

STM32蓝牙连接Android实现云端数据通信(电机控制-开源)

引言 基于 STM32F103C8T6 最小系统板完成电机控制。这个小项目采用 HAL 库方法实现&#xff0c;通过 CubeMAX 配置相关引脚&#xff0c;步进电机使用 28BYJ-48 &#xff08;四相五线式步进电机&#xff09;&#xff0c;程序通过蓝牙连接手机 APP 端进行数据收发&#xff0c; OL…

第一个Qt开发的OpenCV程序

OpenCV计算机视觉开发实践&#xff1a;基于Qt C - 商品搜索 - 京东 下载安装Qt&#xff1a;https://download.qt.io/archive/qt/5.14/5.14.2/qt-opensource-windows-x86-5.14.2.exe 下载安装OpenCV&#xff1a;https://opencv.org/releases/ 下载安装CMake&#xff1a;Downl…

TCP 如何在网络 “江湖” 立威建交?

一、特点&#xff1a; &#xff08;一&#xff09;面向连接 在进行数据传输之前&#xff0c;TCP 需要在发送方和接收方之间建立一条逻辑连接。这一过程类似于打电话&#xff0c;双方在通话前需要先拨号建立连接。建立连接的过程通过三次握手来完成&#xff0c;确保通信双方都…

【小白训练日记——2025/4/15】

变化检测常用的性能指标 变化检测&#xff08;Change Detection&#xff09;的性能评估依赖于多种指标&#xff0c;每种指标从不同角度衡量模型的准确性。以下是常用的性能指标及其含义&#xff1a; 1. 混淆矩阵&#xff08;Confusion Matrix&#xff09; 定义&#xff1a;统…

数据结构——二叉树(中)

接上一篇&#xff0c;上一篇主要讲解了关于二叉树的基本知识&#xff0c;也是为了接下来讲解关于堆结构和链式二叉树结构打基础&#xff0c;其实无论是堆结构还是链式二叉树结构&#xff0c;都是二叉树的存储结构&#xff0c;那么今天这一篇主要讲解关于堆结构的实现与应用 堆…

02-MySQL 面试题-mk

文章目录 1.mysql 有哪些存储引擎、区别是什么?1.如何定位慢查询?2.SQL语句执行很慢,如何分析?3.索引概念以及索引底层的数据结构4.什么是聚簇索引什么是非聚簇索引?5.知道什么叫覆盖索引嘛 ?6.索引创建原则有哪些?7.什么情况下索引会失效 ?8.谈一谈你对sql的优化的经验…

#include<bits/stdc++.h>

#include<bits/stdc.h> 是 C 中一个特殊的头文件&#xff0c;其作用如下&#xff1a; 核心作用 ​​包含所有标准库头文件​​ 该头文件会自动引入 C 标准库中的几乎全部头文件&#xff08;如 <iostream>、<vector>、<algorithm> 等&#xff09;&…