CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

Similar documents
This homework is due by 11:59:59 PM on Thursday, October 12, 2017.

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 4 (document version 1.0) Network Programming using C

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 4 (document version 1.1) Sockets and Server Programming using C

CSCI 4963/6963 Large-Scale Programming and Testing Homework 1 (document version 1.0) Regular Expressions and Pattern Matching in C

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Project 1 (document version 1.3) Process Simulation Framework

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Project 2 (document version 1.4) Contiguous and Non-contiguous Memory Management

Project #1: Tracing, System Calls, and Processes

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Assignment 1. CSI Programming Practice, Fall 2011

1. We have a code sequence that is potentially executed twice.

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lecture 7: file I/O, more Unix

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Midterm Exam Questions (document version 1.1)

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Final Exam Questions (document version 1.1) WITH SELECTED SOLUTIONS

Project 3a: Malloc and Free

pthreads CS449 Fall 2017

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CAAM 420 Notes Chapter 2: The C Programming Language

Intermediate Programming, Spring 2017*

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Operating System Labs. Yuanbin Wu

Programming Tips for CS758/858

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

My malloc: mylloc and mhysa. Johan Montelius HT2016

CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications

CSCI 2200 Foundations of Computer Science (FoCS) Homework 6 (document version 1.0) This homework is due by 11:59:59 PM on Friday, March 23, 2018.

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

Lecture 8: Structs & File I/O

Creating a Shell or Command Interperter Program CSCI411 Lab

Intermediate Programming, Spring 2017*

CS 105, Spring 2015 Ring Buffer

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Programming Assignment 4 (PA4) - myxd Milestone Due: Final Due:

Computer Programming: Skills & Concepts (CP) Files in C

Homework 2 - KW26 - using 40 hexadecimal digits

Friday, February 10, Lab Notes

CSE 303, Winter 2007, Midterm Examination 9 February Please do not turn the page until everyone is ready.

CS 322 Operating Systems Programming Assignment 2 Using malloc and free Due: February 15, 11:30 PM

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Figure 1 Ring Structures

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Cosc 242 Assignment. Due: 4pm Friday September 15 th 2017

Kurt Schmidt. October 30, 2018

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Final Exam

CSC209 Review. Yeah! We made it!

Project 2 Overview: Part A: User space memory allocation

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Operating System Labs. Yuanbin Wu

Recitation: C Review. TA s 20 Feb 2017

Programming Assignment 1 (PA1) - Display Bowtie

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Course Information and Introduction

COMP 321: Introduction to Computer Systems

1. Introduction. 2. Project Submission and Deliverables

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CS 385 Operating Systems Fall 2013 Homework Assignment 2 Inter-Process Communications and Synchronization

CSE 374 Midterm Exam 11/2/15. Name Id #

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions.

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Process Turnaround Time Total Wait Time P 1 12 ms 0 ms P 2 21 ms 12 ms P 3 23 ms 18 ms P 4 20 ms 17 ms

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Programming in C First meeting Tiina Niklander

Debugging! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 5!

Problem Set 1: Unix Commands 1

System calls. Reading from a file. Closing a file

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Project 2 (document version 1.4) CPU Scheduling Algorithms

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

Lecture 9: Potpourri: Call by reference vs call by value Enum / struct / union Advanced Unix

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Parallel and Distributed Computing Programming Assignment 1

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

File Access. FILE * fopen(const char *name, const char * mode);

SPRING 2017 CSCI 304 LAB1 (Due on Feb-14, 11:59:59pm)

CSCI0330 Intro Computer Systems Doeppner. Project C-Shell. Due: November 1, 2017 at 11:59pm

Recitation: Cache Lab & C

CS 105, Spring 2007 Ring Buffer

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

Homework 1 - Extracting Data from a CSV file

Computational Methods of Scientific Programming Fall 2007

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Lecture 19 Notes Data Structures in C

CS 211 Programming Practicum Spring 2017

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

CSCI341. Lecture 22, MIPS Programming: Directives, Linkers, Loaders, Memory

Programming Assignment #3 Event Driven Simulation

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

Programming in C week 1 meeting Tiina Niklander

Transcription:

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads Overview This homework is due by 11:59:59 PM on Tuesday, April 10, 2018. This homework will count as 8% of your final course grade. This homework is to be completed individually. Do not share your code with anyone else. You must use C for this homework assignment, and your code must successfully compile via gcc with absolutely no warning messages when the -Wall (i.e., warn all) compiler option is used. We will also use -Werror, which will treat all warnings as critical errors. Your code must successfully compile and run on Submitty, which uses Ubuntu v16.04.3 LTS. Note that the gcc compiler is version 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.5). Remember, to compile your code, use -pthread to include the Pthread library. Homework Specifications In this third assignment, the goal is to work with threads and focus on synchronization. You will use C and the POSIX thread (Pthread) library to implement a single-process multi-threaded system that solves the knight s tour problem from Homework 2. As with Homework 2, your program investigates whether a valid solution is possible for the knight s tour problem on an m n board. In this assignment, you must keep track of a global variable called max_squares that maintains the maximum number of squares covered by the knight. Also, a global shared array called dead_end_boards is used to maintain a list of dead end board configurations. Child threads add their detected dead end boards to this array, which therefore requires proper synchronization. As with Homework 2, your program simulates valid moves. And for a given board configuration, when multiple moves are detected, each possible move must be assigned to a new child thread, thereby forming a thread tree of possible moves. A valid move constitutes relocating the knight two squares in direction D and then one square 90 from D, where D is up, down, right, or left. Key to this problem is the restriction that a knight may not land on a square more than once in its tour. Also note that the knight starts in the upper-left corner of the board. When a dead end is encountered (i.e., no more moves can be made), the leaf node thread compares the number of squares covered to the global maximum, updating the global maximum, if necessary. Once all child threads have terminated, the main thread reports the number of squares covered, which is equal to product mn if a full knight s tour is possible. The main thread also displays either all of the dead end boards or all dead end boards with at least k squares covered, where k is an optional (third) command-line argument.

Dynamic Memory Allocation As with the previous two homework assignments, your program must use calloc() to dynamically allocate memory for the m n board. More specifically, use calloc() to allocate an array of m pointers, then for each of these pointers, use malloc() or calloc() to allocate an array of size n. Of course, your program must also use free() and have no memory leaks. Note that you do not need to use realloc() for this assignment. Given that your solution is multi-threaded, you will need to be careful in how you manage your child threads and the board; i.e., you will need to allocate (and free) memory for each child thread that you create. Command-Line Arguments and Error Handling There are two required command-line arguments; both are integers n and m, which together specify that the size of the board is m n, where m is the number of rows and n is the number of columns in the board. As noted above, a third optional command-line argument, k, indicates that the main thread should display all dead end boards with at least k squares covered. Validate the inputs m and n to be sure both are integers greater than 2. Further, if present, validate input k to be sure it is a positive integer no greater than m n. If invalid, display the following error message to stderr: ERROR: Invalid argument(s) USAGE: a.out <m> <n> [<k>] If an incorrect number of command-line arguments is given, display the above error message to stderr, then return EXIT_FAILURE. Handling System Call Errors In general, if a system call fails, use perror() to display the appropriate error message on stderr, then exit the program and return EXIT_FAILURE. If a system or library call does not set the global errno, use fprintf() instead of perror() to write an error message to stderr. See the various examples on the course website and corresponding man pages. Note that error messages must be one line only and use the following format: ERROR: <error-text-here> 2

Program Execution To illustrate using an example, you could execute your program and have it work on a 3 3 board as follows: bash$./a.out 3 3 This will generate the thread tree shown below, with <k> indicating the current position of the knight. For clarity on the order of moves, this diagram also shows the order in which each knight visits each square. <k> k1 k6 k3 k1 k4 k7 k4 <k> k6 k2 k7 k2 k5 k3 <k> k5 Note that the center square is not visited at all in this example. Also note that each of the two dead end boards would be added to the global shared array and displayed by the main thread once all child threads have completed. 3

Required Output When you execute your program, you must display a line of output each time you create a new thread and each time you encounter a dead end. Below is example output to illustrate the required output format. In this example, thread ID (tid) 1000 is the top-level main thread, with threads 1001 and 1002 being child threads to thread 1000. bash$./a.out 3 3 THREAD 1000: Solving the knight's tour problem for a 3x3 board THREAD 1000: 2 moves possible after move #1; creating threads THREAD 1001: Dead end after move #8 THREAD 1002: Dead end after move #8 THREAD 1000: Best solution found visits 8 squares (out of 9) THREAD 1000: > kkk THREAD 1000: k.k THREAD 1000: kkk THREAD 1000: > kkk THREAD 1000: k.k THREAD 1000: kkk Match the above output format exactly as shown above, though note that the tid values will vary. Further, interleaving of the output lines may occur, though the first and last lines must be first and last, respectively. 4

Submission Instructions To submit your assignment (and also perform final testing of your code), please use Submitty, the homework submission server. The specific URL is on the course website. Note that this assignment will be available on Submitty a few days before the due date. Please do not ask on Piazza when Submitty will be available, as you should perform adequate testing on your own Ubuntu platform. That said, to make sure that your program does execute properly everywhere, including Submitty, use the techniques below. First, as discussed in class (on 1/18), output to standard output (stdout) is buffered. To ensure buffered output is properly flushed to a file for grading on Submitty, use fflush() after every set of printf() statements, as follows: printf(... ); /* print something out to stdout */ fflush( stdout ); /* make sure that the output is sent to a */ /* redirected output file, if specified */ Second, also discussed in class (on 1/18), use the DEBUG_MODE technique to make sure you do not submit any debugging code. Here is an example: #ifdef DEBUG_MODE printf( "the value of x is %d\n", x ); printf( "the value of q is %d\n", q ); printf( "why is my program crashing here?!" ); fflush( stdout ); #endif And to compile this code in debug mode, use the -D flag as follows: bash$ gcc -Wall -Werror -D DEBUG_MODE homework3.c -pthread 5