Escolar Documentos
Profissional Documentos
Cultura Documentos
Ihr Logo
Contents to be covered:
Process Environment
Main function
Process Termination. Command-line arguments.
Environment List
Memory layout of C program in unix. Memory Allocation
Your Logo
Introduction
In this chapter we examine the environment of a single process. We also see how the main function is called when program is executed.
How the typical memory layout looks like. How to allocate the additional memory. How to use the environment variable. Various ways for process to terminate.
Your Logo
Main function
A c program starts execution with a function called main. The prototype for the main function is int main(int argc, char *argv[]); Where argc is the no. of command-line arguments and argv is an array
Your Logo
Process Termination
There are eight ways for a process to terminate. Normal termination occurs in five ways.
Five normal termination ways are as follows, 1. Return from main fn.
Your Logo
Process Termination
Abnormal termination occurs in three ways.
Your Logo
Exit functions
Three functions terminate a program or process normally are as follow _exit() and _exit() which returns to the kernel immediately,
and exit(), which performs certain clean up processing and then returns to the kernel.
#include<stdlib.h> // standard library header file Void exit(int status); // performs clean up work Void _Exit(int status);// no clean up work, returns im to ker #include <unistd.h>// unix standard library header file Void _exit(int status);// no clean up work, returns im to ker
Your Logo
parameter that is typed after the name of a program in a command interpreter (such as a DOS box or Unix shell).
For example, if you typed the command rm file.s here "file.s" is a
command-line argument which tells the program rm to remove the file "file.s".
This is the part of normal operation of the unix system shells.
Environment List:
Each program is also passed an environment list, like the argument list. The environment list is an array of character pointers, with each pointer
Your Logo
Environment List
environment ptr Environ: environment list environment string HOME=/home/sar\0 PATH=/bin:/usr/bin\0 SHELL=/bin/bash\0 USER=sar\0 NULL LOGNAME=sar\0
Your Logo
in memory for frequently executed programs such as c compiler, the shell so on....
DEPARTMENT OF COMPUTER APPLICATIONS
Your Logo
This segment often called as the bss segment block started by symbol.
Data in this segment are initialized by the kernel before the program starts executing.
4. Stack: In this segment, the automatic variables are stored, along with information that is saved each time when the function is called. The space for automatic and temporary variable is stored on the stack.
Your Logo
And the heap is located between uninitialized data segment and stack
segment. high address Stack heap Uninitialized data Initialized data text Low address
Your Logo
Memory Allocation
Three functions is used for Memory allocation; 1. Malloc: which allocates the specified no. Of bytes of memory. The initial value of the memory is indeterminate. 2. Calloc: which allocates space for a specified no.of objects of a specified size. The space is initialized to all 0 bits. 3. Realloc: Which increase or decrease the size of previously allocated memory. #include<stdlib.h> Void *malloc(size_t size); Void *calloc(size_t nobj,size_t size); Void *realloc(void* ptr,size_t newsize); Void free(void * ptr);
Your Logo
Process control
Contents to be covered;
Process Identifier. Fork function. Vfork function. Wait and wait pid function. Race condition. Six exec function.
Your Logo
Unique pid can be reused, once a process terminates their ids become
This prevents a new process from being mistakenly referred again. Pid with 0 is scheduler process and it is often referred as swapper. In addition to the process id,there are other identifier for every process they are
as follows,
A. pid_t getpid(void) // process id of the calling process. B. pid_t getppid(void) //parent process id of the calling process. C. uid_t getuid(void) //real user id of the calling process. D. uid_t geteuid(void) // effective user id of the calling process so on
Your Logo
Fork function
An existing process can create a new process by calling the function known as fork which is syntactically referred as Pid_t fork(void);
It returns 2values : 1st value throws 0 for child process which indicates
that process can have only single parent process. 2nd value throws pid of the child process for parent process the reason is process can have more than one child.
Reason for 0 in child process is child can have only one parent. Child process can always call getppid to obtain the pid of its parent. The child process is copy of parent.
Your Logo
notifies the parent process by sending the SIGCHILD signal to the parent.
Because the child process termination is asynchronous event.so parent
Your Logo
It blocks all the child process which are still running. Returns immediately with termination status of the child. Returns immediately with an error.
Your Logo
1. the waitpid fn let us wait for one particular process,whereas wait fn will return the status of any terminated child.
2. the waitpid fn provides the non-blocking version of wait. 3. the waitpid fn provides support for job control.
Your Logo
some work with shared data and final outcome depends on order in which process run.
Fork fn initiates race condition because parent and child process
completely replaced by the new program starts executing at its main function.
The process Id does not change across an exec, because a new
process is not created; exec merely replaces the current process-its text,data,heap and stack segments with a brand new program from disk.
DEPARTMENT OF COMPUTER APPLICATIONS
Your Logo
Exec function
#include<unistd.h> Int execl(const char* pathname, const char *arg0,./*(char *)0 */); Int execv(const char *pathname, char *const argv[]); Int execle(const char *pathname,const char *arg0,/* (char *) 0, char*const
envp[]*/);
Int execve(const char *pathname,char *const argv[], char *const envp[]); Int execlp(const char *filename, const char *arg0,./* (char *) 0 */); Int execvp(const char *filename, char *const argv[]); 1. The first difference in these fns is that the 1st four fn take a pathname
argumet,whereas the last 2 take a filename argument. When a filename argument is specified,
2. If filename contains a slash, it is taken as a pathname, otherwise the executable
Your Logo
execlp
execl
execle
build argv
build argv
execvp
execv
use environ
Your Logo
Multitasking
UNIX refers to a task as a process. A user can run several commands in background while executing another in the foreground. When a background task is being executing, user can continue doing another task e.g., printing a large document can be performed in the background while editing some other document in the foreground.
Your Logo
simultaneously.
The theory of multi-user system is to approach 100% computer utilization
A single user cannot use the printer, disk, memory or CPU 100% of the time. But multiple users can increases the use of these devices and resources by having an OS that manages the resources for them.
Your Logo
UNIX is highly portability is the ability of the software operating on one machine to operating as efficiently on another, different machine.
Job Control
Job Control on UNIX refers to the ability to control which job is executed in the
Using Job control can increase the productivity of a user by allowing multiple tasks to be juggled back and forth between background, foreground and suspended states.
Your Logo
allows maximum flexibility for storing information to resemble its real life structure. Multiple users may be grouped by corporate departments
The UNIX Shell The shell is a very powerful and dynamic UNIX utility. It is the primary interface
to the OS (kernel). It can be interactively programmed or it can be used to write scripts to solve simple to complex problem.
Pipes and Filters Pipes and filters contribute to the power of UNIX. These enable several
Your Logo
architecture from the users, making it easier to write programs that run on different hardware implementations.
System Security user without permission i.e., Invalid users cannot access data, making it easier to write programs that run on different hardware implementations.
Being a multi-user OS, UNIX offers protection to one users information from the
Communication The UNIX system has several built in programs, enabling the user to communicate, transfer files across different UNIX systems and between UNIX and other OS system.
Your Logo
Your Logo
we log in with the name and password where password will be read by the
home/mcaii/myfolder
Files &Directories: File system is a hierarchial arrangements of directories and files.
attributes of a file.
DEPARTMENT OF COMPUTER APPLICATIONS
Your Logo
File Name: Name in a directory which is used refer the file is called as File name. referred as path name. A path name begin with a slash is often called as absolute pathname or relative path name.
Path Name: A sequence of one or more file names separated by slash can be
referred as current working directory(CWD). When we login, the working directory is set to our home directory. It is obtained when we enter a password.
Program & Processes: A program is an executable file residing on the disk in a
directory. An executing instance of program is often referred as process. Every process is referred by the unique ID called process id and it is a non-negative integer.
Process Control: There are 3 primary functions is used for process control they
Your Logo
File I/O
Most of the file I/O on unix system can be performed using only five functions:
a non-negative integer.
When we open an existing file or create a new file,the kernel returns a file
Your Logo
File I/O
#include<fcntl.h> int open(const char *pathname,int oflag/*mode_t mode*/); returns file descriptor if OK,-1 on error
The pathname is name of the file to open or create. The oflag argument is combination of one or more following operations which is controlled by the <fcntl.h> header.
-open for reading only. -open for writing only. -open for reading and writing. ONE OF THESE 3 CONSTANTS MUST BE SPECIFIED. Your Logo
File I/O
Creat Function: A new file can also be created by calling the creat fn.
#include<fcntl.h> int creat(const char *pathname,mode_t mode); returns file descriptor opened for writing only if OK,-1 on error
Your Logo
File I/O
negative integer that measures the number of bytes from beginning of the file.
An open files offset can be set explicitly by calling lseek fn.
Your Logo
File I/O
#include<fcntl.h> off_t lseek(int filedes,off_t offset,int whence); Returns: new file offset if OK,-1 on error
The interpretation of the offset depends on the value of the whence argument.
If the whence is SEEK_SET,the files offset is set to offset bytes from
the offset.
If the whence is SEEK_END,the files offset is set to the size of the file plus
the offset.
DEPARTMENT OF COMPUTER APPLICATIONS
Your Logo
File I/O
Read function: Data is read from open file with read function.
#include<fcntl.h> ssize_t read(int filedes, void *buf, size_t nbytes); Returns:no of bytes read,0 if eof,-1 on error
Write Function: Data is written to an open file with write function
#include<fcntl.h> ssize_t write(int filedes, const void *buf, size_t nbytes); Returns:no of bytes written if OK,-1 on error
Your Logo