本周操作系统实验课要求写几个关于Linux下进程通信的小程序。
实验要求如下:
第一个程序已在前篇文章中贴出,本文给出后两个。
使用共享存储区通信:
memserve.c:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define Key 3000
int main(void)
{
int x, shmid;
int *shmptr;
if((shmid=shmget(Key, sizeof(int), IPC_CREAT|0666)) < 0)
printf("shmget error"), exit(1);
if((shmptr=(int *)shmat(shmid, 0, 0)) == (int *)-1)
printf("shmat error"), exit(1);
printf("Serve start: \n");
while(1)
{
scanf("%d",shmptr);
int i=*shmptr;
while(i==*shmptr);
printf("Client number: %d\n",*shmptr);