Lab 7 : Find & Signal
Q?What is this Unix Command find
/>find “where to look” “criteria” “what to do”
/>it is used to locate files in unix/linux filesystem
/>we can search for any set of directories you specify for files that
match the supplied search criteria
/>we can search for files by name, owner, group, type, permissions,
date, and other criteria
Examples:
find
. -name “*.txt” : starting from current directory looks for all
files with txt extension
find . -type d : starting from current directory look for all directory
find . -mindepth 5 : start at level 5
find . -maxdepth 5 : stop search at level 5
Note *: file operators : -not -and -or ! & o
-exec: used to execute commands on the search result
Q?What are unix signals
/>it is an asynchronous notification sent to a process in order to
notify it of an event that occurred.
/>when a signal is sent, the OS interrupts the target process's normal
flow of execution to deliver the signal
/>if a process has perviously registered signal handler, that routine
is executed otherwise, the default signal handler is executed.
Q?How do we send signals
/>kill(1) system calls allows
a user to send signals to processes
/>kill(2) system calls allows
a user to send a specific signal to specific processes, if permission
allows
Ex:SIGKILL SIGCHLD
Q?How do we wait for signals
/>int pause(void);
/>it just waits for the signal to arrive
Q?How do we handle signals
/>We can install signal handler with
the signal() system call to handle signals
/>if a signal handler is not installed for particular signal, the
default signal handler is used. Otherwise, the signal is intercepted and the signal handler is
invoked.
Learning goals: in this laboratory activity you will became
familiar with find command. You will also learn how to register a
signal handler for a process and how to use signals to synchronize
processes execution.
Exercise 1
Use find to do the following:
1.Print the contents of all the directories named bin starting from the
root directory;
2.Search for all the .c files belonging to other users (not you) but in
the same group as yours;
3.Print the pathname of all the files whose size is greater than 100
bytes;
4.Modify all the permissions of the files in your home directory so
that all the users other than you have no permission;
Exercise 2
Write a C program in which the parent and a child process are synchronized
by means of signals. Each process prints an output line, such as:
I’m the parent
I’m the child
I'm the parent
I’m the child
...
Exercise 3
Write a C program in which the parent process creates two child processes,
a producer and a consumer. The producer reads a line of text from
stdin and writes it to a file. The consumer process waits for data to
be written by the producer and once data is available reads data from
the file. Once a line is read by the consumer it shows the text line.
When the producer reads “end” the program terminates.
Hints Use again signals to synchronize read/write operations to the file.
Modify the previous exercise so to add reading and writing to file cap
abilities. Be careful that here the producer and consumer processes
are two child processes. The parent process should wait for both of
them to terminate before calling exit. You need to make sure that
each child process knows the PID of the other child process in order
to synchronize read/write operations to the file by means of signals.
Summary
At the end of this laboratory activity you should have understood how
find command works. You should also have understood the difference between registering
a signal handler and sending a signal to a process. Finally, you should know how to synchronize
processes using signals.
Comments
Post a Comment