教育资源为主的文档平台

当前位置: 查字典文档网> 所有文档分类> > 互联网> Assignment-Shell structure

Assignment-Shell structure

上传者:陈相
|
上传时间:2015-04-24
|
次下载

Assignment-Shell structure

操作系统作业

Basic Shell structure:

while (1) {

printf ("$");

readcommand (command, args); // parse user input

if ((pid = fork ()) == 0) { // child?

exec (command, args, 0);

}

else if (pid > 0) { // parent?

wait (0); // wait for child to terminate

}

else {

perror ("Failed to fork\n");

}

}

The split of creating a process with a new program in fork and exec is mostly a historical accident. See the assigned paper for today.

Example:

$ ls

why call "wait"? to wait for the child to terminate and collect its exit status. (if child finishes, child becomes a zombie

until parent calls wait.)

I/O: file descriptors. Child inherits open file descriptors from parent. By convention:

o file descriptor 0 for input (e.g., keyboard). read_command:

o read (1, buf, bufsize)

o file descriptor 1 for output (e.g., terminal)

o write (1, "hello\n", strlen("hello\n")+1)

o file descriptor 2 for error (e.g., terminal)

How does the shell implement:

$ls > tmp1

just before exec insert:

close (1);

fd = open ("tmp1", O_CREAT|O_WRONLY); // fd will be 1!

The kernel will return the first free file descriptor, 1 in this case.

A pipe is an one-way communication channel. Here is an example where the parent is the writer and the child is the

reader:

int fdarray[2];

if (pipe(fdarray) < 0) panic ("error");

if ((pid = fork()) < 0) panic ("error");

else if (pid > 0) {

close(fdarray[0]);

write(fdarray[1], "hello world\n", 12);

} else {

close(fdarray[1]);

n = read (fdarray[0], buf, MAXBUF);

write (1, buf, n);

}

Question: How does the shell implement pipelines (i.e., cmd 1 | cmd 2 |..)?

 

版权声明:此文档由查字典文档网用户提供,如用于商业用途请与作者联系,查字典文档网保持最终解释权!

下载文档

热门试卷

2016年四川省内江市中考化学试卷
广西钦州市高新区2017届高三11月月考政治试卷
浙江省湖州市2016-2017学年高一上学期期中考试政治试卷
浙江省湖州市2016-2017学年高二上学期期中考试政治试卷
辽宁省铁岭市协作体2017届高三上学期第三次联考政治试卷
广西钦州市钦州港区2016-2017学年高二11月月考政治试卷
广西钦州市钦州港区2017届高三11月月考政治试卷
广西钦州市钦州港区2016-2017学年高一11月月考政治试卷
广西钦州市高新区2016-2017学年高二11月月考政治试卷
广西钦州市高新区2016-2017学年高一11月月考政治试卷
山东省滨州市三校2017届第一学期阶段测试初三英语试题
四川省成都七中2017届高三一诊模拟考试文科综合试卷
2017届普通高等学校招生全国统一考试模拟试题(附答案)
重庆市永川中学高2017级上期12月月考语文试题
江西宜春三中2017届高三第一学期第二次月考文科综合试题
内蒙古赤峰二中2017届高三上学期第三次月考英语试题
2017年六年级(上)数学期末考试卷
2017人教版小学英语三年级上期末笔试题
江苏省常州西藏民族中学2016-2017学年九年级思想品德第一学期第二次阶段测试试卷
重庆市九龙坡区七校2016-2017学年上期八年级素质测查(二)语文学科试题卷
江苏省无锡市钱桥中学2016年12月八年级语文阶段性测试卷
江苏省无锡市钱桥中学2016-2017学年七年级英语12月阶段检测试卷
山东省邹城市第八中学2016-2017学年八年级12月物理第4章试题(无答案)
【人教版】河北省2015-2016学年度九年级上期末语文试题卷(附答案)
四川省简阳市阳安中学2016年12月高二月考英语试卷
四川省成都龙泉中学高三上学期2016年12月月考试题文科综合能力测试
安徽省滁州中学2016—2017学年度第一学期12月月考​高三英语试卷
山东省武城县第二中学2016.12高一年级上学期第二次月考历史试题(必修一第四、五单元)
福建省四地六校联考2016-2017学年上学期第三次月考高三化学试卷
甘肃省武威第二十三中学2016—2017学年度八年级第一学期12月月考生物试卷

网友关注视频

飞翔英语—冀教版(三起)英语三年级下册Lesson 2 Cats and Dogs
二次函数求实际问题中的最值_第一课时(特等奖)(冀教版九年级下册)_T144339
【部编】人教版语文七年级下册《过松源晨炊漆公店(其五)》优质课教学视频+PPT课件+教案,江苏省
外研版英语三起5年级下册(14版)Module3 Unit1
19 爱护鸟类_第一课时(二等奖)(桂美版二年级下册)_T502436
8 随形想象_第一课时(二等奖)(沪教版二年级上册)_T3786594
七年级下册外研版英语M8U2reading
六年级英语下册上海牛津版教材讲解 U1单词
第19课 我喜欢的鸟_第一课时(二等奖)(人美杨永善版二年级下册)_T644386
化学九年级下册全册同步 人教版 第22集 酸和碱的中和反应(一)
沪教版八年级下次数学练习册21.4(2)无理方程P19
【获奖】科粤版初三九年级化学下册第七章7.3浓稀的表示
《小学数学二年级下册》第二单元测试题讲解
人教版历史八年级下册第一课《中华人民共和国成立》
七年级英语下册 上海牛津版 Unit3
沪教版牛津小学英语(深圳用) 四年级下册 Unit 4
沪教版八年级下册数学练习册20.4(2)一次函数的应用2P8
第8课 对称剪纸_第一课时(二等奖)(沪书画版二年级上册)_T3784187
【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,辽宁省
19 爱护鸟类_第一课时(二等奖)(桂美版二年级下册)_T3763925
【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,天津市
沪教版八年级下册数学练习册21.3(3)分式方程P17
苏科版数学七年级下册7.2《探索平行线的性质》
外研版英语七年级下册module3 unit2第一课时
冀教版小学数学二年级下册第二周第2课时《我们的测量》宝丰街小学庞志荣
三年级英语单词记忆下册(沪教版)第一二单元复习
河南省名校课堂七年级下册英语第一课(2020年2月10日)
冀教版英语五年级下册第二课课程解读
【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,湖北省
外研版八年级英语下学期 Module3