How to handle command line arguments and processes

英国代写code范文精选:“How to handle command line arguments and processes”,这篇论文主要介绍了完整编写一个C++程序的过程,文章指出,一个完整的C++程序需要我们创建多个进程并编辑许多code,而其中的分配更涉及到操作命令行参数以及进程中的简单程序。

 

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,

displayys 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

 

1.This is individual assignment. You are not allowed discuss and/or share your code with anyone.

2.Document your program properly. The program description should explain how to run the program and any other feature about your program.

 

Submission

 

1.Your source codes and the photo log files must be in Prog1 directory under our class directory

2.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;
}

 

51Due作为专业的留学教育辅导机构,专业辅导语言学论文代写硕士paper代写英国matlab作业代写,自2004年至今,坚持以学生为中心,全天候服务,为海外留学生完成了数万篇assignment代写、essay代写、report代写、dissertation代写等论文,以优质的英国代写服务赢得留学生的信赖,如有英国代写code需求或者英国it课程补习需求,欢迎咨询51Due哦。

 

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s