Lab 3: Unix and Linux

Embed Size (px)

Citation preview

  • 8/13/2019 Lab 3: Unix and Linux

    1/3

    Lab #3 UNIX and UNIX ThreadsVillanova UniversityFall 2013 CSC1600: Operating Systems

    LEARNING OUTCOMES

    Once you have completed this exercise you will have done the following:

    Gained more familiarity with the UNIX shell commands Worked with a multithreaded programDIRECTIONS

    This lab should provide a gentle introduction, to multi-threaded programming. Thisis a pair programming exercise. Work on this exercise with your programmingpartner following the guidelines in the pair programming introductory document.

    You and your partner need to submit one completed lab, including the path namefor your completed code and a word-processed document with answers to the lab

    questions. You and your partner are only required to complete the first codingexercise to receive credit for the lab. However, as we have a few strong developersin the class, I have included a coding challenge. Once you and your partner havecompleted the coding exercise, if both of you are up for a challenge, considercompleting the coding challenge. If you and your partner submit a workingsolution, I will award you both credit for the completion of an additional lab.

    UNIX EXERCISES AND QUESTIONS

    Login to tanner and complete the following exercises once, and answer thequestions in a separate word-processed document. I have not given you thecommands. Use the reference sheet I have provided in module #0 or thecommand man pages. If you do not have time to complete this lab in class, you andyour partner must complete it on your own and upload it to Blackboard to receivecredit.

    1. Create a lab3 directory in your CSC1600 directory.

    2. Copy the following four files from /mnt/a/csc1600/module2/lab3 to yourcsc1600/lab3 directory: root_veg, stem_veg, cruciferous, leafy_greens.Rename these files, adding the extension .dat to each file.

    3. Execute the command man cat. What does the man command do?

    4. List the contents of your root_veg.dat file.

    5. List the first 5 entries in the leafy_greens.dat file.

    6. List the last 5 entries in the leafy_greens.dat file. What command did you

  • 8/13/2019 Lab 3: Unix and Linux

    2/3

    use? (Please include all options you used when executing your command.)

    7. Count the number of words, lines and characters in the cruciferous.dat file.What command did you use?

    8. Use the man command to review the usage of the grep command.

    9. Use the grep command to determine which file contains the entry cabbage.

    10. Using your knowledge of UNIX, create a sorted file of vegetables in yourcsc1600/lab3 directory, named vegetables.dat. What UNIX command(s)did you use to create this file?

    11. Remove the root_veg.dat, stem_veg.dat, leafy_greens.dat andcruciferous.dat files from your csc1600/lab3 directory. What UNIXcommand(s) did you use to remove these files?

    12. This next question uses the Macbeth data file. Copy the Macbeth file from/mnt/a/csc1600/module2/lab3 to your csc1600/lab3 directory. How manylines of the Macbeth file contain the word the? (Hint: We do not want tocount the words there, them etc., nor do we want to miss the word The.)What UNIX command(s) did you use to count the lines?

    13. What does the kill command do?

    CODING EXERCISE

    1.Copy my account.c program from the /mnt/a/csc1600/module2 directory.

    2. Modify my account.c program to include a thief function. Have the thief functionprint the take and clean out the account.

    3. Code a third thread to execute the thief routine twice during program execution,roughly every three to seven seconds.

    OPTIONAL CODING CHALLENGE

    Do you know how to play Sudoku? With Sudoku, you have a 9 by 9 board,containing the numbers 1 through 9. A Sudoku is considered solved when everyrow contains each of the numbers one through nine with no duplicates, everycolumn contains each of the numbers one though nine with no duplicates and eachof the nine 3 X 3 squares contains each of the numbers one through nine with noduplicates. This first Sudoku has been solved correctly. This second Sudoku hasnot been solved correctly; as the 3 X 3 box at the top right contains two sixes asdoes the second row and the last column.

  • 8/13/2019 Lab 3: Unix and Linux

    3/3

    Code a multithreaded program to verify that a players solution to a Sudoku iscorrect. Your program should first execute a function to read a file containing 81numbers, reading from the top left across to the bottom right. (Your file willcontain have one space between each number and no line feeds.) Your programshould then create three threads, one to verify each of the nine rows, one to verifyeach of the nine columns and one to verify each of the nine boxes. Name yourcode sudoku.c.

    Consider the following when coding your solution. Consider using argv[1] to input the name of your input file so you do not need

    to recompile each time you wish to verify a new Sudoku. It is always a good idea to verify all of your data, number of arguments, file

    contents etc. It is best to avoid global variables. As you are at least passing 81 values to each thread, you will need to pass

    either an array or a structure. Your strategy might include passing a pointer to a validity flag in a structure or

    using the value_ptr in, void pthread_exit(void *value_ptr); to make a flagavailable to any successful join with the terminating thread.

    Your strategy might include massaging the data before executing the threadsand thus executing each of the three threads with the same verification entrypoint or coding three separate entry points, massaging the data within thethreads and calling a common verification function from within each of the threethreads.

    http://www.google.com/imgres?imgurl=http://www.sudokukingdom.com/images/rules_solved.jpg&imgrefurl=http://www.sudokukingdom.com/rules.php&h=327&w=341&sz=16&tbnid=SKbPBlJU2P4MCM:&tbnh=90&tbnw=94&prev=/search?q=solved+sudoku&tbm=isch&tbo=u&zoom=1&q=solved+sudoku&usg=__oJuMQu_rUdfJ8Yvbyrs-MFKUhvo=&docid=dTfgWc5OLMt26M&sa=X&ei=6NItUrTjB6rh4APElYHAAQ&ved=0CCsQ9QEwAA&dur=2010