进程间通信练习

只是实用操作系统小课留下的作业。
一、内容:
编写两个程序mysem.c(编译后为mysem)和myinput.c(编译后为myinput),实现两个程序(进程)之间的通信。要求如下:
1、mysem创建一个共享内存myshm(共享内存ID,内存大小为512字节),供mysem和myinput共享
2、mysem创建一个子进程,该子进程加载myinput
3、myinput从键盘以追加方式向myshm中输入信息(字符型),mysem接收信息并显示在屏幕上
4、当myinput从键盘输入“end”并存入myshm后,myinput结束(退出),mysem收到“end”显示后,也退出,并释放所有的资源。
二、解释:
1、参考课本例14.4 14.5以及第7次小课实验
2、目的:深入了解“共享内存”、子进程加载、信号量、进程间的同步与互斥等操作系统核心内容,以及它们的使用
三、说明:
1、本次作业满分为10分,将直接计入成绩,要求各位同学认真按照要求完成。
2、如发现抄袭者(代码、说明文字有60%雷同者),双方或者多方均按0分计算
3、交作业要求:
(1)打印作业题目和要求
(2)要求分析题意、对代码做相关说明文档(手写)、给出实验结果图
(3)使用学校统一封面,将原代码打印并装订后,连同手写文档统一交给老师
(4)将原代码连同编译后的目标文件,统一发送到彭老师FTP
(5)本作业为课外作业,在4月30日之前交。
myinput.c代码:
#include<stdio.h>
#include<linux/shm.h>
#include<string.h>
#include<stdlib.h>
#include<linux/sem.h>
/* Definition shared memory */
/* Definition shared memory */
#define MAXSIZ 512
/* Definition Identifier of Semaphore */
int emptyid;
int fullid;
/* Main function */
main()
{
/* Definition Semaphore data structure definitions */
struct sembuf P,V;
union semun arg;
/* Definition of shared memory ID */
int myshm;
char *viraddr;
char buffer[BUFSIZ];
/* Create and initialize shared memory */
myshm=shmget(123,MAXSIZ,0666|IPC_CREAT);
viraddr=(char*)shmat(myshm,0,0);
/* Create Semaphore */
emptyid=semget(1234,1,IPC_CREAT|0666);
fullid=semget(1235,1,IPC_CREAT|0666);
/* Initializing Semaphores */
arg.val=1;                            //Mutex initially 1, allowing a process to enter
if(semctl(emptyid,0,SETVAL,arg)==-1)
perror("semctl setval error");
arg.val=0;                            //no data buffer
if(semctl(fullid,0,SETVAL,arg)==-1)
perror("semctl setval error");
/* Initialize P, V operations */
P.sem_num=0;
P.sem_op=-1;
P.sem_flg=SEM_UNDO;
V.sem_num=0;
V.sem_op=1;
V.sem_flg=SEM_UNDO;
/* Cycle Input Message */
while(1)
{
semop(emptyid,&P,1);                //Perform P operations on the emptyid
puts("input some message:");
fgets(buffer,BUFSIZ,stdin);         //Enter Message
strcat(viraddr,buffer);             //Additional way to write shared memory
semop(fullid,&V,1);                 //Perform V operations on the fullid
if(strncmp(buffer,"end",3)==0)
break;
}
exit(0);
}
mysem.c代码:
#include<stdio.h>
#include<linux/shm.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<linux/sem.h>
/* Definition shared memory */
/* Definition shared memory */
#define MAXSIZ 512
/* Definition Identifier of Semaphore */
int emptyid;
int fullid;
/* Main function */
main()
{
/* Definition Semaphore data structure definitions */
struct sembuf P,V;
union semun arg;
/* Definition of shared memory ID */
int myshm;
char *viraddr;
/* Create and initialize shared memory */
myshm=shmget(123,MAXSIZ,0666|IPC_CREAT);
viraddr=(char*)shmat(myshm,0,0);
/* Create Semaphore */
emptyid=semget(1234,1,IPC_CREAT|0666);
fullid=semget(1235,1,IPC_CREAT|0666);
/* Initializing Semaphores */
arg.val=1;printf("message is end");       //Mutex initially 1, allowing a process to enter
if(semctl(emptyid,0,SETVAL,arg)==-1)
perror("semctl setval error");
arg.val=0;                                              //no data buffer
if(semctl(fullid,0,SETVAL,arg)==-1)
perror("semctl setval error");
/* Initialize P, V operation */
P.sem_num=0;
P.sem_op=-1;
P.sem_flg=SEM_UNDO;
V.sem_num=0;
V.sem_op=1;
V.sem_flg=SEM_UNDO;
int p;
while((p=fork())==-1);                          //Create child process
if(p==0)
{
execl("./myinput",0,NULL);                       //Loading myinput
}
else
{
while(1)
{
semop(fullid,&P,1);                             //Perform P operations on the fullid
printf("get message from myinput:\n%s",viraddr);//Printout
semop(emptyid,&V,1);                       //Perform V operations on the emptyid
if(strstr(viraddr,"end"))                      //If the message output end ,end loop
break;
}
wait(0);                             //Waiting for child process killed
shmdt(viraddr);                      //Disconnect and remove the shared memory
shmctl(myshm,IPC_RMID,0);
semctl(emptyid,IPC_RMID,0);          //Remove the semaphore set
semctl(fullid,IPC_RMID,0);
exit(0);
}
}

Tags: ,

Leave a comment