Help Session 2 Programming Assignment 2
Outline Learn how to use the following fork() execlp() wait() system() File operations
Assignment Information Two executables will be needed ParentProgram Main program, that opens, reads and closes the file, forks child processes. Executor Executes the command received as an argument using system() and returns the status of the execution.
ParentProgram.c Can take one or no command line arguments. The argument if passed should be the name of the text file containing a list of commands to be executed. The objective is the ParentProgram.c should open the file provided in the argument, or the default file textfile.txt, and read the content of the file as a single string, and close the file. After that, it should parse the string into separate commands, and for each command, should fork a separate child process which runs Executor.c
fork()
fork() Generates an exact copy of parent process except for the value it returns In a child process, fork() returns zero In the parent process it will return the child s process ID Any process can retrieve its process ID with getpid(), and its parent process ID with getppid() Syntax: pid_t fork();
wait() Makes Initiator wait until the child has been entirely executed Use WIFEXITED() to check whether child process has completed its execution Use WEXITSTATUS() to retrieve return value of child process Syntax pid_t wait(int* stat_loc);
execlp() Executes a new program within a child process Arguments passed the name of the executable and filename like./executor, Executor.c Also pass any needed command line arguments as parameters Terminate list of arguments with NULL Syntax int execlp(./executable_name, Filename, const char *arg,..., NULL);
system() This is a function available to execute the Operating System commands. The function is obviously Operating System dependent. Syntax: int system(const char* command);
Demo 1
File Operations We need three functions for this particular assignment. They are: fopen() fgets() fclose()
fopen() Used to open a file, whose name is given as the argument. It returns a pointer to the opened file. Syntax: FILE * fp = fopen(const char *filename, const char *mode)
fgets() It is used to read content from a file. Syntax char *fgets(char *str, int n, FILE *stream) It reads n-1 characters, till the newline character is encountered, or til end of line character, whichever comes first.
fclose() Closes the stream to the file. Buffers are flushed. Syntax int fclose(file *stream)
Demo 2
Project Choices Teams: Team sizes of 2 or 3. You choose. All of them must put in their share of effort. Choice A: Research a technical topic of current interest. A list of topics will be available. The topic needs to be pre-approved. Choice B: An embedded/iot development project. Research the aim, platform, obtain hardware/software. Need a critical design evaluation and a demo.
Project Deadlines 1. Team composition and idea proposal, Tu 2/20/18 2. Progress report Tue 3/27/18 3. Preliminary report (slides/posters* as needed) Tue 4/24/18 4. Final report (and demos* for Option B) Tue 5/1/18 Note: Posters and demos will be scheduled later depending on other departmental events scheduled. Dates subject to some adjustment.
Option A: Research a technical topic A list of topics will be available Must be of current interest and related to operating systems You will research using in-depth research/industrial publications and news articles. You will learn about the state of the art and current technological trends. A formal report is the specified format will be needed. A presentation using slides/poster will also be needed.
Option B: An embedded/iot system Project must involve a platform (such as a Raspberry Pi) which will communicate with at least one other computer and with at last one sensor. You will specify the project objective, select the appropriate platform, hardware/software needed. Your proposal must include a justification of the choices. After you have built your system, will evaluate for at least one attribute (potential security holes, power consumption, available resolution or reliability) A report, a demonstration, and a set of slides will be needed.