2017-01-03 108 views
2

二維數組我有2個程序,相互使用共享存儲器的塊進行通信。具有共享存儲器

第一個程序從命令行接受一個參數並分叉指定的次數,每個子進程的ID和一個隨機生成的數存儲在一個2D數組中,然後應該傳遞給第二個程序通過連接的內存塊。問題是我不知道如何做到這一點,並希望得到一些幫助,因爲我在這方面有點新手。

這是迄今第一個程序的代碼,並經過全面的測試和工作:

#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! */ 

} 

對不起,我的代碼巨大的牆,只是爲了幫助理解我的工作, 像我說,任何關於共享內存的幫助將非常感謝,因爲我有點不在我的深處!

+0

首先考慮,然後創建共享內存塊只是用它作爲你的陣列。也許演員會幫助明顯。 – user133831

回答

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); 
}