Type
the following C program to see how shell invocation of a command works:
#include <stdio.h>
main ( )
{
int i;
/* fork another process */
i=fork();
if (i < 0 ) { /* error occured */
fprintf(stderr, "Fork
Failed");
exit(-1);
}
else if (i==0){ /* child process */
execlp("/bin/ls","ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to
complete */
wait(NULL);
printf("\n\n\n\n\nChild Complete\n\n\n\n");
exit(0);
}
}
Call the above
program p4.c, and compile it as follows: gcc -o p4 p4.c <Enter>.
Then execute p4
and report the output.
Questions:
4.1 [2] When
does the wait system call returns in this program?
4.2 [4]
Explain the shell invocation of a command?
4.3 [2]
Compare this program with shell invocation of a command?
5. LINUX Signals
A signal is a
notification to a process that an event has occurred. Signals are also called Software
Interrupts. Signals usually occur asynchronously. By this we mean that the
process doesn’t know ahead of time exactly when a signal occur. Signals can be
sent
- By one process
to another process (or to itself).
- By the kernel to
a process.
Every signal has a
name, specified in the header file <signal.h>. There are five
conditions that generates signals:
- The kill()
system call allows a process to send a signal to another process or to
itself. The name kill is somewhat a misnomer, since a signal does not
always terminate a process. Some signals do indeed kill the receiving
process, while others are used to inform the process of some condition
that the receiving process then handles. This is the format of kill()
system call: int kill(int pid, int sig); The kill()
system call does many different things, depending on the values of its
arguments. As expected, a process is not able to send a signal to any
other arbitrary process. To send a signal, the sending process and the
receiving process must belong to the same user, or the sending process
must be the superuser.
- The kill command
is also used to send signals. This command is a program that takes its
command line arguments and issue a kill() system call. For example
the kill –STOP <PID> <Enter> send a SIGSTP
signal to the process with the process ID of <PID>.
- Certain terminal
characters generate signals. For example, every interactive terminal has
associated with it an interrupt character character. The interrupt
character (typically Control-C or Delete) terminates a
process that is running (it generate a SIGINT signal). A terminal
suspend character (typically Control-Z) generates a SIGSTP
signal immediately.
- Certain hardware
conditions generate signals. For example referencing an address outside a
process’ address space generates a SIGSEGV signal.
- Certain software
conditions that are noticed by the kernel cause signals to be generated.
Having described what
signals are and how they are generated, what can a process do with a signal?
- A process can
provide a function that is called whenever a specific type of signal
occurs. The function, called a signal handler, can do whatever the process
wants to handle the condition. This is called catching the signal.
- A process can
choose to ignore a signal. All signals other than SIGKILL, can be
ignored. The SIGKILL signal is special, since the system
administrator should have a guaranteed way of terminating any process.
- A process can
allow the default to happen. You can find the Default actions in the
Figure 2.4 of the Stevens book (UNIX network programming) that I will put
on reserve at the library. Also in this book, you will find complete information
about the system calls that we used in this lab.
Programming
Assignment:
Include <signal.h>
header file in your program, when you are writing the following program. Use
the SIGKILL signal in kill() system call to kill all the
processes in the parent process group. Use the kill(0,SIGKILL); system
call in your program.
[18] Develop a small
program in C using system calls that does the following:
- A parent process
creates a child process that prints it’s PID.
- The child
creates a grandchild and sleeps 120 seconds.
- The grandchild
immediately exists.
- The parent
sleeps for 60 seconds and then kills all the processes.