Download as pdf or txt
Download as pdf or txt
You are on page 1of 292

SUBJECT CODE : 18CS56

As per Revised Syllabus of

Visvesvaraya Technological University


Outcome Based Education (OBE) & Choice Based Credit System (CBCS)
Semester - V (CSE / ISE)

UNIX Programming
Iresh A. Dhotre
M.E. (Information Technology)
Ex-Faculty, Sinhgad College of Engineering
Pune

® ®
TECHNICAL
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge

(i)
UNIX Programming
Subject Code : 18CS56

Semester - V (CSE / ISE)

ã Copyright with Author


All publishing rights (printed and ebook version) reserved with Technical Publications. No part of this book
should be reproduced in any form, Electronic, Mechanical, Photocopy or any information storage and
retrieval system without prior permission in writing, from Technical Publications, Pune.

Published by :
® ®
Amit Residency, Office No.1, 412, Shaniwar Peth,
TECHNICAL Pune - 411030, M.S. INDIA, Ph.: +91-020-24495496/97
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge Email : sales@technicalpublications.org Website : www.technicalpublications.org

Printer :
Yogiraj Printers & Binders
Sr.No. 10/1A,
Ghule Industrial Estate, Nanded Village Road,
Tal. - Haveli, Dist. - Pune - 411041.

ISBN 978-93-90041-52-7

9 789390 041527
VTU 18

9789390041527 [1] (ii)


User applications
User level
Kernel level

System call interface

File system
Interprocess
communication

Character Block Scheduler


Process
control
Main memory
management
Device drivers

Hardware control

Hardware
Computer hardware
level
File mapping
Device
mapping

Framework
for Exec a. out
virtual switch elf
memory

Tape driver

Switch NFS
for Common Vfs
FFS
block Amenities interface
device RFS

Disk
driver

System
Network Framework processes
driver Streams for Time
scheduler sharing
TTY
driver process
Excel sheet

Operating
system
Web
browser
Central
processing
unit
Database
updation

Sorting
program
Unix commands
and libraries

System call
interface

Kernel

Hardware

User written
application
1 st

2 nd

3 rd
Verb Options Arguments 1 Arguments 2 Arguments 3 Arguments N
 
 
/ root

bin etc dev usr vmunix

passwd passwd local


/ root

bin etc dev usr vmunix

passwd passwd local


l

fsl bin etc usr unix dev

iad rupa sh date who passwrd src bin tty00 tty01

cmd

date.C who.C
Directory entry
Name Inode

Inode

Accessed time Size UID GID


Blk 1 Blk 2 Indirect Blk 1 File
content

File File Block


content content addresses
File
content


Permission

type owner group others


rwx rwx rwx

r read
– (file) w write
x execute
d (directory)
l (symbolic link)
b (block special file)
c (character special file)
p (named pipe special file)
s (local socket special file)
STDOUT STDIN STDOUT STDIN

Command 1 Command 2 Command 3


Pipe Terminal
Is wc
Variables

User-defined variables Predefined variables

Shell variables Environmental variables


n th

n th
Contents

 
 
 
 
 
 
 
 

User file File table


descriptor table
0
1
2
3
4 Count
5 2 (/etc/passwd)
6 Count
7 1 Read

Count
1 Rd-Wrt

Count
1 (local)

Count
1 Write

Inode table
user file
descriptor tables
(proc A) file table inode table
0
1
2
3
Count
4 Count
5 1 (/etc/passwd)
1 Read

Count
1 Rd-Wrt
(proc B)
0
1
2 Count
3 Count
1 (local)
4 1 Read
5

Count
1 Write
Count
1 (private)

Count
1 Read
 
 

 
user file file table
descriptors inode table
0
1
2
3
Count
4 Count
5 2 (/etc/passwd)
1

Count

(procB) 1

0
1
2 Count
3 NULL Count
1 (local)
4 NULL 0
5

Count
1
Count
0 (private)

Count
0
/

usr

src include

uts sys realfile.h

sys

inode.h testfile.h
 







{

}
{

}
{

}
{

}
}

}
_exit
or
_Exit
user
functions exit handler
(d
oe e ll
s n xit ca
return

ot

rn
call

re

tu
_exit tu

re
or rn
) call
_Exit main exit exit exit handler
function (does not return) function return user process

ca
return

) ll
call

xit n re
e t ur tu
e rn
o tr
e sn _exit standard I/O
C start-up do or
( cleanup
routine _Exit

exec

Kernel
environment environment environment
pointer list strings

environ : Home =/home/sar\0

PATH=:/bin:/usr/bin\0

SHEEL=/bin/bash\0

USER=sar\0

LOGNAME=sar\0

NULL
High adress Command line arguments
and environment variables

Stack

Heap
Initialized to zero by
BSS exec

Initialized data
Read from program file
by exec
Text
Per process
region table Region table
u area

Main memory
Process table
 

 

 
Parent Process
File
U Area Table
Per Process
Region Table Open Files
Parent
Current Directory
Data
Changed Root

Parent
User
Stack
Kernel Stack

Shared
Text

Inode
Table

Per Process U Area


Region Table Open Files
Child
Current Directory
Data
Changed Root

Child
User
Stack
Kernel Stack

Child Process
 
wait

The waitpid function doesn't wait for the child that terminates first; it has a
number of options that control which process it waits for.
waitpid

waitpid function lets us wait for one particular process, whereas the wait
function returns the status of any terminated child. We'll return to this feature
when we discuss the popen function.
2. The waitpid f
hp

Is > my_files

my_files
User User
processes processes

Kernel

IPC on same host

User User
processes processes

Kernel Kernel

IPC on different hosts


Senders Receivers

P P
P

Data

P
P
User process User process

or
fd(0) fd(1) fd(0) fd(1)

Pipe

Kernel

Parent Child
Fork
fd(0) fd(1) fd(0) fd(1)

Pipe

Kernel
Parent Child

fd(1) fd(0)

Pipe

Kernel
Parent cmdstring(child)

fp stdout

Parent cmdstring(child)

fp stdin
Parent cmdstring(child)
Pipe1
fp(1) stdin

fd(2) stdout
Pipe2
Prog3

Input Prog1
file

Prog2

Server

Read request

Well-known
FIFO
ts

W
es

rit
qu

e
re
re

qu
e
rit

es
W

ts

Client Client
Struct msgqid_ds

Receivers Senders

Messages New
read from msg msg msg messages
head added at
tail
A message queue

Message record

Message table
P semaphore operation
Only a single thread
of execution can
enter the CS at any
one time

Non-critical section Critical Non-critical section


of process A Section (CS) of process B

V semaphore operation
Semaphore set
Semaphore table

Struct sem Struct sem


Struct
semid_ds
sem_base
Process A

Shared region

Process B

Kernel
Process table

Shared memory table

Shared memory region

Struct shmid_ds
High address Command-line arguments
and environment variables
Stack 0xbffff9e4

0x4017a6a0
Shared memory Shared memory of 100000 bytes
0x40162000

Heap 0x0806c368
Malloc of 100000 bytes
0x08053cc8
0x08053cc0
Uninitialized data Array F of 40000 bytes
(bss) 0x0804a080

Shared memory

Text
Low address
User process User process

or
fd[0] fd[1] fd[0] fd[1]

Stream pipe

Kernel

stdin
fd[0] fd[1]
stdout

Parent Child(coprocess)
User process

fd[0] fd[1]

Stream head Stream head

Kernel
Process table entry

fd File
flags pointer
fd 0 :
fd 1 :
fd 2 : File table
fd 3 :
File status flags
Current file offset v-node table
v-node pointer v-node
information
i-node
Process table entry information
current file size

fd file
flags pointer
fd 0 :
fd 1 :
fd 2 :
fd 3 :
fd 4 :
User process

fd[0] fd[1]

Stream head Stream head Kernel

connld
Client Server

fd clifd1 fd[0] fd[1]

Stream Stream Stream Stream


head head head head

connld

Kernel
Kernel process Kernel region
table A process table

Per process region table


Text

File descriptor table Data

Stack
U area
 

 

 
written to file or
to logged-in users or
sent to another host

user
process

syslog

UDP
port 514

UNIX domain Internet domain


datagram socket datagram socket

Kernel
routines

kernel

TCP/IP network
(As per 2018 Pattern)

Semester - V (CSE / ISE)

You might also like