shm_unlink(object_path)
信号処理機能で開いた共有メモリをクリアしようとしています。ただし、コードは機能しません。何が問題なのですか?コードは基本的にこれを行います。親プロセスはユーザー入力から2つの整数を取得し、子をフォークして2つの合計を計算します。合計が計算されると、子は親にパイプを介して合計の計算が完了したことを通知します。シグナルを受信するときSIGINT
、子はそれ自体で終了するべきではありません。親プロセスにシグナルを処理させ、親がSIGKILLシグナルを送信して子プロセスを終了する必要があります。ありがとうございました!
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#define SHARED_OBJECT_PATH "/shared_memory"
#define READ_END 0
#define WRITE_END 1
#define BUFFER_SIZE 1
//Global Variables
int pid; //Child Process ID
void signal_callback_handler(int signum){
if (signum == SIGINT){
printf("I am %d and I am handling SIGINT. \n", getpid()); /* process ID that's handling SIGINT */
kill(pid,SIGKILL);
if (shm_unlink(SHARED_OBJECT_PATH) != 0) {
perror("In shm_unlink()");
exit(1);
}
}
}
int main(int argc, char *argv[]){
int fd,status;
int pfd1[2],pfd2[2]; /*Two communication channels will be created. pfd1 for parent->child, pfd2 child->parent*/
char msg[BUFFER_SIZE];
int first_num, second_num, sum;
int shared_seg_size = 3*(sizeof(int));
int *shared_msg; /* We want a shared segment capable of storing 2 integers and 1 sum */
if(pipe(pfd1) < 0 || pipe(pfd2) < 0){
printf("Failed to create a pipe between parent and child \n");
exit(-1);
}
/* Open the Shared Memory Segment */
fd = shm_open(SHARED_OBJECT_PATH, O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG);
if (fd < 0) {
perror("In shm_open()");
exit(1);
}
fprintf(stderr, "Created shared memory object %s\n", SHARED_OBJECT_PATH);
/* Adjust mapped file size (make room for the whole segment to map) using ftruncate() */
ftruncate(fd, shared_seg_size);
/* Request the shared segment using mmap() */
shared_msg = (int *) mmap(NULL, shared_seg_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (shared_msg == NULL) {
perror("In mmap()");
exit(1);
}
fprintf(stderr, "Shared memory segment allocated correctly (%d bytes).\n", shared_seg_size);
/*End of the opening of the Shared Memory Segment */
if((pid = fork()) < 0){ /* Fork the process */
printf("Fork error \n");
exit(-1);
}
else if (pid > 0){/* Parent code */
signal(SIGINT, signal_callback_handler); /* To handle the Ctrl+C signal and clean up shared memory */
close(pfd1[READ_END]);
close(pfd2[WRITE_END]);
while(1){ /* Keep running the main process to get user input */
printf("Enter the first number: ");
scanf("%d",&first_num);
printf("Enter the second number: ");
scanf("%d",&second_num);
/* Write the two integers to the shared memory segment */
shared_msg[0] = first_num;
shared_msg[1] = second_num;
msg[0] = '1'; /*Write to the pipe and tell child to compute the sum */
write(pfd1[WRITE_END], msg, BUFFER_SIZE);
read(pfd2[READ_END],msg, BUFFER_SIZE);
if (msg[0] == '1'){
sum = shared_msg[2];
printf("The sum is %d\n",sum);
msg[0] = '0'; /*Tell the child to get ready to compute new sum*/
write(pfd1[WRITE_END],msg,BUFFER_SIZE);
}
}
}
else if (pid == 0){/* Child Code */
close(pfd1[WRITE_END]);
close(pfd2[READ_END]);
while(1){
read(pfd1[READ_END],msg,BUFFER_SIZE);
if (msg[0] == '1'){ /*Child should compute sum*/
sum = shared_msg[0]+shared_msg[1];
shared_msg[2] = sum;
msg[0] = '1'; /*Tell the parent, the sum is computed */
write(pfd2[WRITE_END],msg,BUFFER_SIZE);
}
}
}
}