2020年新版操作系统实验报告进程管道及消息通信x

实验题目

进程的管道及消息通信

小组合作

姓名

班级

学 号

一、实验目的

1、 为了掌握linux进程通信系统调用的功能,这里给出了进程通信实 现机制中使用的系统调用命令的格式和如何利用系统调用命令进行

进程通信编程,以便通过学习,提高学生对进城通信系统调用的编程 能力。

2、 理解进程通信的原理,掌握管道通信机制,熟悉无名管道和有名 管道通信的实现。

二.实验环境

Windows xp、VMware、Linux 系统

三、实验内容与步骤

编写两个程实现进程的无名管道和有名管道通信。要求分别调

用 pipe()、close。、write()、read()、popen()、pclose()、mknod()、mkfifo()、

ope n()实现多个进程间的通信。

调试并分析结果。

对观察到的内容做详细记录分析,并写出实验报告。

四、头验过程与分析

1?使用无名管道pipe(),进行父子进程之间的通信。

编写的程序如下:

eh r parent[] =,A nT-isage I o p ipe ' conminL^ L ion An' rra inC Jin I p id ?chanl[2]: h j

eh r parent[] =,A nT-isage I o p ipe ' conminL^ L ion An' rra inC J

in I p id ?chanl[2]: h j ]■ bu f [ ItJl)]:

F ipe< ch-aiil >; 卩 id=fo rk?'';

pr in t fC' tg cica le child cr ror\n' )■: ex i t<Ot

if pid>0)

closet chilli [0]):

pr inlf(*paren1 proce si a ntEMsg电 lo

ttii[e( cha nI[1].parent p s i zeo f(parent)) clo^fC chan!(1]):

pr iniff"pai?ni proce ss uai1 £ Ihe ch lId io vdi \ L<0):

pr in t f(Tparenl pro^e e s te-r mite . \ n' );

child An* )i

close(ckan][I]);

read(chant [C]| ,buf f 100);

pr in L f f " The ]ir s s a ge read by child process close(chan 1[0]): pr intf< * chi Id

from pa rcn I i s; s.s An' ,bu f):

proce s(; riniina te . \n');

运行结果如下:

[too t@]oca llios t ldw]| [root@loca Ihos t Idw]

pa n?n l pi ece ss a tit s iiago

partn I process wills ihe ch i Id to

cc Ifi-1 . c -o 1G-1.exe

to

The nvread by chi Id process

child.

t e r mi na I e .

i'j uir paren l i s : .X

nri<iagc to pipe cormuniea t ion .

child procc ss termini le. pa rtn t process termna te * [l刨。仙 Ihowt Ldw]# I

结果分析:

父进程首先被调用时,运行结果为:

parcn t procc 5 s send a nr s sago to ch i Id . parem procec*; ua Us the chi Id to iermina te.

之后父进程阻塞等待子进程终止,当系统调度子进程运行时,输出如

下的信息:

Tht itt'st&age read bj dvi M process fro tn parent ii: X tru^^age id pipe >oirmitueii lion.

8

child procs unmnaie. ..

The nr s sage read by ch iId procc s 5 from parent i s:A nr s sage to pipe'conrmn ica t ion. th i Id procesi hermin牡 i亡*

之后父进程被唤醒,调度运行,输出如下结果后程序退出。

parcn L proce5 a le rmina te .

对于以上的结果:首先父进程使用 pipe (chan1)系统调用打开一个

无名管道,之后创建一个子进程。子进程复制父进程的打开文件表。

 为了正确通信,父进程关闭读通道 close( chan1[0]),子进程关闭写

通道close( chan1[1])。父进程向管道写,子进程从管道读。完成一 次通信之后,父进程分别关闭自己的写/读通信,管道文件消失。

2.以命名行为参数的管道文件的示例。 (假设有一个可执行程序

chcase从标准输入设备读字符,将小写字母转化成大写字母并输出。

 主程序使用popen创建管道,实现蒋某文本文件中的字幕转化成大写 字母,其中的文本文件名作为参数传进来。) 编写的程序如下:

|inf lude<fltd io.h>

# inc lude< sy s/va i l.h>

#def ine WXIJbE 100

int inn in( in t rr号l ^hur * d r^v[])

i

、hmr I ine[MkXLlM^]:

FILE * fpin + * fp卅

iftargc1-2)

{

fpr m if f j; I de r r h usage :a .011 t^pa thnaiir^ \ n T ) 1 ex i1(1)1

\

d j 1 ( fp iii= fope n( d r gx [ 1 ] , 1 r ' ,}) = -NLLL}

{

fpi hnfC ide I r , "can1 I open J \ 11' harg:v [ 1 ]);

I

]I< ( fpou t=poprn( ' <tirase' < ' i )==MLl )

{

fpr in 1 f( sldcr r / popen error\n' ): ex i KJ);

}

%hile((fsrts( Line +MXLIFChfpin>)]-TILL)

{ ' ”

i f ( f pu t s( I ine h fpou 1 )==(X1( >

fpr inlf( stderr / fputs error to p ipc .\n*): exit(t):

}

I

I

i ff fe r ror( fp in) >

I fpr int f< s iderr +11 fgc i5 er ror An' ); cxit(1):

}

il ft pc lose( fpou i }=■- 1)

I

fpr in i f( s tde r 1 / pc lose e 1 ror -\n' ):

cy i t((1);

运行结果是:

[r<wtffloea Ihoat Idw]# JI吕-一

I iiidauE i!

L

1

U

D

W

E

I

[rooK^loca IIiqs l Idv]# ||

结果分析:通过程序运行结果可知,先打开文本文件,如果文本打开 失败,则执行exit (1),退出程序,如果文本通过函数开成功,则popen 创建一个可写管道,将命令行 chcase的输入与管道的输入连接起来, 然后向管道输入数据,此时命令行就可以通过管道接受文本文件的数 据了,在从文件中读出数据时,独处的内容放在line[]数组中,fpin表示 从刚打开的文件里读出。之后要编写字母大小写转化函数,来实现小 写字母转化成大写字母。

3?创建有名管道。

编写的程序如下:

玄 inc lude f ch I I

chqr Ktr ing[]=*ths i is ■ exaiTp le to show f ifo connunica t ion'; nra in(iirgctargv'i

ltit arge;

ctu r * □ r gv [ ] j

I

mt rd;

char buf[256];

ini i:

ntnodf' fifo'JJI 0777.0;

i 代 irgc?"2)

i

fd=open( ' i i I o' H JN_Y :

} "

c l?

旧ppEiii i iid HLMJN一丫;

for( i-0; K2G; i++)

1

i f ( arji c==2 >

{

pr iiHft 'Xn I have wok: %s" .sir ing): 碑t i id, s L r mg ,4S) i

str祁罷[0汁=1:

read(fdtbnft45):

pr inif( ' \nThe conlcx t by L have read 1 s: ! * tbu t'):

bu([il]= \fl ;

close< fd);

运行结果是:

[rco t.^'luca Ihus t IA ./ 16-3 .exe

J

have

^rn te :

1

hu ve

'atq le :

1

hjvc

?TG LC :

1

hj V€

Lc :

I

have

le :

1

have

vro le :

I

hi) vc

NTQ l£;

1

huve

MTO IC;

J

ha it

^roic;

I

have

MTU LC :

1

have

wrote:

]

hflve

^role :

1

hive

wo le :

J

have

MTO Le 1

I

have

WU If :

1

haw

^Tfile :

1

have

抵to te :

1

have

uro io :

J

hive

?T0 to :

1

have

¥\to le :

J

have

¥sr<)Le i

1

have

we te :

1

have

UTO IE :

J

have

WO LC :

1

I1J V€

le :

I

have

wo le :

g

cdo tes t by

|Tbe

con lex t by

a exarrp k a exanp le a t?xanp le a c xanp le a e xa np le a e?(anp k a exarrp k a cxanp Lc j c\dtip It a exanp le a eKarrp le a exanp le a exanp le a exanp Le 3 esanp le a esanp le a exarrp le a exarrp le a oxanp k j e xa rrp le a exanp le a exanp le

nplITPInplnpl

to i ] rn

to s how f i To to &hav f i fa to sho^ f i to to show f i f□ to ihov f i to to sho^ I i ft? to shav f i fo to sho^v f ifo to sho^ f1 io to $hcnv t i in to shou f i t n to shov f i fo to $ hoH ii fo Id sho^k f 11 u tn &how filo to s huu f i tn to who电 f i fa to th典 f ifo io sho^ Iito io fihovi ilia to s ho^ fito to s how f i fo to f i fa

to sho^ f i fo to s ho露 f i fo

cnnmiii tea t ion <gmxun ka t ion mitin ica t ion co rmun i ca t i on co inmn ii ca t i on co rmun i ca t i qd cqmnin ka t ion €0miuii ica 1 ion co mnin i ca t i on co mrun i ca t i on cgmmii ica t inn conniin ica t ion <?oiniuii ica t ion comniJi ica t ion ec rmnn i ca t i on comiuii uc31 ion conmin ica t ion <?ommn ka t ion tomiuii ica t ion co nnun i ca 11 on co mmii ii ca t i on cnrunni uca t ion tomrnii ka t ion millii icd t ion cd mnin i ca t i on ro mmn ii ca t i on

tti5 i i 5 □ exa np le t □ sho^ f l fo conrruri ica I ion uhs i i* a exanp k to shi>w fifn conmm ica I ion

The

Centex t

by

i

The

con lex l

hy

]

The

con tex i

by

i

The

con tex I

hy

l

Lhe

cont^x L

hy

i

The

ron tex \

by

I

Tlic

conlrxl

by

l

The

con texl

by

i

The

can l

by

I

Tlie

enn tex i

by

i

Tht

can tex l

hy

l

Lhe

con lex I

hy

i

The

ffln tex l

hy

I

The

conlex L

by

l

The

con tex I

by

i

The

can leM

by

I

The

con tex t

i

Tht

can tex l

hy

l

The

con lex L

hy

i

The

con tex l

hy

I

The

con lex L

by

i

The

con texl

hy

l

The

coniexl

by

J

The

ran tex|

i

Z16-3.exe 1

have

read

IS

have

read

is

ha^c

read

have

read

i s

rend

js

have

read

is

11J Vf

read

is

have

rest!

is

have

rc^d

is

have

re ail

have

read

is

read

have

read

is

hjvu

read

have

read

is

have

rc^d

is

lia^c

read

vs

have

read

is

read

have

read

is

luvr

read

have

read

is

have

rc^d

is

have

read

IS

vhs whs xhs yhs 皿 {hs |hs

?hs

空低必阳斟唄嗜來塞她媳宜堀

i s

exsnp le

|O

show

f i fo

rnrrmin i ra t

ion

is

f xarrp lc

10

shaM

f ifo

roiirrun ica t

ion

I s

a

exanp Le

io

tiho^

f i fo

conmili i ca t

ion

i書

a

exanp le

Io

sha^

f ifo

eonmin i ca t

ion

i §

a

exan^) Je

Ld

shoM

f ifa

cuntiun ica t

ion

is

a

exanple

10

fiho^

f ifo

ronmin i ca t

ion

a

exjirp Le

Ld

shosx

rife

coinrun i ca t

ioji

is

exsnp le

Io

sho%

r if。

rnnmin i ca t

ion

is

f xarrp le

10

ihav.

f ifo

roinrun i ca t

ion

I s

a

exanp Le

Io

whs

f i fo

ronnun i ca t

ion

i石

a

exanp le

Io

sho^

f ifo

eonmin i ca t

ion

is

a

exanp Le

Ld

show

f ifo

cuntiun ica t

ion

is

a

exanple

10

rifo

conmin i ca t

ion

i弐

a

i: X_V.]) . V

LO

shoM

f ifo

cuimun i cli t

ioii

is

exsnp le

IQ

show

fifo

rnmnin i ra t

ion

is

f xjrrp Le

10

&hou

f ifo

C0IIITUI11 CA t

ion

i s

exanp Le

Io

Ehm

f i fo

rnrrmin i ca t

ion

is

0

exanple

Io

sho^

f ifo

corrnun i cj t

ion

I s

a

exanp Jc

Lo

show

f i fu

coirnuii i ca t

ion

is

a

exanple

10

fiho*

f ifo

conrnin i ca t

ion

a

exanp le

Ld

shoM

f ifa

eomiun i ca t

ion

i爭

ex3nple

10

show

fifo f ifo

rnnmin i f3 t

ion

i s

exairp Le

lc

ihov.

coinrun i ca t

ion

is

exanp le

to

show

f i fo

rcrrmin i ca t

i on(J]+ Dbne

[roo t \5 loca Ihos f 11 2625

i Ldwl# ?/lG-3

.exo

前台运行结果:

I roo i'^loca Lho s i Idw]# k/lS~3

J h? ve

wrote : l.h ii>

1 5

rrp le

to

ji hew

fifo

cunmiit i c-3 t ion

1 lid “

;nh i $

1 5

£1

cxj irp It

to

s how

fifu

iZUrUTll]) I f d t 1UI1

1 h』vc

wote :vh is

1 s

a

EXii rrp It

to

Ji tlO'V

fife

cvrimi]) i 旦 t iun

[have

wrote :i $

i s

a

esa rrp lr

to

f ifo

corrmin i ca t inn

\ have

^rc>1 e : xh 和

i s

a

eh rp lr

to

s flow

lifo

cnnmiii i w t ion

[have

uroi e : yh i !;

i s

3

np It

to

i bou

fifo

cormun i ca t ion

后台运行结果:

[rooi^loca Iho? t Id诃# /16-3 .exe I &

[I] 2799

结果分析:此程序是把管道和命令联系起来,read()的系统调用格式 是 read(fd,buf,n),参数定义是 int read(fd,buf,n); int fd; char *buf; unsigned n;它的功能是从fd所指示的文件中读出n个字节的数据, 并将它们送至由指针buf所指示的缓冲区中。如该文件被加锁,等待, 直到锁打开为止;write()系统调用格式是read(fd,buf,n),它的功能是 把n个字节的数据,参数定义同read(),而父进程则从管道中读出来 自两个进程的信息,显示在屏幕上。从buf所指向的缓冲区写到由fd 所指向的文件中。程序中的stri ng[0]+=1表示字符串中的第一个字符 的 ASCII 力口 1

对以上程序的整体分析:

管道是单向的、先进先出的,它把一个进程的输出和另一个进程 的输入连接在一起。一个进程(写进程)在管道的尾部写入数据,另 一个进程(读进程)从管道的头部读出数据,管道包括无名管道和有 名管道两种,前者用于父进程和子进程间的通信, 后者可用于运行于

同一系统中的任意两个进程间的通信。

五、实验总结

通过这次实验使我掌握理解了进程通信的原理,掌握管道通信机 制和linux中无名管道和有名管道创建,以及管道通信的实现;同时 也掌握了 linux进程通信系统调用的功能和系统调用命令的格式和如 何利用系统调用命令进行进程通信编程。