实验二 进程通信
实验二 进程通信
一.试验目的
a.程通信原理和基本技术
b.解linux系统进程间通信机构(IPC);
c.linux关于共享内存的概念;
d.linux支持进程间共享内存的系统调用;
e.进程同步概念。
二验内容与要求
(一)闹钟。用c语言编程,实现进程间通过信号进行通信。
用fork()创建两个进程,子进程在等待5秒后用系统调用kill()向父进程发送SIGALRM信号,父进程用系统调用signal()捕捉SIGALRM信号。
二)共享内存。实现利用共享内存机制的生产者/消费者问题的解决方案。生产者将数据写入共享内存。消费者从共享内存取出数据,在屏幕输出。参考如下程序:
执行内容说明:
1、首先调用shmget()函数建立一块共享内存,大小为1024个字节,该函数返回创建
的共享内存标识符。
2、然后调用fork()产生一个子进程(生产者进程)。子进程调用shmat()函数将该共享内存链接(attach)到自己的虚存空间,即可通过普通的内存写操作(例如strcpy等),在该共享内存写入数据。
3、写完数据后,子进程调用shmdt()函数断开与该共享内存的连接。
4、父进程sleep,直到子进程完成上述操作。父进程(消费者)调用shmctl()函数得到关于这块共享内存的相关消息,并打印出来。
5、父进程调用shmat()函数将这块共享内存连接到自己的虚存空间,即可以通过普通的内存读操作(例如printf等),将该共享内存中的字符串读出来。
6、读完数据后,父进程调用shmdt()函数断开与该内存的连接。
7、最后,父进程调用shmctl()函数,销毁该共享内存。
三验步骤
(一)闹钟
1.打开终端,创建clock.文件。如图1,图2
图1
图2
2.编译程序,如图3
图3
3.运行程序,如图4
图4
(二)共享内存
1.打开终端,创建sharestorage.c文件如图5,图6
图5
图6
2.编译程序,如图7
图7
3.运行程序,如图8
图8
四.程序清单:注意加注释(包含关键字、方法、变量等),在每个模块前加注释
- 闹钟。
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
static int alarm_fired = 0; //闹钟未设置
//模拟闹钟
void ding(int sig)
{
alarm_fired = 1; //设置闹钟
}
int main()
{
int pid;
printf("alarm application starting\n");
if((pid = fork( )) == 0)
{ //子进程5秒后发送信号SIGALRM给父进程
sleep(5);
kill(getppid(), SIGALRM);
//exit(0);
return 0;
}
//父进程安排好捕捉到SIGALRM信号后执行ding函数
printf("waiting for alarm to go off\n");
(void) signal(SIGALRM, ding);
pause(); //挂起父进程,直到有一个信号出现
if (alarm_fired)
printf("Ding!\n");
printf("done\n");
//exit(0);
return 0;
}
- 共享内存。
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<errno.h>
#include <stdio.h>
#include <string.h>
#define KEY 1234 /*键*/
#define SIZE 1024 /*欲建立的共享内存的大小*/
int main()
{
int shmid;
char *shmaddr;
struct shmid_ds buf;
shmid=shmget(KEY,SIZE,IPC_CREAT|0600); /*建立共享内存*/
if(shmid==-1)
{
printf("create share memory failed:%s",strerror(errno));
return 0;
}
if(fork( )==0)
{ /*子进程*/
sleep(2);
shmaddr=(char*)shmat(shmid,NULL,0); /*系统自动选择一个地址连接*/
if(shmaddr==(void*)-1)
{
printf("connect to the share memory failed:%s",strerror(errno));
return 0;
}
/*向共享内存内写数据*/
strcpy(shmaddr,"hello,this is shared data.\n");
shmdt(shmaddr); /*断开共享内存*/
//exit(0);
return 0;
}else
{ /*父进程*/
wait(0);
shmctl(shmid,IPC_STAT,&buf); /*取得共享内存的相关信息*/
printf("size of the share memory: shm_segsz=%dbytes\n",buf.shm_segsz);
printf("process id of the creator:shm_cpid=%d\n",buf.shm_cpid);
printf("process id of the last operator:shm_lpid=%d\n",buf.shm_lpid);
shmaddr=(char*)shmat(shmid,NULL,0); /*系统自动选择一个地址连接*/
if(shmaddr==(void*)-1)
{
printf("connect the share memory failed:%s",strerror(errno));
return 0;
}
printf("print the content of the share memory:");
printf("%s\n",shmaddr);
shmdt(shmaddr); /*断开共享内存*/
/*当不再有任何其它进程使用该共享内存时系统将自动销毁它*/
shmctl(shmid,IPC_RMID,NULL);
}
}
五.测试结果(运行结果、结果分析)
1.闹钟程序实验结果如下
闹钟程序实验结果
2.共享内存程序实验结果如下
共享内存程序实验结果
六.总结(实验效果分析、心得体会,遗留问题)
通过这次实验我理解进程通信原理和基本技术,了解linux系统进程间通信机构(IPC),知道了linux关于共享内存的概念,了解了linux支持进程间共享内存的系统调用,巩固进程同步概念。同时我也明白了实际操作之前把理论知识学好在实验中才能得心应手,虽然会遇到各种问题但还是能通过知识解决问题。今后还是认真听讲该科目的内容。