CS4023 Week04 Lab Exercise

Similar documents
Introduction: What is Unix?

Getting started with Hugs on Linux

Lab 1 Introduction to UNIX and C

You should see something like this, called the prompt :

15-122: Principles of Imperative Computation

Lab 1 Introduction to UNIX and C

Getting started with Hugs on Linux

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

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

Introduction to Linux

last time in cs recitations. computer commands. today s topics.

Laboratory 1 Semester 1 11/12

Linux Command Line Interface. December 27, 2017

Mills HPC Tutorial Series. Linux Basics I

Introduction. Overview of 201 Lab and Linux Tutorials. Stef Nychka. September 10, Department of Computing Science University of Alberta

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

1 Getting Started with Linux.

CMSC 201 Spring 2017 Lab 01 Hello World

Short Read Sequencing Analysis Workshop

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Spring 2018 Lab 01 Hello World

Computers and Computation. The Modern Computer. The Operating System. The Operating System

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

Some Linux (Unix) Commands that might help you in ENSC351

CSE 390a Lecture 1. introduction to Linux/Unix environment

CS Fundamentals of Programming II Fall Very Basic UNIX

Perl and R Scripting for Biologists

CSC 112 Lab 1: Introduction to Unix and C++ Fall 2009

Linux File System and Basic Commands

CSE 391 Lecture 1. introduction to Linux/Unix environment

CISC 220 fall 2011, set 1: Linux basics

CS4023 Week06 Lab Exercise

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

Intro to HPC Exercise

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

CS : Computer Programming, Spring 2000 Programming Project #1: Thinking of a Number Due in class Thursday 27 January 2000

Tutorial 1: Unix Basics

CSE 391 Lecture 1. introduction to Linux/Unix environment

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

Scripting Languages Course 1. Diana Trandabăț

Chapter-3. Introduction to Unix: Fundamental Commands

CS354 gdb Tutorial Written by Chris Feilbach

Working with Basic Linux. Daniel Balagué

Computer Science 62 Lab 8

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Introduction to Linux. Fundamentals of Computer Science

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

CSC209. Software Tools and Systems Programming.

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

CSC209. Software Tools and Systems Programming.

CMPS 12M Winter 2018 Lab 1 Due: Monday January 11:59pm

The Command Line. Matthew Bender. September 10, CMSC Command Line Workshop. Matthew Bender (2015) The Command Line September 10, / 25

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Lab #0 Getting Started Due In Your Lab, August 25, 2004

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

COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

UTA Tech Orientation Spring 2019

The Unix Shell & Shell Scripts

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

CSE115 Lab exercises for week 1 of recitations Spring 2011

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

Introduction to Linux Spring 2014, Section 02, Lecture 3 Jason Tang

Introduction to Linux for BlueBEAR. January

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Preparing and Running C Programs for CS 136 (W08)

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

Introduction to Linux

The Online Unix Manual

Preparation of alignments for variant calling with GATK: exercise instructions for BioHPC Lab computers

CSCI 2132 Software Development. Lecture 3: Unix Shells and Other Basic Concepts

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Compiling C/C++ programs

Lab #1 Installing a System Due Friday, September 6, 2002

Using WestGrid from the desktop Oct on Access Grid

EXPERIMENT 1. FAMILIARITY WITH DEBUG, x86 REGISTERS and MACHINE INSTRUCTIONS

Welcome (back) to CS1007!

Creating a Shell or Command Interperter Program CSCI411 Lab

CMSC 201 Fall 2016 Lab 13 More Recursion

Introduction. File System. Note. Achtung!

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Using the Zoo Workstations

Vi & Shell Scripting

EE516: Embedded Software Project 1. Setting Up Environment for Projects

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Lab #2 Physics 91SI Spring 2013

Introduction to Unix: Fundamental Commands

Module 10: Unix Primer

Unix background. COMP9021, Session 2, Using the Terminal application, open an x-term window. You type your commands in an x-term window.

Part I. UNIX Workshop Series: Quick-Start

INSE Lab 1 Introduction to UNIX Fall 2017

CSC111 Computer Science II

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

Introduction to Linux

24 Writing Your First Script

CMSC 201 Spring 2017 Lab 12 Recursion

Transcription:

CS4023 Week04 Lab Exercise Lab Objective: We will use this lab to log in to our Linux accounts and to look at some simple programs that perform a few elementary system calls. By the end of the lab we will have developed the machinery to more or less mimic UNIX s cp copy command. Here s a quick summary of the tasks: ❶ Create a series of hierarchical directories to manage our work throughout the remainder of the semester ❷ Copy the C source code readfile.c into your directory and compile it ❸ Watch it in action, tracing system events using the strace and ptrace utilities ❹ Learn some of the basic tricks of the good programmer ❺ Extend the given program so that it reads a file and writes the contents somewhere else ❻ See how you should submit your program for automatic grading. ❼ Consider what is wrong with the program we have just worked on. In Detail ❶ Note that there are a few alternatives given below for this first step so please read the entire instructions before acting. Over the semester we will have labs, programming assignments, etc. and it is a good idea to keep these in an organised way. My suggestion is that you create a subdirectory of your home directory called cs4023 that will be the top-level point for all module-related material. The command to create a directory in Linux is mkdir, so you could do mkdir ~/cs4023 which makes a subdirectory located in your home directory. Next you should make a subdirectory called labs in this for each week s work. This can be done with mkdir ~/cs4023/labs Finally, for this week s lab, Week04, you should create its own subdirectory with mkdir ~/cs4023/labs/week04 1

An alternative to making each level of the hierarchy at a time is to tell mkdir to make the parent subdirectory if it doesn t exist. So the following command can take the place of all of the previous ones. mkdir -p ~/cs4023/labs/week04 The -p is for make parents if they don t already exist. Note that it is very similar to the command before it, but you had to do a lot of extra work in order to achieve that. Yet another alternative is to bring up a GUI-based file manager and use the Create... menu option there. ❷ Using the C code provided in the class directory, ~cs4023/labs/week04, copy this file into your directory ~/cs4023/labs/week04 with cp ~cs4023/labs/week04/readfile.c ~/cs4023/labs/week04 Just a single character... Here s something that gets people every time. The tilde character, ~, has a special use at the CLI (command-line interface). Several different shells including bash, which we are using treat the tilde character as denoting a user s home directory. So, no matter where you are in your own filesystem, you can use ~ as a shorthand for your home directory, which, for user d9994023 will be /home/d9994023. But you can also use ~ to refer to somebody else s home directory. In the previous command ~cs4023 is a shorthand for the home directory of user cs4023. So there is a huge difference between ~cs4023 and ~/cs4023: the former is the home directory of user cs4023 while the latter is a sub-directory called cs4023 of your home directory. In summary: watch out for not confusing the two arguments to the cp command! Once this is done you can examine the source code in a text editor. The one I use is emacs, which may or not be installed on your lab machine. If it is you can fire it up with emacs readfile.c & but you need to be in the correct directory for this. As an alternative editor many people in the past have preferred gedit and this is on your machines. You can change working directory with the command cd ~/cs4023/labs/week04 The ampersand at the end is important for it runs the program in the background, allowing you to give further commands at the shell prompt. Please spend a while looking at the code; it shouldn t all make sense to you but between looking at the source and running it (later) you should get an idea of what it does. One thing to look at is the while loop. As you know from your Java days while executes the body of the while-loop as long as some condition is true. In this case the condition 2

is decided by comparing the result of c = getc(from) to the special character EOF. This special character exists at the end of every UNIX file and is used, for example, by a diskreading program to tell when to stop reading. As long as c is not this special character the while-loop will keep on truckin (man). And what is the truckin? The character is put to stdout, which is the screen in this case. Now compile this program with the command gcc -o readfile readfile.c Actually, the command gcc does two jobs: it firstly compiles the file readfile.c into CPUspecific code (machine or object code) and then links together this object code with any necessary support modules of code to create an executable program. Once you have compiled your program you should have an executable called readfile that you can run as follows: readfile If you have been following along with the previous labs, doing as was suggested, then the above should work. If it doesn t work it is likely that you have not modified your $PATH environment variable to instruct the shell to look in the current working directory first for commands to execute. This environment variable should be in either of the files ~/.bashrc or ~/.bash_profile in your home directory. Today s Tip You can avoid always having to specify,./, the current working directory, by a quick edit of your.bash profile in your home directory. In this file you will find a line that begins with PATH=. What follows is a list of directories, each separated from the next by a :. Whenever you issue a command, this list of directories or locations is where the shell looks to try and find an executable program. So by putting just a simple. at the end of the line will now tell the shell interpreter to look in the current directory as a last resort for a command to execute. 1) Don t forget the : separator and, 2) this will work next time you log in; for the present session, issue the command sh $HOME/.bash profile. When you do get the program to run, it should report back immediately with file.txt: No such file or directory. This is because we don t have such a file in this directory. In order to keep readfile happy please create a file called file.txt now. You can edit this from scratch with anything you like or you can do something like head -5 readfile.c > file.txt which will insert the first 5 lines of your C source file into the file file.txt. If parsimoniousness is your concern you could even do touch file.txt (Eventhough we mentioned it in class recently this one is worth doing a man on. Do it now. That is, at the shell prompt give the command man touch. Note that order is important, 3

this is not the same as touch man. 1 ) Once you have created something and called it file.txt you should be able to run readfile and have it exit successfully. Now we will cause the program to fail and this will demonstrate the power of the perror() function call in the program. Change the protections on the file file.txt so that it is not readable. You can do this with chmod 0333 file.txt or chmod ugo-r file.txt Now run readfile again. Note the different error message. When attempting to open the file fopen() returned NULL and we then had perror() decode the reason why it failed. Please, please get in the habit of testing the result of every system call you make fopen() in this case and reporting the error with perror() in the case of failure. Note also the fclose() system call that closes the file opened for reading. Just like your mother told you to always close gates that you opened, so too should you fclose files fopened by you. Don t forget to change back to something sensible the protections on the file file.txt now. ❸ A very useful tool to help in debugging programs that make system calls or otherwise interact with the kernel is the strace program. This program and its (simpler and less verbose) counterpart, ltrace, allow you to see a report of system calls and library calls strace is for system calls and ltrace is for library calls. They do their job by you giving your command as normal and just prepending either one or the other. For example ltrace readfile reports the library calls made in running our program. It is the program ltrace that is executed first and this manages the running of our program readfile. ❺ You will now write a new program called copyfile that will read the file file.txt and now write the contents to the file copy.txt. Firstly make a copy of the readfile.c source code and save it in copyfile.c. Now edit 2 this file so that the copying of file.txt gets done. This involves two main steps: opening and closing two files instead of one writing each byte read from file.txt to copy.txt You will need to look at the syntax of the fopen() system call to find out how to open a file for writing. Do man fopen to learn this. 1 Every lab sheet should have at least one lousy pun. 2 Use any editor you like. I use emacs because it knows about C code and it colour codes keywords, etc. 4

The second item will require you to modify the body of the while loop in the program so that the character is no longer put to standard output (stdout). You will need to put it to the destination file instead. ❻ This is the first lab that you will be assessed on. There will be 5 assessments in total, each worth 4%. To be assessed you will need to have the program written, working properly and handed in by 10.00, Thu. Week05. The command to submit your program for this week will be: handin -m cs4023 -p w04 Please type this command exactly as given. handin works by taking a copy of your source code and storing it in a safe place. This copy is then compiled according to the same instructions as you have been given. The resulting program is then run on various test data and the output is compared characterfor-character to what the correct result should be. If there is any deviation then your program is considered to have failed that test. So, over the semester, when you are given a programming assignment please make sure that the output of your program matches exactly with the output you were asked to generate. ❼ Closing comments: The trouble with this copyfile program is its inflexibility: it is impossible to read from a file other than file.txt or to write it to anything other than copy.txt. What we would like to be able to do is to specify the name of the file to read as a part of the command. This is where command-line arguments come in to play and we will discuss these next time. 5