jelasin 的學(xué)生作業(yè):
#ifndef __USE_GNU
#define __USE_GNU
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// system v ipc headers
#include
#include
#include
#include
// posix ipc headers
#include
#include
#include
int main(int argc, char const *argv[])
{
// system v ipc shared memory
key_t key = ftok("/tmp", 'A');
int shm_id_sysv = shmget(key, 1024, IPC_CREAT | 0666);
if (shm_id_sysv == -1) {
perror("shmget");
exit(1);
}
void *shm_ptr_sysv = shmat(shm_id_sysv, NULL, 0);
if (shm_ptr_sysv == NULL)
{
perror("shmat");
exit(1);
}
fprintf(stderr, "Shared memory: %d\n", shm_id_sysv);
FILE *fp = popen("ipcs -m", "r");
if (fp == NULL)
{
perror("popen");
exit(1);
}
char line[1024];
void *ptr_shm = shm_ptr_sysv;
ssize_t nbytes = 0;
while ((nbytes = fread(line, 1, 1024, fp)) > 0)
{
strncpy(ptr_shm, line, nbytes);
ptr_shm = (int8_t*)ptr_shm + nbytes;
}
pclose(fp);
strcpy(ptr_shm, "Hello, system v ipc shared memory!\n");
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(1);
}
else if (pid == 0)
{
// child process
char buf[1024];
key_t key = ftok("/tmp", 'A');
int shm_id = shmget(key, 1024, 0);
void *shm_ptr = shmat(shm_id, NULL, 0);
strcpy(buf, shm_ptr);
fprintf(stderr, "Child process: %d\n", getpid());
fprintf(stderr, "Read from Shared memory: %s\n", buf);
exit(EXIT_SUCCESS);
}
else
{
// parent process
int status;
waitpid(pid, &status, 0);
}
if (shmctl(shm_id_sysv, IPC_RMID, NULL) == -1)
{
perror("shmctl");
return -1;
}
// --------------------------------------------------------------------------------------
// posix ipc shared memory
int shm_fd_posix = shm_open("/posix_shm", O_CREAT | O_RDWR, 0666);
if (shm_fd_posix == -1) {
perror("shm_open");
exit(1);
}
if (ftruncate(shm_fd_posix, 1024) == -1) {
perror("ftruncate");
exit(1);
}
void *shm_ptr_posix = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_posix, 0);
if (shm_ptr_posix == MAP_FAILED)
{
perror("mmap");
exit(1);
}
fprintf(stderr, "Shared memory: %d\n", shm_fd_posix);
fp = popen("ipcs -m", "r");
if (fp == NULL)
{
perror("popen");
exit(1);
}
ptr_shm = shm_ptr_posix;
while ((nbytes = fread(line, 1, 1024, fp)) > 0)
{
strncpy(ptr_shm, line, nbytes);
ptr_shm = (int8_t*)ptr_shm + nbytes;
}
pclose(fp);
strcpy(ptr_shm, "Hello, posix ipc shared memory!\n");
pid = fork();
if (pid == -1)
{
perror("fork");
exit(1);
}
else if (pid == 0)
{
// child process
int shm_fd = shm_open("/posix_shm", O_RDONLY, 0666);
void *shm_ptr = mmap(NULL, 1024, PROT_READ, MAP_SHARED, shm_fd, 0);
char buf[1024];
strcpy(buf, shm_ptr);
fprintf(stderr, "Child process: %d\n", getpid());
fprintf(stderr, "Read from Shared memory: %s\n", buf);
exit(EXIT_SUCCESS);
}
else
{
// parent process
int status;
waitpid(pid, &status, 0);
}
if (munmap(shm_ptr_posix, 1024) == -1) {
perror("munmap");
exit(1);
}
if (close(shm_fd_posix) == -1) {
perror("close");
exit(1);
}
if (shm_unlink("/posix_shm") == -1) {
perror("shm_unlink");
exit(1);
}
return 0;
}
? 5 ./main
Shared memory: 15
Child process: 18101
Read from Shared memory:
------------ 共享內(nèi)存段 --------------
鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài)
0x00000000 3 jelasin 606 10483788 2 目標(biāo)
0x00000000 4 jelasin 606 10483788 2 目標(biāo)
0x41030001 15 jelasin 666 1024 1
Hello, system v ipc shared memory!
Shared memory: 3
Child process: 18104
Read from Shared memory:
------------ 共享內(nèi)存段 --------------
鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài)
0x00000000 3 jelasin 606 10483788 2 目標(biāo)
0x00000000 4 jelasin 606 10483788 2 目標(biāo)
0x00000000 15 jelasin 666 1024 1 目標(biāo)
Hello, posix ipc shared memory!