110360余明华.docx

上传人:b****0 文档编号:17641384 上传时间:2023-07-27 格式:DOCX 页数:32 大小:199.07KB
下载 相关 举报
110360余明华.docx_第1页
第1页 / 共32页
110360余明华.docx_第2页
第2页 / 共32页
110360余明华.docx_第3页
第3页 / 共32页
110360余明华.docx_第4页
第4页 / 共32页
110360余明华.docx_第5页
第5页 / 共32页
110360余明华.docx_第6页
第6页 / 共32页
110360余明华.docx_第7页
第7页 / 共32页
110360余明华.docx_第8页
第8页 / 共32页
110360余明华.docx_第9页
第9页 / 共32页
110360余明华.docx_第10页
第10页 / 共32页
110360余明华.docx_第11页
第11页 / 共32页
110360余明华.docx_第12页
第12页 / 共32页
110360余明华.docx_第13页
第13页 / 共32页
110360余明华.docx_第14页
第14页 / 共32页
110360余明华.docx_第15页
第15页 / 共32页
110360余明华.docx_第16页
第16页 / 共32页
110360余明华.docx_第17页
第17页 / 共32页
110360余明华.docx_第18页
第18页 / 共32页
110360余明华.docx_第19页
第19页 / 共32页
110360余明华.docx_第20页
第20页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

110360余明华.docx

《110360余明华.docx》由会员分享,可在线阅读,更多相关《110360余明华.docx(32页珍藏版)》请在冰点文库上搜索。

110360余明华.docx

110360余明华

实验八Linux进程间通信

1.实验目的 

(1)学习和掌握Linux信号概念,并能使用系统调用完成电子表计时程序。

(2)学习和掌握Linux进程间通信的IPC方法。

(3)学习和掌握利用信号量实现进程间的同步通信。

2.实验内容

●Linux信号

输入程序8-1,8-2编译并运行写出执行结果。

8-1结果

8-2结果

对比执行结果,你得到的启示是:

区别在于,8_2中ouch函数中又进行安装信号函数。

即当ouch函数捕获外部中断的SGIINT信号后,改变安装信号函数(接受中断信号,进程结束)

●利用Linux信号SIGALAM,设计并实现电子表程序。

#include

#include

intsecond=0,minutes=0,hour=0;

voidtime(){

alarm

(1);

second++;

if(second>=60){

second=0;

minutes++;

}

if(minutes>=60){

hour++;

minutes=0;

}

printf("time:

%d:

%d:

%d\r",hour,minutes,second);

fflush(stdout);

}

intmain(){

signal(SIGALRM,time);//产生SIGALRM信号便调用time函数

raise(SIGALRM);

while

(1){

}

return0;

}

●IPC方法—管道

双向管道实现聊天室功能,原理图如下!

●IPC方法—命名管道(FIFO)

输入程序8-4,8-5编译并运行写出执行结果。

●IPC方法—信号量

输入程序8-6semun.h和8-7sem1.c,编译执行并写出结果。

●IPC方法—内存映射

输入程序8-8和8-9,编译执行并写出结果。

_fd=open(filenm,O_RDWR|O_CREAT);

mmap_addr=(char*)mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);

_________________________________________________________________________________mmap函数中,0表示有系统分配映射内存的地址;__4096,表示映射内存大小为4096B,

●PROT_READ|PROT_WRITE表示允许程序对内存读写操作,MAP_SHARED表示对内存的操作对其他程序来说是可见的。

  

●IPC方法—共享内存

输入程序8-10和8-11,编译执行并写出结果。

简述程序8-10和8-11,是否实现了共享内存互斥访问,如何实现?

●IPC方法—消息队列

输入程序8-12和8-13,编

译执行并写出结果。

3.主要仪器设备及软件

(1)硬件:

计算机、网络

(2)软件:

VMWareworkstation、RedHat9.0

4.附录:

程序清单

(1)8-1

#include

#include

#include

voidouch(intsig)

{

printf("OUCH!

-Igotsignal%d\n",sig);

}

intmain()

{

structsigactionact;

act.sa_handler=ouch;

sigemptyset(&act.sa_mask);

act.sa_flags=0;

sigaction(SIGINT,&act,0);

while

(1){

printf("HelloWorld!

\n");

sleep

(1);

}

}

(2)8-2

#include

#include

#include

voidouch(intsig)

{

printf("OUCH!

-Igotsignal%d\n",sig);

(void)signal(SIGINT,SIG_DFL);

}

intmain()

{

(void)signal(SIGINT,ouch);

while

(1){

printf("HelloWorld!

\n");

sleep

(1);

}

}

(3)8-3—lucyToPeter.c

#include

#include

#include

intmain(void)

{

intpipe1_fd[2],pipe2_fd[2];

charparent_buf[256],child_buf[256];

intlen;

intchild_status;

if(pipe(pipe1_fd)<0)

{

printf("pipe1createerror\n");

return-1;

}

if(pipe(pipe2_fd)<0)

{

printf("pipe2createerror\n");

return-1;

}

if(fork()==0)//child

{

printf("\n");

//pipe1_fd[0]isusedtoread,pipe2_fd[1]isusedtowrite

close(pipe1_fd[1]);

close(pipe2_fd[0]);

while

(1)

{

len=read(pipe1_fd[0],child_buf,255);

child_buf[len]='\0';

printf("peter_Lucy:

%s\n",child_buf);

printf("Peter:

");

fgets(child_buf,256,stdin);

child_buf[strlen(child_buf)-1]='\0';

if(strncmp(child_buf,"quit",4)==0){

close(pipe1_fd[0]);

close(pipe2_fd[1]);

exit(0);

}

write(pipe2_fd[1],child_buf,strlen(child_buf));

sleep

(1);

}

close(pipe1_fd[0]);

close(pipe2_fd[1]);

exit();

}

else{//parent

//pipe1_fd[1]isusedtowrite,pipe2_fd[0]isusedtoread

close(pipe1_fd[0]);

close(pipe2_fd[1]);

while

(1){

printf("Lucy:

");

fgets(parent_buf,256,stdin);

if(strncmp(parent_buf,"quit",4)==0){

close(pipe1_fd[1]);

close(pipe2_fd[0]);

exit(0);

}

write(pipe1_fd[1],parent_buf,strlen(parent_buf));

sleep

(1);

len=read(pipe2_fd[0],parent_buf,255);

parent_buf[len]='\0';

printf("Lucy_peter:

%s\n",parent_buf);

}

close(pipe1_fd[1]);

close(pipe2_fd[0]);

wait(&child_status);

exit();

}

}

(4)8-4—lucy.c

#include

#include

#include

#include

#include

#include

#include

intmain(void)

{

charwrite_fifo_name[]="write-fifo";

charread_fifo_name[]="read-fifo";

intwrite_fd,read_fd;

charbuf[256];

intlen;

structstatstat_buf;

intret=mkfifo(write_fifo_name,S_IRUSR|S_IWUSR);

if(ret==-1){

printf("FailtocreateFIFO%s:

%s",write_fifo_name,strerror(errno));

exit(-1);

}

write_fd=open(write_fifo_name,O_WRONLY);

if(write_fd==-1){

printf("FailtoopenFIFO%s:

%s",write_fifo_name,strerror(errno));

exit(-1);

}

while((read_fd=open(read_fifo_name,O_RDONLY))==-1){

sleep

(1);

}

while

(1){

printf("Lucy:

");

fgets(buf,256,stdin);

buf[strlen(buf)-1]='\0';

if(strncmp(buf,"quit",4)==0){

close(write_fd);

unlink(write_fifo_name);

close(read_fd);

exit(0);

}

write(write_fd,buf,strlen(buf));

len=read(read_fd,buf,256);

if(len>0){

buf[len]='\0';

printf("Peter:

%s\n",buf);

}

}

}

(5)8-5—peter.c

#include

#include

#include

#include

#include

#include

intmain(void)

{

charwrite_fifo_name[]="read-fifo";

charread_fifo_name[]="write-fifo";

intwrite_fd,read_fd;

charbuf[256];

intlen;

intret=mkfifo(write_fifo_name,S_IRUSR|S_IWUSR);

if(ret==-1){

printf("FailtocreateFIFO%s:

%s",write_fifo_name,strerror(errno));

exit(-1);

}

while((read_fd=open(read_fifo_name,O_RDONLY))==-1){

sleep

(1);

}

write_fd=open(write_fifo_name,O_WRONLY);

if(write_fd==-1){

printf("FailtoopenFIFO%s:

%s",write_fifo_name,strerror(errno));

exit(-1);

}

while

(1){

len=read(read_fd,buf,256);

if(len>0){

buf[len]='\0';

printf("Lucy:

%s\n",buf);

}

printf("Peter:

");

fgets(buf,256,stdin);

buf[strlen(buf)-1]='\0';

if(strncmp(buf,"quit",4)==0){

close(write_fd);

unlink(write_fifo_name);

close(read_fd);

exit(0);

}

write(write_fd,buf,strlen(buf));

}

}

(6)8-6—semun.h

#ifdefined(__GNU_LIBRARY__)&&!

defined(_SEM_SEMUN_UNDEFINED)

/*unionsemunisdefinedbyincluding*/

#else

/*accordingtoX/OPENwehavetodefineitourselves*/

unionsemun{

intval;/*valueforSETVAL*/

structsemid_ds*buf;/*bufferforIPC_STAT,IPC_SET*/

unsignedshortint*array;/*arrayforGETALL,SETALL*/

structseminfo*__buf;/*bufferforIPC_INFO*/

};

#endif

(7)8-7—sem1.c

#include

#include

#include

#include

#include

#include

#include"semun.h"

staticintset_semvalue(void);

staticvoiddel_semvalue(void);

staticintsemaphore_p(void);

staticintsemaphore_v(void);

staticintsem_id;

 

intmain(intargc,char*argv[])

{

inti;

intpause_time;

charop_char='O';

srand((unsignedint)getpid());

sem_id=semget((key_t)1234,1,0666|IPC_CREAT);

printf("semid=%d\n",sem_id);

if(argc>1){

if(!

set_semvalue()){

fprintf(stderr,"Failedtoinitializesemaphore\n");

exit(EXIT_FAILURE);

}

op_char='X';

sleep(5);

}

/*Thenwehavealoopwhichentersandleavesthecriticalsectiontentimes.

There,wefirstmakeacalltosemaphore_pwhichsetsthesemaphoretowait,as

thisprogramisabouttoenterthecriticalsection.*/

for(i=0;i<10;i++){

if(!

semaphore_p())exit(EXIT_FAILURE);

printf("%c",op_char);fflush(stdout);

pause_time=rand()%3;

sleep(pause_time);

printf("%c",op_char);fflush(stdout);

/*Afterthecriticalsection,wecallsemaphore_v,settingthesemaphoreavailable,beforegoingthroughtheforloopagainafterarandomwait.Aftertheloop,thecalltodel_semvalueismadetocleanupthecode.*/

if(!

semaphore_v())exit(EXIT_FAILURE);

pause_time=rand()%2;

sleep(pause_time);

}

printf("\n%d-finished\n",getpid());

if(argc>1){

sleep(10);

del_semvalue();

}

exit(EXIT_SUCCESS);

}

/*Thefunctionset_semvalueinitializesthesemaphoreusingtheSETVALcommandinasemctlcall.Weneedtodothisbeforewecanusethesemaphore.*/

staticintset_semvalue(void)

{

unionsemunsem_union;

sem_union.val=1;

if(semctl(sem_id,0,SETVAL,sem_union)==-1)return(0);

return

(1);

}

/*Thedel_semvaluefunctionhasalmostthesameform,exceptthecalltosemctlusesthecommandIPC_RMIDtoremovethesemaphore'sID.*/

staticvoiddel_semvalue(void)

{

unionsemunsem_union;

if(semctl(sem_id,0,IPC_RMID,sem_union)==-1)

fprintf(stderr,"Failedtodeletesemaphore\n");

}

/*semaphore_pchangesthesemaphoreby-1(waiting).*/

staticintsemaphore_p(void)

{

structsembufsem_b;

sem_b.sem_num=0;

sem_b.sem_op=-1;/*P()*/

sem_b.sem_flg=SEM_UNDO;

if(semop(sem_id,&sem_b,1)==-1){

fprintf(stderr,"semaphore_pfailed\n");

return(0);

}

return

(1);

}

/*semaphore_vissimilarexceptforsettingthesem_oppartofthesembufstructureto1,sothatthesemaphorebecomesavailable.*/

staticintsemaphore_v(void)

{

structsembufsem_b;

sem_b.sem_num=0;

sem_b.sem_op=1;/*V()*/

sem_b.sem_flg=SEM_UNDO;

if(semop(sem_id,&sem_b,1)==-1){

fprintf(stderr,"semaphore_vfailed\n");

return(0);

}

return

(1);

}

(8)8-8—lucy.c

#include

#include

#include

#include

#include

#include

#include

#include

#ifdefined_GNU_LIBRARY_)&&!

defined(_SEM_SEMUN_UNDEFINED)

/*unionsemunisdefinedbyincluding*/

#else

/*accordingtoX/OPENwehavetodefineitourselves*/

unionsemun{

intval;/*valueforSETVAL*/

structsemid_ds*buf;/*bufferforIPC_STAT,IPC_SET*/

unsignedshort*array;/*arrayforGETALL,SETALL*/

/*Linuxspecificpart:

*/

structseminfo*_buf;/*bufferforIPC_INFO*/

};

#endif

#definePROJID0xFF

intsemid;

voidterminate_handler(intsigno)

{

semctl(semid,0,IPC_RMID);

exit(0);

}

intmain(void)

{

charfilenm[]="shared-file";

charzero_blk[4096];

char*mmap_addr;

intfd;

key_tsemkey;

structsembufgetsem,setsem;

unionsemunseminit;

intret;

semkey=ftok(

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2