2. Recall lab 10. The C program designed for lab 10 is shown in
the next page. In this C program, a parent process will receive all
the PIDs -- Person IDs found in children processes.
With this C program,
(1) increase the number of processes to 10 (5 points) and the
number of lines read by each child process to 200 (5 points),
and
(2) add C codes printing (e.g., printf) the total number of the
PIDs received from the children. Its output is also presented in
the next page. (50 Points)
Provide a screenshot showing your C program, execution step, and
output.
For question 2, you need test.txt used for the labs.
Submit a doc/pdf file including screenshots showing your C
programs, execution steps and outputs. If the output is not in your
screenshot, 0 will be given.
ref code for question 2
After modifying the C code in question 2, you must show the same
output below. As you see the screenshot below, you need test.txt
for testing.
$ cat lab1e_1.c jkim@DESKTOP-UN6A38S: escisi // Labie for CSCI 315 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #define NUM_THREADS #define BLOCK 5 188 struct thread_arg char *filename; int start; int end; void female_38_58 (struct thread_arg *); int main (int arge, char *argv[]) //pthread_t thread[18]; int err_code, i=8; int p_id, P[2]; char token(1888]; struct thread_arg myArg[NUM_THREADS]; pipe( p); while (1) { printf("In main: creating a process %d\n", i); myArg.filename = argv[1]; myArg.start = BLOCK*i+1; myArg end = BLOCK*(i+1); p_id = fork(); if (p_id < 0) { printf("Cannot create a process. In"); exit(-1); } if (p_id == e) { // child process close(p[@]); dup2(P[1], 1); female_38_5€ (myArg+i); return; i++; if(i>=NUM_THREADS) break; close(P[1]); /* close the fd of the read end of pipe */ dup2(P[@], @); while(scanf("%s", token) != EOF) { printf("Parent says %s\n", token); /* add the suffix string */ fflush(stdout); } wait(NULL); return;
void female_30_50 (struct thread_arg *td) { long int DATA[4]; char comment[256]; int i, n, m, hid, flag; n=; int start, end, lines=8; 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, "%10 %ld %d %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]); < } printf("PIOS-%d\n", n); fclose(rFile); fclose(wFile); jkim@DESKTOP-UN6A38S: /cci3$
jkim@DESKTOP-UN6A385:-/sci31 $ ./final test.txt In main: creating a process o In main: creating a process 1 In main: creating a process 2 In main: creating a process 3 In main: creating a process 4 In main: creating a process 5 In main: creating a process 6 In main: creating a process 7 In main: creating a process 8 In main: creating a process 9 The Sum of person IDs is 356 AFSTAN CASAA
2. Recall lab 10. The C program designed for lab 10 is shown in the next page. In this C program, a parent process will
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
2. Recall lab 10. The C program designed for lab 10 is shown in the next page. In this C program, a parent process will
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!