Exercise 1: Basic Tools

Similar documents
Introduction to Supercomputing

Laboratory 1 Semester 1 11/12

Problem Set 1: Unix Commands 1

Practical Session 0 Introduction to Linux

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

Introduction: What is Unix?

CS480. Compilers Eclipse, SVN, Makefile examples

CSC111 Computer Science II

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

CSE 351. Introduction & Course Tools

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Lab 1 Introduction to UNIX and C

CS 261 Recitation 1 Compiling C on UNIX

CENG 334 Computer Networks. Laboratory I Linux Tutorial

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

Intermediate Programming, Spring Misha Kazhdan

Linux at the Command Line Don Johnson of BU IS&T

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

Introduction to Linux

Introduction to Linux

Perl and R Scripting for Biologists

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

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

Introduction to the UNIX command line

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

Introduction: The Unix shell and C programming

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

Introduction To. Barry Grant

Computer Systems and Architecture

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

Introduction to UNIX Command Line

Unix Tools / Command Line

CENG393 Computer Networks Labwork 1

The Unix Shell & Shell Scripts

CS197U: A Hands on Introduction to Unix

UNIX. The Very 10 Short Howto for beginners. Soon-Hyung Yook. March 27, Soon-Hyung Yook UNIX March 27, / 29

Introduction to remote command line Linux. Research Computing Team University of Birmingham

Working with Shell Scripting. Daniel Balagué

CS CS Tutorial 2 2 Winter 2018

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

COMP s1 Lecture 1

CS Fundamentals of Programming II Fall Very Basic UNIX

Command-line interpreters

Introduction to Linux

Computer Systems and Architecture

CSE 15L Winter Midterm :) Review

CS 460 Linux Tutorial

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

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

Lab 1 Introduction to UNIX and C

Introduction in Unix. Linus Torvalds Ken Thompson & Dennis Ritchie

Introduction to Linux

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Introduction to the shell Part II

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Lecture 01 - Working with Linux Servers and Git

CISC 220 fall 2011, set 1: Linux basics

Parallel Programming Pre-Assignment. Setting up the Software Environment

Recitation #1 Boot Camp. August 30th, 2016

A Hands-On Tutorial: RNA Sequencing Using High-Performance Computing

Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

Unix/Linux Operating System. Introduction to Computational Statistics STAT 598G, Fall 2011

Department of Computer Science College of Engineering Boise State University

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

CSCI 2132 Software Development. Lecture 5: File Permissions

Mills HPC Tutorial Series. Linux Basics I

Linux and Git Boot Camp

9 and 11-Jan CSCI 4152/6509 Natural Language Processing Lab 1: FCS Computing Environment, SVN Tutorial. FCS Computing Environment, SVN Tutorial

CS356: Discussion #1 Development Environment. Marco Paolieri

Reviewing gcc, make, gdb, and Linux Editors 1

Linux Command Line Interface. December 27, 2017

Introduction to the Linux Command Line January Presentation Topics

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

Introduction to Linux Environment. Yun-Wen Chen

Chapter-3. Introduction to Unix: Fundamental Commands

Using the Unix system. UNIX Introduction

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

Lab #2 Physics 91SI Spring 2013

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

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

CS370 Operating Systems

Introduction to Linux

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

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

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

Introduction. File System. Note. Achtung!

UoW HPC Quick Start. Information Technology Services University of Wollongong. ( Last updated on October 10, 2011)

Shell Programming (Part 2)

Introduction to Linux

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

Linux Command Line Primer. By: Scott Marshall

INF322 Operating Systems

Introduction to the Linux Command Line

Basic Unix Commands. CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Useful Unix Commands Cheat Sheet

Chapter 4. Unix Tutorial. Unix Shell

Recitation #1 Unix Boot Camp. August 29th, 2017

Transcription:

Exercise 1: Basic Tools This exercise is created so everybody can learn the basic tools we will use during this course. It is really more like a tutorial than an exercise and, you are not required to submit your results. The student assistants are in the lab to help you not to check on your progress. The exercise is optional, but you should still do it to make sure you are prepared for the rest of the course. For the last part of the exercise you must have a group account on stud.ntnu.no. Since it takes some time to create the group account, you should apply for one as soon as possible. You apply for a group account on the following website: http://www.stud.ntnu.no/kundesenter/ 1. The GNU/Linux environment The computers on the real-time lab (room 246, SB2) have three different operation systems; Windows, Ubuntu and QNX. When the computer boots, a menu where you can select between these is presented. For now you should select Ubuntu 10.04 with Linux 2.6.32-generic-pae, a GNU/Linux distribution. 1.1. Ubuntu When Ubuntu is started you will get a login window. You should use student as both user name and password. If you don t already know Ubuntu, you should play around with the user interface. You should be able to find the programs for the following tasks: 1.2. Shell Web browsing (Firefox) Office programs (Open Office) Editors (Gedit, Emacs, Vim, Nano, Kate) Command line (text) interface (Terminal) File browsing Even if Ubuntu has a graphical user interface, there are still many operations that are easier, faster or necessary to do from a command line interface (CLI). You can get a window with a CLI by starting the program Terminal. The CLI is provided by one of several shell programs, most widely used are bash, csh and tcsh. The GNU/Linux default is bash, and you can check what type of shell your environment uses with the following command: $ echo $SHELL

The first $-sign above is there just to indicate that the following text is a command to be type in the CLI. It should therefore not be written in the shell. Also remember to hit ENTER (this you know of course). There are many other environment variables such as $PATH (where the shell searches for commands) and $HOME (the user home directory). In bash you can use the env and export commands to view and modify the environment variables. In the shell find out more about apropos, help, info, and man by typing for instance: $ man man Hitting 'q' in man will exit the program. Find out what the following commands do: ls, cd, pwd, cp, mv and rm Find a command that tells you the date and the time on the system. 1.3. Directories and files Your home directory is /home/student. Since this directory is shared with other groups using the same computer you should create the following structure: /home/student/gruppexx/ex1/ using the command mkdir. You can create a simple text-file by using echo and the shell: $ echo -n "I do really love" > test.txt $ echo " TTK4147" >> test.txt Try du, file, cat and wc on the file you just created. $ du -h test.txt $ file test.txt $ cat test.txt $ wc test.txt Within the directory created for this exercise use the tar program to extract the contents of the archivefile exercise1-files.tar which can be downloaded from it's learning. Use less on the file junk.txt which was extracted from the archive to see its contents. Use grep to find all files in the folder containing the word 'real-time'. How old are these files? Now update the timestamp of the files using touch. In the shell you may use '?' and '*' as wildcards. An important concept in UNIX is redirection of input/output, also known as pipes. For instance, you can use a pipe to process the result from the grep command with the sort command. Try the following command: $ grep real junk.txt sort

The tool find is very powerful. It may search for files using names, sizes, creation time and much more. Use find to list all files in the directory that are larger than 1 KB.

1.4. Mounting a network directory To mount your NTNU home directory to a local folder on disk, enter the following command: $ smbmount //sambaad.stud.ntnu.no/<username> <dir> -o username=<username>,workgroup=win-ntnu-no Replace '<username>' with your NTNU user-name, and '<dir>' with the directory you created using mkdir. To unmount the network directory enter: $ smbumount <dir> It is important that you back-up you data to a network folder (or some other media) prior to leaving the lab, and delete any files that you have stored locally. The computers may be reinstalled and all files deleted without any notice. 1.5. Shell-scripts It is possible to create small programs, or scripts, that are executed by the shell. Create a file named script.sh containing the following: #! /bin/bash for i in $(seq 1 10); do echo "Hello world $i" done Execute it by doing: $ bash script.sh or: $ chmod +x script.sh $./script.sh What do the command chmod and the comment on the first line of the script do?

1.6. Jobs To see the programs running on the system you can use top, jobs and ps. Do this now. Execute the command sleep 5. Then try adding an '&' after the command. What happens? Try running jobs to examine running processes. Start sleep 5 again, and press CTRL+C while the process is running. Try again but use CTRL-Z instead. What is the difference between CTRL+C and CTRL+Z. What does the command fg do? 1.7. Editor A programmer needs an editor to edit his code files. There are religious wars among programmers (and especially GNU/Linux programmers) about what is the best editor. We are not taking sides in this conflict, and you are free to use whatever editor you like. If you have no preference, and just want something that just works, then gedit will probably be your best choice. Others editors are: emacs, nano, vim, kate and ed. 2. Programming 2.1. GCC The first Linux C-program you should create in this course is Hello world. Use your editor to create the "Hello world" C-program shown below. Store the file hello.c in the working directory created for this exercise. #include <stdio.h> int main(int argc, char *argv[]) { printf( Hello world\n ); return 0; We will learn more about the GNU compiler tools in later exercises. For now, try to compile the program you just created with gcc: $ gcc -o hello hello.c Run the program with: $./hello 2.2. Makefile You should now create a more advanced program, inspect the following C code and figure out what it does:

#include <stdio.h> int main(int argc, char *argv[]) { while(argc--) printf ("%s\n",*argv++); return 0; Create a file name sort.c containing the following: #include <stdlib.h> void sort(int count, char *array[]) { int x,y; char* holder; for(x = 1; x < count; x++) for(y = 1; y < count-1; y++) if( atoi(array[y]) > atoi(array[y+1]) ) { holder = array[y+1]; array[y+1] = array[y]; array[y] = holder; Make a call to the sort() function from the main() program, in order to sort the arguments list, and then print out the results. #include <stdio.h> #include "sort.h" int main(int argc, char *argv[]) { int i; printf("arguments before sort: "); for (i=1;i < argc;i++) printf("%s ",argv[i]); printf("\n"); sort(argc,argv); printf("arguments after sort: ");

for (i=1;i < argc;i++) printf("%s ",argv[i]); printf("\n"); return 0; The sort.h file should only contain a declaration of the function: void sort(int count, char *array[]); You can compile the files by using: $ gcc -o sort_args sort.c main.c And run it: $./sort_args 34 2 919 34 42 51353 This is fine for smaller programs. But with larger programs and multiple files there is a problem: each time one of the files changes, all files are compiled again. A waste of computing resources! Instead, create this Makefile: CFLAGS = -g -Wall -ansi -pedantic objects = sort.o main.o sort_args: $(objects) gcc -o sort_args $(objects) main.o: sort.h clean: rm sort_args $(objects) Make sure this works by running the command make sort_args. Use make clean first, to remove old object files. Next, make some changes in either sort.c or main.c, and verify that only the changed files are re-compiled. When programming in Linux using the GNU tools it might be useful to use the GNU debugger: GDB. If you have time to spare now might be a good time to learn how to use GDB. It is not strictly required for the remaining exercises but will be of help.

3. Subversion (svn) In this and the following exercises you should use a revision control system (RVS). Subversion (svn) will be shown in the exercises, but you can also use other systems like GIT if you prefer. You should have registered a common area for the group on the NTNU servers prior to this exercise. If you don t have this, then you should try to fix this for the next exercise. Now login to your NTNU account and do the following: lab$ ssh user@login.stud.ntnu.no ntnu$ cd /path/to/group/dir ntnu$ svnadmin create svn ntnu$ chown <username>:<groupname> svn R ntnu$ chmod g+rwx svn -R ntnu$ exit lab$ export SVN=svn+ssh://user@login.stud.ntnu.no/path/to/group/dir/svn lab$ svn mkdir $SVN/trunk -m "Creating trunk folder" lab$ svn mkdir $SVN/trunk/01 -m "Creating folder for exercise 01" lab$ svn mkdir $SVN/trunk/02 -m "Creating folder for exercise 02" lab$ svn checkout $SVN/trunk group-xx To find the full path to your SVN repository on the NTNU server use the pwd command. After this you should have a directory named "group-xx" (where XX is replaced with your group number). This folder has the subdirectories "01" and "02" for the first and second exercise respectively. You may now add the file from the previous exercise by copying them into the folder and doing the following: $ cd group-xx/01 $ svn add my-report.ods $ svn add my-file.c $ svn commit -m "Added files for exercise 1" Find what the following svn-commands does using svn help: add, delete, move, diff, copy, commit. You don't have to note this down; it s only to get acquainted with the system. Learn more about SVN here. Put SVN in your professional toolbox, it s a gem. You will never regret to use revision control in any programming project, even if you re the only participant.