Process and Signals - Linux System Programming - Lecture Notes, Study notes of Linux skills

Topics covered in this lecture handout are: Process, Signals, UNIX, Zombies, Structure, PID, Command, PSTREE.

Typology: Study notes

2011/2012

Uploaded on 10/24/2012

alia_maru
alia_maru 🇮🇳

4.5

(41)

57 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Process and signals
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Process and Signals - Linux System Programming - Lecture Notes and more Study notes Linux skills in PDF only on Docsity!

Process and signals

Introduction

  1. Processes and signals form a fundamen- tal part of the UNIX operating environ- ment. They control almost all activities performed by UNIX computer system.
  2. An understanding of how UNIX manages processes will hold any systems program- mer, applications programmer or system administrator in good stead.
  3. We will look at how processes are han- dled in the Linux environment and how to find out what computer is doing at any given time. And how to start and stop process, how to make process cooperates with each other, how to avoid zombies(in which parents died before children).

1

A process consists of the executable program, its data and stack, variables(occupying system memory), open files(file descrip- tor) and an environment.

UNIX allows many users to access the system at the same time. Each user can run many programs, or even many instances of the same program, at the same time. The system itself runs other programs to manage system resources and control user access.

Typically, UNIX system will share code and system libraries between processes, so that there is only one copy of the code in memory at any one time.

For example: two users, Neil and rick both run the command “grep” on the shell at the same time to look for dif- ferent strings in different files. This pro- duces out two processes, each has it own process identifier, PID. And one copy of program code of “grep” is loaded into

the memory as read only, two users share this code. And the system library is also shared(remind you the shared library).

  • What process is running?
    1. ps command ps prints out the information about the active processes. without options it only prints out the information about process associated with the control- ling terminal.

$ ps PID TTY TIME CMD 25089 pts/1 00:00:00 tcsh 25115 pts/1 00:00:00 ps

$ps -au USER PID %CPU %MEM VSZ RSS TTY lchang 25089 0.0 0.5 2860 1380 pts/ lchang 25116 0.0 0.3 2856 896 pts/

Explanation: USER: user’ name; PID: process identifier; CPU: cputime/real

|-gnome-name-serv |-gnome-smproxy |-gnome-terminal-+-gnome-pty-helpe | |-tcsh | ‘-tcsh---pstree |-identd---identd---3[identd] |-jserver |-keventd |-khubd |-klogd |-kreclaimd |-kswapd |-kupdated |-lockd |-login---tcsh---startx---xinit-+-.xini | ‘-X |-magicdev |-mdrecoveryd |-5[mingetty] |-panel |-portmap |-rpc.statd |-rpciod

|-sawfish |-sshd |-syslogd |-tasklist_applet |-xemacs |-xfs |-xinetd |-xscreensaver ‘-ypbind---ypbind---2*[ypbind]

Explanation: pstree command displays all process as a tree with its root being the first process that runs, called init. If the user name is specified, then that user’s processes are at the root of the tree.If a process creates more than one process of the same name, pstree visu- ally merges the identical branches by putting them in square brackets and prefixing them with the number of times the process is repeated.

  • Parent and child

processes share CPU, the child pro- cess has a copy of the parents’ en- vironment, open files, user identifier, current working directory and signals. The call to fork in the parent returns the PID of the new child process. At the same time, the call return to the new child a 0 to indicate it is a child.

pid_t new_pid; new_pid = fork();

switch(new_pid){ case -1: /* erro / break; case 0: /we are child/ break; default: /we are parent*/ break; }

  1. Exercise: A program calls fork()

#include <sys/types.h>

#include <unistd.h> #include <stdio.h>

int main() { pid_t pid; char *message; int n;

printf("fork program starting\n"); pid = fork();

switch(pid){ case -1: fprintf(stderr, "fork failed"); exit(1); case 0: n=3; message = "Child"; break; default: n=4; message = "Parent";

exit status. The status information is written to location that “stat loc” points to. If the parent doesn’t wait and child still exits, the child is put in a zombie state, it will stay in the state until ei- ther the parent call wait or the parent dies. If the parent dies before child, the init process adopts the orphaned zombie processes. The wait call is not only used to put parent go to sleep, also to make sure that the process terminates properly.

The exit status are defined in < sys/wait.h > as followed:

WIFEXITED(stat_val): Non-zero if the child is terminated normally. WEXITSTATUS(stat_val): If WIFEXITED is non-zero, this returns the exit code. WIFSIGNALED(stat_val):Non-zero if the child

is terminated on an uncaught signal. WTERMSIG(stat_val):If WIFSIGNAL is non-zero, this returns the signal number. WIFSTOPPED(stat_val):Non-zero if the child has stopped. WSTOPPED(stat_val): If WIFSTOPPED is non-zero, this returns the signal number.

  1. Exercise: A program calls wait()

#include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h>

int main() { pid_t pid; char *message; int n; int stat_val;

printf("fork program starting\n");

printf("Child %d has finished\n", child_pid); if(WIFEXITED(stat_val)) printf("Child exit with code %d\n", WEXITSTATUS(stat_val)); else printf("Child terminate abnormally\n"); } exit(WEXITSTATUS(stat_val)); }

Signals

  • What is signal?

A signal is an event generated by the UNIX system in response to some con- dition, upon which a process may in turn take some action.

Signals are generated by some error con- ditions, such as memory segment viola- tions, floating point processor errors or il- legal instructions. They are generated by the shell and terminal handlers to cause interrupts.

Signals can also be explicitly sent from one process to another as a way of pass- ing information or modifying behavior.

Generally, Signals can be generated, caught and acted upon, or ignored.

Signal names are defined by including the header file < signal.h > as followed: 3

The signal is to be caught or ignored is given as argument sig. The function to be called when the specified sig- nal is received is given as func. This function must take a single int argu- ment(the signal received) and is of type void. This function can be one of these two special values:

SIG_IGN: Ignore the signal SIG_DFL : Restore the default behavoir

  1. A program CtrlC

#include <signal.h> #include <stdio.h> #include <unistd.h>

void ouch(int sig) { printf("Ouch! -I got the signal %d\n",sig); (void) signal(SIGINT, SIG_DFL); }

int main() { (void) signal(SIGINT, ouch); while(1){ printf("Hello World!\n"); sleep(1); } }

Explanation: the function ouch reacts to the signal which is passed in the pa- rameter sig. And this function is called when a signal is occurred. It prints out a message, and then handle the signal SIGINT(by default generated by press- ing Ctrl-C), reset it back to the default behavior. The main function has an infinite loop of printing a message, unless a signal is received and handled by call for sig- nal(). Execute this program to see what is happened.