OPERATING SYSTEM_Processes1

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

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值