【问题标题】:2D arrays with shared memory具有共享内存的二维数组
【发布时间】:2017-01-03 09:35:31
【问题描述】:

我有 2 个使用共享内存块相互通信的程序。

第一个程序从命令行获取一个参数并按指定的次数分叉,每个子进程的进程 ID 和随机生成的数字存储在一个二维数组中,然后应该通过附加的内存块。问题是我不知道如何做到这一点,希望能得到一些帮助,因为我在这方面有点新手。

这是迄今为止第一个程序的代码,并且已经过全面测试和工作:

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

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("\nError with command line argument!\n");
        exit(1);
    }

    /* Program is simulation of a printer queue, forked child processes
       act as jobs in the queue with pids being the job number and a
       randomly generated value being the priority order (this comes into
       play in program 2) */

    srand(time(null));
    pid_t pid;

    int amount, i, pID, rNum;

    amount = atoi(argv[1]);

    int procID[amount]; /* 1D array that will hold pid for each process */
    int randNum[amount]; /* 1D array that will hold random number to
                            determine priority level of the print jobs */

    int rows = amount;
    int cols = 2;

    int printDetails[rows][cols]; /* 2D array that will hold the values
                                     from both 1D arrays, displaying all
                                     data from print queue when output */

    printf("\nPrint queue started:");
    getchar();

    for (i = 0; i < amount; i++)
    {
         pid = fork();

         if (pid < 0)
         {
             perror("Error with fork!");
             exit(1);
         }

         if (pid == 0)
         {
             pID = getpid();
             rNum = rand()%50;

             printf("\nPrint Job : %d", pID);
             printf("\nPriority Level : %d\n", rNum);

             procID[i] = pID;
             randNum[i] = rNum;

             sleep(1);
         }

         else
         {
             wait(NULL);
             return 0;
         }
    }

    printf("\n%d successful print jobs created\n", amount);

    printf("\n-----PRINT DETAILS-----\n");
    printf("\nJob No:\tPriority:\n");

    for (i = 0; i < rows; i++)
    {
        printDetails[i][0] = procID[i];
        printDetails[i][1] = randNum[i];

        printf("%d\t%d\n", printDetails[i][0], printDetails[i][1];
    }

    printf("\n-----END OF LIST-----\n");

    /* Create shared memory segment using shmget and shmat,
       how do I insert the array above into this, like I said
       complete noob! */

}

抱歉,为了帮助理解我正在使用的代码, 就像我说的,任何关于共享内存的帮助都将不胜感激,因为我在这里有点超出我的深度!

【问题讨论】:

  • 考虑首先创建一个共享内存块,然后将其用作您的数组。也许演员表会有助于让它变得明显。

标签: c linux ipc shared-memory


【解决方案1】:

写时复制机制将在您在第二个进程上更改它的那一刻分配一个新指针......当它死去时,它将占用新分配的内存和不同的数据......解决方案是分配一个动态指针...当您更改它的值时,它不会动态分配新值并使用旧值并更改其数据

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

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("\nError with command line argument!\n");
        exit(1);
    }

    /* Program is simulation of a printer queue, forked child processes
       act as jobs in the queue with pids being the job number and a
       randomly generated value being the priority order (this comes into
       play in program 2) */

    srand(time(NULL)); // null -> NULL
    pid_t pid;

    int amount, i, pID, rNum;

    amount = atoi(argv[1]);

    int* procID =(int*) calloc(amount,sizeof(int)); /* 1D array that will hold pid for each process */
    if(!procID)
    return -1;
    int* randNum =(int*) calloc (amount,sizeof(int)); /* 1D array that will hold random number to
                            determine priority level of the print jobs */
    if(!randNum)
    return -1;
    int rows = amount;
    int cols = 2;
    int k;
    int** printDetails = (int**) calloc (rows, sizeof(int*)); /* 2D array that will hold the values
                                     from both 1D arrays, displaying all
                                     data from print queue when output */
    if(!printDetails)
    return -1;
    for(k=0; k<rows;k++)
    {
        printDetails[k] = (int*) calloc (cols, sizeof(int));
        if(!printDetails[k])
        return -1;
    }
    printf("\nPrint queue started:");
    getchar();

    for (i = 0; i < amount; i++)
    {
         pid = fork();

         if (pid < 0)
         {
             perror("Error with fork!");
             exit(1);
         }

         if (pid == 0)
         {
             pID = getpid();
             rNum = rand()%50;

             printf("\nPrint Job : %d", pID);
             printf("\nPriority Level : %d\n", rNum);

             procID[i] = pID;
             randNum[i] = rNum;

             sleep(1);
         }

         else
         {
             wait(NULL);
             return 0;
         }
    }

    printf("\n%d successful print jobs created\n", amount);

    printf("\n-----PRINT DETAILS-----\n");
    printf("\nJob No:\tPriority:\n");

    for (i = 0; i < rows; i++)
    {
        printDetails[i][0] = procID[i];
        printDetails[i][1] = randNum[i];

        printf("%d\t%d\n", printDetails[i][0], printDetails[i][1]);
    }

    printf("\n-----END OF LIST-----\n");

    /* Create shared memory segment using shmget and shmat,
       how do I insert the array above into this, like I said
       complete noob! */
    for(k=0; k<rows; k++)
        free(printDetails[k]);
    free(printDetails);
    free(randNum);
    free(procID);
}

【讨论】:

    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多