我试图使用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);
}
}
}
}
最佳答案
我试过你的密码。更改此:
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()
返回了一个错误(因为以前的第一个CTRL+C删除了SMEM)关于c - shm_unlink说“没有这样的文件或目录”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14461164/