views:

360

answers:

3

Assume all the variables have been declared previously... because they have been. The child process does not print anything which makes me think it's not being executed. The parent process runs fine, albeit it doesn't get the shared memory. I apologize for the length of this code...

// create 5 child process
for(int k=0;k<5;k++){

    // fork a child process
    pid = fork();

    // error occured on fork
    if (pid < 0) {
        fprintf(stderr, "Fork Failed");
        return 1;
    }
    // this is what the child process will run
    else if (pid == 0) {
        //create a shared mem segment
        segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);

        //attach the shared memory segment
        shared_memory = (char *) shmat(segment_id, NULL, 0);

        printf("this is child");

        double x = 0;
        double sum = 0;

        // Run process that sums the function
        for(int i=0; i<n; i++){
            // get random number in range of x1-x2
            x = rand()%(x2 - x1 + 1) + x1;
            sum = sum + f(x);
        }

        //write output to the shared memory segment
        sprintf(shared_memory, "%f", sum);
        execlp("/bin/ls", "ls", NULL);

     }

    // this is what the parent process will run
    else {

       //print output from shared memory
        printf("\n*%s", shared_memory);

        //detach shared memory
        shmdt(shared_memory);

        //Here we add the shared memory to the array
        // To add together at the end
        // but since I cant get the memory to share
        // the array can't be implemented

        //remove the shared memory segment
        shmctl(segment_id, IPC_RMID, NULL);

        wait(NULL);
    }
} // End of for statement
+5  A: 

The C stdout stream internally buffers data. It's likely that your "this is child" message is being buffered, and the buffer isn't being flushed by execlp, so it just disappears. Try a fflush(stdout); after the printf. Incidentally, you should do this before the fork() as well, so that child and parent don't both try to write output buffered from before the fork.

Doug
Doubly likely to be the problem since the printed output does not include a newline. If the output was line buffered, the newline would force the data to appear.
Jonathan Leffler
Also, this is a good reason for printing diagnostics to stderr - it is not buffered and doesn't get lost so easily.
Jonathan Leffler
A: 

Get rid of all the shared memory stuff first and see if the child process can printf successfully then.

By the look of that snippet, you are initializing shared_memory in the child process, but not in the parent process.

Daniel Earwicker
+1  A: 

Print to stderr it is not buffered.

fprintf(stderr,"Plop\n");

Also the shared memory stuff (segment_id, shared_memory) is not initialized in the parent (or if it is then it is not the same as the child).

Furthermore the parent is potentially destroying the shared memory stuff while the child is still processing. The parent should wait first then process the data produced by the child.

Martin York