Problem Set 1: Unix Commands 1

Similar documents
Laboratory 1 Semester 1 11/12

Exercise 1: Basic Tools

Project 2: Shell with History1

We first learn one useful option of gcc. Copy the following C source file to your

Software Development With Emacs: The Edit-Compile-Debug Cycle

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Introduction to Supercomputing

Programming Tips for CS758/858

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

PRINCIPLES OF OPERATING SYSTEMS

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CSE 351. Introduction & Course Tools

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Programming Studio #9 ECE 190

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CMPUT 201: Practical Programming Methodology. Guohui Lin Department of Computing Science University of Alberta September 2018

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Unix and C Program Development SEEM

Assignment 1: Port & Starboard

CS354 gdb Tutorial Written by Chris Feilbach

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Reviewing gcc, make, gdb, and Linux Editors 1

CSE 351. GDB Introduction

Intermediate Programming, Spring Misha Kazhdan

EL2310 Scientific Programming

EL2310 Scientific Programming

Assignment 1: Build Environment

CS 253: Intro to Systems Programming 1/21

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

Lab 1 Introduction to UNIX and C

COMP s1 Lecture 1

Lab 1 Introduction to UNIX and C

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CSE 374 Programming Concepts & Tools

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

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

Practical Session 0 Introduction to Linux

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 5

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

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

Exercise Session 6 Computer Architecture and Systems Programming

COL100 Lab 2. I semester Week 2, Open the web-browser and visit the page and visit the COL100 course page.

Using a debugger. Segmentation fault? GDB to the rescue!

CSC111 Computer Science II

Introduction to Linux

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers

Projects and Environment Introduction

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

Week 1, continued. This is CS50. Harvard University. Fall Cheng Gong

Recitation: Bomb Lab. September 17 th 2018

Chapter 2. Basics of Program Writing

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

{C} Tools of the Trade

Instructions PLEASE READ (notice bold and underlined phrases)

RVDS 3.0 Introductory Tutorial

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

CS631 - Advanced Programming in the UNIX Environment. UNIX development tools

Lecture 07 Debugging Programs with GDB

Dalhousie University CSCI 2132 Software Development Winter 2018 Lab 8, March 22

Source level debugging. October 18, 2016

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference

23. Check that hello.txt now contains (substitute your info for mine): hello rcwhaley: Dr. Whaley

Introduction to the Command line. Introduction to the command line. Introduction to the Command line. GNU/Linux at South Wales

EECS2031 Software Tools

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

RVDS 4.0 Introductory Tutorial

Getting started with UNIX/Linux for G51PRG and G51CSA

Assignment 2: I2C Simulator - V1.02

CS Programming Languages Fall Homework #2

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Lecture 7: file I/O, more Unix

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Using gdb to find the point of failure

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! Solution. printed

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

CSC209. Software Tools and Systems Programming.

Programming Assignment #1: A Simple Shell

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

just a ((somewhat) safer) dialect.

COP4530 Data Structures, Algorithms and Generic Programming Recitation 3 Date: January 20 & 22, 2009

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

23. Check that hello.txt now contains (substitute your info for mine): hello rcwhaley: Dr. Whaley

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

Introduction to Linux

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Assignment 3: Inheritance

Topic 6: A Quick Intro To C

CSE 303: Concepts and Tools for Software Development

DAY 4. CS3600, Northeastern University. Alan Mislove

Operating System Labs. Yuanbin Wu

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

Transcription:

Problem Set 1: Unix Commands 1 WARNING: IF YOU DO NOT FIND THIS PROBLEM SET TRIVIAL, I WOULD NOT RECOMMEND YOU TAKE THIS OFFERING OF 300 AS YOU DO NOT POSSESS THE REQUISITE BACKGROUND TO PASS THE COURSE. s See course webpage for due date and time. Submit deliverables to CourSys: https://courses.cs.sfu.ca/ Late penalty is 10% per calendar day (each 0 to 24 hour period past due). Maximum 2 days late (20%) This assignment is to be done individually. Do not show another student your code, do not copy work found online, and do not post questions about the assignment online. Please direct all questions to the instructor or TA: cmpt-300-d2-help@sfu.ca; You may use general ideas you find online and from others, but your solution must be your own. See the marking guide for details on how each part will be marked. When asked to explain something, don't copy and paste text found online or in the man pages. YOU CAN READ MAN PAGES AND THE INTERNET BUT ALL WORDS HAVE TO BE YOUR OWN Explain the idea in your own words. 1. Unix/Linux Commands 1.1 Readings If not very familiar with Unix/Linux commands, read Chapter 4 and Sec 9.2 of Running Linux (Available as an Electronic Book through SFU Library). 1.2 Running Commands After reading and practicing, create a text file named Commands.txt and in it write a one-line description for each of the following commands. I suggest you create a folder for CMPT 300, and sub folders for each assignment. 1. xemacs & 2. cd 3. cat ~/.bashrc > tmpfile.txt 4. ln -s tmpfile.txt ~/tmp-alias 5. ls -al 6. chmod a+rwx tmpfile.txt 7. grep bash /etc/passwd 8. ps -ef more 1 Created by Mohamed Hefeeda, modified by Brian Fraser. Some parts provided by Prof. Wei Tsang Ooi of University of Singapore. Generated Jan 6, 2016, 01:38 PM Page 1/8 Dr. Fraser

9. man 2 chown 10. gcc test.c 2> error-msg Suggestion: Use xemacs or emacs for typing your answers (for practice). Generated Jan 6, 2016, 01:38 PM Page 2/8 Dr. Fraser

2. Compiling a C Program In a file named gcc_and_gdb.txt, answer each of the questions in bold. List these answers as 2a), 2b), and so on, for question (a), and (b), and so on. 1. Copy the following code into a file named hello.c #include <stdio.h> #include <stdlib.h> void say_hello (int times) { for (int i=0; i < times; i++) { printf ("Hello World\n"); int main (int argc, char *argv[]) { say_hello(atoi(argv[1])); return 0; 2. Compile the code as follows (the $ indicates command entered in shell, don't actually type $): $ gcc -std=c99 hello.c The -std=c99 option enables the C99 extensions to C, which among other things includes declaring a variable inside the for-loop statement. This should generate the executable a.out. 3. Run a.out, giving it the parameter 5: $./a.out 5 In Linux/UNIX, you cannot simply type a.out to run the executable because the current directory is not in the path where the OS looks for programs that match the entered name. 4. Instead of building to a.out (default executable's name), use the -o option: $ gcc -std=c99 -o hello hello.c Run this executable with: $./hello 5 Now, let's explore the different stages of converting a C program into an executable: pre-processing, compiling, and linking. 5. Run the command $ gcc -E hello.c The outputs are too long and scrolls too fast. You can either pipe the output to less: $ gcc -E hello.c less Or redirect the output to a file and view hello.out in your favourite editor: $ gcc -E hello.c > hello.out (a) What does the -E option mean? What does the pre-processor do? 6. Run the command: $ gcc -std=c99 -c hello.c Generated Jan 6, 2016, 01:38 PM Page 3/8 Dr. Fraser

(b) What file is created by this command? State its name and explain what it is. 7. Now run: $ gcc -std=c99 hello.o (c) What do you get? 8. Comment out the main() function in hello.c using /* and */ (but leave the function say hello() in there). Run again: $ gcc -std=c99 hello.c (d) What error message do you see? Briefly explain this error. 9. Run $ gcc -std=c99 -c hello.c (e) Any error message now? Explain the differences in the outputs you see above, before and after commenting out main() and with and without -c. 3. Debugging with gdb 1. Uncommented main() which you commented out in the previous question. 2. Run the program hello without any arguments: $./hello (a) What do you get? What does this error message mean? 3. To find out what causes the error, we are going to use the debugger gdb. 4. Recompile hello.c with the -g option to create an executable file with additional information for the debugger (so-called debug information ). $ gcc -g -std=c99 -o hello hello.c 5. To run the debugger on the executable hello, run $ gdb hello You should see a number of lines of text. Te final line should be the gdb prompt: (gdb) You can now issue commands into gdb by typing on the prompt. 6. The first command we are going to issue is run, or its abbreviation, r. (gdb) r 7. The debugger will now run hello. When a segmentation fault is received, the debugger will display where the error occurs. (b) What is the name of the function within which segmentation fault occurs? You might not recognize the function where the error occurs as it does not appear inside the code hello.c at all. Some of the function calls we made in hello.c must have lead to this function. 8. To print the stack frame, run either of the following. bt is the abbreviation for backtrace. (gdb) where or (gdb) bt Generated Jan 6, 2016, 01:38 PM Page 4/8 Dr. Fraser

(c) Which library function we call in hello.c causes the error? Generated Jan 6, 2016, 01:38 PM Page 5/8 Dr. Fraser

Now, lets trace through the code line-by-line, examining the variables to find out what went wrong. 9. To examine the variables while the program is running, we need to first "break" the program. To do this, we set the breakpoint at the function main() with b command and rerun the program. (gdb) b main (gdb) r If asked, reply that yes, you do want to start the program from its beginning. 10. The debugger will now stop at main(). Let's examine the content of the variable argc and argv with the print command (abbreviated p). (gdb) p argc (gdb) p argv (d) Record the output. What does the value argv means? 11. Run each of the following commands: (gdb) info local (gdb) info args (e) What does each of these commands do? 12. The variable argv is an array of strings. Recall that each string in C is an array of char. Predict what each of the following will do, then run them. (gdb) p argv[0] (gdb) p argv[0][1] 13. Now run: (gdb) p argv[1] (f) What do you get? Explain why running hello without command line argument leads to a segmentation fault error? 14. You can quit gdb with the q command: (gdb) q 4. Pointers in gdb 1. Create a C program with the following code (no #include's are needed): int main(void) { int *x = 0; int y = 0; return 0; Compile the code with debugging option and load the resulting executable in a debugger. 2. Now, tell the debugger to break at line number 6 (the closing curly-brace of the function main()) and run the program. Then list the source code: (gdb) b 6 (gdb) r (gdb) list (a) Use gdb to print out and record the values of the following: x, y, *x, *y, &x, &y. Generated Jan 6, 2016, 01:38 PM Page 6/8 Dr. Fraser

What do you see? What do they mean? Generated Jan 6, 2016, 01:38 PM Page 7/8 Dr. Fraser

3. Now, we are going to use the debugger "set" command to change the values of these variables. (gdb) set x = &y (gdb) set *x = 1 (b) Use gdb to print out and record the value of any of the following which change: x, y, *x, *y, &x, &y. For each changed value, explain why? 4. Now, run the following (gdb) set *(x+1) = 10 (c) Use gdb to print out and record the values of the following: x, y, *x, *y, &x, &y. What has changed? Why? 5. Now run the following (gdb) set x = &y (gdb) set y = x Predict what this will do. Hint, try forcing the print to display in hex by adding /x: (gdb) p /x y (d) Use gdb to print out and record the values of the following: x, y, *x, *y, &x, &y. What values do you see? Explain the changes. 6. Now, exit from the debugger, change your C program to the following, and re-compile int main(void) { int *x = 0; int y = x; return 0; 7. You should get a warning message. Consider what causes the warning. 8. Now, recompile with the -Wall option of gcc. (e) What is the option -Wall for? 9. Edit the program to remove all the warning messages. 5. Deliverables Submit the following deliverabels to CourSys: 1. Commands.txt 2. gcc_and_gdb.txt You do not need to submit your code. Please remember that all submissions will automatically be compared for unexplainable similarities. Generated Jan 6, 2016, 01:38 PM Page 8/8 Dr. Fraser