공유 메모리 세그먼트를 통해 두 프로세스간에 통신하기를 원하는 코드가 있습니다. 내 문제는 내가 메모리 세그먼트를 연결하는 오류가 걸릴 이유를 모르겠다.공유 메모리 세그먼트를 연결할 때 오류가 발생했습니다.
#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;
}
견적과 다른 두 개의 파일을 컴파일합니다.
process.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() 인수에 약간의 변경을가했지만 해결할 수 없었다.
오류가 발생하지만 어떤 오류가 발생합니까? ['errno'] (http://man7.org/linux/man-pages/man3/errno.3.html)를 확인 했습니까? 예를 들어 사용 해본 적이 있습니까? ['strerror'] (http://man7.org/linux/man-pages/man3/strerror.3.html)에 오류의 인쇄 가능한 문자열이 있습니까? –
System V 기능을 반드시 사용해야합니까? 그렇지 않으면'mmap'은 공유 메모리를 관리하는 "현대적인"방법입니다. [이 SO post] (http://stackoverflow.com/questions/21311080/linux-shared-memory-shmget-vs-mmap) – LPs
일반적으로 파일에 어떤 것도 포함시키지 마십시오. 해당 파일 내에서 사용되지 않습니다. I.E. Process.h 파일의 헤더 파일에는 해당 내용이 사용되지 않습니다. 실제로 필요할 때 헤더 파일을 포함 시키십시오. – user3629249