Question
For the program given written in C, collect the final result of adding 1 to 2 billion by using shared memory among 4 processes. You
For the program given written in C, collect the final result of adding 1 to 2 billion by using shared memory among 4 processes.
- You can refer to the sample code (ipcsm.c) discussed in class for reference.
- Step 1: Create a shared memory space (4*8=32 bytes) for holding 4 partial sum (long)
- Step 2: Every process calculates the partial sum and writes the result on a location on the created shared memory without conflicting with each other.
- Step 3: After all the processes finished their calculation, the master process collects all the partial results and deliver the final result to the terminal.
Program given:
#include
#include
#include
#include
#include
void sum1b(); //sum 1 to 1 billion
void sum2b(); //sum 1 billion to 2 billion
void sum3b(); //sum 1 to 1 billion
void sum4b(); //sum 1 billion to 2 billion
int main(){
int status;
pid_t pid1 = fork();
//timer
struct timeval start, end;
long mtime, seconds, useconds;
gettimeofday(&start, NULL); //timer
if(pid1 < 0){ //fork failed
fprintf(stderr, "Fork Failed!");
return 1;
}else if(pid1 == 0){ //child process
pid_t pid2 = fork();
if(pid2==0){
pid_t pid3 = fork();
if(pid3==0){
sum4b();
}else{
sum3b();
wait(NULL);
}
}else{
sum2b();
wait(NULL);
}
}else{ //parent process
sum1b();
wait(NULL);
gettimeofday(&end, NULL); //timer
//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, pid1);
}
return 0;
}
void sum1b(){
long sum =0;
for(int i=1;i<500000000;i++){
sum += i;
}
printf("The sum of 1 to 0.5b is: %ld ", sum);
}
void sum2b(){
long sum =0;
for(int i=500000000;i<1000000000;i++){
sum += i;
}
printf("The sum of 0.5b to 1b is: %ld ", sum);
}
void sum3b(){
long sum =0;
for(int i=1000000000;i<1500000000;i++){
sum += i;
}
printf("The sum of 1 to 1.5b is: %ld ", sum);
}
void sum4b(){
long sum =0;
for(int i=1500000000;i<2000000000;i++){
sum += i;
}
printf("The sum of 1.5b to 2b is: %ld ", sum);
}
ipcsm.c:
#include
#include
#include
#include
#include
#include
#define SHM_SIZE 1024 /* make it a 1K shared memory segment */
//display sharedMemory: ipcs -m
//remove sharedMemory: ipcrm -M 0x0000162e
int main(int argc, char *argv[])
{
key_t key;
int shmid;
char *data;
int mode;
if (argc > 2) {
fprintf(stderr, "usage: shmdemo [data_to_write] ");
exit(1);
}
/* make the key: */
if ((key = ftok("ipcsm.c", 'R')) == -1) /*Here the file must exist */
{
perror("ftok");
exit(1);
}
/* create the segment: */
if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
/* attach to the segment to get a pointer to it: */
data = shmat(shmid, (void *)0, 0);
if (data == (char *)(-1)) {
perror("shmat");
exit(1);
}
/* read or modify the segment, based on the command line: */
if (argc == 2) {
printf("writing to segment: \"%s\" ", argv[1]);
strncpy(data, argv[1], SHM_SIZE);
} else
printf("segment contains: \"%s\" ", data);
/* detach from the segment: */
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started