Session 11 : Operating System Programming Concepts : Lab 11 - Semaphore & AWK
Lab
11 : Semaphore & Awk
Q?
What are semaphore
/>
semaphore are the synchronization primitives used for synchronizing
processes
/>
the simplest semaphore is a variable that can take only the values 0
and 1 (Binary sempahore)
/>
wait(sv) : if sv is greater than 0, decrement sv
if sv is zero, suspend execution of this process
/>
post(sv) : if some other process has been suspended waiting for sv
make it resume execution
if no process is suspended waiting for sv, increment sv
Q?
How are semaphore system call
/>
int
sem_init(sem_t *sem, int pshared, unsigned int value);
int
sem_wait(sem_t *sem);
int
sem_post(sem_t *sem);
Example
:
sem_init(&semA,
0, 1);
sem_init(&semB,
0,0);
pthread_create(&thIDA,
NULL, tfA, (void *)NULL);
pthread_create(&thIDB,
NULL, tfB, (void *)NULL);
….....
//tfA →
thread function A
sem_wait(&semA);q
sleep(1);
printf(“A\n”);
sem_post(&semB);
AWK
Q?
How can you perform explicit input with getline in awk
/>
The awk language has a special built-in command called getline that
can be used to read input under your explicit control
/>
getline returns 1 if it finds a record, and 0 if the end of the file
is encountered
/> if
there are some errors in getting a record, such as a le that cannot
be opened, then getline returns -1
Example:
{
if(NF
== 2 || $1
== "@include"){
while((getline
line
< $2)
> 0)
print
line
print
"hurray"
close($2)
}else
print
"jasldfkj"
}
/>
here the getline function takes its input from the FILE file and put
it into variable var
getline
var < file
Q?
What are patterns in awk
/>
patterns in awk control the execution of rules
/> a
rule is executed when its pattern matches the current input record
/>
there are following kinds of pattern
- /regular expression/
- Expression
- Range → part1, part2
- special pattern to supply start-up or clean-up information to awk → BEGIN, END
- empty pattern matches every input record → null
Q?
What are regular expression
/> is
a way of describing a class of string
/> a
regular expression in slashes (`/`) is an awk pattern that matches
every input record whose text belongs to that class
Q?
How to use regular expressions in comparison exp
/>
exp - /regexp/ (exp !~ /regexp/)
/>
this is true if the expression exp (taken as a character string) is
(not) matched by regexp
Some
of the Operators :
^ →
beginning of the string or the
beginning of a line within the string
$ →
matches only at the end of a string
or the end of a line within the string
. →
matches any single character except
a newline
[…] →
matches any one of the
characters that are enclosed in the square brackets
| →
used to specify alternatives
(…..)
→ used to concatenate regular expressions containing the alternativ
operator |
* →
the preceding regular expression is to be repeated as many times as
possible to find a match
+ →
the symbol is similar to '*', but the preceding expression must be
matched at least once
? →
the symbol is similar to '*', but the preceding expression can be
matched once or not at all
\ →
this is used to supress the special meaning of a character when
matching
Comparison
expression
x< y
→ true if x is less than y
x <=
y → true if x is less than or equal to y
x > y
→ true if x is greater than y
x >=
y → true if x is greater than or equal to y
x == y →
true if x is equal to y
x ~ y →
true if x matches the regular expression described by y
x !~ y →
true if x does not match the regular expression described by y
Boolean
expression
|| →
or
&&
→ and
! →
Not
Q?
what are range pattern
/> it
is made by two pattern separated by a comma, of the form
→ begpat,
endpat
/>
this matches the ranges of consecutive input records
/>
the first pattern begpat , controls where the range begins
/>
endpat controls where it ends
Example
:
awk ' $1
== “B”, $1 ==”E” '
/>
what it does is prints every record between B/E pairs, inclusive
Q?
what are BEGIN END special patterns
/>
they are not used to match input records
/>
they are used for supplying start-up or clean-up information to the
awk script
/> A
BEGIN rule is executed, once, before the rest input record has been
read
/> An
END rule is executed, once , after all the input has been read
Example:
BEGIN {
print “Count the number of line of a file : ”}
{++nlines}
END
{print “the number of line is ” nlines}
Q?
What are arrays in awk
/>
arrays in awk are similare with c programming language array , but
there are some fundamental difference
/> we
do not need to specify the size of an array before we start to use if
in awk
/>
any number or string in awk may be used as an array index
/>
each array is a collection of pairs in awk :
Example:
{for
(i=1;i<=NF;i++)
array[$i]++;}
END{#
for all elements of the array
for( w
in array)
{
print
“word:” w “frequency:” array[$w];
}
}
Comments
Post a Comment