BCA-404 AND PROGRAMMING (UNIT-III)

General Purpose Utilities

Calendar The to see the calculator of any specific month or a complete year. Syntax: cal [[ month ] year ]

ECHO is one of the most commonly and widely used built-in command for bash and C shells, that typically used in scripting language and batch files to display a line of text/string on standard output or a . Syntax: echo [OPTION]... [STRING]...

Printf: Display text in formatted %s String %30s print space in a 30 characters wide %d decimal %o octal %x Hexadecimal %f Floating point number Example : $ "%s\n" "hello printf" "in" "bash script" hello printf in bash script bc - Unix calculator bc is a unix command which stands for bench calcultor. Depending on the way we use it, it can either look like a programming language or as a interactive mathematic shell. Let us see in this article how to use bc to do some simple arithmetic. passwd ---- Assigning passwd username are the users w ---- to know who are the users connected who am i

Uname: features of . -a ----- all the details uname -r ---- kernel version

DATE : Displaying the System Date you can display the current date with date command, which shows the date and to nearest second. Syntax: date

Process states and Transitions in a UNIX Process

Process A process is an instance of a program in execution. A set of processes combined together a complete program. There are two categories of processes in Unix, namely  processes: They are operated in user mode.  Kernel processes: They are operated in kernel mode.

Process States The states that a Process enters in working from start till end are known as Process states. These are listed below as:  Created-Process is newly created by system call, is not ready to run  User running-Process is running in user mode which means it is a user process.  Kernel Running-Indicates process is a kernel process running in kernel mode.  Zombie- Process does not exist/ is terminated.  Preempted- When process runs from kernel to user mode, it is said to be preempted. e-notes by Naveen Kumar Gupta

Page 1

BCA-404 UNIX AND SHELL PROGRAMMING (UNIT-III)

 Ready to run in memory- It indicated that process has reached a state where it is ready to run in memory and is waiting for kernel to schedule it.  Ready to run, swapped– Process is ready to run but no empty main memory is present  , swapped- Process has been swapped to secondary storage and is a blocked state.  Asleep in memory- Process is in memory(not swapped to secondary storage) but is in blocked state.

Process Transitions The working of Process is explained in following steps:

1. User-running: Process is in user-running. 2. Kernel-running: Process is allocated to kernel and hence, is in kernel mode. 3. Ready to run in memory: Further, after processing in main memory process is rescheduled to the Kernel.i.e.The process is not executing but is ready to run as soon as the kernel schedules it. 4. Asleep in memory: Process is sleeping but resides in main memory. It is waiting for the task to begin. 5. Ready to run, swapped: Process is ready to run and be swapped by the processor into main memory, thereby allowing kernel to schedule it for execution. 6. Sleep, Swapped: Process is in sleep state in secondary memory, making space for execution of other processes in main memory. It may resume once the task is fulfilled. 7. Pre-empted: Kernel preempts an on-going process for allocation of another process, while the first process is moving from kernel to user mode. 8. Created: Process is newly created but not running. This is the start state for all processes. 9. Zombie: Process has been executed thoroughly and call has been enabled.The process, thereby, no longer exists. But, it stores a statistical record for the process. This is the final state of all processes.

Manipulation of a process address space

The region table entry contains the following information:

 The i-node of the file from which the region was initially loaded  The of the region (text, shared memory, private data, or stack)  The size of the region  The location of the region in physical memory.  The state of the region: locked, in demand, being loaded into memory, valid, loaded into memory  The reference count, giving the number of processes that reference the region e-notes by Naveen Kumar Gupta

Page 2

BCA-404 UNIX AND SHELL PROGRAMMING (UNIT-III)

The operations that manipulate regions are:

 lock a region  unlock a region  allocate a region  attach a region to the memory space of a process  change the size of a region  load a region from a file into the memory space of a process  free a region  detach a region from the memory space of a process, and duplicate the contents of a region

sleep process in unix sleep command is used to create a dummy job. A dummy job helps in delaying the execution. It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format. This command pauses the execution for an amount of time which is defined by NUMBER. Syntax: sleep NUMBER[SUFFIX]...

Process creation in unix A process is simply a running program. A process is said to be born when the program starts execution and remains alive till the process is active. After the execution the process is said to be died.A process in a Unix system is created by fork system call. Every process except process 0 is created. The process that invokes the fork system call is parent process and the newly created process is the child process. Every process has one parent process but a parent can have many child process. The kernel identifies it process by its process identification number (PID). Process 0 is a special process created "by hand" when the system boots.

There are 3 distinct phase in mechanism of process creation and uses 3 system calls: fork(), exec() and (). fork(): Creates a child process. A new process is created because an existing process creates an exact copy of itself. This child process has the same environment as its parent but only the PID is different. This procedure is known as forking. exec(): After forking the process, the address space of the child process is overwritten by the new process data. This is done through exec call to the system. No new process is created over here. The PID & PPID remains unchanged. wait(): The parent then executes wait system call to wait for the child process to complete its execution. The important attributes that are inherited by the child process from its parents are: Real UID and GID, PGID, value, Environment setting, Current working directory, memory segments etc. When the process dies it immediately moves to the zombie state and remains in that state until the parent picks it up to exit status. A zombie is the harmless dead child.

Process termination in unix Process termination occurs when the process is terminated The exit() system call is used by most operating systems for process termination.

Some of the causes of process termination are as follows −  A process may be terminated after its execution is naturally completed. This process leaves the processor and releases all its resources.  A child process may be terminated if its parent process requests for its termination.  A process can be terminated if it tries to use a resource that it is not allowed to. For example - A process can be terminated for trying to into a read only file.  If an I/O failure occurs for a process, it can be terminated. For example - If a process requires the printer and it is not working, then the process will be terminated.  In most cases, if a parent process is terminated then its child processes are also terminated. This is done because the child process cannot exist without the parent process.  If a process requires memory than is currently available in the system, then it is terminated because of memory scarcity.

The user-id of a process  Every process has a real user ID (the UID), an effective user ID (the EUID), a real user group ID (the GID), and an effective user group ID (the EGID). The user group ID is distinct from the process group ID.  The real IDs are used for accounting and user-user communication, while the effective IDs are used to control access to files and control signal sending. e-notes by Naveen Kumar Gupta

Page 3

BCA-404 UNIX AND SHELL PROGRAMMING (UNIT-III)

 A user ID is associated with each user login name in the password file /etc/passwd. This is assigned to the user's shell as its UID and EUID, and is inherited by processes spawned from the shell.  The user IDs can be accessed with the system calls: int getuid(void) which gets the UID of the process, and int geteuid(void) which gets the EUID of the process.  Each entry in /etc/passwd also has a group ID which is given to the shell as its initial GID and EGID.  The group IDs can be accessed with the system calls: int getgid(void) which gets the GID of the process and int getegid() which gets the EGID of the process.  Only the may change the user IDs of a process, using the system calls: int setuid(int uid) which sets the UID and EUID, and int setgid(int gid) which sets the GID and EGID. Both calls return 0 on success, or -1 on error.  Ordinary users may change effective IDs to real IDs, by using these calls with the arguments equal to the real IDs.

The shell

The shell is a program that takes commands from the keyboard and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell.

e-notes by Naveen Kumar Gupta

Page 4