Question: I've attempted to complete this program in C regarding forks with no avail. Here's the assignment: Here's the skeleton program: #include #include #include #include #include
I've attempted to complete this program in C regarding forks with no avail. Here's the assignment:
Here's the skeleton program:
#include
#include
#include
#include
#include
void sum1b(); //sum 1 to 1 billion
void sum2b(); //sum 1 billion to 2 billion
int main(){
int status;
pid_t pid;
//timer
struct timeval start, end;
long mtime, seconds, useconds;
pid = fork();
gettimeofday(&start, NULL); //timer
if(pid
fprintf(stderr, "Fork Failed!");
return 1;
}else if(pid == 0){ //child process
sum1b();
}else{ //parent process
sum2b();
wait(NULL);
}
gettimeofday(&end, NULL); //timer
if(pid > 0){
//timer
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
printf("Elapsed time: %ld milliseconds on pid=%d ", mtime, pid);
}
return 0;
}
void sum1b(){
long sum =0;
for(int i=1;i
sum += i;
}
printf("The sum of 1 to 1b is: %ld ", sum);
}
void sum2b(){
long sum =0;
for(int i=1000000000;i
sum += i;
}
printf("The sum of 1b to 2b is: %ld ", sum);
}
Background Fork system call use for creates a new process, which is called child process, which runs concurrently with process (which process called system call fork) and this process is called parent process. After a new child process created, both processes will execute the next instruction following the fork() system call. A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process. It takes no parameters and returns an integer value. Below are different values returned by fork(). Negative: creation of a child process was unsuccessful. 0: Returned to the newly created child process. Positive: Returned to parent or caller. The value contains process ID of newly created child process. More explanation and code examples can be found here: https://www.geeksforgeeks.org/fork-system-call/ Procedure Use the command line to execute programs in the following steps. 1. Start a Linux terminal on your machine. 2. Copy Ex03_serial.c from the Blackboard and compile and execute it from the command line. Record the number of milliseconds your program takes for reference. 4o96 mg 3. Copy Ex03_parallel.c from the class server and compile and execute it from the command line. Record the number of milliseconds on two processes in your program. Ju 79 Question 1: How long that your two programs took to complete the two jobs?! Question 2: What do you find between the execution times of two programs? Why? Serial loro then parallel 4. Can you modify the Ex03_parallel.c from running 2 processes in parallel to run 4 processes in parallel? 1. Modify your sumlb() and sum2b() to sum_10), sum_20), sum_30), sum_40) each add only a quarter of 2 billion. 2. How can you fork() 4 processes in total and assign each process a different task? Question 3: Upload your modified source code to blackboard. Question 4: What if you divide your task from 2 or 4 to 8? Will the 8 processes run 8 times faster? you have 4 cores on your machine in Cheek 151). Background Fork system call use for creates a new process, which is called child process, which runs concurrently with process (which process called system call fork) and this process is called parent process. After a new child process created, both processes will execute the next instruction following the fork() system call. A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process. It takes no parameters and returns an integer value. Below are different values returned by fork(). Negative: creation of a child process was unsuccessful. 0: Returned to the newly created child process. Positive: Returned to parent or caller. The value contains process ID of newly created child process. More explanation and code examples can be found here: https://www.geeksforgeeks.org/fork-system-call/ Procedure Use the command line to execute programs in the following steps. 1. Start a Linux terminal on your machine. 2. Copy Ex03_serial.c from the Blackboard and compile and execute it from the command line. Record the number of milliseconds your program takes for reference. 4o96 mg 3. Copy Ex03_parallel.c from the class server and compile and execute it from the command line. Record the number of milliseconds on two processes in your program. Ju 79 Question 1: How long that your two programs took to complete the two jobs?! Question 2: What do you find between the execution times of two programs? Why? Serial loro then parallel 4. Can you modify the Ex03_parallel.c from running 2 processes in parallel to run 4 processes in parallel? 1. Modify your sumlb() and sum2b() to sum_10), sum_20), sum_30), sum_40) each add only a quarter of 2 billion. 2. How can you fork() 4 processes in total and assign each process a different task? Question 3: Upload your modified source code to blackboard. Question 4: What if you divide your task from 2 or 4 to 8? Will the 8 processes run 8 times faster? you have 4 cores on your machine in Cheek 151)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
