c中的过程与消息队列之间的沟通

发布于 2025-02-02 12:56:49 字数 4798 浏览 1 评论 0原文

这将是该项目的基本版本。我有X用户生成由父亲处理的交易。然后,我试图建立与父亲和孩子之间的消息队列建立连接,但是当我与Execve发射Process Consect Child时,它不会创建消息,而父亲则陷入困境。如果我在父亲去时运行一个用户(孩子),它将填写消息的队列,父亲抓住它并打印出记忆错误崩溃。我该如何优化通信?这对我来说是一个全新的论点,我在C中非常noob(我去了Java)。我希望我的目的对您来说很清楚,以便获得尽可能多的建议来实现我的任务。谢谢。

更新:孩子和父母交流,但是:

- 如果我想使用共享内存将变量x定义为父母卡住的队列,而无需做任何事情。

- 如果我在子里放置了一个参数og argv的printf(例如:当父母通过ID时)。孩子除了什么都没有打印 在此之后定义的其他printf

- 通信工作,但请给我:检测到的堆叠粉碎。我搜索并发现会有分段的错误,但是我 找不到哪里。第54行的Erro:返回后,是否可能?

#include "config.h"

int main(int argc, char const *argv[])
{
    char * args[2] = {CHILD_NAME,NULL};
    int q_id,num_bytes;
    struct msgUser mt_msg;

#ifndef SO_USERS_NUM
    printf("Error in main:  SO_USERS_NUM not defined");
#endif
    /* 
        Creation users, main do forks as SO_USERS_NUM is setting on confing library.
        A child do a execve to the same file .c with the required operation.
        args: array of string that contains data usefull to user
    */
    q_id = msgget(KEY_QUEUE, IPC_CREAT | 0600);
    for (int i = 0; i < SO_USERS_NUM; i++)
    {
        switch (fork())
        {
        case 0:
#ifdef CHILD_NAME            
            execve(CHILD_NAME,args,NULL);
#endif
            exit(EXIT_FAILURE);
            break;
         case -1:
            printf("Error in main:  forkkodio");
            break;
        default:
            break;
        }
    }
     for (int i = 0; i < SO_USERS_NUM; i++)
        {
            printf("Ho finito di aspettare:%d  sono il padre: %d\n",wait(NULL),getppid());
        }
    
    while (1) {
        /* now receiving the message */
        num_bytes = msgrcv(q_id, &mt_msg, 120, 3, 0);

        if (num_bytes >= 0) {
            /* received a good message (possibly of zero length) */
            printf("messaggio ricevuto: %d \n",mt_msg.numero);
            break;
        }

    }

    return 0;
}

孩子:

#include "user_manager.h"
#include "config.h"

int main(int argc, char const *argv[])
{
    //printf("\n user id: %0x",atoi(argv[1]));
  
    int q_id;
    struct msgUser my_msg;

    q_id = msgget(KEY_QUEUE, IPC_CREAT | 0600);
    my_msg.mtype = 3;
    my_msg.numero = 33;
    
    msgsnd(q_id, &my_msg, 120, 0);

    return 0;
}

config.h:

#ifndef _CONFIG_H_
#define _CONFIG_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/wait.h>

struct msgUser {
    long mtype;             /* message type, must be > 0 */
    int numero;
};
/*
    KEY_QUEUE define the start key for the master process.
    Children nodes will increase this value to create owner's queue
*/
#define KEY_QUEUE 0x200800                     
/*
   CHILD_NAME name of users's code lauched by execve
*/
#define CHILD_NAME "user"
/*
   SO_USERS_NUM define the maximum number of users
*/
#define SO_USERS_NUM 1
/*
   SO_NODES_NUM define the maximum number of nodes
*/
#define SO_NODES_NUM 10
/*
    SO_BUDGET_INIT initial budget of each user
*/
#define SO_BUDGET_INIT 1000
/*
    SO_REWARD the percentage of reward paid by each user for processing a transaction
*/
#define SO_REWARD 5 
/*
    SO_MIN_TRANS_GEN_NSEC minimum value of time (expressed in nano-seconds)     
    that elapses between the generation of a transaction and the next by a user
*/
#define SO_MIN_TRANS_GEN_NSEC 1000
/*
    SO_MAX_TRANS_GEN_NSEC maximum value of time (expressed in nano-seconds)     
    that elapses between the generation of a transaction and the next by a user
*/
#define SO_MAX_TRANS_GEN_NSEC 3000
/*
    SO_RETRY maximum value of failure by an user to start a transaction.
*/
#define SO_RETRY 3
/*
    SO_TP_SIZE number of transaction in the same node in the same time
*/
#define SO_TP_SIZE 6
/*
    SO_MIN_TRANS_PROC_NSEC minimun time for node to elaborate a transaction block
*/
#define SO_MIN_TRANS_PROC_NSEC 1000
/*
    SO_MAX_TRANS_PROC_NSEC maximum time for node to elaborate a transaction block
*/
#define SO_MAX_TRANS_PROC_NSEC 3000
/*
    SO_SIM_SEC simulation's duration
*/
#define SO_SIM_SEC 120
/*
    SO_NUM_FRIENDS node's friends
*/
#define SO_NUM_FRIENDS 3
/*
    SO_HOPS maximum nuber of transaction forward to a friend before the master create a new node.
*/
#define SO_HOPS 5
/*
    SO_REGISTRY_SIZE maximun number of blocks processed by master
*/
#define SO_REGISTRY_SIZE 3
/*
    SO_BLOCK_SIZE number of a transaction in a block
*/
#define SO_BLOCK_SIZE 3

/*
    defined set of generic function's master
*/


#endif

this would be a basic version of what the project will be. I have X user that generate transaction handled by a father. Then, I've tried to establish a connection with a message queue between father and child but when i launch process child with execve it doesn't create the message and the father stuck. If i run a user(child) while father is going it will fill queue with the message and the father catch it and print with an error of memory crashed. How can i optimize the communication??this is a whole new argument for me and i'm very noob in C. (i went from Java). i hope my purpose will sounds clear to you, in order to receive as many suggestion possible to achive my tasks. Thanks.

UPDATE: child and parent communicate but:

-if i wanna use shared memory to define a variable x as id for the queue the parent stuck and don't do anything.

-if i put a printf of an argument og argv in child (example: when i pass the id by the parent). The child will print nothing except for
other printf defined after that

-The communicarion work but give me: stack smashing detected. I searched and found out that there would be a segmention fault, but i
can't find where.Is it right? erro at line 54: after return, is it possible?

Parent

#include "config.h"

int main(int argc, char const *argv[])
{
    char * args[2] = {CHILD_NAME,NULL};
    int q_id,num_bytes;
    struct msgUser mt_msg;

#ifndef SO_USERS_NUM
    printf("Error in main:  SO_USERS_NUM not defined");
#endif
    /* 
        Creation users, main do forks as SO_USERS_NUM is setting on confing library.
        A child do a execve to the same file .c with the required operation.
        args: array of string that contains data usefull to user
    */
    q_id = msgget(KEY_QUEUE, IPC_CREAT | 0600);
    for (int i = 0; i < SO_USERS_NUM; i++)
    {
        switch (fork())
        {
        case 0:
#ifdef CHILD_NAME            
            execve(CHILD_NAME,args,NULL);
#endif
            exit(EXIT_FAILURE);
            break;
         case -1:
            printf("Error in main:  forkkodio");
            break;
        default:
            break;
        }
    }
     for (int i = 0; i < SO_USERS_NUM; i++)
        {
            printf("Ho finito di aspettare:%d  sono il padre: %d\n",wait(NULL),getppid());
        }
    
    while (1) {
        /* now receiving the message */
        num_bytes = msgrcv(q_id, &mt_msg, 120, 3, 0);

        if (num_bytes >= 0) {
            /* received a good message (possibly of zero length) */
            printf("messaggio ricevuto: %d \n",mt_msg.numero);
            break;
        }

    }

    return 0;
}

Child:

#include "user_manager.h"
#include "config.h"

int main(int argc, char const *argv[])
{
    //printf("\n user id: %0x",atoi(argv[1]));
  
    int q_id;
    struct msgUser my_msg;

    q_id = msgget(KEY_QUEUE, IPC_CREAT | 0600);
    my_msg.mtype = 3;
    my_msg.numero = 33;
    
    msgsnd(q_id, &my_msg, 120, 0);

    return 0;
}

config.h:

#ifndef _CONFIG_H_
#define _CONFIG_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/wait.h>

struct msgUser {
    long mtype;             /* message type, must be > 0 */
    int numero;
};
/*
    KEY_QUEUE define the start key for the master process.
    Children nodes will increase this value to create owner's queue
*/
#define KEY_QUEUE 0x200800                     
/*
   CHILD_NAME name of users's code lauched by execve
*/
#define CHILD_NAME "user"
/*
   SO_USERS_NUM define the maximum number of users
*/
#define SO_USERS_NUM 1
/*
   SO_NODES_NUM define the maximum number of nodes
*/
#define SO_NODES_NUM 10
/*
    SO_BUDGET_INIT initial budget of each user
*/
#define SO_BUDGET_INIT 1000
/*
    SO_REWARD the percentage of reward paid by each user for processing a transaction
*/
#define SO_REWARD 5 
/*
    SO_MIN_TRANS_GEN_NSEC minimum value of time (expressed in nano-seconds)     
    that elapses between the generation of a transaction and the next by a user
*/
#define SO_MIN_TRANS_GEN_NSEC 1000
/*
    SO_MAX_TRANS_GEN_NSEC maximum value of time (expressed in nano-seconds)     
    that elapses between the generation of a transaction and the next by a user
*/
#define SO_MAX_TRANS_GEN_NSEC 3000
/*
    SO_RETRY maximum value of failure by an user to start a transaction.
*/
#define SO_RETRY 3
/*
    SO_TP_SIZE number of transaction in the same node in the same time
*/
#define SO_TP_SIZE 6
/*
    SO_MIN_TRANS_PROC_NSEC minimun time for node to elaborate a transaction block
*/
#define SO_MIN_TRANS_PROC_NSEC 1000
/*
    SO_MAX_TRANS_PROC_NSEC maximum time for node to elaborate a transaction block
*/
#define SO_MAX_TRANS_PROC_NSEC 3000
/*
    SO_SIM_SEC simulation's duration
*/
#define SO_SIM_SEC 120
/*
    SO_NUM_FRIENDS node's friends
*/
#define SO_NUM_FRIENDS 3
/*
    SO_HOPS maximum nuber of transaction forward to a friend before the master create a new node.
*/
#define SO_HOPS 5
/*
    SO_REGISTRY_SIZE maximun number of blocks processed by master
*/
#define SO_REGISTRY_SIZE 3
/*
    SO_BLOCK_SIZE number of a transaction in a block
*/
#define SO_BLOCK_SIZE 3

/*
    defined set of generic function's master
*/


#endif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文