C Family Programming

 

1. Please refer to Figure 5.7 – Polling I/O Read operation in the textbook and slides
1.a What information does the status register include? (4 points)
1.b Please use a while-loop to implement step 4 in Figure 5.7. (6 points)
1.c How do you use the information in the status register to implement step 2? Please use a while loop to write down the pseudo-code. (5 points)

Get Your Custom Essay Written From Scratch
Are You Overwhelmed With Writing Assignments?
Give yourself a break and turn to our top writers. They’ll follow all the requirements to compose a premium-quality piece for you.
Order Now

2. Textbook, P. 151 -> question 13. In this question, you just need to
a. Explain the basic idea about how to implement xRead().
b. Use a high-level language pseudocode (for example, C style pseudocode in the textbook) to develop an application, which can continue processing after the xRead() call, but can block itself before using data in the process of being read.

(25 points)

3. Programming question
(60 points)

Please use thread to complete the following program: one process opens a file data.txt, then creates a thread my_thread. The job of the thread my_thread is to count how many lines exist in the file data.txt, and return the number of lines to the calling process. The process then prints this number to the screen.

Basically, you need to implement main_process.c and thread_function.c.

Basic structure of main_process.c:

int main ()
{
Open the file data.txt and obtain the file handler fh;

Create a thread my_thread using pthread_create; pass fh to my_thread;

Wait until my_thread terminates, using pthread_join;

Print out how many lines exist in data.txt.
}

Basic structure of thread_function.c:

void *count_lines(void *arg)
{
Obtain fh from arg;

Count how many lines num_lines exist in fh;

Close fh;

Return num_lines
}
For information about pthread_create and pthread_join:

man pthread_create
man pthread_join

How to compile and link your program:

gcc -o my_example main_process.c thread_function.c -lpthread

You can create a simple data.txt to test your program.

For more examples, please refer to Chapter 12 of the following textbook:

Unix System Programming, K.A. Robbins and S. Robbins, Prentice Hall, ISBN: 0-13-042411-0, 2003

Class Policy:

Some programming questions are used in previous semesters. Please do NOT borrow solutions from those students who have taken this class before. We keep a database of all previous solutions.

Programming solutions will be checked carefully for plagiarism. Students who are caught copying directly will receive 0 for this assignment.

Programming solutions will be checked carefully for plagiarism. Students who are caught copying directly will receive 0 for this assignment.
.