Académique Documents
Professionnel Documents
Culture Documents
SYSTEM PROGRAMMING
&
COMPILER DESIGN
LAB MANUAL
FOR VI SEMESTER
COMPUTER SCIENCE & ENGINEERING
S.J.B.I.T
Dept. of C.S.E
1.Lex Introduction
The word lexical in the traditional sense means pertaining to words. In terms of
programming languages, words are objects like variable names, numbers, keywords etc. Such
words are traditionally called tokens.
A lexical analyzer, or lexer for short, will take input as a string of individual letters and
divide this string into tokens. Additionally, it will filter out whatever separates the tokens (the
so-called white-space), i.e., lay-out characters (spaces,newlines etc.) and comments.
The lexical analyzer is the first phase of a compiler. Its main task is to read the input
characters and produce as output a sequence of tokens that the parser uses for syntax analysis.
This interaction, summarized schematically in Fig-1.1, is commonly implemented by making
the lexical analyzer be a subroutine or a co routine of the parser.
token
Source
Program
LEXICAL
ANALYZER
PARSER
get next
token
SYMBOL
TABLE
Several tools have been built for constructing lexical analyzers from special purpose
notations based on regular expressions.
In this section, we describe a particular tool, called Lex that has been widely used to
specify lexical analyzers for a variety of languages. We refer to the tool as Lex compiler, and
its input specification as the Lex language.
Lex is generally used in the manner depicted in Fig 1.2.First, a specification of a lexical
analyzer is prepared by creating a program lex.l in the lex language. Then, lex.l is run through
the Lex compiler to produce a C program lex.yy.c.The program lex.yy.c consists of a tabular
representation of a transition diagram constructed from the regular expression of lex.l,
together with a standard routine that uses the table to recognize lexemes. The actions
associated with regular expression in lex.l are pieces of C code and are carried over directly
to lex.yy.c.Finally lex.yy.c is run through the C compiler to produce an object program
a.out,which is the lexical analyzer that transforms an input stream into a sequence of tokens.
S.J.B.I.T
Dept. of C.S.E
Lex source
Program lex.l
Lex
compiler
lex.yy.c
lex.yy.c
C
Compiler
a.out
a.out
input
stream
Sequence of
tokens
S.J.B.I.T
Dept. of C.S.E
expression.
11. ( ) Groups of series of regular expressions together into a new regular expression .
12. Blanks between tokens are optional ,with the exception that keywords must be
surrounded by blanks,newlines,the beginning of the program,or the final dot.
S.J.B.I.T
Dept. of C.S.E
PART-A
%%
int main(int argc,char *argv[])
{
++argv,--argc;
if(argc>0)
yyin=fopen(argv[0],"r");
else
yyin=stdin;
yylex();
printf("Number of characters:%d\n",ch);
printf("Number of spaces:%d\n",sp);
printf("Number of words:%d\n",wd);
printf("Number of lines:%d\n",ln);
}
OUTPUT
Number
Number
Number
Number
of
of
of
of
characters:14
spaces:0
words:2
lines:2
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation
The first section, the definition section, introduces any initial C program code we want copied
into final program. i.e. Take a variable for representing a character, line, space and words.
%{
int ch=0,ln=0,sp=0,wd=0;
%}
The next section is the rules section .Each rule is made of two parts: a pattern and an action,
separated by white space. The lexer that lex generates will execute the action when it
recognizes the pattern.
1.To count the number of line the pattern: [\n] .After lexer recognizes the [\n] pattern it
should perform the action .Here the action is to increment the line value, so the action is
represented by {ln++;}
2.To count the space the pattern : .After lexer recognizing the pattern it should perform
the action. Here the action is to increment the space value, so the action is represented by
{sp++;}
3.To count the words and characters the regular expression :[^ \t\n]+. Here ^ Matches the
beginning of a line as the first character of a regular expression. The tool lex provides an
internal variable yyleng which contains the length of the string our lexer recognized.+
symbol matches one or more occurrence of the preceding regular expression. So the action is
represented by {ch+=yyleng,wd++ ;}
The next section is procedures section, we know that when a program is invoked ,the
execution starts from the function main( ). We can pass the parameters to the function main( )
whenever the program is invoked and are called command line parameters.
To access the command line parameters the function main should have the following format :
Syntax:
The function main( ) can take two arguments namely argc and argv where argc must be an
integer variable whereas argv is an array of strings.argc indicates the number of parameters
passed and argv represents a parameter that is passed to function main.
The file should be opened before writing a file or before reading a file.
Syntax: fopen(char *filename, char *mode)
Return values
-file pointer if successful.
-NULL if unsuccessful.
A lex lexer reads its input from the standard I/O file yyin.The default value of yyin is stdin,
since the default input source is standard input. If you to change the source you should
mentioned it explicitly.
yylex(): You call yylex ( ) to start or resume scanning. If a lex action does a return to
pass a value to the calling program ,the next call to yylex( ) will continue from the point
where it left off. All the code in the rules section is copied into yylex( ).
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
OUTPUT
Enter the input filename:
sum.c
Enter the output filename:
add.c
Number of comment lines in the given file: 2
Atlast see the content of add file using the command
[root@localhost ~]# cat add.c
cat add.c
#include<stdio.h>
void main()
{
int a=1,b=2;
printf("%d",a+b);
}
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation
The first section, the definition section, introduces any initial C program code we want copied
into final program. i.e. Take a variable for representing comment line.
%{
int comment=0;
%}
The next section is the rules section .Each rule is made of two parts: a pattern and an action,
separated by white space. The lexer that lex generates will execute the action when it
recognizes the pattern.
1. C style comment line is enclosed with /* and */ characters. To count the numbers of
comment line
the pattern: "/*"[\n]*.*[\n]*"*/".After lexer
recognizes the
"/*"[\n]*.*[\n]*"*/" pattern it should perform the action .Here the action is to increment
the comment value , so the action is represented by {comment++;}.As you know . (dot)
doesnt match the newline character ,it matches the character.
2.Now to eliminate the comment line and then to copy the remaining content in file the
pattern : /*"[\"*/"]* . Here the metacharacter \ is to suppress the character *. After lexer
recognizes the pattern it inserts whitespace in the place of comment line and copies the
remaining content of the file.
fprintf()function
The function is similar to that of printf( ) except the syntax .The Prototype of fprintf is:
Syntax : fprintf(fp, control string ,list)
fp : file pointer associated with the file.
S.J.B.I.T
Dept. of C.S.E
OUTPUT
Enter the Arithmetic expression:
2+3*4
+* (Press Ctrl d)
Number of Operators=2
Number of Identifiers=3
Valid expression
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation
The first section, the definition section, introduces any initial C program code we want
copied into final program. i.e. Take variables for representing the operators, identifiers and
for brackets.
%{
int count=0,ids=0,bracket=0;
%}
The next section is the rules section .Each rule is made of two parts: a pattern and an action,
separated by white space. The lexer that lex generates will execute the action when it
recognizes the pattern.
1. To recognize the operators the pattern is : [ + ] ,[ - ],[ * ],[ / ]. After lexer recognizes the
pattern it should perform the action .Here the action is to display the operator and to count the
operator, so the action is represented by {printf("operator");count++;}
2.To recognize the identifiers the pattern is :[a-zA-Z0-9]+ . After lexer recognizes the pattern
it should perform the action .Here the action is to increments the identifier value.
3. To recognize the brackets the pattern is [ ( ] and [ )].After lexer recognizes the pattern it
should perform the action. Here the action is to increment brackets and decrement the bracket
values respectively.
S.J.B.I.T
Dept. of C.S.E
and " |
or " |
but " |
/*Words which leads to the compound statement*/
because " |
than " |
nevertheless " {flag=1;}
%%
int main()
{
printf("Enter the sentence:\n");
yylex();
if(flag==1)
printf("Given sentence is compound statement\n");
else
printf("Given sentence is simple statement\n");
}
OUTPUT
Enter the sentence:
abc is alphabet
abc is alphabet (Press Ctrl d)
Given sentence is simple statement
[root@localhost ~]# ./a.out
Enter the sentence:
abc or 123 are not equal
abc123 are not equal (Press Ctrl d)
Given sentence is compound statement
Theoretical Explanation
The first section, the definition section, introduces any initial C program code we want
copied into final program. i.e. Take variables for representing the words which leads to a
compound statement.
%{
int flag=0;
%}
The next section is the rules section .Each rule is made of two parts: a pattern and an action,
separated by white space. The lexer that lex generates will execute the action when it
recognizes the pattern.
1. List few words which lead to compound statement. After lexer recognizes the pattern
it should perform the action .Here the action is to set the value as it recognizes the
words in the given statement.
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
OUTPUT
Identifiers in the given file are
bangalore
_bangalore
banga_lore
Number of identifiers are 3
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation
The first section, the definition section, introduces any initial C program code we want
copied into final program. i.e. Take variables for representing the identifiers.
%{
int ids=0;
%}
The next section is the rules section .Each rule is made of two parts: a pattern and an action,
separated by white space. The lexer that lex generates will execute the action when it
recognizes the pattern.
1. Invalid identifiers start with number and symbols . To recognize an invalid identifiers that
start with number the pattern is ([0-9]+[a-zA-Z]*).To recognize an invalid identifiers that
start with underscore then followed by the number the pattern is ([_][0-9]*).Then
recognize an invalid identifiers that start with symbols the pattern is
([`!@#$%^&*-+=][a-zA-Z]+).
After lexer recognizes the pattern it should perform the action Here the action is eliminated
in those in the output. So the action is just { ; }.
2.A valid identifiers start with an alphabet or underscore followed alphabet , number or
underscore. To recognize valid identifiers the pattern is : ([a-zA-Z]|[_])(([0-9]|[a-zAZ]|[_])*). After lexer recognizes the pattern it should perform the action Here the action is
to display the valid identifiers and to count that identifiers.
yytext( )
Whenever a lexer matches a token , the text of the token is stored in the null terminated string
yytext . In some implementations of lex , yytext is a character array declared by :
extern char yytext[ ]
The contents of yytext are e replaced each time a new token is matched. If yytext[ ] is an
array ,any token which is longer than yytext will overflow the end of the array and cause the
lexer to fail in some hard to predict way. In AT&T lex ,the standard size for yytext [ ] is 200
character.
S.J.B.I.T
Dept. of C.S.E
2.YACC Introduction
The unix utility yacc (Yet Another Compiler Compiler) parses a stream of token, typically
generated by lex, according to a user-specied grammar.
Definition section
There are three things that can go in the denitions section:
C code: Any code between %{ and %} is copied to the C le. This is typically used for
dening le variables, and for prototypes of routines that are dened in the code segment.
Denitions: The denition section of a lex le was concerned with characters; in yacc this is
tokens.
Example : %token NUMBER.
These token denitions are written to a .h le when yacc compiles this le.
Associativity rules These handles associativity and priority of operators.
S.J.B.I.T
Dept. of C.S.E
This is the general form of context-free grammars, with a set of actions associated with each
matching right-hand side. It is a good convention to keep non-terminals (names that can be
expanded further) in lower case and terminals (the symbols that are nally matched) in upper
case.
The terminal symbols get matched with return codes from the lex tokenizer. They are typically denes coming from %token denitions in the yacc program or character values.
~]#
~]#
~]#
~]#
lex filename.l
yacc -d filename.y
cc lex.yy.c y.tab.c ll
./a.out
S.J.B.I.T
#makes lex.yy.c
#makes y.tab.c and y.tab.h
#compile and link C files
Dept. of C.S.E
{return
{return
{return
{return
ID;}
NUMBER;}
yytext[0];}
0;}
/*Logical EOF*/
%%
Yacc part
%token NUMBER ID
%left '+''-'
%left '*''/'
/*token definition*/
/*Operator precedences*/
/*Operator precedences*/
%%
expr:expr '+' expr;
|expr '-' expr;
|expr '*' expr;
|expr '/' expr;
|'('expr')'
|NUMBER
|ID
;
/*Grammar*/
%%
int main()
{
printf("Enter the Expression\n");
yyparse();
printf("Valid Expression\n");
}
int yyerror()
{
printf("Expression is invalid\n");
exit(0);
}
~]#
~]#
~]#
~]#
lex filename.l
yacc -d filename.y
cc lex.yy.c y.tab.c -ll
./a.out
OUTPUT
Enter the Expression
+23
Expression is invalid
Theoretical Explanation
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
is : [0-9]+. Action is
PARSER(Yacc code)
The token definition for the number ad identifiers
%token NUMBER ID
yyerror():Whenever a yacc parser detects a syntax error ,it calls yyerror ( ) to report the
error to the user.
S.J.B.I.T
Dept. of C.S.E
{return
{return
{return
{return
DIG;}
LET;}
yytext[0];}
0;}
/*Logical EOF*/
%%
Yacc part
%token LET
%token DIG
%%
stmt:id {printf("Valid identifier \n");}
;
id: letter next
| letter {;}
;
next: letter next
| digit next
| letter
| digit {;}
;
letter: LET {;}
;
digit: DIG {;}
;
%%
int main()
{
printf("Enter an identifier:");
yyparse();
}
int yyerror()
{
printf("Not a valid identifier\n");
exit(0);
}
OUTPUT
Enter an identifier:ab12
Valid identifier
Enter an identifier:12dc
Not a valid identifier
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation
LEXER(lex code)
1. To match the digit the pattern is [0-9].Action is to return the token DIG value.
2. To match the letter the pattern is [a-z].Action is to return the token DIG value.
PARSER
The grammar to recognize a valid identifier
statement: id
;
id: letter next
| letter {;}
;
next: letter next
| digit next
| letter
| digit {;}
;
letter: LET {;}
;
digit: DIG {;}
S.J.B.I.T
Dept. of C.S.E
%%
Yacc part
%token NUM
%left '+''-'
%left '*''/'
%%
stmt : expr { printf("Result:%d\n",$1);return 0; }
;
expr :expr'+'expr {$$=$1+$3;}
| expr'-'expr
{$$=$1-$3;}
| expr'*'expr
{$$=$1*$3;}
| expr'/'expr
{$$=$1/$3;}
| '('expr')'
{$$=-$2;}
| NUM
{$$=$1;}
;
%%
int main()
{
printf("Enter the expression\n");
yyparse();
}
int yyerror()
{
printf("Invalid input\n");
exit(0);
}
~]#
~]#
~]#
~]#
lex filename.l
yacc -d filename.y
cc lex.yy.c y.tab.c -ll
./a.out
OUTPUT
Enter the expression
2+3
Result:5
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation
Lexer
1.String of digit is number ,whitespace is ignored. Whenever the lexer returns a token to the
parser ,if the toke has an associated value, the lexer must store the value in yylval befor
returning. We explicitly declare yylval.
Parser
The grammar is as follows
expr
|
|
|
|
|
;
:expr'+'expr
expr'-'expr
expr'*'expr
expr'/'expr
'('expr')'
NUM
{$$=$1+$3;}
{$$=$1-$3;}
{$$=$1*$3;}
{$$=$1/$3;}
{$$=-$2;}
{$$=$1;}
S.J.B.I.T
Dept. of C.S.E
{return A;}
{return B;}
{return yytext[0];}
{return yytext[0];}
%%
Yacc part
%token A B
%%
str : s'\n'
s : A s B ;
| ;
{return 0;}
%%
int main()
{
printf("Type the string\n");
yyparse();
printf("Valid string");
}
int yyerror()
{
printf("Invalid string");
exit(0);
}
~]#
~]#
~]#
~]#
lex filename.l
yacc -d filename.y
cc lex.yy.c y.tab.c -ll
./a.out
OUTPUT
Type the string
aaabbb
Valid string
Theoretical Explanation
LEXER
1.To match the string a the pattern is a. Action is to return the token A value .
2.To match the string b the pattern is b. Action is to return the token B value .
PARSER
The grammar is as follow:
Compiled by: Darshan.K.R
string : s'\n'
s : A s B ;
S.J.B.I.T
Dept. of C.S.E
{return
{return
{return
{return
A;}
B;}
yytext[0];}
yytext[0];}
%%
Yacc Part
%token A B
%%
str: s'\n' {return 0;}
s : x B ;
x : x A
| ;
%%
int main()
{
printf("Type the string\n");
yyparse();
printf("Valid string");
}
int yyerror()
{
printf("Invalid string");
exit(0);
}
~]#
~]#
~]#
~]#
lex filename.l
yacc -d filename.y
cc lex.yy.c y.tab.c -ll
./a.out
OUTPUT
Type the string
aaaaab
Valid string
Theoretical Explanation
LEXER
1.To match the string a the pattern is a. Action is to return the token A value .
2.To match the string b the pattern is b. Action is to return the token B value .
PARSER
The grammar is as follow:
string : s \n
s : x B ;
x : x A
| ;
S.J.B.I.T
Dept. of C.S.E
example.sh
The first line is interpreter line. Here, this line specifies the shell we are using i.e Bourne shell
A shell script is executed by using the shell command sh as shown below.
[root@localhost ~]# sh example.sh
or else use
Todays date is : Sat Jan 27 09:10:18 IST 2004
My Shell :/bin/sh
3.1 Comments
In shell scripts comments are written using the hash (#) character as the first character of the
comment line.
3.2 The read Command
The read command or statement is the shells internal tool for taking the input from the user
,i.e., making scripts interactive. It is used with one or more variables. Input supplied through
the standard input is read into the variables.
#!/bin/sh
echo Enter the value of x
read x
echo The value of x is : $x
value.sh
When you use a statement like read x the script pauses at that point to take input from the
keyboard. Whatever you enter is stored in the variable x. Since this is a form of assignment
,no $ is used before x. To display that value we have to use $ symbol along with variable.
S.J.B.I.T
Dept. of C.S.E
Variable
Significance
$#
$*
$0
$1,$2,$3.
$?
$$
$!
if command is successful
then
execute commands
else
execute commands
fi
if command is successful
then
execute commands
elif command is
then
commands
else
commands
fi
Format 1
Compiled by: Darshan.K.R
Format 2
S.J.B.I.T
Format 3
Dept. of C.S.E
Meaning
Equal to
Not equal to
Greater than
Greater that or equal to
Less than
Less than or equal to
The operators begins with (hyphen) ,followed by a two-letter string. The operators are quite
mnemonic; -eq implies equal to ,-lt less than and so on.
Example:
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
a=8 ; b=9
test $a eq $b ; echo $?
1
test $a lt $b
0
# do is a keyword
#done is a keyword
S.J.B.I.T
Dept. of C.S.E
S.J.B.I.T
Dept. of C.S.E
PART B
UNIX Programming
1a.Non-recursive shell script that accepts any number of arguments and prints them in the
Reverse order, ( For example, if the script is named rargs then executing rargs A B C should
produce C B A on the standard output).
#!/bin/sh
c=$#
echo "The arguments in reverse order are:"
while [ $c -ne 0 ]
do
eval echo \$$c
c=`expr $c - 1`
done
OUTPUT
The arguments in reverse order are:
three
two
one
[root@localhost ~]# sh filename.sh A B C
The arguments in reverse order are:
C
B
Theoretical Explanation:
The first line is the interpreter line informing that we are using Bourne shell. We can use any
of the shell which is available like Korn shell (ksh), Bash shell (bash), C shell (c).
The special shell parameters $# holds the number of arguments passed in the command line
.Assume you are passing three arguments X Y Z .So $# value will be 3.
The while loop does the operation still control command returns a true exit status.
The use of eval command makes the shell to scan the command line once more, that is,
second time and then actually executes the command line. Here because of metacharacter \
the first $ is overlooked and the next variable $c is gets evaluated resulting 3. After this
evaluation the statement will be equivalent to echo $3.So the positional parameter $3 value is
displayed and so on the remaining positional parameter value is displayed.
While evaluating the expression the operands must be enclosed on either side by whitespace.
S.J.B.I.T
Dept. of C.S.E
OUTPUT
Child process has been created
Enter the command to be executed
date
Tue Jan 20 16:17:01 IST 2009
Enter 1 to continue and 0 to exit
1
Child process has been created
Enter the command to be executed
cal
January 2009
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Enter 1 to continue and 0 to exit
Theoretical Explanation:
All processes in a UNIX system ,expect the very first process (Process 0) which is created by
the system boot code and remaining are created via the fork system call.
The fork system call is used to create a child process .The function prototype of fork is :
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation:
Listing the file attributes is done with ls l (long) option. This option displays all the 7
attributes of a file like its permission,links,owner ,group owner ,size ,last modification time
and filename.
[root@localhost ~]# ls l
total 45
-rw-rr-- 1 chan sjbit 1765 Jan 27 10:89 data.txt
To extract the specific field we need to use simple filter cut follows with c option with a list
of column numbers. Ranges can be specified using the hyphen.
To extract the permission field we have use cut c 1-10.
Here we are using pipeline mechanism to redirect the output of ls l to the simple filter cut.
A pipe is general mechanism by using which ,the output of one program is redirected as the
input to another program directly without using any temporary files in between. In pipeline
,the command on the left of the | must use standard output and the one on the right must use
standard input.
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
/*creating a file*/
/*writing the data*/
/*To set the offset*/
OUTPUT
abcdefghijklmnop^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@ABCDEFGHIJKLMNOP
Theoretical Explanation:
creat Function
A new file can be created by calling the creat function. The function prototype of creat
function
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
write Function
Data is written to an open file with the write function.
#include <unistd.h>
ssize_t write(int filedes, const void *buf, size_t nbytes);
lseek Function
Every open file has an associated "current file offset," normally a non-negative integer that
measures the number of bytes from the beginning of the file .Read and write operations
normally start at the current file offset and cause the offset to be incremented by the number
of bytes read or written. By default, this offset is initialized to 0 when a file is opened, unless
the O_APPEND option is specified.
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
The interpretation of the offset depends on the value of the whence argument.
If whence is SEEK_SET, the file's offset is set to offset bytes from the beginning of the
file.
If whence is SEEK_CUR, the file's offset is set to its current value plus the offset. The
offset can be positive or negative.
If whence is SEEK_END, the file's offset is set to the size of the file plus the offset. The
offset can be positive or negative.
S.J.B.I.T
Dept. of C.S.E
OUTPUT
run:
total 224
-rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rwxr-xr-x
drwxr-xr-x
-rw-r--r-drwxr-xr-x
-rw-r--r--rw-r--r--
1
1
1
1
1
1
1
2
1
2
1
1
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
17
28
618
614
477
476
22195
4096
35781
4096
9610
76
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
20
24
24
24
24
24
24
25
24
25
24
24
07:36
07:28
19:03
19:02
07:26
07:26
19:55
11:45
19:55
11:45
19:55
19:55
1
100
1a.l
1a.l~
1b.l
1b.l~
a.out
d
lex.yy.c
o
y.tab.c
y.tab.h
run/d:
total 0
run/o:
total 0
Maximum size file is
35781 lex.yy.c
Theoretical Explanation:
To take the input from the user we have to use read statement with a variable.
Recursively to descend all the files in list we need to use ls command with R option.Also
l is used for long listing. The output of ls command is piped to a terminal using tee
command ,as well as to a file. Then further it is piped to the simple filter cut . To extract
specific column we need use c option with a list of column numbers, delimited by a comma.
To extract the filesize field we need to use the range 38-43 and to extract the file name we
are use 57-64.Then at last the output is redirected to an another file.
To display the content of a file we can use cat command. Then the file content is sorted with
respect to numerals using sort n option. Then the sorted output is redirected to an another
file. So the last line while have the maximum value ,we can extract that using the filter tail
with -1 option from the file.
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
OUTPUT
x.c
File is regular file
data1
File is a block file
data2
File is a character file
data3
Symbolic link file
S.J.B.I.T
Dept. of C.S.E
Theoretical Explanation:
lstat Functions
#include <sys/stat.h>
int lstat(const char *restrict pathname, structstat *restrict buf);
{
st_mode;
st_ino;
st_dev;
st_rdev;
st_nlink;
st_uid;
st_gid;
st_size;
st_atime;
st_mtime;
st_ctime;
st_blksize;
st_blocks;
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
File Types
Most files on a UNIX system are either regular files or directories, but there are additional
types of files. The types are:
1. Regular file. The most common type of file, which contains data of some form. There
is no distinction to the UNIX kernel whether this data is text or binary. Any
interpretation of the contents of a regular file is left to the application processing the
file.
2. Directory file. A file that contains the names of other files and pointers to
information on these files. Any process that has read permission for a directory file
can read the contents of the directory, but only the kernel can write directly to a
directory file
3. Block special file. A type of file providing buffered I/O access in fixed-size units to
devices such as disk drives.
4. Character special file. A type of file providing unbuffered I/O access in variablesized units to devices. All devices on a system are either block special files or
character special files.
5. FIFO. A type of file used for communication between processes. It's sometimes
called a named pipe.
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
Type of file
S_ISREG()
regular file
S_ISDIR()
directory file
S_ISCHR()
S_ISBLK()
symbolic link
S_ISSOCK() socket
S.J.B.I.T
Dept. of C.S.E
extension(Not mandatory)
the editor command and write the content of the file
data1.txt
data2.txt
OUTPUT
#to unbundled ,bash this file
echo data1.txt
1>&2
cat >data1.txt << 'End of data1.txt'
Unix
End of data1.txt
echo data2.txt
1>&2
cat >data2.txt << 'End of data2.txt'
shell programming
End of data2.txt
S.J.B.I.T
/*Content of data1.txt*/
/*Content of data2.txt*/
Dept. of C.S.E
OUTPUT
I am the child and my parent id 16599
Child process
My own id is 16600
My id is 16600 and my child id is 0
My id is 16599 and my child id is 16600s
Theoretical Explanation:
Every process has a unique process ID, a non-negative integer. Because the process ID is the
only well-known identifier of a process that is always unique, it is often used as a piece of
other identifiers, to guarantee uniqueness.
Process ID 0 is usually the scheduler process and is often known as the swapper. No program
on disk corresponds to this process, which is part of the kernel and is known as a system
process. Process ID 1 is usually the init process and is invoked by the kernel at the end of the
bootstrap procedure.
In addition to the process ID, there are other identifiers for every process. The following
functions return these identifiers.
Compiled by: Darshan.K.R
S.J.B.I.T
Dept. of C.S.E
S.J.B.I.T
Dept. of C.S.E
BIBILOGRAPHY
1.Lex & yacc , 2nd Edition by John R.Levine ,Tony Mason & Doug Brown.
2.Unix Concepts and applications ,Fifth Edition by Sumitabha Das.
3.Unix System programming by Terrence Chan.
4. The UNIX Programming Environment by Brain W. Kernighan and Rob Pike.
5. Compiler Design by Alfred V Aho, Ravi Sethi, Jeffrey D Ullman:
Compilers- Principles, Techniques and Tools, Addison-Wesley,
S.J.B.I.T
Dept. of C.S.E