OPERATING SYSTEM_Processes1

本文深入探讨操作系统中的进程概念,解析进程创建、状态共享、并发执行等问题,并通过具体代码实例解释fork()函数的工作原理及进程间如何共享或独立管理资源。

OPERATING SYSTEM

摘要:有关操作系统–进程的理解&课后习题解答。
主要语言:English

Processes

3.1
Q:Using the program shown in Figure 3.30, explain what the output will be at LINE A.

#include <sys/types.h> 
#include <stdio.h> 
#include <unistd.h>
int value = 5;
int main()
{
	pid t pid;
	pid = fork();
	if (pid == 0) { /* child process */ 
		value += 15;
		return 0;
	}
	else if (pid > 0) { /* parent process */
		wait(NULL);
        printf("PARENT: value = %d",value); /* LINE A */
        return 0;
	} 
}

A:PARENT:value=5

E:When the parent process calls the fork function, the newly created child process is not exactly the same as the parent process. The child process gets a copy of the parent process user-level virtual address space, but the copy is independent, and the copy includes text, data, and Bss segment, heap, and user stack.
Therefore, when the program is executed in the figure, the child process has its own virtual address space, which stores a copy of the same code, data, and user stack as the parent process, and is independent of the address space of the parent process. Therefore, both the child process and the parent process have a value variable, which does not affect each other.
When the program is executed, the value in the child process will change to 20, but the value of the parent process will still be 5.

3.2
**Q:**Including the initial parent process, how many processes are created by the program shown in Figure 3.31?
#include <stdio.h> #include <unistd.h>

int main()
{
/* fork a child process */
	fork();
/* fork another child process */
	fork();
/* and fork another */
	fork();
	return 0;
}

A: 8 peocesses are created.
E:
附上自己画的图:
在这里插入图片描述
来自教材的图示:
P119

3.3
Q: Original versions of Apple’s mobile iOS operating system provided no means of concurrent processing. Discuss three major complications that concurrent processing adds to an operating system.

  1. Battery life and memory use concerns电池寿命和内存空间
    If a running process requires large space in memory then other processs needs to be dumped back to hard disk.
    如果一个正在运行的进程需要很大的内存空间,其他进程可能会因为内存空间不足而被退回硬盘。
  2. Data security数据安全性
    If the operating system does not allocate a fixed memory address space for each process, a process may affect or damage the data of other processes. For example, a virus will attempt to destroy and modify data from other processes.
    操作系统如果没有为每一个进程分配固定的内存地址空间, 一个进程可能会去影响或者损坏其他进程的数据。
  3. Time overhead 时间开销
    Switching from one process to another process leads to time overhead, this requires storing the current register values and loading the register values of the next process from its PCB(program Control Block).
    从一个进程切换到另一个进程会导致时间开销,这需要存储当前寄存器值并从其PCB(程序控制块)中加载下一进程的寄存器值。

MULTITASKING IN MOBILE SYSTEMS
Because of the constraints imposed on mobile devices, early versions of iOS did not provide user-application multitasking; only one application runs in the foreground and all other user applications are suspended. Operating- system tasks were multitasked because they were written by Apple and well behaved. However, beginning with iOS 4, Apple now provides a limited form of multitasking for user applications, thus allowing a single foreground application to run concurrently with multiple background applications. (On a mobile device, the foreground application is the application currently open and appearing on the display. The background application remains in memory, but does not occupy the display screen.) The iOS 4 programming API provides support for multitasking, thus allowing a process to run in the background without being suspended. However, it is limited and only available for a limited number of application types, including applications
• running a single, finite-length task (such as completing a download of content from a network);
• receiving notifications of an event occurring (such as a new email message);
• withlong-runningbackgroundtasks(suchasanaudioplayer.)
Apple probably limits multitasking due to battery life and memory use concerns. The CPU certainly has the features to support multitasking, but Apple chooses to not take advantage of some of them in order to better manage resource use.
Android does not place such constraints on the types of applications that can run in the background. If an application requires processing while in the background, the application must use a service, a separate application component that runs on behalf of the background process. Consider a streaming audio application: if the application moves to the background, the service continues to send audio files to the audio device driver on behalf of the background application. In fact, the service will continue to run even if the background application is suspended. Services do not have a user interface and have a small memory footprint, thus providing an efficient technique for multitasking in a mobile environment.
BY Operating-System-Concepts—9th 2012.12.

3.5
Q: When a process creates a new process using the fork() operation, which of the following states is shared between the parent process and the child process?
a. Stack
b. Heap
c. Shared memory segments
A: C.Shared memory segments
E: When the parent process calls the fork function, the newly created child process is not exactly the same as the parent process. The child process gets a copy of the parent process user-level virtual address space, but the copy is independent, and the copy includes text, data, and Bss segment, heap, and user stack.当父进程调用fork函数时, 新创建的子进程不完全与父进程相同, 子进程会获得一份父进程用户级虚拟地址空间的拷贝, 但是此拷贝是独立的, 拷贝内容包括文本、数据和bss段、堆以及用户栈。
stack heap 由子进程新建。

3.6
Q: Consider the “exactly once”semantic with respect to the RPC mechanism. Does the algorithm for implementing this semantic execute correctly even if the ACK message sent back to the client is lost due to a network problem? Describe the sequence of messages, and discuss whether “exactly once” is still preserved.
考虑关于RPC机制的“恰好一次”语义。 即使由于网络问题丢失了发送回客户端的ACK消息,用于实现这种语义的算法也能正确执行吗? 描述消息的顺序,并讨论是否仍然“仅一次”。
A: 参考[https://wenku.baidu.com/view/059dccfff242336c1fb95e44.html]
The exactly once" semantics ensure that a remore procedure will be executed exactly once and only once. The client sends the RPC to the serveralong with a timestamp. The client will also start a timeout clock. The client will then wait for one of two occurrences: (1) it will receive an ACK from the server indicating that the remote procedure was performed,or (2) it will time out. If the client times out, it assumes the server wasunable to perform the remote procedure so the client invokes the RPC asecond time, sending a later timestamp. The client may not receive theACK for one of two reasons: (1) the original RPC was never received bythe server, or (2) the RPC was correctly received- -and performed- bythe server but the ACK was lost. In situation (1), the use of ACKs allowsthe server ultimately to receive and perform the RPC. In situation (2),the server will receive a duplicate RPC and it will use the timestamp toidentify it as a duplicate so as not to perform the RPC a second time. Itis important to note that the server must send a second ACK back to theclient to inform the client the RPC has been performed.
个人理解:客户端将RPC与时间戳timestamp一起发送到服务器并启动超时时钟timeout clock。然后,客户端将等待以下两种情况之一:
(1)从服务器接收到一个ACK,指示已执行RPC
(2)超时。如果客户端超时,则假定服务器无法执行远程过程,因此客户端第二 次调用RPC,并发送稍后的时间戳。
客户端可能由于以下两个原因之一而未收到ACK:
(1)服务器从未接收过原始RPC
(2)服务器已正确接收并执行了RPC,但ACK丢失了。
在情况(1)中,ACK的使用最终允许服务器接收并执行RPC。在情况(2)中,服务器将收到重复的RPC,它将使用时间戳将其标识为重复的,以免第二次执行RPC。
E: At most once:至多一次。消息在传递时,最多会被送达一次。也就是说,没什么消息可靠性保证,允许丢消息(少量)。
At least once:至少一次。消息在传递时,至少会被送达一次。也就是说,不允许丢消息,但是允许重复消息(少量)。
Exactly once:恰好一次。消息在传递时,只会被送达一次,不允许丢失也不允许重复。「很严格了」

3.14
Q: Using the program in Figure 3.34, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)

#include <sys/types.h> 
#include <stdio.h> 
#include <unistd.h>
int main()
{
	pid_t pid, pid1;
    /* fork a child process */
    pid = fork();
	if (pid < 0) { /* error occurred */ 
		fprintf(stderr, "Fork Failed"); 
		return 1;
	}
	else if (pid == 0) { /* child process */
        pid1 = getpid();
        printf("child: pid = %d",pid); /* A */
        printf("child: pid1 = %d",pid1); /* B */
}
else { /* parent process */
              pid1 = getpid();
              printf("parent: pid = %d",pid); /* C */
              printf("parent: pid1 = %d",pid1); /* D */
              wait(NULL);
}
return 0;
}

A: A:0 B:2603 C:2603 D:2600
E: 首先是getpid()函数:用来取得目前进程的pid,许多程序利用取到的此值来建立临时文件,以避免临时文件相同带来的问题。返回值为当前进程的pid。
fork函数执行一次, 返回两次,父进程返回子进程的PID, 子进程返回0。所以a的pid是0。c的pid是子进程的实际pid。两个pid1是真实的pid。

About fork()

In UNIX, as we’ve seen, each process is identified by its process identifier, which is a unique integer. A new process is created by the fork() system call. The new process consists of a copy of the address space of the original process. This mechanism allows the parent process to communicate easily with its child process. Both processes (the parent and the child) continue execution at the instruction after the fork(), with one difference: the return code for the fork() is zero for the new (child) process, whereas the (nonzero) process identifier of the child is returned to the parent.

BY Operating-System-Concepts—9th 2012.12.

当调用fork()时,将执行以下动作:

  • 向系统申请一个新PID
  • 创建子进程,复制父进程的PCB,获得父进程的数据空间、堆、栈等资源的副本
  • 在父进程中返回子进程的PID,在子进程中返回0

在这里插入图片描述

Main words by myself:

  1. 调用一次,返回两次。
    fork函数执行一次, 返回两次,父进程返回子进程的PID, 子进程返回0。(如果出现错误,fork则返回一个负值)
    但是用getpid函数获取的是当前实际的pid【见3.14】
  2. 并发执行,独立过程。
    当父进程调用fork()后,fork()得到一个新的PID并进行复制副本、复制PCB等一系列子进程的准备工作后,此时父子两个进程将并发执行,这时候共有两个fork()存在,父子进程中都在等待着fork()函数的最后一步:返回值。
    (fork – 创建新进程;exit – 终止进程;exec – 执行一个应用程序wait – 将父进程挂起,等待子进程终止;getpid – 获取当前进程的PID;nice – 改变进程的优先 见【3.2】)
  3. 独立、相似的地址空间。
    每个进程的有相同的用户栈、本地变量值、相同的堆、相同的全局变量值, 以及相同的代码。但是父进程和子进程都是独立的进程, 它们都有自己的私有地址空间。 父进程和子进程对x所做的任何改变都是独立的, 不会反映在另一个进程的存储器中。 【见3.1】
  4. 共享/继承文件。 子进程继承了父进程所有打开的文件。 当父进程调用fork函数时, stdout文件是被打开的, 并指向屏幕。 所以子进程的输出也指向了屏幕。会输出两个。【见3.5】
有关“fork()到底生成了多少程序”这一类问题的总结:

(由于应试,自己总结的规律,不一定有科学根据)

  • 有if:n+1个
  • 无if:2n

放几个参考资料:
fork图解:https://www.cnblogs.com/sea-stream/p/11234950.html
作业:https://blog.csdn.net/zhousiyu369/article/details/93759978
getpid:https://www.cnblogs.com/becomebetter/articles/2442264.html

下载代码方式:https://pan.quark.cn/s/dd3561eca308 在软件开发领域,面向对象编程(OOP)是一种普遍采纳的结构化方法,它使得开发者能够借助模拟现实环境中的实体和关系来构建软件系统。在本案例中,我们观察到的是一个关于借助抽象类来执行不同几何图形面积求解的实践应用。现在,让我们详细分析这一议题。 标题 "应用抽象类计算面积" 清晰地表明我们将要讨论一个抽象类,此类设定了一个用于测量图形面积的标准函数,但并未提供实际的执行过程。抽象类在诸如C#或Java等编程语言中通常借助`abstract`修饰符进行声明,它们无法直接创建对象实例,仅能作为其他类的基础模板。 描述部分提及的"图形界面应用"暗示这是一个基于视觉用户界面(GUI)的系统,可能运用了.NET Framework的Windows Forms或WPF技术,或者是Java平台的Swing或JavaFX框架。在这样环境下,用户能够通过视觉元素与这些几何体进行互动,例如输入相关尺寸并观看到计算得出的面积值。 抽象类“几何体”内嵌了“计算面积”这一抽象函数。在代码层面,这可以被表述为: ```csharp public abstract class GeometricShape { public abstract double CalculateArea(); } ``` 随后,有三个派生类:圆(Circle)、矩形(Rectangle)和三角形(Triangle),它们各自提供了这个抽象函数的具体实现。比如,圆的面积是通过π乘以半径的平方得到的,矩形的面积是长和宽的乘积,而三角形的面积可能是底乘以高再除以2的结果。这些类将提供具体实现来计算它们各自的面积: ```csharp p...
内容概要:本文系统研究了移相控制全桥LLC谐振变换器的工作特性,深入分析其在不同工作模式下的运行机理与性能表现,重点探讨了软开关实现、高效率能量转换及宽范围电压调节等关键技术优势。通过Simulink搭建精确的仿真模型,对谐振腔参数、开关频率、电压增益、系统效率等关键指标进行仿真分析,验证了理论设计的正确性。同时,详细研究了移相控制策略对系统动态响应、稳定性和轻载/重载工况适应性的影响,揭示了控制参数与电路参数之间的耦合关系,为高频高效电源设计提供了理论依据和实践指导。; 适合人群:具备电力电子技术、模拟电路及自动控制理论基础,从事开关电源、新能源变换器、电动汽车充电模块或高频电源系统研发的工程师及高校研究生。; 使用场景及目标:①掌握全桥LLC谐振变换器的拓扑结构、工作原理与关键参数设计方法;②理解移相控制在实现零电压开通(ZVS)和零电流关断(ZCS)中的作用机制;③通过Simulink仿真掌握变换器建模、参数优化与性能评估流程,服务于实际产品开发与学术课题研究。; 阅读建议:建议读者结合提供的Simulink仿真模型进行同步操作,重点关注谐振网络(Lr, Lm, Cr)参数与移相角之间的匹配设计,深入理解软开关条件的形成过程,并通过调整负载和输入电压进行多工况仿真,以全面掌握系统动态特性。
一、项目概述 本项目设计并实现了一个基于STM32F103C8T6微控制器的温湿度监测与报警系统。系统通过DHT11传感器实时采集环境温湿度数据,当数据超过预设阈值时,蜂鸣器发出声音报警,同时通过4位数码管实时显示当前温湿度值。整个系统采用Type-C接口供电,支持USB串口通信,便于数据调试与传输。 二、硬件组成 1. 主控模块:STM32F103C8T6最小系统(含晶振、复位、BOOT电路、去耦电容)。 2. 传感器模块:DHT11温湿度传感器(单总线通信)。 3. 报警模块:有源蜂鸣器(三极管驱动)。 4. 显示模块:4位共阴数码管(TM1637驱动)。 5. 通信模块:CH340N USB转串口芯片(Type-C接口)。 6. 电源模块:AMS1117-3.3稳压电路(5V转3.3V)。 7. 调试接口:SWD调试排针(SWDIO/SWCLK)。 三、开发流程 1. 原理图设计:使用嘉立创EDA绘制完整原理图,包含各功能模块。 2. PCB设计:进行PCB布局布线,注重电源完整性、信号隔离及晶振走线。 3. 制板焊接:嘉立创打样,手工焊接元器件。 4. 软件开发:STM32CubeMX配置工程,编写DHT11驱动、TM1637显示、串口通信及报警逻辑。 5. 调试测试:通过SWD下载程序,串口输出数据,测试温湿度采集与报警功能。 四、项目特点 - 完整的嵌入式系统开发流程(硬件设计→制板→软件编程→调试)。 - 多传感器数据融合与实时显示。 - 低功耗设计,支持USB供电与串口通信。 - 模块化设计,便于功能扩展(如添加WiFi模块、数据存储等)。 五、应用前景 适用于家庭环境监测、农业大棚、仓库温湿度监控等场景
内容概要:本文提出了一种基于TOGI-SOGI混合积分器的光储并网谐波自适应抑制方法,并通过Simulink实现完整仿真验证。该方法融合三重二阶广义积分器(TOGI)与标准二阶广义积分器(SOGI),能够精确提取电网电压中的基波正序分量,有效分离并抑制高次谐波成分,尤其在电网电压畸变条件下显著提升了并网逆变器的控制精度与电能质量。文中详细阐述了混合积分器的结构设计、谐波检测机制、自适应调节策略及其在锁相环(PLL)中的集成应用,并通过构建光储并网系统模型进行多工况仿真,结果表明该方法具备优良的动态响应特性、强鲁棒性及谐波抑制能力。; 适合人群:具备电力电子、新能源发电、自动控制等相关专业背景,熟悉Simulink仿真环境,从事光伏并网控制、电能质量治理、微电网运行与控制等领域研究的科研人员、工程技术人员及研究生。; 使用场景及目标:①应用于电网存在谐波污染的光伏发电并网场景,提升逆变器在非理想电网下的运行稳定性;②为基于广义积分器的先进锁相技术与谐波补偿策略提供理论支持与实现范例;③服务于高校科研项目复现、学位论文研究、电力电子控制器原型开发及学术成果验证。; 阅读建议:建议学习者结合提供的Simulink模型深入剖析TOGI-SOGI的内部信号流向与参数整定逻辑,重点关注谐波分量的解耦过程与自适应控制模块的实现机制,可通过设置不同谐波含量、频率偏移等扰动工况进行对比测试,以全面掌握其在复杂电网环境下的适应性与优越性。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值