Beruflich Dokumente
Kultur Dokumente
Department of Computer Applications Rajalakshmi Engineering College Thandalam, Chennai 602 105 Lab Manual 600208 - System Software Lab
By
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
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, command mode, insert mode and command line mode. 1. Command mode: letters or sequence of letters interactively command vi. Commands are case sensitive. The ESC key can end a command. 2. Insert mode: Text is inserted. The ESC key ends insert mode and returns you to command mode. One can enter insert mode with the "i" (insert), "a" (insert after), "A" (insert at end of line), "o" (open new line after current line) or "O" (Open line above current line) commands. 3. Command line mode: One enters this mode by typing ":" which puts the command line entry at the foot of the screen. 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
Action
Dr. N. Prabakaran
Page 3
7/4/2011
MC9228 SYSTEM SOFTWARE LAB 5b e 5e 0 (zero) $ ) ( G % '. '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. Place cursor on {}[]() and type "%". 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. Move cursor to previous lower case mark.
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. Press esc key to end insertion. Delete word Delete 4 words Change word Action
Dr. N. Prabakaran
Page 4
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. Decrement number under the cursor. 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". This will find "string s;", "s = fn(x);", "x = fn(s);", etc n N . Terminate session:
Find next occurrence of search_word Find previous occurrence of search_word repeat last command action.
Use command: ZZ Save changes and quit. Use command line: ":wq" Save (write) changes and quit. Use command line: ":w" Save (write) changes without quitting. Use command line: ":q!" Ignore changes and quit. No changes from last write will be saved. Use command line: ":qa" Quit all files opened.
Dr. N. Prabakaran
Page 5
7/4/2011
MC9228 SYSTEM SOFTWARE LAB Exercise No : 2 Date Aim To execute the Unix shell Ccmmands and get familiar with the UNIX environment Description 1. Access Control 3. Editors emacs - screen-oriented text editor ex - line-oriented text editor pico - simple, screen-oriented text editor sed - stream-oriented text editor vi - full-screen text editor vim - full-screen text editor ("vi-improved") cd - change working directory chmod - change the protection of a file or directory exit - terminate a shell logout - sign off; end session (C shell and bash shell only) rlogin - log in remotely to another UNIX system CC - C++ compiler cc - C compiler crontab - maintain periodic tasks csh - C shell command interpreter g++ - GNU C++ compiler kill - kill a process sh - Bourne shell command interpreter /bin/time - time a command
2. Programming Tools
Dr. N. Prabakaran
Page 6
7/4/2011
MC9228 SYSTEM SOFTWARE LAB cmp - compare two files comm - select/reject lines common to two sorted files compress - compress a file uncompress - restore compressed file cp - copy files crypt - encrypt/decrypt files (not on ADS) diff - compare the contents of two ASCII files file - determine file type grep - search a file for a pattern ln - make a link to a file ls - list the contents of a directory mkdir - create a directory mv - move or rename files and directories pwd - show the full pathname of your working directory quota - display disk usage and limits rm - delete (remove) files rmdir - delete (remove) directories sort - sort or merge files tee - copy input to standard output and other files umask - change default file protections uniq - report (or delete) repeated lines in a file wc - count lines, words, and characters in a file cat - show the contents of a file; catenate files fold - fold long lines to fit output device head - show first few lines of a file lpr - print a file lprm - remove jobs from the printer spooling queue more - display a file, one screen at a time page - like "more", but prints screens top to bottom
Dr. N. Prabakaran
Page 7
7/4/2011
MC9228 SYSTEM SOFTWARE LAB tail - show the last part of a file zcat - display a compressed file clear - clear terminal screen echo - echo arguments netstat - show network telnet - run Telnet to log in to remote host bg - put suspended process into background fg - bring process into foreground jobs - list processes ^y - suspend process at next input request ^z - suspend current process date - show date and time finger - look up user information history - list previously issued commands last - indicate last login of users Ps - show process status pwd - display full pathname of working directory uptime - show system load, how long system has been up w - show who is on system, what command each job is executing who - show who is logged onto the system whois - Internet user name directory service
6. Miscellaneous
7. Process Control
Dr. N. Prabakaran
Page 8
7/4/2011
MC9228 SYSTEM SOFTWARE LAB Exercise No : 3 Date Shell Programming - to display some well known commands
Program : clear echo "This is information provided by mysystem.sh. Program starts now." echo "Hello, $USER" echo "Today's date is `date`, this is week `date +"%V"`." echo "These users are currently connected:" w | cut -d " " -f 1 - | grep -v USER | sort -u echo echo "This is `uname -s` running on a `uname -m` processor." echo "This is the uptime information:" uptime
Dr. N. Prabakaran
Page 9
7/4/2011
Grep, Awk
awk is a programmable, pattern-matching, and processing tool available in UNIX. It works equally well with text and numbers awk is not just a command, but a programming language too . awk utility is a pattern scanning and processing language. 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.
Program
$1 is field #1, $2 is field #2, etc. 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. Prabakaran
Page 10
7/4/2011
MC9228 SYSTEM SOFTWARE LAB Exercise No : 5 Date Aim To write a C Program to demonstrate the use of CREATE, OPEN, READ and WRITE system calls. Description The C Program to create a new file and write the description about the CREATE , 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. The prototype of the function is int creat(const char *pathname, mode_t mode) File descriptor is returned if the file is opened, else the function returns a negative number . <sys/types.h>, <sys/stat.h>, <fcntl.h> header files are required to use this function . This function creates and also opens the file in write only mode Open A file is opened or created using the open system call. 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, int oflag, ./*, mode_t mode */) Close function This function is used to close an open file. Header files required for this function is(unistd.h>. It returns 0 if OK else in case of failure to close the file it returns -1. 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, write, close system calls
Dr. N. Prabakaran
MC9228 SYSTEM SOFTWARE LAB This function is used to read data from a open file. If the read is successful, the number of bytes read is returned. However , if the end of the file is encountered , 0 is returned. In case of error it returns -1. The prototype of the function is ssize_t read(int fd, void *buff, size_t nbytes); Write Data is written to an open file using this function. If the write was successful it returns the number of bytes written . In case of error writing to the file -1 is returned. The write generally begins with the current offset If the file was opened with O_APPEND, then offset is set to the current end of the file After write, the offset is incremented by number of bytes written The prototype of the function is ssize_t write(int fd, const void *buf, size_t nbytes);
Algorithm Step 1: Include the header file sys/stat.h, fcntl.h, string.h and stdio.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. Step 4: Create a new file using creat(() function. 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 9: Now, open the same file using open () system call, 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. N. Prabakaran
Page 12
7/4/2011
Exercise No : 6 Date
Aim To write a C Program to demonstrate the use of lseek function Description Every open file has an associated current file offset. 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. An open files offset can be explicitly positioned using the lseek system call The required header files are <sys/types.h>, <unistd.h> The prototype of the function is off_t lseek(int fd, off_t offset, int whence) On success it returns the new offset , else it returns error The whence indicate whether offset should be considered a. from the beginning of the file (with whence 0, SEEK_SET), b. from the current position of the read/write offset (with whence 1, SEEK_CUR), c. from the end of the file (with whence 2, SEEK_END). The files offset can be greater than the files current size. In that case the fille write will extend the file . This is referred to as creating a hole in the file. Any bytes that have not been written are read back as 0. Algorithm Step 1 : get the string to be entered from the user Step 2 : use open sytem call to open a file f1.txt in Read Write mode Step 3 : Using the Write System call , write the user inputs into the f1.txt Step 3 : Open the file f2.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. N. Prabakaran
Page 13
7/4/2011
Description : The fork() function creates a new process. The new process (child process) is an exact copy of the calling process (parent process) except as detailed below. The child process has a unique process ID. The child process ID also does not match any active process group ID. The child process has a different parent process ID (that is, the process ID of the parent process). 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 child process has its own copy of the parent's open directory streams. Each open directory stream in the child process may share directory stream positioning with the corresponding directory stream of the parent.
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. N. Prabakaran
Page 14
7/4/2011
To write a C Program demonstrate the use of Exec Description : The exec() family of functions will initiate a program from within a program. Program
#include <unistd.h> main() { execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);
Dr. N. Prabakaran
Page 15
7/4/2011
Exercise No : 9 Date
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. Message queues are implemented as linked lists of data stored in shared memory. The message queue itself contains a series of data structures, one for each message, each of which identifies the address, type, and size of the message plus a pointer to the next message in the queue. To allocate a queue, a program uses the msgget() system call . 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, int msgflg); 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. N. Prabakaran
Page 16
7/4/2011
Exercise No : 10 Date
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. Pipes provide a method of one-way communications (hence the term half-duplex) between processes. When a process creates a pipe, the kernel sets up two file descriptors for use by the pipe. One descriptor is used to allow a path of input into the pipe (write), while the other is used to obtain data from the pipe (read). At this point, the pipe is of little practical use, as the creating process can only use the pipe to communicate with itself int pipe( int fd[2] ); fd[0] is set up for reading, 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. N. Prabakaran
Page 17
7/4/2011
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. However a FIFO has a name in the filesystem, allowing unrelated processes to communicate. FIFOs must be opened for read only or write only (not read and write). The order the FIFOs is opened is important. For example, an open for read will block until the FIFO is also opened for a write. Likewise if you open one for write then the calling process will block if the FIFO is not opened for reading. To open the system tasks FIFO without blocking, is to open it non-blocking for read. Then, if you also open it for write only. Fifo is created as follows int mkfifo (const char *pathname, mode_t mode); Algorithm Step 1 : In the client program create a fifo Step 2 : write the data through the fifo Dr. N. Prabakaran Page 18 7/4/2011
MC9228 SYSTEM SOFTWARE LAB Step 3: On the Server Side , Create a new fifo Step 4 : use the descriptor to read the contents of the fifo
Dr. N. Prabakaran
Page 19
7/4/2011
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. Signals can be initiated by programs, users, or administrators. Each Unix signal has a default set of effects on a Unix program. Programmers can code their applications to respond in customized ways to most signals. These custom pieces of code are called signal handlers. Two signals are unable to be redefined by a signal handler. SIGKILL always stops a process and SIGSTOP always moves a process from the foreground to the background. These two signals cannot be "caught" by a signal handler. Program #include <stdio.h> #include <unistd.h> #include <signal.h> void catch_int() { signal(SIGINT, catch_int); printf("Don't do that\n"); } main() { signal(SIGINT, catch_int); for ( ;; ) pause(); }
Dr. N. Prabakaran
Page 20
7/4/2011
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()|. The original owner of a shared memory segment can assign ownership to another user with shmctl(). It can also revoke this assignment. Other processes with proper permission can perform various control functions on the shared memory segment using shmctl(). Once created, a shared segment can be attached to a process address space using shmat(). It can be detached using shmdt(). Once attached, the process can read or write to the segment, as allowed by the permission requested in the attach operation. A shared segment can be attached multiple times by the same process. A shared memory segment is described by a control structure with a unique ID that points to an area of physical memory. The identifier of the segment is called the shmid. The structure definition for the shared memory segment control structures and prototypes can be found in <sys/shm.h>. 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. N. Prabakaran
Page 21
7/4/2011
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. N. Prabakaran
Page 22
7/4/2011
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.
Algorithm Step 1: Open and Read the input file Step 2: If the input line has the opcode START do the following 2.1 Find if there is any operand field after START, initalize the LOCCTR to the Operand value. 2.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.1 If there is a symbol in the label field 4.1.1 Check the Symbol Table, if the symbol is not available then enter the symbol into the SYMTAB, along with the memory address in which it is stored. Otherwise , the error message should be displayed 4.2 If there is a opcode 4.2.1 4.2.2 If opcode is present in the OPTAB, then increment the LOCCTR by 3 If opcdoe is WORD, then increment LOCCTR by 3 ; if opcode is BYTE, then increment LOCCTR by 1; If opcdoe is RESW, then increment LOCCTR by the integer equivalent of the operand value*3; if opcode is RESB then increment LOCCTR by the integer equivalent of the operand value. 4.2.3 Write the processed lines in the intermiediate file alogn with their location counters. Dr. N. Prabakaran Page 23 7/4/2011
MC9228 SYSTEM SOFTWARE LAB Step 5 To find the length of the program, Substract the starting address of the program from the final value of the LOCCTR Step 6: Close all the files and exit
Dr. N. Prabakaran
Page 24
7/4/2011
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. Finally, generate the Object code.
Algorithm Step 1: Open and Read the first line from the intermediate file. Step 2: If the first line is the opcode START , then write the label, opcode and operand field values of the corresponding statement directly to the final output file. Step 3 Do the following steps until the opcode is END 3.1 Start write the location counter, opcode and operand fields of the corresponding statement to the output file, along with the object code. 3.2 If there is no symbol/label in the operand field, then the operand address is assigned as zero and it is assembled with the object code of the instruction. 3.3 If the opcode is BYTE, WORD, RESB etc convert the constants to the object code. Step 4 : Close the files and exit
Dr. N. Prabakaran
Page 25
7/4/2011
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 , insert value of x.
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. ii. iii. 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. Step 5: Step 6: Read next Text record from the input file. Go the address specified in End record Close all the files and exit
Dr. N. Prabakaran
Page 26
7/4/2011
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. From the details of the header read store the control setion length in a variable b. Enter the control section name with its address into the external symbol table. Step 4: From each symbol in the subsequent D records the symbol must be entered into the symbol table along with its address, added along with the corresponding control section until the an end record is reached. 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. Repeat the process from step from 3 tyo 5 until there are no more records.
Dr. N. Prabakaran Page 27 7/4/2011
Text Editor
Aim : To write a C Program to generate text editor Description Design with Menu for new, 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. ii. if choice is 1 then, enter the text using editor and type end to terminate if choice is 2 then, enter the file name to be open. If file name found open the file in read mode and enter the content else display invalid file name. iii. if choice is 3 the, get the data filename from the user, open the file in write mode and move the content to the file and close the file iv. v. if choice is 4 then, enter he filename to be deleted. If file found delete the file otherwise display file not found if choice is 5 then, terminate the execution.
Dr. N. Prabakaran
Page 28
7/4/2011