Skip to content

Instantly share code, notes, and snippets.

@hustlijian
Last active August 23, 2022 05:10
Show Gist options
  • Save hustlijian/7132944 to your computer and use it in GitHub Desktop.
Save hustlijian/7132944 to your computer and use it in GitHub Desktop.
mmap ----------- a short demo for mmap in linux! 一个使用mmap实现进程间通信的例子。

source

http://www.perfgeeks.com/?p=723

useage

  1. compile the master.c slave.c(gcc,or make)
  2. echo "0" > /tmp/shm_command
  3. ./master in one terminal
  4. ./slave in other terminal
all:
gcc -o master ./master.c
gcc -o slave ./slave.c
clean:
rm -f master slave
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void listen();
int main(int argc, char *argv[])
{
listen();
return 0;
}
void listen()
{
int fd;
char *buf;
char *fname = "/tmp/shm_command";
char command;
time_t now;
fd = open(fname, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
if (fd == -1)
{
perror("open");
exit(1);
}
buf = mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED)
{
perror("mmap");
exit(1);
}
if (close(fd) == -1)
{
perror("close");
exit(1);
}
*buf = '0';
sleep(2);
for (;;)
{
if (*buf == '1' || *buf == '3' || *buf == '5' || *buf == '7')
{
if (*buf > '1')
printf("%ld\tgood job [%c]\n", (long)time(&now), *buf);
(*buf)++;
}
if (*buf == '9')
{
break;
}
sleep(1);
}
if (munmap(buf, 4096) == -1)
{
perror("munmap");
exit(1);
}
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void ready(unsigned int t);
void job_hello();
void job_smile();
void job_bye();
char get_command(char *buf);
void wait();
int main(int argc, char *argv[])
{
wait();
return 0;
}
void ready(unsigned int t)
{
sleep(t);
}
/* command 2 */
void job_hello()
{
time_t now;
printf("%ld\thello world\n", (long)time(&now));
}
/* command 4 */
void job_simle()
{
time_t now;
printf("%ld\t^_^\n", (long)time(&now));
}
/* command 6 */
void job_bye()
{
time_t now;
printf("%ld\t|<--\n", (long)time(&now));
}
char get_command(char *buf)
{
char *p;
if (buf != NULL)
{
p = buf;
}
else
{
return '0';
}
return *p;
}
void wait()
{
int fd;
char *buf;
char *fname = "/tmp/shm_command";
char command;
time_t now;
fd = open(fname, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
if (fd == -1)
{
perror("open");
exit(1);
}
buf = mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED)
{
perror("mmap");
exit(1);
}
if (close(fd) == -1)
{
perror("close");
exit(1);
}
for (;;)
{
command = get_command(buf);
/*printf("%c\n", command);*/
switch(command)
{
case '0':
printf("%ld\tslave is ready...\n", (long)time(&now));
ready(3);
*buf = '1';
break;
case '2':
job_hello();
*buf = '3';
break;
case '4':
job_simle();
*buf = '5';
break;
case '6':
job_bye();
*buf = '7';
break;
default:
break;
}
if (*buf == '8')
{
*buf = '9';
if (munmap(buf, 4096) == -1)
{
perror("munmap");
exit(1);
}
return;
}
sleep(1);
}
if (munmap(buf, 4096) == -1)
{
perror("munmap");
exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment