【发布时间】:2017-03-22 04:17:17
【问题描述】:
我有以下代码,我想通过共享内存段在两个进程之间进行通信。我的问题是我在附加内存段时出错,我不知道为什么。
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "process.h"
int main(int argc, char** argv)
{
/*- Check the command-line arguments -*/
if(argc != 2)
{
printf("\n--- Wrong input at command-line arguments ---\n\n");
printf("--- The program terminate --- \n");
return 1;
}
int N = atoi(argv[1]); // The amount of total threads to be created
int status; // for the wait() function
char* data; // a string to use for shared memory segment
/*- Variables for the shared memory segment -*/
key_t key = 1003; // for the shmget() key argument
int shmid; // to store the returned value from shmget()
int size = 1024; // for the shmget() size argument
/* ************************************************************************************/
pid_t pid = fork(); // create second process
if(pid < 0) // if something going wrong
{
printf("\n\n---- Error in function fork!----\n");
exit(1);
}
else if(pid == 0) // the child process (P)
{
// create the shared memory segment for the P process
shmid = CreateShmSegment(key, size, IPC_CREAT);
if(shmid == -1) // check if creating shared memory return an error
{
printf("\n---Error at creating the memory segment! ---\n");
exit(1);
}
// attach the shared memory segment
data = AttachShmSegment(shmid);
if(data == (char*)-1) // check if attached the shared memory return an error
{
printf("\n---Error at attaching the memory segment! ---\n");
exit(1);
}
char* str = data;
sprintf(str, "testing");
// ChildProcess();
printf("\n** Child process! ** \n");
printf("N = %d\n", N);
printf("write: %s\n",str);
// detach the shared memory segment
if(shmdt(data) == -1) //check for error
{
printf("\n---Error at detaching memory segment! ---\n");
exit(1);
}
}
else // the parent process (C)
{
// create the shared memory segment for the C process
shmid = CreateShmSegment(key, size, IPC_CREAT);
if(shmid == -1) // check if creating shared memory return an error
{
printf("\n---Error at creating the memory segment! ---\n");
exit(1);
}
// attach the shared memory segment
data = AttachShmSegment(shmid);
if(data == (char*)-1) // check if attached the shared memory return an error
{
printf("\n---Error at attaching the memory segment! ---\n");
exit(1);
}
// ParentProcess();
wait(&status);
printf("\n** Parent process! **\n");
printf("N = %d\n",N);
printf("read from segment: %s\n", data);
// detach the shared memory segment
if(shmdt(data) == -1) //check for error
{
printf("\n---Error at detaching memory segment! ---\n");
exit(1);
}
// deallocate the shared memory segment
if(DeallocateShmSegment(shmid) == -1) //check for error
{
printf("\n---Error at destroy memory segment! ---\n");
exit(1);
}
}
return 0;
}
我引用和另外两个文件进行编译。
进程.h:
#ifndef __Processes_H_
#define __Processes_H_
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
void ChildProcess();
void ParentProcess();
int CreateShmSegment(key_t, size_t, int);
void* AttachShmSegment(int);
int DetachShmSegment(const void*);
int DeallocateShmSegment(int);
#endif
和process.c:
#include "process.h"
void ChildProcess()
{
}
/***************************************************************/
void ParentProcess()
{
}
/****************************************************************/
int CreateShmSegment(key_t key, size_t size, int flag)
{
int id;
id = shmget(key, size, flag);
return id;
}
/*****************************************************************/
void* AttachShmSegment(int id)
{
char* data = NULL;
data = (char*) shmat(id, NULL, 0);
return data;
}
/*****************************************************************/
int DetachShmSegment(const void* addr)
{
return shmdt(addr);
}
/*****************************************************************/
int DeallocateShmSegment(int id)
{
return shmctl(id, IPC_RMID, NULL);
}
我不知道附加内存出了什么问题。我已经在网上搜索并对 shmat() 的参数进行了一些更改,但我无法解决它。
【问题讨论】:
-
必须使用System V功能?否则
mmap是管理共享内存的“现代”方式。看看this SO post -
一般来说,不要将
#include任何内容放入该文件中未使用的文件中。 IE。在 Process.h 文件中不包含头文件,这些内容未在该文件中使用。只需在实际需要的地方包含那些头文件即可。 -
一般来说,错误信息应该输出到
stderr,而不是stdout,所以在if(argc !=2), The enclosed calls toprintf()`之后应该调用fprintf( stderr, ... ) -
当系统函数返回错误指示时,代码应显示与该错误相关的错误消息。所以这一行:
printf("\n---Error at creating the memory segment! ---\n");应该类似于:perror( "shared memory create ( shmget() )failed" );然后会向 stderr 输出类似于以下的消息:shared memory create (shmget())failed: Permission denied`
标签: c debugging fork shared-memory