Unit 4
Unit 4
Unit 4
By
Kala.C.L
Lecturer, Dept of ISE
SJBIT, Bengaluru
UNIT-4
UNIX PROCESSES
MAIN FUNCTION
PROTOTYPE:
int main(int argc, char *argv[ ]);
Normal termination
* return from main
* calling exit
* calling _exit
Abnormal termination
* calling abort
* terminated by a signal
exit and _exit functions
#include <stdlib.h>
Int atexit (void (*fun) void));
Atexit function calls these functions in
reverse order of their registration
NULL
Memory layout of a C program
Text segment – sharable copy
Initialized data segment – variables
specifically initialized in the program
Uninitialized data segment – “bss”
segment
data is initialized to arithematic 0 or null
Stack – return address and information
about caller’s environment
Heap – dynamic memory allocation takes
place on the heap
High address
heap
Uninitialised data Intialized to 0 by exec
#include <stdio.h>
int setjmp (jmp_buf env);
void longjmp (jmp_buf env, int val);
env is of type jmp_buf ,this data type is
form of array that is capable of holding all
information required to restore the status
of the stack to the state when we call
longjmp
#include <sys/time.h>
#include <sys/resource.h>
int getrlimit (int resource ,struct
rlimit *rlptr);
int setrlimit (int resource ,const struct
rlimit *rlptr);
Struct rlimit
{
rlim_t rlim_cur; /*soft limit*/
rlim_t rlim_max; /*hard limit */
}
1. Soft link can be changed by any process
to a value <= to its hard limit
2. Any process can lower its hard limit to a
value greater than or equal to its soft
limit
3. Only super user can raise hard limit
RLIMIT_CORE – max size in bytes of a
core file
RLIMIT_CPU – max amount of CPU time in
seconds
RLIMIT_DATA – max size in bytes of data
segment
RLIMIT_FSIZE – max size in bytes of a file
that can be created
RLIMIT_MEMLOCK – locked in-memory
address space
RLIMIT_NOFILE – max number of open
files per process
RLIMIT_NPROC – max number of child
process per real user ID
RLIMIT_OFILE – same as RLIMIT_NOFILE
RLIMIT_RSS – max resident set size in
bytes
RLIMIT_STACK – max size in bytes of the
stack
RLIMIT_VMEM – max size in bytes of the
mapped address space
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "ourhdr.h"
#define doit(name) pr_limits(#name, name)
static voidpr_limits(char *, int);
int main(void)
{
doit(RLIMIT_CORE);
doit(RLIMIT_CPU);
doit(RLIMIT_DATA);
doit(RLIMIT_FSIZE);
#ifdef RLIMIT_MEMLOCK
doit (RLIMIT_MEMLOCK);
#endif
#ifdef RLIMIT_NOFILE /* SVR4 name */
doit (RLIMIT_NOFILE);
#endif
#ifdef RLIMIT_OFILE /* 44BSD name */
doit (RLIMIT_OFILE);
#endif
#ifdef RLIMIT_NPROC
doit (RLIMIT_NPROC);
#endif
#ifdef RLIMIT_RSS
doit(RLIMIT_RSS);
#endif
doit(RLIMIT_STACK);
#ifdef RLIMIT_VMEM
doit(RLIMIT_VMEM);
#endif
exit(0);
}
static void
pr_limits(char *name, int resource)
{
struct rlimit limit;
if (getrlimit(resource, &limit) < 0)
err_sys("getrlimit error for %s",
name);
printf("%-14s ", name);
if (limit.rlim_cur == RLIM_INFINITY)
printf("(infinite) ");
else
printf("%10ld ", limit.rlim_cur);
if (limit.rlim_max == RLIM_INFINITY)
printf("(infinite)\n");
else
printf("%10ld\n", limit.rlim_max);
}
Kernel support for processes
Kernel region table
Process table
data
parent File table
text
Fd table
child
Fd table
stack
data
Besides open files the other properties
inherited by child are
Real user ID, group ID, effective user ID,
effective group ID
Supplementary group ID
Process group ID
Session ID
Controlling terminal