共享内存
编译指令:
gcc shared_memory.cpp -I/usr/local/include/ -L/usr/local/lib -lrt -o shared_mem
代码:
/**
* Sample code for sharing memory between processes
* Two processes will iteratively increase a counter which values stored in a shared memory
*
*/
#include <stdio.h>
#include <unistd.h> // for fork()
#include <sys/mman.h> // for shared memory created
#include <sys/stat.h> // for mode constants
#include <fcntl.h> // for O_* constant
#define SHARED_OBJ_NAME "/somename"
// shared data struct
struct message
{
int pid;
int counter;
};
bool write_message(int pid, int value)
{
int shmFd = shm_open(SHARED_OBJ_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(shmFd, sizeof(message));
message *msg_ptr = (message*)mmap(NULL, sizeof(message), PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0);
printf("Process %d: Increase the counter.\n", pid);
msg_ptr->pid = pid;
msg_ptr->counter = value;
munmap(msg_ptr, sizeof(message));
// remember to close to not hit an error of
// opening too many files
close(shmFd);
return true;
}
bool read_message(int curr_pid, int &curr_value)
{
int shmFd = shm_open(SHARED_OBJ_NAME, O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(shmFd, sizeof(message));
message *msg_ptr = (message*)mmap(NULL, sizeof(message), PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0);
if (msg_ptr->pid == curr_pid)
{
printf("Process %d: No new msg available.\n", curr_pid);
return false;
}
else
{
printf("Process %d: Receive %d from PID %d.\n", curr_pid, msg_ptr->counter, msg_ptr->pid);
curr_value = msg_ptr->counter;
munmap(msg_ptr, sizeof(message));
}
close(shmFd);
return true;
}
int main(int argc, char **argv)
{
printf("Init the initial value.\n");
write_message(-1, 0);
// create a child process by calling folk,
// it returns a non-zero pid for parent process and 0 for child process created
pid_t pid = fork();
//--- PARENT PROCESS
if (pid != 0)
{
for (int i = 0; i < 5; i++)
{
int value;
// only write message if reading sucessfully
if (read_message(pid, value)) write_message(pid, ++value);
sleep(0.1);
}
}
//--- CHILD PROCESS
else
{
for (int j = 0; j < 5; j++)
{
int value;
if (read_message(pid, value)) write_message(pid, ++value);
sleep(0.1);
}
}
printf("=========== End of process %d\n", pid);
//shm_unlink(SHARED_OBJ_NAME);
return 0;
}
运行结果:
Init the initial value.
Process -1: Increase the counter.
Process 440735: Receive 0 from PID -1.
Process 440735: Increase the counter.
Process 440735: Receive 2 from PID 0.
Process 440735: Increase the counter.
Process 440735: No new msg available.
Process 440735: No new msg available.
Process 440735: Receive 4 from PID 0.
Process 440735: Increase the counter.
=========== End of process 440735
Init the initial value.
Process -1: Increase the counter.
Process 0: Receive 1 from PID 440735.
Process 0: Increase the counter.
Process 0: Receive 3 from PID 440735.
Process 0: Increase the counter.
Process 0: Receive 5 from PID 440735.
Process 0: Increase the counter.
Process 0: No new msg available.
Process 0: No new msg available.
=========== End of process 0
FIFO
编译指令:
gcc fifo.cpp -I/usr/local/include/ -L/usr/local/lib -lrt -o fifo
/**
* Example for using named pipe for communicating between processes
* This demo is for a unidirectional named pipe which transfer data in one direction
*/
#include "csapp.h"
#define NAMED_PIPE "/var/lock/pipename"
// shared data struct
struct message
{
int pid;
int counter;
};
int main(int argc, char **argv)
{
// create the named pipe (fifo) with permission
int ret = mkfifo(NAMED_PIPE, 0666);
if (ret < 0)
printf("Error when creating FIFO. %s\n", strerror(errno));
// create a child process by calling folk,
// it returns a non-zero pid for parent process and 0 for child process created
pid_t pid = fork();
//--- the parent process will write to the pipe only
if (pid != 0)
{
int fd = open(NAMED_PIPE, O_WRONLY);
for (int i = 0; i < 5; i++)
{
message msg;
msg.pid = pid;
msg.counter = i;
printf("Process %d: Write %d.\n", pid, i);
ret = write(fd, &msg, sizeof(msg));
if (ret < 0)
printf("Process %d: Error while writing message. %s\n", pid, strerror(errno));
sleep(0.1);
}
close(fd);
}
//-- child process will read only
else
{
int fd = open(NAMED_PIPE, O_RDONLY);
for (int i = 0; i < 5; i++)
{
message msg;
ret = read(fd, &msg, sizeof(msg));
if (ret < 0)
printf("Process %d: Error while reading message. %s\n", pid, strerror(errno));
printf("Process %d: Received value %d from the parent process %d.\n", pid, msg.counter, msg.pid);
sleep(0.1);
}
close(fd);
}
unlink(NAMED_PIPE);
return 0;
}
运行结果:
Process 424351: Write 0.
Process 424351: Write 1.
Process 424351: Write 2.
Process 424351: Write 3.
Process 424351: Write 4.
Process 0: Received value 0 from the parent process 424351.
Process 0: Received value 1 from the parent process 424351.
Process 0: Received value 2 from the parent process 424351.
Process 0: Received value 3 from the parent process 424351.
Process 0: Received value 4 from the parent process 424351.
原文地址:http://www.cnblogs.com/coldarra/p/16852479.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性