1

我试图用来shm_unlink(object_path)清除我在信号处理功能中打开的共享内存。但是,代码不起作用。出了什么问题?代码基本上是这样做的:父进程从用户输入中获取两个整数,并派生一个子进程来计算两者的总和。一旦计算了总和,子节点就会通过管道通知父节点它已完成计算总和。当收到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);
            }
        }
    }
}
4

1 回答 1

1

我试过你的代码。改变这个:

    if (shm_unlink(SHARED_OBJECT_PATH) != 0) {
        perror("In shm_unlink()");
        exit(1);
    }

    if (shm_unlink(SHARED_OBJECT_PATH) != 0) {
        perror("In shm_unlink()");
        exit(1);
    }
    else {
        perror("All right!");
        exit(0);
    }

否则你需要两个 Ctrl + C退出...第一个删除共享内存,第二个让你的程序退出,因为shm_unlink()返回一个错误(因为 shmem 之前被第一个 Ctrl + c 删除)

于 2013-01-22T15:33:29.813 回答