c - forking processes, reading a file but not showing its content -
i have following code have enter 2 arguments input where: 1st argument number. if number multiple of 2, 15 child processes created fork. if number multiple of 3, 10 processes created. if number multiple of 5, 7 processes created. 2nd argument file size (in bytes) divided number of processes created , each child process going read 1 part of file, save part variable , parent shows text in variable. example, run program ./p 5 /home/directoryfile.c. i'm having 7 child processes , let's filesize 700 bytes. means every child process should read 100 bytes, save variable (appending content) , parent shows content together. problem variable texttosend should show content in parent doesn't show anything... believe there should problem sprintf line in child.
//gcc test.c -o p //./p 5 /home/directoryfile.c #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <dirent.h> #include <sys/wait.h> int main(int argc, char *argv[]) { int studentid, children = 0, j, i, childnumber[15], fdfile, fdread; float bytestoread; char directory[50]; char *buffer = malloc(256), *texttosend = malloc(256); system("clear"); if(argc-1 < 1) { printf("\nsome arguments missing\n"); return exit_failure; } studentid = atoi(argv[1]); strcpy(directory,argv[2]); if((studentid%2) == 0) { children = 15; } else { if((studentid%3) == 0) { children = 10; } else { if((studentid%5) == 0) { children = 7; } else { printf("\nstudentid not multiple of 2, 3 o 5\n"); return exit_failure; } } } struct stat fileinfo; stat(argv[2],&fileinfo); bytestoread = fileinfo.st_size / children; printf("children: %d\n",children); printf("file size: %lld\n",(long long int) fileinfo.st_size); printf("bytes: %.2f\n",bytestoread); fdfile = open(directory,o_rdonly); if(fdfile == -1) { printf("\nerror opening fileo\n"); return exit_failure; } for(i=0;i<children;i++) { childnumber[i] = fork(); if(childnumber[i] == -1) { printf("\nerror creating child process\n"); return exit_failure; } if(childnumber[i] == 0) { fdread = read(fdfile,buffer,bytestoread); if(fdread == -1) { printf("\nerror reading file\n"); return exit_failure; } printf("%s",buffer); //printf("\n\n------------------------\n\n"); sprintf(texttosend,"%s%s",texttosend,buffer); return exit_success; } else { //waitpid(childnumber[i],null,wnohang); } } printf("\nthis content of file: %s\n",texttosend); close(fdfile); for(j=0;j<children;j++) { wait(null); } return exit_success; }
what happen? buffer showing part of file correctly... sprintf function?
you using sprintf
print local buffer not shared among processes.
when fork
child receives full private copy of of parents memory private. memory private default unless allocated shared.
if allocate shared
char * texttosend = mmap(null, 256, prot_read | prot_write, map_anonymous | map_shared, -1, 0);
then have same memory accessible processes.
this not account fact have tell other processes start of buffer has moved, require shared variable , synchronised access. @ moment not appending text sprintf
, overwriting previous content.
in end if wish pool results different processes may want use pipes (pipe(2)
) rather shared memory.
Comments
Post a Comment