Codingdumb Codingdumb - 5 months ago 20
C Question

How can I share the memory when i call fork()?

Last time, I questioned .

I want output

0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

but

my out put is

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

what's the problem with my code?

=

I wrote this question,
many people said that I should use shared memory,
but I have never listen to that in class..

but I should solve in 2days.

What's the simple way to get the solution.

I found the 'shm*' but, there's no more easy way to solve this problem than shm*?

If it exists, Plz teach me the simple example .....

Thank you...

Answer Source

You can communicate PIPEs. It is another IPC mechanism.

From linux manual: int pipe(int fd[2]) -- creates a pipe and returns two file descriptors, fd[0], fd[1]. fd[0] is opened for reading, fd[1] for writing.

Code for your requirement:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
    int     fd[2], nbytes;
    pid_t   pid;
    int arr[10]={0,};
    int readbuffer[10];
    int state, c, i, j;

    pipe(fd);

    if((pid = fork()) == -1)
    {
        printf("can't fork, error\n");
        exit(-1);
    }
    else if(pid == 0)
    {
            /* Child process closes up input side of pipe */
            close(fd[0]);

            /* Send "arr" through the output side of pipe */
            printf("\nProducer is created.\n");

            printf("array: ");
            for(c=0; c<10; c++)
            {
                printf("%d ", arr[c]);
                arr[c]=c+1;
            }

            write(fd[1], arr, (sizeof(int)*10));
            exit(0);
    }
    else
    {
            /* Parent process closes up output side of pipe */
            close(fd[1]);

            pid=wait(&state);
            sleep(1);
            printf("\nConsumer takes control of array");

            /* Read in arr from the pipe */
            nbytes = read(fd[0], arr, (sizeof(int)*10));

            printf("\narray:");

            for(j=0;j<10;j++) 
            {
                printf(" %d", arr[j]);
            }

            printf("\nConsumer is done.");

            printf("\narray: ");
            for ( i =0; i<10; i++)
            {
                arr[i]=-1;
                printf("%d ", arr[i]);
            }
            printf("\ndone\n");
            exit(0);

    }

    return(0);
}