简体   繁体   English

将结构数组放入UNIX的共享内存中,以便客户端程序可以访问它

[英]putting an array of structs into shared memory in unix so that it can be accessed by a client program

So i'm currently trying to code in unix using shared memory and the fork() function, I have an array of 10 structs and I would like to put that array into shared memory so that it can be accessed by a client program. 因此,我目前正在尝试使用共享内存和fork()函数在UNIX中进行编码,我有10个结构的数组,我想将该数组放入共享内存中,以便可由客户端程序进行访问。 I was hoping someone could point me in the right direction on how to do this. 我希望有人可以指出正确的方向。

the code I currently have is: 我目前拥有的代码是:

//  Compiler Directives


//  Standard Library Inclusions
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <time.h>
//Other Inclusions

struct strProcess 
{
  int nPriority;
  int nPid;
};
//  Function Prototypes (if not included within a header file)
int frand (int nInput);
int finval (int nInput);
void fsortasc(struct strProcess pArray[],int nInput);
//  Main
int main(void)
{
// Variable Declarations
int     nShmid,i,arraySize,nRpriority,j, nInput;
key_t   nKey;
char    *ptrshm, *ptrs;
int     nSize;
pid_t   pid; 
struct  strProcess pArray[10];
struct  strProcess *Array;

Array = pArray;
// Code start

nKey = 5678;
FILE *f = fopen("logfile.txt", "w");

if (f == NULL)
{
      printf("Error opening file!\n");
      exit(1);
}

printf("please enter the amount of processes to create for this cycle between 1 and 10 \n");
scanf("%d",&nInput);
if (nInput <= 0 || nInput > 10)
{
  nInput = finval(nInput);
}
printf("%d", nInput);

nSize = sizeof(pArray) * 10;
 //create segment
if ((nShmid = shmget(nKey,nSize, IPC_CREAT | 0666)) <0)
{
      perror("shmget");
      exit(1);
}
printf("segment created \n\n");
fprintf(f, "shared memory segment created");

Array *pArray = shmat(shmid,NULL, 0);
if (Array* pArray (-1)) 
{
      perror("shmat");
      exit(1);
}
printf("segment attached \n\n");
fprintf(f, "shared memory segment attached");

for(i = 0 ; i < nInput; i++)
{
    if ((pid = fork()) < 0)
    {
      perror("fork");
      exit(1);
    }
    if (pid == 0)
    {
      Array[i].nPid = getpid();
      nRpriority = frand(nInput);
      Array[i].nPriority = nRpriority;
      printf("print job created with Pid %d and priority number %d", 
      getpid(), nRpriority);
      fprintf(f, "print job created with Pid %d and priority number %d", 
      getpid(), nRpriority);
    }
}

fprintf(f, " %d processes have been created", nInput);
fsortasc(pArray, nInput);   /*sort array into ascending order by nRpriority values*/

// Function Definitions - in alphabetical order
int finval (int nInput)
{
while(nInput <= 0 || nInput > 10)                               
{
    printf("please enter a number between 1 and 10 \n");
    scanf("%d", &nInput);                                       
} 
return nInput;                                                
}

int frand (int nInput)
{   
int nRand;
nRand = (rand() % nInput)+1;                  /*set nRand == a random number 
                                               inbetween nInput and 1*/
return nRand;                                 /*return the random number*/
}

void fsortasc(struct strProcess pArray[],int nInput)  
{
struct strProcess temp;     /*temporary storage for elements being swapped*/
int i, j;

for (i = 0; i < nInput - 1; i++)                                 
{
    for (j = 0; j < (nInput - 1-i); j++)
    {
        if (pArray[j].nPriority > pArray[j + 1].nPriority)                      /*if the current element is greater than the next element*/
        {
            temp = pArray[j];                                                   
            pArray[j] = pArray[j + 1];                                          
            pArray[j + 1] = temp;                                               
        }                                                                       
    }
}

I have an array of 10 structs and I would like to put that array into shared memory ? 我有10个结构的数组,我想将该数组放入共享内存中吗? It's very simple, first create array 10 struct variable and then create the shared memory using shmget of required size and then attach that shared memory with pointer and finally copy array of 10 structs into pointer attached with shmat . 非常简单,首先创建array 10 struct variable ,然后使用所需size shmget创建shared memory ,然后使用pointer attachshared memory ,最后copy 10个结构的数组copy到带有shmat指针中。 I added below simple code to understand your requirement. 我在下面添加了简单的代码以了解您的要求。

typedef struct company {
        int emp_id;
}cmp;
int main(int argc,char *argv[]) {
        cmp cmp_info[10];
        int shm_id, sze = sizeof(cmp_info) ,i;
        /* I have an array of 10 structs -- with some data like emp_id*/
        for(i=0 ;i<10 ;i++) {
        printf("\n enter emp % Id \n",i);
        scanf("%d",&cmp_info[i].emp_id);
        }
        /* create the shared memory of 'sze' size. */
        shm_id = shmget(10,sze, IPC_CREAT | 0664);
        perror("shmget");
        /* attach the shared memory with shm_id */
        cmp *shm_ptr = shmat(shm_id, NULL, 0);
        perror("shmat");
        /* I have an array of 10 structs and I would like to put that array into shared memory  */
        shm_ptr = cmp_info;//now shared memory contains array of 10 struct data 

        /** print using shm_ptr to verify **/
        for(i=0;i<10;i++) {
        printf("Employee[%d] Id is : [%d]\n",i,shm_ptr[i].emp_id);
        }
        /* once above things are done clients program can read from shared memory */
        /** finaly de-atach the shared memory */
        shmdt(shm_ptr);
}

Below snapshot is for your code, Explanation is in comments. 快照下方是您的代码,注释位于注释中。

struct strProcess {
        int nPriority;
        int nPid;
};
int main(int argc,char *argv[]) {
        // Variable Declarations
        int     nShmid,i,arraySize,nRpriority,j, nInput;
        key_t   nKey;
        char    *ptrshm, *ptrs;
        int     nSize;
        struct  strProcess pArray[10];//array of 10 structure
        struct  strProcess *Array;
        //Array = pArray;
        nKey = 5678;
        FILE *f = fopen("logfile.txt", "w");
        if(f == NULL) {
                printf("Error opening file!\n");
                exit(1);
        }
        nSize = sizeof(pArray);
        //create segment
        if((nShmid = shmget(nKey,nSize, IPC_CREAT | 0666)) < 0) {
                perror("shmget");
                exit(1);
        }
        else {
                perror("shmget");
                fprintf(f, "\n shared memory segment created\n");
        }
        Array  = shmat(nShmid, NULL, 0);
        perror("shmat");
        /** loop to create exaCtly 10 process */
        nInput = 10; /** call finval function **/
        for(i = 0 ; i < nInput; i++) {
                if(fork() == 0) {
                        srand(getpid());
                        Array[i].nPid = getpid();
                        nRpriority = rand()%10 + 1;//putting random no b/w 1 to 10..u can call your function also
                        Array[i].nPriority = nRpriority;
                        fprintf(f, "\nprint job created with Pid [%d] and priority number [%d]\n",
                                        Array[i].nPid, Array[i].nPriority);
                        break;//must to avoid repeating
                }
                else {
                        ;//parent does nothing
                }
        }
        shmdt(Array);
        //fprintf(f,"\n total [%d] processes have been created\n",nInput);
        /* call fsortasc(pArray, nInput); */
        fclose(f);
}

I hope it helps. 希望对您有所帮助。

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

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