You are on page 1of 28

MC9228 – SYSTEM SOFTWARE LAB

Department of Computer Applications Rajalakshmi Engineering College Thandalam, Chennai 602 105 Lab Manual 600208 - System Software Lab

First Year MCA / II Semester

By

Dr. N. Prabakaran & Ms. G. Padmavathi

Febuary 2010 – June 2010

Dr. N. Prabakaran

Page 1

7/4/2011

MC9228 – SYSTEM SOFTWARE LAB TABLE OF CONTENTS Sl No Date of Exercise Exercise Features of text editors Basic UNIX commands Shell Programming Grep, sed, awk Implementation of create , read, write , close system calls Implementation of lseek system calls Process management – Fork Process management – Exec Message queues 10. 11. 12. 13. 14. 15. 16. 17. 18. Pipe FIFO Signals Shared memory Pass1 of 2 pass Assembler Pass2 of 2 pass Assembler Absolute Loader A Direct Linking Loader Text Editor Record to be completed on

Dr. N. Prabakaran

Page 2

7/4/2011

Commands are case sensitive. Partial list of interactive commands: Cursor movement: Keystrokes h/j/k/l spacebar -/+ ctrl-d ctrl-u ctrl-f ctrl-b H L W w 5w B b Move cursor left/down/up/right Move cursor right one space Move cursor down/up in first column Scroll down one half of a page Scroll up one half of a page Scroll forward one page Scroll back one page Move cursor to top of page Move cursor to bottom of page Move cursor a word at a time Move cursor ahead 5 words Move cursor back a word at a time Move cursor back a word at a time Features of Text Editor Action M (shift-h) Move cursor to middle of page Dr. 3. "A" (insert at end of line).MC9228 – SYSTEM SOFTWARE LAB Exercise No : 1 Date Aim To learn the various feature of the vi text editor Description The vi editor has three modes. N. One can enter insert mode with the "i" (insert). command mode. The ESC key ends insert mode and returns you to command mode. 1. insert mode and command line mode. Command mode: letters or sequence of letters interactively command vi. Command line mode: One enters this mode by typing ":" which puts the command line entry at the foot of the screen. The ESC key can end a command. "o" (open new line after current line) or "O" (Open line above current line) commands. Prabakaran Page 3 7/4/2011 . "a" (insert after). Insert mode: Text is inserted. 2.

'a 'A ]' [' Move cursor back 5 words Move cursor to end of word Move cursor ahead to the end of the 5th word Move cursor to beginning of line Move cursor to end of line Move cursor to beginning of next sentence Move cursor to beginning of current sentence Move cursor to end of file Move cursor to the matching bracket. N. Move cursor to previous lower case mark. Move cursor to previously modified line. Move cursor to line mark "a" generated by marking with keystroke "ma" Move cursor to line mark "a" (global between buffers) generated by marking with keystroke "mA" Move cursor to next lower case mark. Delete word Delete 4 words Change word Action Dr. Place cursor on {}[]() and type "%". Press esc key to end insertion. Editing commands: Keystrokes i a A ESC u U o dd 3dd D C dw 4dw cw Insert at cursor Append after cursor Append at end of line Terminate insert mode Undo last change Undo all changes to entire line Open a new line Delete line Delete 3 lines. Delete contents of line after cursor Delete contents of line after cursor and insert new text. Prabakaran Page 4 7/4/2011 .MC9228 – SYSTEM SOFTWARE LAB 5b e 5e 0 (zero) $ ) ( G % '.

Use command: ZZ Save changes and quit. No changes from last write will be saved. etc n N .". This will find "string s. Decrement number under the cursor. Use command line: ":q!" Ignore changes and quit. "x = fn(s). Terminate session: • • • • • Find next occurrence of search_word Find previous occurrence of search_word repeat last command action. Search for search_string Search backwards (up in file) for search_string /\<search_string\>{CR} Search for search_word Ex: /\<s\> Search for variable "s" but ignore declaration "string" or words containing "s".". Use command line: ":qa" Quit all files opened. Prabakaran Page 5 7/4/2011 .MC9228 – SYSTEM SOFTWARE LAB x r R s S ~ ctrl-a ctrl-x /search_string{CR} ?search_string{CR} Delete character at cursor Replace character Overwrite characters from cursor onward Substitute one character under cursor continue to insert Substitute entire line and begin to insert at beginning of line Change case of individual character Increment number under the cursor. N. Use command line: ":wq" Save (write) changes and quit. Use command line: ":w" Save (write) changes without quitting.". Dr. "s = fn(x).

Bourne shell command interpreter /bin/time . File and Directory Management Dr.full-screen text editor ("vi-improved") cd .time a command Unix Commands for shell 2. Programming Tools 4.sign off.kill a process sh .maintain periodic tasks csh .change working directory chmod .C++ compiler cc .log in remotely to another UNIX system CC .stream-oriented text editor vi .C compiler crontab .MC9228 – SYSTEM SOFTWARE LAB Exercise No : 2 Date Aim To execute the Unix shell Ccmmands and get familiar with the UNIX environment Description 1.change the protection of a file or directory exit .simple. Editors • • • • • • • • emacs . N.line-oriented text editor pico . end session (C shell and bash shell only) rlogin .GNU C++ compiler kill .terminate a shell logout .full-screen text editor vim . Prabakaran Page 6 7/4/2011 . screen-oriented text editor sed .screen-oriented text editor ex .C shell command interpreter g++ . Access Control • • • • • • • • • • • 3.

show first few lines of a file lpr .show the full pathname of your working directory quota . but prints screens top to bottom 5. catenate files fold .fold long lines to fit output device head .copy input to standard output and other files umask .move or rename files and directories pwd .display a file.restore compressed file cp .report (or delete) repeated lines in a file wc . File Display and Printing Dr.delete (remove) files rmdir .count lines.print a file lprm . N.MC9228 – SYSTEM SOFTWARE LAB • • • • • • • • • • • • • • • • • • • • • • • • • • • • cmp .make a link to a file ls .delete (remove) directories sort .create a directory mv . one screen at a time page .compare two files comm .list the contents of a directory mkdir . words.compress a file uncompress .show the contents of a file.sort or merge files tee .remove jobs from the printer spooling queue more .copy files crypt .change default file protections uniq .select/reject lines common to two sorted files compress .determine file type grep . and characters in a file cat .compare the contents of two ASCII files file .search a file for a pattern ln .like "more".display disk usage and limits rm .encrypt/decrypt files (not on ADS) diff . Prabakaran Page 7 7/4/2011 .

show network telnet .suspend process at next input request ^z .show system load.show the last part of a file zcat .show who is on system.show who is logged onto the system whois .run Telnet to log in to remote host bg . how long system has been up w . what command each job is executing who .show process status pwd .show date and time finger . Prabakaran Page 8 7/4/2011 .clear terminal screen echo .put suspended process into background fg .display a compressed file clear .look up user information history .bring process into foreground jobs .echo arguments netstat .indicate last login of users Ps . N. Process Control Dr. Miscellaneous 7.Internet user name directory service 6.suspend current process date .MC9228 – SYSTEM SOFTWARE LAB • • • • • • • • • • • • • • • • • • • • • tail .display full pathname of working directory uptime .list processes ^y .list previously issued commands last .

Program starts now." echo "This is the uptime information:" uptime Dr. $USER" echo "Today's date is `date`. this is week `date +"%V"`.to display some well known commands Program : • • • • • • • • • • clear echo "This is information provided by mysystem.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 3 Date Shell Programming .sh.| grep -v USER | sort -u echo echo "This is `uname -s` running on a `uname -m` processor. N." echo "These users are currently connected:" w | cut -d " " -f 1 ." echo "Hello. Prabakaran Page 9 7/4/2011 .

pattern-matching. Program $1 is field #1. Awk awk is a programmable. and processing tool available in UNIX. It searches one or more files to see if they contain lines that match specified patterns Perform associated actions • ex: writing the line to the standard output or incrementing a counter each time it finds a match. Awk Description: • • • • Grep. etc. but a programming language too . It works equally well with text and numbers awk is not just a command. awk utility is a pattern scanning and processing language. echo one two | awk '{print $1}' echo one two | awk '{print $2}' echo one two | awk '{print $0}' awk '{print $3}' $filename awk '{print $1 $5 $6}' $filename awk '{print $0}' $filename Dr. N.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 4 Date Aim : To demonstrate the use of grep . Prabakaran Page 10 7/4/2011 . $2 is field #2.

READ and WRITE system calls into it and display the same after reading it again from the file requires the use of the following system calls • Create This function is used to create a new file. else the function returns a negative number . Description The C Program to create a new file and write the description about the CREATE . It returns 0 if OK else in case of failure to close the file it returns -1. close system calls Dr. mode_t mode */) • Close function This function is used to close an open file. If the system call was successfully executed it returns the file descriptor or else it returns -1. The prototype of the function is int open(const char *pathname. ….h> header files are required to use this function . <fcntl. Header files required for this function is(unistd. <sys/types. N./*.h>. read. write. <sys/stat. OPEN. This function creates and also opens the file in write only mode • Open A file is opened or created using the open system call. int oflag. The prototype of the function is int creat(const char *pathname.h>.h>. The prototype of the function is int close(int fd) When a process terminates all the open files are automatically closed by the kernel • Read Page 11 7/4/2011 Implementation of create . READ and WRITE system calls.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 5 Date Aim To write a C Program to demonstrate the use of CREATE. mode_t mode) File descriptor is returned if the file is opened. Prabakaran .

size_t nbytes). The prototype of the function is ssize_t read(int fd. Algorithm Step 1: Include the header file sys/stat. 0 is returned. If the read is successful.h. if the end of the file is encountered . In case of error writing to the file -1 is returned. • Write Data is written to an open file using this function. void *buff. N. In case of error it returns -1. size_t nbytes). the number of bytes read is returned. However . fcntl. const void *buf. open the same file using open () system call. Prabakaran Page 12 7/4/2011 . in the read only mode Step 10: Read the contents of file using read system call and check it was successful Step 11: Print the contents of the file on the screen Dr. the offset is incremented by number of bytes written The prototype of the function is ssize_t write(int fd.MC9228 – SYSTEM SOFTWARE LAB This function is used to read data from a open file.h Step 2: Declare the necessary variables Step 3: Get a data to be entered into the file from the user and store it in a variable.h. Step 5: If the file was created successfully goto step 6 else print that there was error and close the program Step 6: Use the write system call to write data into the created file Step 7: Check if the entire data has be written successfully Step 8: Close the file using close () system call. Step 4: Create a new file using creat(() function. then offset is set to the current end of the file After write. string. Step 9: Now.h and stdio. If the write was successful it returns the number of bytes written . The write generally begins with the current offset If the file was opened with O_APPEND.

N. <unistd. SEEK_SET). In that case the fille write will extend the file . write the user inputs into the f1.h> The prototype of the function is off_t lseek(int fd.txt using open sytem call Step 4 : Write the user input into f2.txt Step 5 : Use lseek to move the file offset to the beginning of the file Step 6 : Now use the write system call to write the contents into the file Dr. This is referred to as creating a hole in the file. int whence) On success it returns the new offset . Prabakaran Page 13 7/4/2011 . from the beginning of the file (with whence 0. • The file’s offset can be greater than the file’s current size. from the current position of the read/write offset (with whence 1. Any bytes that have not been written are read back as 0. b. An open file’s offset can be explicitly positioned using the lseek system call The required header files are <sys/types. else it returns error The whence indicate whether offset should be considered a.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 6 Date Implementation of lseek system call Aim To write a C Program to demonstrate the use of lseek function Description • • • • • • Every open file has an associated “current file offset”. SEEK_END). It is an non negative integer that measures the number of bytes from the beginning of the file When a file is opened the file offset is set to 0. off_t offset.txt in Read Write mode Step 3 : Using the Write System call . from the end of the file (with whence 2. Algorithm Step 1 : get the string to be entered from the user Step 2 : use open sytem call to open a file f1. c.txt Step 3 : Open the file f2. SEEK_CUR).h>.

Algorithm : Step 1: call the fork function Step 2: check the return value of the fork function Step 3 : If the retrun value of the fork function is 0 then it is the child Step 4: if the return value of the fork function is greater than 0 then it is the parent Step 5:if the return value of the fork is -1 then error in creating child process Dr. The child process has a unique process ID. Prabakaran Page 14 7/4/2011 . The child process has its own copy of the parent's open directory streams. the process ID of the parent process). The child process has a different parent process ID (that is. The child process has its own copy of the parent's file descriptors. Each of the child's file descriptors refers to the same open file description with the corresponding file descriptor of the parent. The new process (child process) is an exact copy of the calling process (parent process) except as detailed below.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 7 Date Aim : Program to Demonstrate Fork To write a C Program demonstrate the use of fork Description : The fork() function creates a new process. The child process ID also does not match any active process group ID. Each open directory stream in the child process may share directory stream positioning with the corresponding directory stream of the parent. N.

Prabakaran Page 15 7/4/2011 . "-l". N. "-t".h> main() { execl("/bin/ls". Program #include <unistd.MC9228 – SYSTEM SOFTWARE LAB Excercise No :8 Date Aim : Program to demonstrate Exec To write a C Program demonstrate the use of Exec Description : The exec() family of functions will initiate a program from within a program. "/bin/ls". (char *) 0). "-r". } Dr.

type.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 9 Date Program to perform inter process communication using message queues Aim : To write a C Program to perform inter process communication using Message Queues Description • • • Message queues provide a way of sending a block of data from one process to another. Messages are placed in the queue by msgsnd() system calls and retrieved by msgrcv(). Other operations related to managing a given message queue are performed by the msgctl() system call int msgget(key_t key. N. each of which identifies the address. one for each message. Message queues are implemented as linked lists of data stored in shared memory. The message queue itself contains a series of data structures. a program uses the msgget() system call . Algorithm Step 1 : In the sender program use the ftok to get the key Step 2 : Use the msgget() to get the message queue Step 3 : Use msgsnd() to send the message through the queue Step 4 : In the receiver program use the ftok to get the key Step 5 : Use the msgget() to get the message queue Step 6 : Use msgrcv() to receive the message through the queue Dr. and size of the message plus a pointer to the next message in the queue. • • • To allocate a queue. int msgflg). Prabakaran Page 16 7/4/2011 .

fd[0] is set up for reading. N. the pipe is of little practical use. fd[1] is set up for writing Algorithm Step 1 : Create a pipe for messaging passing using the Pipe function Step 2 : Use fork() to create a child Step 3 : In the Parent process use the fd[1] to write the contents into the file Step 4: In the child process use the fd[0] to read the contents sent by the parent Dr. as the creating process can only use the pipe to communicate with itself int pipe( int fd[2] ). When a process creates a pipe.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 10 Date Program to implement inter process communication using pipes Aim : To write a C Program to perform inter process communication through Pipes Description • • • • • A pipe is a method of connecting the standard output of one process to the standard input of another. the kernel sets up two file descriptors for use by the pipe. while the other is used to obtain data from the pipe (read). Pipes provide a method of one-way communications (hence the term half-duplex) between processes. One descriptor is used to allow a path of input into the pipe (write). Prabakaran Page 17 7/4/2011 . At this point.

allowing unrelated processes to communicate. FIFOs must be opened for read only or write only (not read and write). Then.MC9228 – SYSTEM SOFTWARE LAB Exercise No: 11 Date Program to implement inter process communication using FIFO Process Aim : To write a C Program to perform inter process communication using FIFO Description • • • • • • • • UNIX FIFO provides one-way communication between processes similar to pipes. Likewise if you open one for write then the calling process will block if the FIFO is not opened for reading. if you also open it for write only. For example. N. Algorithm Step 1 : In the client program create a fifo Step 2 : write the data through the fifo Dr. To open the system tasks FIFO without blocking. Prabakaran Page 18 7/4/2011 . is to open it non-blocking for read. The order the FIFOs is opened is important. mode_t mode). an open for read will block until the FIFO is also opened for a write. Fifo is created as follows int mkfifo (const char *pathname. However a FIFO has a name in the filesystem.

Create a new fifo Step 4 : use the descriptor to read the contents of the fifo Dr. N.MC9228 – SYSTEM SOFTWARE LAB Step 3: On the Server Side . Prabakaran Page 19 7/4/2011 .

users.h> void catch_int() { signal(SIGINT.MC9228 – SYSTEM SOFTWARE LAB Exercise No : 12 Date Program to catch signal Aim to write a program in c to catch a signal Description : A signal is a message which can be sent to a running process. SIGKILL always stops a process and SIGSTOP always moves a process from the foreground to the background. Prabakaran Page 20 7/4/2011 . ) pause(). } main() { signal(SIGINT. Programmers can code their applications to respond in customized ways to most signals.h> #include <signal. Each Unix signal has a default set of effects on a Unix program. printf("Don't do that\n").. These two signals cannot be "caught" by a signal handler. Two signals are unable to be redefined by a signal handler. N. or administrators.h> #include <unistd. Program #include <stdio. catch_int). for ( . } Dr. catch_int). Signals can be initiated by programs. These custom pieces of code are called signal handlers.

Algorithm Server Step 1: Create a shared memory using the shmget() function Step 2 : Attach the shared memory to the user space by the shmat() function Program to perform inter process communication using shared memory Dr. • The structure definition for the shared memory segment control structures and prototypes can be found in <sys/shm. the process can read or write to the segment. Once created.h>. • • A shared segment can be attached multiple times by the same process. as allowed by the permission requested in the attach operation. Prabakaran Page 21 7/4/2011 . The original owner of a shared memory segment can assign ownership to another user with shmctl(). Other processes with proper permission can perform various control functions on the shared memory segment using shmctl().MC9228 – SYSTEM SOFTWARE LAB Exercise No : 13 Date Aim : To write a C Program to perform inter process communication using Shared Memory Description • • • • • One program will create a memory portion which other processes can access process creates a shared memory segment using shmget()|. It can be detached using shmdt(). The identifier of the segment is called the shmid. It can also revoke this assignment. a shared segment can be attached to a process address space using shmat(). Once attached. N. A shared memory segment is described by a control structure with a unique ID that points to an area of physical memory.

Prabakaran Page 22 7/4/2011 . N.MC9228 – SYSTEM SOFTWARE LAB Step 3 : Now use the read and write functions to perform operations on the memory Client Step 1: Create a shared memory using the shmget() function Step 2 : Attach the shared memory to the user space by the shmat() function Step 3 : Now use the read and write functions to perform operations on the memory Dr.

1.2 If there is a opcode 4. the error message should be displayed 4.MC9228 – SYSTEM SOFTWARE LAB Exercise No :14 Date Aim : Pass 1 of 2 Pass Assembler To write a C Program to generate symbol table and assign addresses form input table Description • • Memory address is assigned for all labels and mnemonics Generate the Symbol table using optable. Prabakaran Page 23 7/4/2011 . if opcode is “BYTE”. then increment the LOCCTR by 3 If opcdoe is “WORD”. If opcdoe is “RESW”.2. N. Algorithm Step 1: Open and Read the input file Step 2: If the input line has the opcode “START” do the following 2.2 If opcode is present in the OPTAB. if opcode is “RESB” then increment LOCCTR by the integer equivalent of the operand value.2.2. if the symbol is not available then enter the symbol into the SYMTAB. 2.1 If there is a symbol in the label field 4. then increment LOCCTR by the integer equivalent of the operand value*3.2 Otherwise if there is no value in the operand field then LOCCTR is set to 0 Step 3: Write the input line to the intermediate file Step 4: Do the following steps until the opcode is “END” 4. Otherwise . then increment LOCCTR by 3 . initalize the LOCCTR to the Operand value. 4. Dr. then increment LOCCTR by 1.3 Write the processed lines in the intermiediate file alogn with their location counters.1 4. along with the memory address in which it is stored.1 Find if there is any operand field after “START”.1 Check the Symbol Table.

Substract the starting address of the program from the final value of the LOCCTR Step 6: Close all the files and exit Dr. Prabakaran Page 24 7/4/2011 .MC9228 – SYSTEM SOFTWARE LAB Step 5 To find the length of the program. N.

Step 2: If the first line is the opcode “START” . Step 4 : Close the files and exit Dr. RESB etc convert the constants to the object code. opcode and operand field values of the corresponding statement directly to the final output file.3 If the opcode is BYTE. Algorithm Step 1: Open and Read the first line from the intermediate file. then write the label.1 Start write the location counter. N. Prabakaran Page 25 7/4/2011 . opcode and operand fields of the corresponding statement to the output file. WORD. Finally. then the operand address is assigned as zero and it is assembled with the object code of the instruction.MC9228 – SYSTEM SOFTWARE LAB Exercise No :15 Date Aim : Pass 2 of 2 Pass Assembler To write a C Program to generate symbol table and assign addresses form input table Description • • • Memory address is assigned for all labels and mnemonics Generate the Symbol table using optable. 3. along with the object code.2 If there is no symbol/label in the operand field. Step 3 Do the following steps until the opcode is “END” 3. 3. generate the Object code.

MC9228 – SYSTEM SOFTWARE LAB Exercise No :16 Date Absolute Loader Aim : To write a C Program to generate the memory address for text record Description • • Write the hexadecimal representation from the text record If RESB is character . Prabakaran Page 26 7/4/2011 . iii. Algorithm Step 1: Step 2: Step 3: Step 4: Read the Header Record Verify program name and length Read first Text Record from the input file Process the following steps until an End record is reached i. Go the address specified in End record Close all the files and exit Dr. Step 5: Step 6: Read next Text record from the input file. ii. if object is in character form converts it to internal hexadecimal representation Move object codes to specified locations in memory Write the starting location counter of value of block of object code and corresponding internal representation to the output file iv. N. insert value of x.

Prabakaran Page 27 7/4/2011 . added along with the corresponding control section until the an end record is reached. Repeat the process from step from 3 tyo 5 until there are no more records. Step 5: Step 6: Assign the starting address of next control section as the address of the current control section plus the length of the control section. Enter the control section name with its address into the external symbol table. From the details of the header read store the control setion length in a variable b. N.MC9228 – SYSTEM SOFTWARE LAB Exercise No :17 Date A Direct Linking Loader Aim : To write a C Program to link all the control sections. Description • • Assign the address for first control sections Assign the starting addresses of next control section of the current control section Algorithm Step 1: Step 2: Step 3: Enter the location where the program has to be loaded Assign the address got from the user as the first control section address Read first Header Record from the control sections a. Dr. Step 4: From each symbol in the subsequent ‘D’ records the symbol must be entered into the symbol table along with its address.

open the file in write mode and move the content to the file and close the file iv. If file found delete the file otherwise display file not found if choice is ‘5’ then. if choice is ‘4’ then.MC9228 – SYSTEM SOFTWARE LAB Exercise No :18 Date Text Editor Aim : To write a C Program to generate text editor Description • Design with Menu for new. get the data filename from the user. v. ii. terminate the execution. if choice is ‘1’ then. N. Prabakaran Page 28 7/4/2011 . enter he filename to be deleted. enter the text using editor and type ‘end’ to terminate if choice is ‘2’ then. If file name found open the file in read mode and enter the content else display invalid file name. iii. Dr. open save delete and exit the file Algorithm Step 1: Step 2: Design a Menu using Switch Case Get the choice and do the following i. enter the file name to be open. if choice is ‘3’ the.