공유 메모리를 사용하여 클라이언트 및 서버 역할을하는 processor.c 및 receiver.c라는 두 프로세스간에 통신 할 수있는 프로그램을 만들려고합니다.IPC - Linux의 공유 프로그래밍
첫 번째 프로그램 receiver.c는 한 번에 한 줄씩 사용자의 입력으로 알파 숫자 문자열을 수신하는 무한 루프에서 실행됩니다. 표준 입력에서 한 행을 읽은 후에이 프로그램은이 정보를 다른 프로그램에 보냅니다. 두 프로세스 간의 데이터 공유는 공유 메모리를 통해 이루어져야합니다. 두 번째 프로그램 인 processor.c는 출력 파일 vowels.out을 생성하고 사용자 입력이 수신자 프로그램에 의해 보내질 때까지 대기합니다. 수신자로부터 한 줄을 받자 마자 그 줄에있는 모음의 수를 세고 vowels.out 파일의 원래 줄과 함께 모음 수를 덤프합니다. 이 프로그램은 또한 무한 루프에서 실행됩니다. 여기
는 프로그램입니다 -processor.c
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdio.h>
#define SHM_SIZE 10240 /* make it a 10K shared memory segment */
void main()
{
int v=0;
char ch;
int id_shm;
key_t random_key; /* Keys are used for requesting resources*/
char *shmem, *seg;
/* Segment named "6400", should be created by the server. */
random_key = 6400;
/* Locate the segment. Used - shmget() */
if ((id_shm = shmget(random_key, SHM_SIZE, 0660)) < 0) {
perror("shmget");
exit(1);
}
/* Attaching segment to the data spaces. Used - shmat() */
if ((shmem = shmat(id_shm, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
/* Creation of File */
FILE *op_file;
op_file = fopen("vowels.out", "a");
/* Now read what the server put in the memory. */
seg = shmem;
while (shmem != NULL) /* 'while' - use of entry controlled loop */
{
while ((ch=fgetc(op_file))!=EOF)
{
if (ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||ch=='U'||ch=='u')
{
v++;
}
}
fprintf(op_file, "%s, %d \n", shmem, v);
*shmem = "*";
while(*shmem == "*")
{
sleep(6);
}
}
fclose(op_file);
}
receiver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 10240 /* make it a 10K shared memory segment */
void main()
{
key_t random_key; /* Keys are used for requesting resources*/
int id_shm;
char *data, *shmem;
random_key = 6400;
/* Creation of the segment. Used - shmget()*/
if ((id_shm = shmget(random_key, SHM_SIZE, 0660 | IPC_CREAT)) == -1)
{
perror("shmget");
exit(1);
}
/* Addition of segment to the data spaces. Used - shmat() */
if ((shmem = shmat(id_shm, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}
while (1) /* 'while' - use of entry controlled loop to check on access */
{
scanf("%s", data);
data = shmem; /* Mapping of data */
while(*shmem != "*")
{
sleep(6);
}
}
}
하지만 지정 오류가 발생하고 Segmentation Fault
아무도 도와 줄 수 없나요? 이 프로그램에서 내가 잘못하고있는 것이 있습니까?
미리 감사드립니다.
세그먼테이션 오류가 발생하는 줄을 격리 할 수 있습니까? 디버그 정보 (예 : gcc에서'-g')로 컴파일하면 오류가 발생합니까? Linux를 사용하면서 Valgrind를 사용하여 오류에 대한 추가 정보를 수집 할 수도 있습니다. –
저는 리눅스 초보자이며 윈도우 8.1 머신에서 가상 박스를 사용하고 있습니다. 컴파일 과정에서 다음과 같은 경고가 표시됩니다. - receiver.c : 36 : 15 : 포인터와 정수의 비교 [기본적으로 사용 가능] –