c语言代写 Assignment 4 COMP 2401

Assignment 4 COMP 2401
Date: November 26, 2018

Due: on December 7, 2018 before 23:55 Submission: Electronic submission on cuLearn.

Objectives:

  1. Reading from binary files: opening and closing a file (fopen() and fclose(), reading from a file (fread())
  2. Process spawning: differentiating between parent and child processes (fork()), examining the return code of the child process(), waiting for a child process (wait() and waitpid())
  3. Program Morphing – changing from one program to another (excevp())
  4. Inter process communication – using signals and return code
  5. Usage of signals – signals and function pointers.

Submission (10 pts.)

  •   Submission must be in cuLearn by the due date.
  •   Submit a single tar file with all the c and h files. The tar file name is A4.tar
  •   The tar file should include all programs and code (code that you wrote and code that was provided as part of the assignment)
  •   Submit a Readme.txt file explaining

o Purpose of software

o Who the developer is and the development date
o How the software is organized (partitioned into files)
o Instruction on how to compile the program (give an example) o Any issues/limitations problem that the user must be aware of o Instructions explaining how to use the software

Coding Instructions:

  1. Comments in Code – as provided in the slides given in class
  2. No usage of global variables. All data must be passed or received via function parameters except when specifically stated.

Provided Resources

The following files are provided in A4.tar
©2018 Doron Nussbaum COMP 2401 Page1 of 6

  1. isPrime.c – the base program for checking whether a number is a prime number
  2. prime.bin – a file that contains 12 unsigned numbers in binary format
  3. prime.txt – a file that contains 12 unsigned numbers in ASCII format. The numbers correspond to the numbers in the file prime.bin
  4. createBinary.c – a program that creates a binary file from a given set of numbers. Assuming that the executable is a.out the usage is: a.out filename num1 num2 etc. where filename is the binary file name, num1 is the first unsigned number num2 is the second unsigned number etc.

Grading:

Assignmentisgradedoutof100. Note:

1. that the grade may be reduced to 0 if the correct file names are not used. Part or all the assignment may be tested by another program.

2. You can earn more than 100 points in this assignment.

You will be graded with respect to what you have submitted. Namely, you earn points for working code and functionality.

The grading will look at how the functions are coded, whether functions meet the required functionality, memory allocation and de-allocation, setting pointers to NULL as needed, etc.

1 Background

You are tasked to write a program that accepts a set of unsigned integers and determines which numbers are prime numbers. In order to complete the tasks you are given a program (isPrime.c), which accepts a single unsigned integer as a command line parameter and return:

0 if the number is not a prime number

1 if the number is a prime number

2 if the command line argument did not include a number

The given program assumes that the input, if provided to the program, is correct and therefore the program does not require to check whether the input is an unsigned integer.

Not all programs can fully utilize the computation power of the CPU. Often a program is required to wait for slow resources (e.g., reading from or writing to a disk).

The assignment mimics such programs by artificially inserting wait time during execution. See the function usleep(x) that puts the program in a sleep mode for x micro seconds 0.000001 seconds. In this program x=100 (0.1 milliseconds or 0.0001 seconds in each for loop). This allows the assignment to present the benefit of task parallelization or task distribution among multiple processes.

This assignment is a progressive assignment. Namely, it is divided into several tasks each building on the previous one. In each of the first three tasks (Task 1 – Task 3), you are asked to create a program (including a makefile). Thus, once you completed the code for one task you can use it (by duplicating it) and expanding it in order to complete the next task.

2 Task 0 Understanding the base code (0 points)

In this task you will understand the prime testing program that was provided.
1. Review the program isPrime.c. Make sure that you understand the program and how it works. Do the

©2018 Doron Nussbaum COMP 2401 Page2 of 6

following:
1.1. Compile the program and create an executable called isPrime. 1.2. Test the program as follows:

  1. 1.2.1.  Test 1 isPrime 1535068679
  2. 1.2.2.  Test 2 isPrime 1535068677
  3. 1.2.3.  Test 2 isPrime 39821

Since you will not see any output use the debugger to ensure that your code is working by putting breakpointsateachofthereturn()functioncalls. Alternatively,youcantemporarilyaddaprintstatement indicating whether the number was a prime number of note. This can assist you in testing Task I.

2.

3

Compile the file and create an executable program called isPrime.

Task I Morphing (40 points)

Task overview

In this task you will write a program that will morph itself into the isPrime program.

advantage of the isPrime program from Task 0.
You can assume that if the file exists then it contains at least one unsigned integer

Here you will take

The program that you write will read from a binary file the first unsigned integer and then morph itself to the isPrime program. The file name will be given as a command line argument.

To do

  1. (10 pts.) Create a program singlePrime that accepts a binary file name as a single command line parameter. Here you can use the tutorial tools that you developed. Name the code file singlePrime.c,
    1. 1.1.  (5 pts.) The program will check that a file name was provided as command line argument (hint use the value of argc to determine it). If no file was provided the program will print how to use the program – “Usage: singlePrime filename”. See below for correct program return code.
    2. 1.2.  (5 pts.) The program will check if the file exists. If the file does not exist then the program should produce an error message: “file file.bin does not exist”, where file.bin is the provided file name. See below for correct program return code.
  2. (10 pts.) Create a function morph() which will take as input a string (the input number) and morphs the program to the isPrime program using the excev or execvp system function call.
    1. 2.1.  Prototype int morph(char *number);
    2. 2.2.  Input: number – the unsigned number to be checked
    3. 2.3.  Return – -1 if the morph failed
  3. (10 pts.) If the binary file exists then the program should read the first unsigned integer from the file, convert it to a string using the function sprintf() and then call the morph function that you created.
  4. (5 pts.) Program return codes:
    4.1. 0 – if the input number is not a prime number 4.2. 1 – if the input number is a prime number 4.3. 2 – if file name was not provided
    4.4. 3 – if the file does not exist
  5. (5 pts.) Create a makefile to compile the program. The make file name should be Makefile1. The executable program name should be singleMorph.

©2018 Doron Nussbaum COMP 2401 Page3 of 6

6. Test your program. Here you are provided with: a. a binary file prime.bin which contains 12 unsigned integers in a binary format. b. a utility to create a binary file (the utility is createBinary.c).

4 Task II Spawning a single child (30 points)

Task overview

In this task you will write a program that will create a manger-worker relation by spawning a child process. The manager will be the parent process and the worker will be the child process. The child process will morph itself into the isPrime program. The parent program will wait until the child program completes its task. The parent process will read the return code of the child process and then based on the return code will print whether the given input number is a prime number.

To do

  1. (5 pts.) Copy the program from Task I into a file with the name singleSpawn.c. If you added a print statement to the isPrime program (see Task 0) then remove it.
  2. (10 pts.) Expand the program to so that the program shall spawn a single child. The child should morph itself into the isPrime program using the morph() function from Task I. Note, that the program should conduct the same checks for the binary file as it did not Task I.
  3. (10 points) The parent program should wait until the child process has completed its execution and then, using the return code from the child process, prints whether the input number is a prime number or not a prime number.
    3.1. Here you will have to use the wait() function.
  4. (5 points) Create a makefile to compile the program. Makefile name should be Makefile2. The executable program name should be singleSpawn. I

5 Task III Spawning multiple children (55 points)

Task overview

In this task you will write a program that will spawn multiple child processes. Each child process will morph itself into the isPrime program. The parent program will collect the results from each child process. Then when all the child processes have terminated (have completed their work), the parent process will only print the prime numbers.

You can assume that the binary file (if it is exists) contains at least 10 unsigned int numbers. For simplicity the program will need to process only the first 10 numbers. You can also obtain additional points by doing the bonus section in this task.

To do

  1. (5 points) Copy the program from Task II into a file with the name multiSpawn.c.
  2. Reading data from file (10 pts.) – The program shall read the first 10 numbers into an array of unsigned integers using a single call to fread().

©2018 Doron Nussbaum COMP 2401 Page4 of 6

  1. (20 points) The program should spawn a child process for each of the input numbers. Each child should morph itself into the isPrime program using the function from Task I. Here the program needs to “remember” which number was assigned to each of the child processes. The simplest way of handling it is to create another array of 10 integers, e.g., childProcessIds[10], and then store the child process id of each spawned child in the correspond array location. For example, assume that the numbers, which were read from the file, are stored in the array numbers[10]. Thus, when the program spawns the third child then the program will assign the child the third number (i.e., numbers[2]), and store the child process id (cpid) in childProcessIds[2]. 3.1. The program needs to spawn all child processes before it starts to collect the responses from the child processes. This can be accomplished using a for loop
    Pseudo code (assuming that manager want to have k workers) create a for loop where i goes from 0 to k-1 { fork a process
    if it is a child process then morph using the number in numbers[i] if it is a parent process then store the child process id in childProcessIds[i]
    The parent code after the spawning to collect the information from the children (see next step below)
  2. (25 points) The parent program should only print the prime numbers in the given input. This will be done by examining the return code from the child processes. Note that if the child processes are not spawned before the program starts to collect the replies from the children then the maximum for this section is 20.
    1. 4.1.  (5 points) Here you will have to use the waitpid() function, which waits for child processes to complete their tasks. You will invoke the function as waitpid(-1, &status, 0) where -1 indicates wait for any child process to complete their task.
    2. 4.2.  (15 points) In order to complete this step the program uses the array of child process ids that were collected in step 3 above. Then when the waitpid() returns the child process id, cpid, the parent program search for cpid in the array childProcessIds and prints the corresponding integer if it is a prime number.
    This can be accomplished using a while loop
    Here the parent process collects the return codes from the child processes

}

©2018 Doron Nussbaum COMP 2401 Page5 of 6

Pseudo code
While there are more children {

cpid  waitpid()
check the return code of the child process
if the return code is 1 (which means a prime number

indexthe location of cpid in the array childProcessIds print the number in numbers[index]

}

4.3. (5 points) The parent program also needs to know when to quit. This can be done in two ways a. check the return code from waitpid(); If it is -1 then there are no more children or an error has occurred. In this case the program can quit. B. count how many times it has received input from the children and quit once argc-1 children have responded.

  1. (5 points) Create a make file for the program. Makefile name should be Makefile3. The program name should be multiSpawn
  2. Bonus Section (15 points)

6.1. Allocating memory
In Part 3 and 4 above you processed only the first 10 numbers in the file prime.bin. Here instead of hard coding the number of integers to be processed (e.g., 10) the program will determine how many numbers are in the file then allocate memory for the array of numbers and array of process ids. Determining how many numbers are contained in the binary file is accomplished by determining the file size: a. moving the file pointer to the end of the file using fseek() and then b. determining the file size using ftell().

Bonus Task IV Signals (15 points)

In this task you will write enhance the program in Task III to use a simple signal SIGUSR1

  1. Add a counter to your program from Task III, which tracks/collects the number of processes that have responded so far. The counter will be a global variable.
  2. If signal SIGUSR1 is invoked then the program should print how many processes have finished their execution and how many processes are still working. Note, that in order to accomplish this you will need to use global variables within the file scope. For example you can either declare it as int countFinished or int countFinished. You will also need to keep as a global variable the total number of child processes that were launched. Otherwise you will not be able to complete access the information from the interrupt function.
  3. Create a make file for the program. Makefile name should be Makefile4. The program name should be multiSpawnSignal

©2018 Doron Nussbaum COMP 2401 Page6 of 6