Operating System代写:CS3270 Process

练习操作系统中多进程的用法,操作系统的入门级作业。

Purpose

In this assignment, you will write a complete C++ program that creates multiple processes.

Assignment

This assignment deals with simple programs that manipulate command-line arguments and processes. In particular, you will learn how to use command-line arguments and study the relationship between parent and child processes.

Part 1. Command-line Arguments

Source code file name: prog1_01_lastname.cpp (or prog1_01_lastname.c)
You can pass in command-line arguments to your C/C++ program using the following arguments in the main function.
Write a C/C++ program that accepts “LastName” as the 1 st argument, which needs to be displayed when program runs, and a set of numbers that the program will use to find N th’s primer numbers. Specifically, the numbers passed as arguments will be the indices of the prime numbers. For example,

To compile, g++ -o prog1_01 prog1_01_lee.cpp
To run, ./prog1_01 LEE 7 11 14
Output,
     Last name is LEE
     7 th prime number is 13
    11 th prime number is 29
    14 th prime number is 41

You program should not assume how many arguments would be passed to the program.

Part 2. fork

Source code file name: prog1_02_lastname.cpp (or prog1_02_lastname.c)
(Make a copy of the source code for Part 1 and work on the followings.)
Write a C/C++ program that uses the same arguments passed the program as Part 1 and creates a child process to perform different computations as the parent process. Specifically, the program:

  1. Reads in the arguments,
  2. Calculates a set of prime numbers given the arguments,
  3. Store the prime numbers in a dynamically-allocated array,
  4. Create a child process using fork(),
  5. Parent process displays its id and the child process’s id, (use getpid() for process id)
    displays the prime numbers,
    displays the sum of the prime numbers,
  6. Child process displays its id, (use getpid() for process id)
    adds each prime number’s index to each prime number in the array,
    displays the numbers in the array,
    displays the sum of the numbers in array
To compile, g++ -o prog1_02 prog1_02_lee.cpp
To run, ./prog1_02 LEE 7 11 14
Output,
     Last name is LEE
     7 th prime number is 13
    11 th prime number is 29
    14 th prime number is 41

    In Parent process,
    I am the parent; my ID = 12959
    I am the parent; my child ID = 12960
    I am the parent; Prime numbers = 13, 29, 41,
    I am the parent; Sum = 83

    In Child process,
    I am the child; my ID = 12960
    I am the child; Numbers = 20, 40, 55,
    I am the child; Sum = 115

Creating outputs .photo

Use the photo command to create log files for each program run/output.
For Part 1, photo_prog1_01_lastname.txt
For Part 2, photo_prog1_02_lastname.txt

Notes

  • This is individual assignment. You are not allowed discuss and/or share your code with anyone.
  • Document your program properly. The program description should explain how to run the program and any other feature about your program.

Submission

  • Your source codes and the photo log files must be in Prog1 directory under our class directory
  • Hardcopy of the source codes and the log files must be submitted with a separate cover page (MUST BE STAPLED)

fork example in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
#include <unistd.h> // for fork()
...
int main (int argc, char **argv)
{

...
pid_t pid;
...
pid = fork();
if (pid == 0) // child process
{
... // child process's processing
}
else if (pid > 1) // parent process
{
... // parent process's processing
}
else // (pid < 0) process creation failed
{
printf("fork() failed\n");
return 1;
}
...
return 0;
}