如何让守护程序进程与C

时间:2018-02-09 04:12:48

标签: c multithreading parent-child daemon

我正在编写一个程序,其中父进程(稍后成为守护进程)创建一个子进程,然后这个子进一步创建多个线程,这些线程并发运行以处理查询。这需要线程通过子进程与守护进程通信,然后守护进程通过子进程将控制消息发送回线程。我为此编写了以下代码。

typedef struct shared SHAREDM;
struct shared{
     char tmsg[500];
     char pmsg[500];
} shrd_tc,*shrd_pc;

int main(){
    initialize();
    int pid;
    int shmID;
    int status;
    shmID = shmget(IPC_PRIVATE, sizeof(SHAREDM), IPC_CREAT | 0666);
    if (shmID < 0) {
          printf("*** shmget error (server) ***\n");
          exit(1);
    }
    shrd_pc = (SHAREDM*) shmat(shmID, NULL, 0);  //shared memory b/w parent process and child process
    pid=fork();         
    int st=0;
    if(pid<0){
        printf("\n Error ");
        exit(1);
    }
    /*
      *Child will create threads that will run queries concurrently and communicate to parent process (daemon) 
    via child process 
      * child and threads communcate through shared memory 'shared_tc' of type SHAREDM
      * parent(daemon) and child also communicate through shared memory 'shared_pc' of type SHAREDM 
    */
    else if(pid==0) {    

        pthread_t threads[20];
        long taskids[20];   
        for(int i=0;i<20;i++){
               taskids[i]=i+1;
               pthread_create( &threads[i], NULL, query, (void*)taskids[i]);
        }
        int t=10;
        while(t--){
            printf("Hello from child\n");
            // child will check continuously whether daemon has written in the shared memory
            printf("%s\n", shrd_pc->pmsg);

            //child will check continuouly whether threads has written something in the shared memory
            printf("%s\n", shrd_tc.tmsg);

            sleep(1);   
        }


        for(int i=0;i<20;i++){
             pthread_join(threads[i],NULL);
        }

    }
    else{
        //unmask the file mode
        umask(0);
        int sid;
        //set new session
        sid = setsid();
        if(sid < 0){
        // Return failure   
           exit(1);
        }
        // Change the current working directory to root.
        chdir("/");
        // Close stdin. stdout and stderr

        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
        int t=10;
        while(t--){
            //doemon will write continuous in shared memory
            sprintf(shrd_pc->pmsg,"%s","Hello I am parent process");
            sleep(1);
        }  
    }



    return 0;
}
void* query(void* thread_id){
     long tid=(long)thread_id;   
     int T=10;
     while(T--){
          //printf("Hello from thread %ld\n",tid);
          //thread writing something to shared memory between child and threads
          sprintf(shrd_tc.tmsg,"%s %ld","Hello I am thread ",tid);
          sleep(1);
     } 
     pthread_exit(0);
}

当我试图让守护进程向shrd_pc-&gt; pmsg写一些内容时,它无法执行此操作。我无法在子进程中读取shrd_pc-&gt; pmsg。如何让这个守护程序进程在shrd_pc-&gt; pmsg中写入,以便以后的child可以读取相同的内容?

1 个答案:

答案 0 :(得分:1)

您的子进程内存是父进程的副本,不与线程共享。

要在流程之间进行通信,可以使用a lot种方法,但您可能需要查看this答案,了解如何保留与您的孩子共享地址空间的某些部分。