• Help With Memory Mapping in C
    0 replies, posted
Keeping this concise, my assignment requires me to write a multi-processed program that works with a memory-mapped file containing a string of characters. After the parent process maps the file to memory, it spawns 2 children processes to modify the file. Child 1 outputs the contents of the file, converts the file's contents to their upper case equivalent, then outputs the file's new contents. Child 2 waits 1 second to let child 1 finish, outputs the file's contents, removes any hyphen " - " characters, then outputs the file's new contents. My problem with both child processes is that after first displaying the file's contents, the processes attempt to modify the contents of the file, but neither child outputs the file's new contents. I get no errors when running or compiling so I can't find out what the problem is. And of course, I'm new to memory mapping so feel free to let me know what I'm doing wrong. Here is my source code: /* Anthony Luna CSC 139 T/Th 12:00 - 1:15 */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/mman.h> #include <fcntl.h> #include <sys/stat.h> #include <signal.h> #include <string.h> int main (int argc, char *argv[]) { struct stat buf; int fd, length, status, i, j, k; char *mm_file; char *string = "this is a lowercase-sentence."; length = strlen(string); fd = open(argv[1], O_CREAT | O_RDWR, 0666); [B]//Creates file with name given at command line[/B] write(fd, string, strlen(string)); [B]//Writes the string to be modified to the file[/B] fstat(fd, &buf); //used to determine the size of the file [B]//Establishes the mapping[/B] if ((mm_file = mmap(0, (size_t) buf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (caddr_t) - 1) { fprintf(stderr, "mmap call fails\n"); } [B]//Initializes child processes[/B] pid_t MC0; pid_t MC1; [B]//Creates series of child processes which share the same parent ID[/B] if((MC0 = fork()) == 0) { printf("Child 1 %d reads: \n %s\n", getpid(), mm_file); [B]//{convert file content to uppercase string};[/B] for (i = 0; i < length; i++) { string[i] = toupper(string[i]); } [B]//sync the new contents to the file[/B] msync(0, (size_t) buf.st_size, MS_SYNC); printf("Child 1 %d reads again: \n %s\n", getpid(), mm_file); exit(EXIT_SUCCESS); //Exits process } else if ((MC1 = fork()) == 0) { sleep(1); //so that child 2 will perform its task after child 1 finishes printf("Child 2 %d reads: \n %s\n", getpid(), mm_file); [B]//{remove hyphens}[/B] for (j = 0; j < length; i++) { if (string[i] == '-') { string[i] = ' '; } } [B]//sync the new contents to the file[/B] msync(0, (size_t) buf.st_size, MS_SYNC); printf("Child 2 %d reads again: \n %s\n", getpid(), mm_file); exit(EXIT_SUCCESS); //Exits process } // Waits for all child processes to finish before continuing. waitpid(MC0, &status, 0); waitpid(MC1, &status, 0); return 0; } Then my output is as follows: [B]virtual-machine:~$ ./testt file[/B] Child 1 3404 reads: this is a lowercase-sentence. Child 2 3405 reads: this is a lowercase-sentence. All child processes have finished. Now exiting program. [B]virtual-machine:~$ [/B]But my desire result would be: [B]virtual-machine:~$ ./testt file[/B] Child 1 3404 reads: this is a lowercase-sentence. Child 1 3404 reads again: THIS IS A LOWERCASE-SENTENCE. Child 2 3405 reads: THIS IS A LOWERCASE-SENTENCE. Child 2 3405 reads: THIS IS A LOWERCASE SENTENCE. All child processes have finished. Now exiting program. [B]virtual-machine:~$[/B] Any help is greatly appreciated.
Sorry, you need to Log In to post a reply to this thread.