Question: WRITE CODE IN C Fork system call use for creates a new process, which is called child process , which runs concurrently with process (which
WRITE CODE IN C
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.
Procedure
Use the command line to execute programs in the following steps.
- Start a Linux Subsystem terminal on your machine.
- Copy asgn1_serial.c from the class server and compile and execute it from the command line. Record the number of milliseconds your program takes for reference.
- Copy asgn1_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.
- Modify the asgn1_parallel.c from running 2 tasks in parallel to run 4 tasks in parallel. (refer to the Ex03_parellel_sol.c provided in last week.)
- Modify your sum1b() and sum2b() to sum_1(), sum_2(), sum_3(), sum_4() each add only a quarter of 2 billion.
- How can you fork() 4 processes in total and assign each process a different task?
- 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.
parallel_sol.c:
#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); }
serial.c:
#include#include #include #include #include void sum1b(); //sum 1 to 1 billion void sum2b(); //sum 1 billion to 2 billion long totalsum; int main(){ //timer struct timeval start, end; long mtime, seconds, useconds; gettimeofday(&start, NULL); //timer sum1b(); sum2b(); gettimeofday(&end, NULL); //timer printf("Total Sum is: %ld ", totalsum); //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 ", mtime); } void sum1b(){ long sum =0; for(int i=1;i<1000000000;i++){ sum += i; } totalsum += sum; printf("The sum of 1 to 1b is: %ld ", sum); } void sum2b(){ long sum =0; for(int i=1000000000;i<2000000000;i++){ sum += i; } totalsum += sum; printf("The sum of 1b to 2b is: %ld ", sum); }
parallel.c
#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 < 0){ //fork failed 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<1000000000;i++){ sum += i; } printf("The sum of 1 to 1b is: %ld ", sum); } void sum2b(){ long sum =0; for(int i=1000000000;i<2000000000;i++){ sum += i; } printf("The sum of 1b 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
Get step-by-step solutions from verified subject matter experts
