unix45

Upload: sandeep-sheoran

Post on 14-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 unix45

    1/15

    1

    Name: Bhawani Pratap Singh

    Registration No: 520840181

    Learning Centre: Atmark Infotech

    Learning Centre Code: 01669

    Course: Bachelor of Computer Applications (BCA)

    Subject: BC0056 UNIX Operating System

    Semester: 5th Semester

    Module No: I

    Date of Submission: 20 November 2010

    Marks Awarded:

    Signature of Signature ofSignature of

    Coordinator Centre headEvaluator

  • 7/27/2019 unix45

    2/15

    2

    August 2010

    Bachelor of Computer Application (BCA) Semester 5

    BC0056 UNIX Operating System 4 Credits

    (Book ID: B0973)

    Assignment Set 1 (40 Marks)

    Each question carries eight marks

    1. What are the layers present in UNIX Architecture? List and explaineach of them.

    Answer

    Architecture of UNIX

    UNIX System Kernel

    Three major tasks of kernel:

    Process Management

    Device Management

    File Management Three additional Services for Kernel:

    Virtual Memory

    Networking

    Network File Systems Experimental Kernel Features:

    Multiprocessor support

    Lightweight process (thread) support

  • 7/27/2019 unix45

    3/15

    3

    The layers of UNIX

    Other applications

    Standard application

    Standard libraries

    File system Process control

    Hardware

    Process Control Subsystem

    Process Synchronization Interprocess communication Memory management: Scheduler: process scheduling (allocate CPU to Processes)

    File system

    A file system is a collection of files and directories on a disk or tape in standard UNIXfile system format.

    Kernels file system regulates data flow between the kernel and secondary storage

    devices.

    System Call for

    WC

    grep

    fopen()

    fork()

  • 7/27/2019 unix45

    4/15

    4

    1. Process Control:

    fork: create a new process

    wait: allow a parent process to synchronize its execution with the exit of a child process.

    exec: invoke a new program.exit: terminate process execution

    2. File system:

    File: open, read, write, lseek, closeinode: chdir, chown chmod, stat fstatothers: pipe dup, mount, unmount, link, unlink

    System call: fork()

    fork: the only way for a user to create a process in Unix operating system. The process that invokes fork is called parent process and the newly created process is

    called child process. The syntax of fork system call:

    newpid = fork(); On return from fork system call, the two processes have identical copies of their user-

    level context except for the return value pid. In parent process, newpid = child process id In child process, newpid = 0.

    2. What is the significance of root and swap? Discuss their uses andcharatertistics.

    Answer

    Most UNIX systems spilt a disk into as many as 8 partitions. If there are multiple disks, everysuch disk should have at least one file system on it. In general, most UNIX has two filesystems:

    Root file system

    It contains the root directory, /bin/usr/bin/etc/sbin/usr/sbin/dev and /lib directories, all thetools and utilities that are just adequate to keep the system booted in single user mode. When

  • 7/27/2019 unix45

    5/15

    5

    the system is booted in single user mode, this is the only file system available to the systemadministrator. In computerfile systems, the root directory is the first or top-most directory in ahierarchy. It must be present in every UNIX system. It can be likened to the root of a tree the starting point where all branches originate.

    Metaphor

    To use the example of a physical file cabinet, if the separate drawers in the file cabinet arerepresented as the highest level ofsub-directories in the file system or system prompt, then theroom the file cabinet is in, may be represented as the root directory. That is, the otherdirectories may be inside it, but the root directory cannot go in any other directories, at least inthat file system. In most operating systems, files can be placed inside the root directory, aswell in its sub-directories. One may envision this as placing paper files anywhere in the room,or into any file cabinet within the room.

    Multiple root directories

    UNIX abstracts the nature of this tree hierarchy entirely, and in Unix and Unix-likesystems,the root directory is denoted /. Though the root directory is conventionally referred to as /, thedirectory entry itself has no name its name is the "empty" part before the initial directoryseparator character (/). All filesystem entries, including mountedpartitions are "branches" ofthis root.

    In UNIX-like operating systems, each processhas its own idea of what the root directory is.For most processes this is the same as the system's actual root directory, but it can be changedby calling the chrootsystem call. This is typically done for security purposes to restrict whichfiles a process may access to just a subset of the file hierarchy.

    Root

    On many Unixes, there is also a directory which is named root. Confusingly, it is not a rootdirectory in the sense of this article, but rather the home directory of the Superuser(conventionally known as "root").

    Swapping in Unix

    In computer operating systems, paging is one of the memory-management schemes by whicha computer can store and retrieve data from secondary storage for use in main memory. In the

    paging memory-management scheme, the operating system retrieves data from secondarystorage in same-size blocks called pages. The main advantage of paging is that it allows thephysical address space of a process to be noncontiguous. Before paging, systems had to fit

    http://en.wikipedia.org/wiki/Computinghttp://en.wikipedia.org/wiki/File_systemhttp://en.wikipedia.org/wiki/File_systemhttp://en.wikipedia.org/wiki/Directory_(file_systems)http://en.wikipedia.org/wiki/Tree_(data_structure)http://en.wikipedia.org/wiki/Subdirectoryhttp://en.wikipedia.org/wiki/Subdirectoryhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Computer_filehttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/Unix-likehttp://en.wikipedia.org/wiki/Unix-likehttp://en.wikipedia.org/wiki/Mount_(computing)http://en.wikipedia.org/wiki/Partition_(IBM_PC)http://en.wikipedia.org/wiki/Process_(computing)http://en.wikipedia.org/wiki/Process_(computing)http://en.wikipedia.org/wiki/Chroothttp://en.wikipedia.org/wiki/System_callhttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/roothttp://en.wikipedia.org/wiki/Home_directoryhttp://en.wikipedia.org/wiki/Superuserhttp://en.wikipedia.org/wiki/Computinghttp://en.wikipedia.org/wiki/File_systemhttp://en.wikipedia.org/wiki/Directory_(file_systems)http://en.wikipedia.org/wiki/Tree_(data_structure)http://en.wikipedia.org/wiki/Subdirectoryhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Computer_filehttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/Unix-likehttp://en.wikipedia.org/wiki/Mount_(computing)http://en.wikipedia.org/wiki/Partition_(IBM_PC)http://en.wikipedia.org/wiki/Process_(computing)http://en.wikipedia.org/wiki/Chroothttp://en.wikipedia.org/wiki/System_callhttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/roothttp://en.wikipedia.org/wiki/Home_directoryhttp://en.wikipedia.org/wiki/Superuser
  • 7/27/2019 unix45

    6/15

    6

    whole programs into storage contiguously, which caused various storage and fragmentationproblems.

    Paging is an important part of virtual memory implementation in most contemporary general-purpose operating systems, allowing them to use disk storage for data that does not fit into

    physical Random-access memory (RAM). Paging is usually implemented as architecture-specific code built into the kernel of the operating system.

    The main functions of paging are performed when a program tries to access pages that are notcurrently mapped to physical memory (RAM). This situation is known as a page fault. Theoperating system must then take control and handle the page fault, in a manner invisible to theprogram. Therefore, the operating system must:

    1. Determine the location of the data in auxiliary storage.2. Obtain an empty page frame in RAM to use as a container for the data.3. Load the requested data into the available page frame.

    4. Update the page table to show the new data.5. Return control to the program, transparently retrying the instruction that caused the

    page fault.

    Because RAM is faster than auxiliary storage, paging is avoided until there is not enoughRAM to store all the data needed. When this occurs, a page in RAM is moved to auxiliarystorage, freeing up space in RAM for use. Thereafter, whenever the page in secondary storageis needed, a page in RAM is saved to auxiliary storage so that the requested page can then beloaded into the space left behind by the old page. Efficient paging systems must determine thepage to swap by choosing one that is least likely to be needed within a short time. There arevariouspage replacement algorithmsthat try to do this.

    Most operating systems use some approximation of the least recently used (LRU) pagereplacement algorithm (the LRU itself cannot be implemented on the current hardware) orworking set based algorithm.

    If a page in RAM is modified (i.e. if the page becomes dirty) and then chosen to be swapped,it must either be written to auxiliary storage, or simply discarded.

    To further increase responsiveness, paging systems may employ various strategies to predictwhat pages will be needed soon so that it can preemptively load them.

    Swap prefetch

    A few operating systems use anticipatory paging, also called swap prefetch. These operatingsystems periodically attempt to guess which pages will soon be needed, and start loading them

    http://en.wikipedia.org/wiki/Page_replacement_algorithmhttp://en.wikipedia.org/wiki/Page_replacement_algorithmhttp://en.wikipedia.org/wiki/Page_replacement_algorithmhttp://en.wikipedia.org/wiki/Least_recently_usedhttp://en.wikipedia.org/wiki/Working_sethttp://en.wikipedia.org/wiki/Page_replacement_algorithmhttp://en.wikipedia.org/wiki/Least_recently_usedhttp://en.wikipedia.org/wiki/Working_set
  • 7/27/2019 unix45

    7/15

    7

    into RAM. There are various heuristics in use, such as "if a program references one virtualaddress which causes a page fault, perhaps the next few pages' worth of virtual address

    space will soon be used" and "if one big program just finished execution, leaving lots of

    free RAM, perhaps the user will return to using some of the programs that were recently

    paged out". Pagingsometimes referred to a memory allocation scheme that used fixed-length

    pages as opposed to variable-lengthsegments, without implicit suggestion that virtualmemorytechnique were employed at all or that those pages were transferred to disk. Such usage is raretoday.Some modern systems use the term swapping along with paging. Historically, swappingreferred to moving from/to secondary storage a whole program at a time, in a scheme knownas roll-in/roll-out. In the 1960s, after the concept of virtual memory was introducedin twovariants, either using segments or pagesthe term swapping was applied to moving,respectively, either segments or pages, between disk and memory. Today with the virtualmemory mostly based on pages, not segments, swappingbecame a fairly close synonym ofpaging, although with one difference.

    In many popular systems, there is a concept known as page cache, of using the same singlemechanism forbothvirtual memory anddisk caching. A page may be then transferred to orfrom any ordinary disk file, not necessarily a dedicated space. Page inis transferring a pagefrom the disk to RAM. Page out is transferring a page from RAM to the disk. Swap inandoutonly refer to transferring pages between RAM and dedicated swap space or swap file, andnot any other place on disk.

    3. What do you mean by a Process? What are the various possible statesof Process? Discuss.

    Answer

    http://en.wikipedia.org/wiki/Memory_allocationhttp://en.wikipedia.org/wiki/Segmentation_(memory)http://en.wikipedia.org/wiki/Segmentation_(memory)http://en.wikipedia.org/wiki/Segmentation_(memory)http://en.wikipedia.org/w/index.php?title=Roll-in/roll-out&action=edit&redlink=1http://en.wikipedia.org/wiki/Page_cachehttp://en.wikipedia.org/wiki/Page_cachehttp://en.wikipedia.org/wiki/Cachehttp://en.wikipedia.org/wiki/Cachehttp://en.wikipedia.org/wiki/Memory_allocationhttp://en.wikipedia.org/wiki/Segmentation_(memory)http://en.wikipedia.org/w/index.php?title=Roll-in/roll-out&action=edit&redlink=1http://en.wikipedia.org/wiki/Page_cachehttp://en.wikipedia.org/wiki/Cache
  • 7/27/2019 unix45

    8/15

    8

    A process is a collection of interrelated work or tasks initiated in response to an event thatachieves a specific result for the customer of the process.

    In general, a UNIX process is one execution of a program or command.

    Every process transforms one or more input streams into one or more output streams.Whenever you type a UNIX command, you are creating a process to carry out the work of that

    command. In a multitasking computer system, processes may occupy a variety of states. Thesedistinct states may not actually be recognized as such by the operating system kernel; howeverthey are a useful abstraction for the understanding of processes.

    Created (Also called New)

    When a process is first created, it occupies the "created" or "new" state. In this state, theprocess awaits admission to the "ready" state. This admission will be approved or delayed bya long-term, or admission scheduler. Typically in most desktop computer systems, thisadmission will be approved automatically, however for real-time operating systems thisadmission may be delayed. In a real time system, admitting too many processes to the "ready"state may lead to over saturation and over contention for the systems resources, leading to an

    inability to meet process deadlines.

    Ready or waiting(Also called waiting or runnable)

    A "ready" or "waiting" process has been loaded into main memory and is awaiting executionon a CPU (to be context switched onto the CPU by the dispatcher, or short-term scheduler).There may be many "ready" processes at any one point of the systems execution.Example : in a one processor system, only one process can be executing at any one time, andall other "concurrently executing" processes will be waiting for execution.

    A ready queue or run queue is used in computer scheduling. Modern computers are capable of

    running many different programs or processes at the same time. However, the CPU is onlycapable of handling one process at a time. Processes that are ready for the CPU are kept in aqueue for "ready" processes. Other processes that are waiting for an event to occur, such as

  • 7/27/2019 unix45

    9/15

    9

    loading information from a hard drive or waiting on an internet connection, are not in theready queue.

    Running

    A process moves into the running state when it is chosen for execution by the CPU. The

    process's instructions are executed by one of the CPUs (or cores) of the system. There is atmost one running process per CPU or core.

    Blocked

    A process that is blocked on some event such as I/O operation completion or a signal isblocked and kept in a blocked state; CPUs cannot select any processes from this state. Oncethe event for which they were waiting occurs, the process is moved to the ready state.

    Terminated

    A process may be terminated, either from the "running" state by completing its execution orby explicitly being killed. In either of these cases, the process moves to the "terminated" state.

    If a process is not removed from memory after entering this state, the process will be called azombie.

    Additional process states

    Two additional states are available for processes in systems that support virtual memory. Inboth of these states, processes are "stored" on secondary memory (typically a hard disk).

    Swapped out and waiting (Also called suspended and waiting)

    In systems that support virtual memory, a process may be swapped out, that isremoved from main memory and placed in virtual memory by the mid-term scheduler.From here the process may be swapped back into the waiting state.

    Swapped out and blocked (Also called suspended and blocked) Processes that are blocked may also be swapped out. In this event the process is bothswapped out and blocked, and may be swapped back in again under the samecircumstances as a swapped out and waiting process, although in this case, the processwill move to the blocked state, and may still be waiting for a resource to becomeavailable).

    4. What is the purpose of setting accounts in Unix ? Explain the conceptof privileges and its significance.

    Answer

    Purpose of setting accounts in UNIX

  • 7/27/2019 unix45

    10/15

    10

    Consider there is a user user1 then:

    For security- every user needs to have an account, which includes user name and password.This also indicates who owns the files and directories on the system and what groups the usersmay belong to. A system administrator is responsible for creating, modifying and deletinguser accounts. When a user account is created, an entry is made in the /etc/passwdfile. When

    a group account is created, an entry is made in the/etc/group file. The user name is a uniquename that identifies the user1 to the system. The system administrator will make sure thatno one has the same user name before allowing user1 to have it. The password assigned istemporary and it can be changed by user1 afterwards.

    To start a UNIX system, users are expected to log on to the system. The system prompts asksto enter the user name and password. After entering the correct information and if the systemadministrator is set up correctly, the user can now use the system. If incorrect username andpassword has been entered then again the user will be asked to enter the information, this goestill a user enters correct information.

    PrivilegesOn many computeroperating systems, the super user, or root, is a special user account usedforsystem administration. This account should be used to do system wide administrativeaccount. It is important to be careful when using this account because it has special features.Most users cannot set up a new user account nor do other administrative procedures. Thesuper-user can do anything on the system. For this reason every user must have their own useraccount, which does not have root privilege. By this, their personal activities will affect onlytheir environment and will be in no danger of causing system wide problems.

    Separation of administrative privileges from normal user privileges makes an operatingsystem more resistant to viruses and other malware. Additionally, in organizations,

    administrative privileges are often reserved for authorized individuals in order to controlabuse, misuse, or other undesired activities by end-users.

    In a multiuser, non-personal environment, most likely there is an only one user privilege. Thissecurity is even more important when more than one person is involved because one mistakeby the root can affect every user and the entire system. UNIX also has security to helpprevent different users from harming each other on a multiuser system. Each user has apersonal environment and can selectively let groups to access their works. While working in ateam and the user wants his work to be shared by many people on the system, then the usershould allow access to everyone.

    5. What are the uses of Erase, Kill and eof commands in UNIX? Discuss.

    http://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/User_accounthttp://en.wikipedia.org/wiki/System_administrationhttp://en.wikipedia.org/wiki/System_administrationhttp://en.wikipedia.org/wiki/Computer_virushttp://en.wikipedia.org/wiki/Malwarehttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/User_accounthttp://en.wikipedia.org/wiki/System_administrationhttp://en.wikipedia.org/wiki/Computer_virushttp://en.wikipedia.org/wiki/Malware
  • 7/27/2019 unix45

    11/15

    11

    Answer

    Different control characters are present in UNIX, some are erase, kill and eof. To see theirvalues or control associated to them types stty command.

    EraseOn a system, erase which means to back up over the last character typed, done by typing ^H.

    The erase key helps us to correct our typing errors. To erase any character using erase, first

    type the character, say x, then press ^H (control H) to erase it. If everything is done

    correcting then the cursor should have backed up to be on top of x and the next key will be

    displayed where x was present. If ^H is not working for erase then we can set our own

    value for it by using $ stty command. To set a key for erase

    $stty erase ^H or any other value of the users choice.

    The erase key is one of the most used control keys, because typing without mistakes isdifficultto do. Therefore, most keyboards have one or more special keys that are suited to this job.Either delete or backspace always works as an erase key.

    Kill

    The kill control is similar to the erase control character, in the it allows the user to back up

    over typing mistakes. Whereas erase backs up one character at a time, kill backs up all the

    way to the prompt. Therefore, if the user is typing a really long command and realize, toward

    the end, that he forgot to do some other command first. In this case the user can start over by

    typing the control character associated with kill. Normally kill command is set to ^X, but it

    can be changed according to users convenience. After using kill command all the lines typed

    by the user will be erased and cursor will be present just after the prompt.

    Eof (end of file)

    The eof control character is used to signal the end of output. The letters eof come from end

    of file. The normal value of the eof control character is ^D, but should be verified by using

    the stty command. The stty command is also used to set the value of control characters.

  • 7/27/2019 unix45

    12/15

    12

    Name: Bhawani Pratap Singh

    Registration No: 520840181

    Learning Centre: Atmark Infotech

    Learning Centre Code: 01669

    Course: Bachelor of Computer Applications (BCA)

    Subject: BC0056 UNIX Operating System

    Semester: 5th Semester

    Module No: II

    Date of Submission: 20 November 2010

    Marks Awarded:

  • 7/27/2019 unix45

    13/15

    13

    Signature of Signature ofSignature ofCoordinator Centre head

    Evaluator

    August 2010

    Bachelor of Computer Application (BCA) Semester 5

    BC0056 UNIX Operating System 4 Credits

    (Book ID: B0973)

    Assignment Set 2 (40 Marks)

    Each question carries eight marks

    1. Explain the significance of option I, -v with grep command?

    2. Describe the various utilities used for taking back-up andcompression activities.

    Answer

    File compression and backup

    UNIX systems usually support a number of utilities for backing up and compressing files. The

    most useful are

    tar (tape archiver) tar backs up entire directories and files onto a tape device or into a

    single disk file known as an archiver. An archiver is a file that contains other files plus

  • 7/27/2019 unix45

    14/15

    14

    information about them, such as their filename, owner, timestamps and access permissions. tar

    does not perform any compression by default.

    To create a disk file tar archive use

    $ tar cvf archivenamefilename

    Where archivenamefilename will usuallyhave a .tar extension. c option means create, v

    means verbose (o/p filenames as they are archived) and fmeans file. To list the content of a

    tararchive we use, $ tar tvf archivename

    To restore files from a tar archive, $ tar xvf archivename

    cpio cpio is another facility for creating and reading archives. Unlike tar, cpio doesnt

    automatically archive the contents of directories, so its common to combine cpio with find

    when creating an archive: $ find print depth |cpio ov Htar >archivename

    This will take all the files in the current directory and all directories below and place them in

    an archive called archivename. The depth option controls the order in which the filenames

    are produced and is recommended to prevent problems with directory permissions when doing

    a restore. The o option creates the archive, the v option prints the names of the files

    archived as they are added and theH option specifies an archive format type, in this case it

    creates a tar archive. Another common archive type is crc, a portable format with a checksum

    for error control. To list the contents of a cpio archive use, $ cpio tv < archivename

    To restore $ cpio idv < archivename. The doption will create directories as necessary. To

    force cpio to extract files on top of files of the same name that already exists.

    Compress, gzip compress and gzip are utilities for compressing and decompressing

    individual files which may or may not be archive files. To compress file,

    .$ compress filename. Or .$ gzip filename.

    In each case,filename will be deleted and replaced by a compressed file called filename.Zor

    filename.gz.The reverse the compression process, $ compress d filename or $ gzip filename.

    3. Explain with an example the use of redirection input and output.

    4. Write a shell script to display the sum of n numbers.

  • 7/27/2019 unix45

    15/15

    15

    5. How do you perform the following in emacs?

    i. Delete character under cursor

    ii. Delete wordiii. Delete to end of line(s)