Introduction to Linux and C

Lab 0

DUE 9/11/2003

 

Introduction: Most of the assignments given out during this semester will require you to know some basic Unix (Linux) commands and programming skills in C and C++. Whereas all of you know C++, you may not have used C before. Well, you have one week to become an expert! Same for Linux.

 

References:  C:

                        http://www.cyberdiem.com/vin/learn.html

                        http://members.tripod.com/~johnt/c.html

                        http://www.cs.cf.ac.uk/Dave/C/CE.html

 

Linux:             http://www.redhat.com/docs/manuals/linux/RHL-9-Manual/

                        (do not print these manuals in the lab)

                        http://www.linux.org/docs/

 

Introduction to C: Implement the following in C:

 

PROBLEM – 1
            Write a program that takes a text filename and an unsigned integer n at the command prompt (See http://www.mattjustice.com/cnotes/c_cmdline.html ) and displays the last n lines from the text file. If n is 0 or greater than the number line in the test file, your program should display an error message. (Use argv, argc for reading in command line; Use dynamic memory -  malloc(), calloc(), realloc() and free())

PROBLEM – 2

            Write a program to remove all comments from a C program. Don’t forget to handle quoted strings and characters properly. C comments do not nest.  Assume that the input is syntactically correct (i.e. it compiles without errors). You should accept the name of the C file to read from the command line.

PROBLEM – 3
            Write a program that declares an integer, a short integer, a long integer, a float, a double, an integer array, a character array. When executed , the program should print the                  memory address and size of all its variables.

PROBLEM – 4
            Use all available string functions (strcpy, strcmp, strlen, strstr etc.) in a program. Be creative in defining this problem. Credit will be given for small “cute” programs.

 

PROBLEM – 5

            What is the result of executing the following program segment? Why?

int i, test[2];

 test[0] = test[1] = -1;

 i = 0;

 test[i] = i = i + 1;

 printf("%d %d\n", test[0], test[1]);

 

Introduction to Linux:

 

1)      What is the first process a typical Linux kernel starts?

2)      Explore and report your Linux box’s startup procedure in detail. You should read and explain what each startup script does, what executable files they execute.

3)      Find a file in the Linux box assigned to you in ISIS that contains the string “Hello CS392” and report the command you used to find this file

4)      What is does the command strace do? Run any of the code you compiled in the above section with strace and explain the results.

5)      Write a shell script to execute all executable files in the current directory and capture their output in out.lab0.

6)      Modify ???startup such that it appends the current time and date to a file in root’s directory.

7)      What command would you use to see the processes that are running currently on your Linux system? Explain how you can kill (stop) a process.

8)      How do you set up variables in bash? And how would you remove them with out logging out?

9)      What is the command to know the last-bootup time of the system?

10)  What is meant by real and effective user-ID’s?

11)  Categorize the following commands in Linux as one of  1)File and Directory Management, 2) User Management, 3) Process Management, 4) Compiler and Linker, 5) Communication, 6) Editing, or 7) Miscellaneous commands. Also give a brief (one to four line description) for each command. You should describe their use and  the most important/popular switches/options used with these command.

 


1)      grep

2)      find

3)      man

4)      slocate

5)      strace

6)      vi

7)      ssh

8)      sftp

9)      ls

10)  chmod

11)  chown

12)  chgrp

13)  passwd

14)  useradd and adduser

15)  su

16)  whereis

17)  lsmod

18)  insmod

19)  gcc

20)  gdb

21)  make

22)  fdisk and cfdisk

23)  | pipe, >/>> redirection operators

24)  ln

25)  rm

26)  cp

27)  mv

28)  ld

29)  ftp

30)  more / less

31)  cat

32)  tar

33)  top

34)  ps

35)  kill

36)  df

37)  last

38)  gunzip

39)  patch

40)  bzcat

41)  bunzip

42)  mkdir

43)  rmdir

 


Handin: Handin in a hard copy of your written answers and a floppy disk (Or CD) containing your source files and your executables. Make sure that both the floppy and the written report contain your name and ID. Attach the two together or place them in an envelope and submit in the drop box in ISIS by 11:00 PM on the due date.