Upload
sai-srinivas
View
219
Download
0
Embed Size (px)
Citation preview
8/9/2019 Pramod Linux
1/11
INTRODUCTION TO LINUX OPERATING SYSTEM and
FILEs CONCEPTs and PROCESS management
By
KPRAMOD KUMAR REDDY
M!Te"# $DSCE%
R&'' N&!()*((D*+),
8/9/2019 Pramod Linux
2/11
System Ca''s
The kernel provides a set of interfaces for applications running in user mode
to interact with the system. These interfaces, also known as system calls
System calls give applications access to hardware and other kernel
resources.
System calls ensure security and stability.
Use- S.a"e /s Ke-ne' S.a"e
8/9/2019 Pramod Linux
3/11
FILESI/O
"-eate System Ca''
int creat (const char * pathname, int flags)
Flags !"#$!%&', !"#!%&', !"#$#
!"#+T , !"+%$, !"%!%-&!,
!"%!TTT', !"T#/% , !"0&,
!"S'%, !"$1#T!#', !"$1#T,
!"+S'%, !"&+#2F1&
3!$ #+$, #1T, 0/T #31SS1!%
F!# !%#, 2#!/ 33-#S +%$
!T4#S
&.en System Ca''
int open (const char * pathname, int flags)
int open (const char * pathname, int flags, mode"t mode)
Flags !"#$!%&', !"#!%&', !"#$#
!"#+T , !"+%$, !"%!%-&!,
!"%!TTT', !"T#/% , !"0&,!"S'%, !"$1#T!#', !"$1#T, !"+S'%,
!"&+#2F1&
3!$ #+$, #1T, 0/T #31SS1!%
F!# !%#, 2#!/ 33-#S +%$
!T4#S
E0am.'e .-&g-am t& &.en a F1'e
5include 6stdio.h7
5include 6fcntl.h7
5include 6sys8types.h7
5include 6sys8stat.h7
int main( )
9
int fd,count:
8/9/2019 Pramod Linux
4/11
char buf;:
fd?open(@$emo.tAt@,!"#$#):
printf(@new file disciptorBdCn@,fd):
count?read(fd,buf,
8/9/2019 Pramod Linux
5/11
5include 6fcntl.h7
int main()
9
int fd:
char ch:
fd?open(@$emo.tAt@,!"#$!%&'):
while(read(fd,ch,G)H?=)
putchar(ch):
close(fd):
printf(@Cn@):
D
8/9/2019 Pramod Linux
6/11
3-1te System Ca''
ssiEe"t write(int fd, void *buf, siEe"t count)
E0am.'e .-&g-am 2&- 3-1te system "a''
5include6stdio.h7
5include6unistd.h7
5include6fcntl.h7
int main()
9
int fd:
char ch:
fd ? open(@$emoG.tAt@,!"#$#I!"#+T):$#e-e 3e a-e
&.en1ng a 21'e a2te- "-eat1ng 1t4 we can see it from syntaA%
while((ch?getchar())H?!F)
write(fd,ch,G):
close(fd):$It 1s 5sed t& "'&se t#e 21'e 3#1"# #as 6een &.ened%
D
8/9/2019 Pramod Linux
7/11
"'&se system "a''
int close ( int fd )
'see7 System Ca''
!ff"t lseek ( int fd, off"t offset, int where)
here S"ST, S"/#, S"%$
The lseek call enables you to reposition a file descriptor in a file. ass it the
file descriptor and two additional arguments specifying the new position.
1f the third argument is S"ST, lseek interprets the second argument as
a position, in bytes, from the start of the file.
1f the third argument is S"/#, lseek interprets the second argument as
an offset, which may be positive or negative, from the current position. 1f the third argument is S"%$, lseek interprets the second argument as
an offset from the end of the file.+ positive value indicates a position
beyond the end of the file.
E0am.'e .-&g-am 2&- 'see7 synta0
5include6stdio.h7
5include6fcntl.h7
5include6sys8types.h7
8/9/2019 Pramod Linux
8/11
int main()
9
int fd:
char ch:
fd? open(@lsk.tAt@,!"#$#I!"#+T,JJJ):
lseek(fd,K,S"ST):
while((ch?getchar()H?!F)
write(fd,ch,G):
close(fd):
D
P-am&d 75ma-is shown before program is eAecuted
8/9/2019 Pramod Linux
9/11
e can see the appended data after pramod which is pramod reddy
So we can see data as .-am&d.-am&d -eddy
PROCESS MANAGEMENT
fork
fork - create a child process
SYNOPSIS
#include
#include unistd.h>
pid_t fork(void!
"S$%IP&ION
forkcreates a child process that differs from the parent process only in its
PID and PPID, and in the fact that resource utilizations are set to 0. Filelocks and pending signals are not inherited.
Under Linu, forkis implemented using copy-on-!rite pages, so the only
penalty incurred "y fork is the time and memory re#uired to duplicate the
parent$s page ta"les, and to create a uni#ue task structure for the child.
8/9/2019 Pramod Linux
10/11
vfork
%fork - create a child process and "lock parent
SYNOPSIS
#include
#include
pid_t vfork(void!
"S$%IP&ION
&he vfork'( function has the same effect as fork'(, ecept that the
"eha%iour is undefined if the process created "y vfork'( either modifies
any data other than a %aria"le of type pid)t used to store the return %aluefrom vfork'(, or returns from the function in !hich vfork'( !as called, or
calls any other function "efore successfully calling_exit'( or one of
the execfamily of functions.
'it)'itpid
!ait, !aitpid - !ait for process termination
SYNOPSIS
#include sys/types.h>
#include sys/'it.h>
pid_t 'it(int *status!
pid_t 'itpid(pid_tpid) int *status) int options!
"S$%IP&ION
&he 'itfunction suspends eecution of the current process until a child haseited, or until a signal is deli%ered !hose action is to terminate the current
process or to call a signal handling function. If a child has already eited "y
the time of the call 'a so-called *zom"ie* process(, the function returns
immediately. +ny system resources used "y the child are freed.
8/9/2019 Pramod Linux
11/11
e+ecve
eec%e - eecute program
SYNOPSIS
#include
int e+ecve(const chr *filename) chr *const argv,-) chr *const envp,-!
"S$%IP&ION
e+ecve(eecutes the program pointed to "yfilename. filenamemust "e either a
"inary eecuta"le, or a script starting !ith a line of the form *# interpreter arg*.In the latter case, the interpreter must "e a %alid pathname for an eecuta"le !hich
is not itself a script, !hich !ill "e in%oked as interpreterarg filename.
argvis an array of argument strings passed to the ne! program. envpis an array
of strings, con%entionally of the form keyvlue, !hich are passed as en%ironment
to the ne! program. oth, argvand envpmust "e terminated "y a null pointer. &he
argument %ector and en%ironment can "e accessed "y the called program$s main
function, !hen it is defined as int 0in(int r1c) chr *r1v,-) chr *envp,-.
Re2e-en"es4!
Beg1nn1ng L1n50 P-&g-amm1ng 6y! R1"#a-d St&nes and Ne1' Matt#e3
L1n50 Ke-ne' De8e'&.ment 9 R&6e-t L&8e t#1-d ed1t1&n