由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一道练习题(C,OS)
相关主题
C -> assemblyPlease Help, dynamic memory after fork()
问个c++ 编译的问题问一个关于ANSI C中system命令的问题
how to assign new value to loop variables?问一个简单的问题:fork(), execvp(), 怎么传递函数
LabVIEW问题:对高手来说很简单!问个问题,关于gdb的
【包子求助】20M*20M的loop怎么搞?Linux shell编程的问题
如何实现N层循环嵌套问问跟Linux Kernel Programming 有关的问题
Perl 6 改动很大很恶心怎样用C调用外部程序并且*捕捉其输出呢*
请教一个Socket Programming 问题(C language)怎么在c++中输出一个文本文件的内容?
相关话题的讨论汇总
话题: nice话题: output话题: pid话题: echo话题: fork
进入Programming版参与讨论
1 (共1页)
E*******F
发帖数: 2165
1
这段code运行时为什么会有segmentation fault?
用的是nachos(一个模拟操作系统的教学软件)
#include "syscall.h"
#include "stdlib.h"
#define OUTPUT_LOOP_LIMIT 500
/*
* Set this up to experiment with different nice values. Negative
* values can cause unexpected execution orders. Figuring out WHY
* is an excellent exercise for the student.
*/
#define A_NICE 0
#define B_NICE 5
#define C_NICE 10
#define D_NICE 15
int
main()
{
int a,b,c,d;
int pid;
Echo ("Starting...\n", 12);
/* Divorce the test processes from the main process. The main
* thread will create the others and then simply wait for them to
* finish. This step is taken to ensure that the each of the four
* threads is treated as similarly as possible.
*/
/* Commence the forking */
pid = Fork();
if (pid == 0)
{
/*
* Child #1: This will fork the other test processes as well
* as becoming T-A
*/
/* Child #1 Forks a Grandchild #1 */
pid = Fork();
if (pid != 0) {
/*
* Child #1
* Now we fork a second time.
* Grandchild #2 is created
*/
pid = Fork();
if (pid != 0) {
/*
* Child #1: The first Child process makes it here. Now we
* adjust its static priority by calling the NICE system
* call, and then looping to output a series of
* identifying characters.
*/
Nice (A_NICE);
for (a = 0; a < OUTPUT_LOOP_LIMIT; a++) {
Echo ("A", 1);
}
Exit (0);
} else {
/*
* Grandchild #2
* Now we adjust its static priority by calling the
* NICE system call, and then looping to output a series of
* identifying characters.
*/
Nice (B_NICE);
for (b = 0; b < OUTPUT_LOOP_LIMIT; b++) {
Echo ("B", 1);
}
Exit (0);
}
} else {
/*
* Grandchild #1 forks GreatGrandchild #1
*/
pid = Fork();
if (pid != 0) {
/*
* GrandChild #1
* Now we adjust its static priority by calling the
* NICE system call, and then looping to output a series of
* identifying characters.
*/
Nice (C_NICE);
for (c = 0; c < OUTPUT_LOOP_LIMIT; c++) {
Echo ("C", 1);
}
Exit (0);
} else {
/*
* GreatGrandchild #1
* Now we adjust its static priority by calling the
* NICE system call, and then looping to output a series of
* identifying characters.
*/
Nice (D_NICE);
for (d = 0; d < OUTPUT_LOOP_LIMIT; d++) {
Echo ("D", 1);
}
Exit (0);
}
}
}
/*Original Thread*/
Wait (0);
Echo ("\nFinished\n", 10);
Exit(0); /* and then we're done */
}
k****5
发帖数: 546
2
用valgrind 查一下。

【在 E*******F 的大作中提到】
: 这段code运行时为什么会有segmentation fault?
: 用的是nachos(一个模拟操作系统的教学软件)
: #include "syscall.h"
: #include "stdlib.h"
: #define OUTPUT_LOOP_LIMIT 500
: /*
: * Set this up to experiment with different nice values. Negative
: * values can cause unexpected execution orders. Figuring out WHY
: * is an excellent exercise for the student.
: */

1 (共1页)
进入Programming版参与讨论
相关主题
怎么在c++中输出一个文本文件的内容?【包子求助】20M*20M的loop怎么搞?
fork(): why both if and else are executed?如何实现N层循环嵌套
问一个进程调度的问题。 (转载)Perl 6 改动很大很恶心
fork() in Windows C/C++请教一个Socket Programming 问题(C language)
C -> assemblyPlease Help, dynamic memory after fork()
问个c++ 编译的问题问一个关于ANSI C中system命令的问题
how to assign new value to loop variables?问一个简单的问题:fork(), execvp(), 怎么传递函数
LabVIEW问题:对高手来说很简单!问个问题,关于gdb的
相关话题的讨论汇总
话题: nice话题: output话题: pid话题: echo话题: fork