简体   繁体   English

使用信号访问共享内存到子进程

[英]Access shared memory to child process with signal

I need some help with my c program.我的 c 程序需要一些帮助。 Basically, in my c program, I wish my parent process calculates a+b and my child process calculates 3*(a+b).基本上,在我的 c 程序中,我希望我的父进程计算 a+b,而我的子进程计算 3*(a+b)。 However, I don't how to access the shared memory after sending the signal.但是,我不知道发送信号后如何访问共享内存。 Any help is appreciated.任何帮助表示赞赏。

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

void sigcont(int shmid); 

int main (int argc, char *argv[]){
    int shmid;
    int *result;
    pid_t pid;

    int size_data = 2 * sizeof (int);
    shmid = shmget (IPC_PRIVATE, size_data, 0666 | IPC_CREAT);
    
    pid = fork ();
    if (pid == 0) {
        signal(SIGCONT, sigcont); 
        while (1);
    }
    else {
        sleep(1);
        result = (int *) shmat (shmid, NULL, 0);
        result[0] = atoi (argv[2]) + atoi (argv[1]);
        printf ("Result in parent process %d: %d.\n", getpid() , result[0]);
        printf ("Send a SIGCONT to process %d.\n\n", pid);
        shmdt (result);
        kill (pid, SIGCONT);
        wait(NULL);
        printf ("\nThis is the End.\n");
    }
  return 0;
}

void sigcont(int shmid){
    printf("Get a SIGCONT.\n");
    printf("Result in child process ID: %d.\n", getpid());
    exit(0);
}

What I have tried:我尝试过的:

  1. Put one parameter in void sigcont(int shmid);将一个参数放入 void sigcont(int shmid); As I am learning from https://www.geeksforgeeks.org/signals-c-set-2/ .当我从https://www.geeksforgeeks.org/signals-c-set-2/学习时。 Do signal() and kill() must use in pair? signal() 和 kill() 必须成对使用吗? I get Thus, I tried to pass a value to it.我得到 因此,我试图向它传递一个值。 Not working.不工作。

  2. Sigstop() and Sigcont() I tried to avoid using signal() in my child process so that I can access shmid directly. Sigstop() 和 Sigcont() 我试图避免在我的子进程中使用 signal() 以便我可以直接访问 shmid。 But I couldn't find a lot of examples of Sigstop() and Sigcont().但是我找不到很多 Sigstop() 和 Sigcont() 的例子。 It fails as well.它也失败了。

It's really hard to get my desired result even it looks simple.即使看起来很简单,也很难得到我想要的结果。 Can anyone help with this?有人能帮忙吗? Any help is appreciated.任何帮助表示赞赏。

Notes: added error checking;备注:增加了错误检查; call shmat() and save to global before fork() , child to inherit attached shared mem;fork()之前调用shmat()并保存到全局,孩子继承附加的共享内存; SIGCONT better for stopped process, selected SIGUSR1 ; SIGCONT更适合停止的进程,选择SIGUSR1 arg to sig handler is signal #; arg 到 sig 处理程序是信号 #; child was spinning, for minimal overhead better to pause() .孩子在旋转,为了最小的开销,最好是pause()

This version is an example, and does not attempt to support every system imagined.这个版本是一个例子,并不试图支持想象的每一个系统。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int *shmloc;


void shandler(int sig)
{
    printf("pid %d, sig %d\n", getpid(), sig);
    printf("mult result: %d\n", *shmloc * 3);
}


int main(int argc, char *argv[])
{
    int a, b, shmid, size_data, sig;
    pid_t pid;

    if (argc < 3) {
        fprintf(stderr, "missing args a b\n");
        return (1);
    }

    // OP: shmget() size arg, some systems may require a minimum, may roundup
    size_data = 2 * sizeof (int);
    shmid = shmget(IPC_PRIVATE, size_data, 0666 | IPC_CREAT);
    if (shmid == -1) {
        fprintf(stderr, "shmget failed, %s\n", strerror(errno));
        return (1);
    }

    shmloc = shmat(shmid, NULL, 0);
    if (shmloc == (void *) -1) {
        fprintf(stderr, "shmat failed, %s\n", strerror(errno));
        return (1);
    }

    sig = SIGUSR1;
    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "fork failed, %s\n", strerror(errno));
        return (1);
    }

    // child only
    if (pid == 0) {
        signal(sig, shandler); 
        pause();
        printf("child exit\n");
        _exit(0);
    }

    sleep(1);
    // OP: care about non-digits in cmd-line args?
    a = atoi(argv[1]);
    b = atoi(argv[2]);
    *shmloc = a + b;
    printf("parent pid %d, add (%d, %d) result %d\n", getpid(), a, b, *shmloc);
    shmdt(shmloc);
    printf("sending signal %d to child %d\n", sig, pid);
    kill(pid, sig);
    wait(NULL);
    printf("\nThis is the End.\n");

    return (0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM