Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<unistd.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<sys/wait.h>
- #define NUM_THREADS 10
- #define BLOCK 200
- struct thread_arg{
- char *filename;
- int start;
- int end;
- };
- int x = 0;
- void female_30_50(struct thread_arg *);
- int main (int argc, char *argv[]){
- int err_code, i=0, p_id, p[2], counter=0;
- char token[1000];
- struct thread_arg myArg[NUM_THREADS];
- pipe(p);
- while (1) {
- printf("In main: creating thread %d\n", i);
- myArg[i].filename = argv[1];
- myArg[i].start = BLOCK*i+1;
- myArg[i].end = BLOCK*(i+1);
- p_id = fork();
- if (p_id < 0){
- printf("Cannot create a process.\n");
- exit(-1);
- }
- if(p_id==0){
- close(p[0]);
- dup2(p[1],1);
- female_30_50(myArg+i);
- return 0;
- }
- i++;
- if(i>=NUM_THREADS) break;
- }
- close(p[1]); /* close the fd of the rear end of the pipe */
- dup2(p[0],0);
- while(scanf("%s", token) != EOF){
- printf("Parent says %s\n", token); /* add the suffix string */
- fflush(stdout);
- }
- wait(NULL);
- return 0;
- }
- void female_30_50(struct thread_arg *td){
- long int DATA[4];
- char comment[256];
- int i, n, m, hid, flag;
- n=0;
- int start, end,lines =0;
- FILE *rFile, *wFile;
- rFile=fopen(td->filename, "r");
- wFile=fopen("output.txt", "w");
- start = td->start;
- end = td->end;
- fgets(comment, 256, rFile);
- while(!feof(rFile)){
- lines++;
- fscanf(rFile, "%ld %ld %ld %ld", DATA, DATA+1, DATA+2, DATA+3);
- if(lines >= start && lines <= end){
- if(DATA[3] == 2 && DATA[2]>=30 && DATA[2]<=50){
- n++;
- fprintf(wFile, "%ld", DATA[1]);
- }
- }
- }
- x = x + n;
- printf("%d\n", n);
- fclose(rFile);
- fclose(wFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement