Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

20
Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    2

Transcript of Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 1: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 1

Task Control:Signals and AlarmsChapter 7 and 8

B. Ramamurthy

Page 2: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 2

Multi-tasking

• How to create multiple tasks? Ex: Xinu create() • How to control them?

– ready()– resched()

• How to synchronize them? How to communicate among them?

• XINU: semaphores, send and receive messages

• How to (software) interrupt a process? signals

Page 3: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 3

Examples

• Consider g++ myProg.c– You want to kill this process after you started the

compilation..hit cntrl-C

• Consider execution of a program called “badprog”>badprogIt core dumps .. What happened? The error in the

program results in a signal to kernel to stop and dump the offending code

• Consider “kill –p <pid>”– Kill issues a termination signal to the process

identified by the pid

Page 4: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Linux Processes

• Similar to XINU Procs.• Lets understand how to create a

linux process and control it.• Chapter 7 and 8 of text book.• Chapter 7 : multi-tasking• Chapter 8: Task communication

and synchronization

Page 4

Page 5: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 5

Process creation• Four common events that lead to a process

creation are:1) When a new batch-job is presented for

execution.2) When an interactive user logs in / system

initialization.3) When OS needs to perform an operation

(usually IO) on behalf of a user process, concurrently with that process.

4) To exploit parallelism an user process can spawn a number of processes.

Page 6: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 6

Termination of a process• Normal completion, time limit exceeded, memory

unavailable• Bounds violation, protection error, arithmetic error,

invalid instruction• IO failure, Operator intervention, parent

termination, parent request, killed by another process

• A number of other conditions are possible. • Segmentation fault : usually happens when you

try write/read into/from a non-existent array/structure/object component. Or access a pointer to a dynamic data before creating it. (new etc.)

• Bus error: Related to function call and return. You have messed up the stack where the return address or parameters are stored.

Page 7: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 7

Process control

• Process creation in unix is by means of the system call fork().

• OS in response to a fork() call:– Allocate slot in the process table for new process.– Assigns unique pid to the new process..– Makes a copy of the process image, except for

the shared memory.– both child and parent are executing the same

code following fork()– Move child process to Ready queue. – it returns pid of the child to the parent, and

a zero value to the child.

Page 8: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 8

Process control (contd.)

• All the above are done in the kernel mode in the process context. When the kernel completes these it does one of the following as a part of the dispatcher:– Stay in the parent process. Control returns to

the user mode at the point of the fork call of the parent.

– Transfer control to the child process. The child process begins executing at the same point in the code as the parent, at the return from the fork call.

– Transfer control another process leaving both parent and child in the Ready state.

Page 9: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 9

Process Creation (contd.)

• Parent process create children processes, which, in turn create other processes, forming a tree of processes

• Generally, process identified and managed via a process identifier (pid)

• Resource sharing– Parent and children share all resources– Children share subset of parent’s resources– Parent and child share no resources

• Execution– Parent and children execute concurrently– Parent waits until children terminate

Page 10: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 10

Process Termination

• Process executes last statement and asks the operating system to delete it (exit)– Output data from child to parent (via wait)– Process’ resources are deallocated by operating system

• Parent may terminate execution of children processes (abort)– Child has exceeded allocated resources– Task assigned to child is no longer required– If parent is exiting

• Some operating system do not allow child to continue if its parent terminates– All children terminated - cascading termination

Page 11: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Example Code1. int retVal;

2. printf(" Just one process so far\n");3. printf(" Invoking/Calling fork() system call\n");

4. retVal = fork(); /* create new process*/

5. if (retVal == 0)6. printf(" I am the child %d \n",getpid());

7. else if (retVal > 0)8. printf(" I am the parent, child has pid %d \n", retVal);

9. else10. printf(" Fork returned an error %d \n", retVal);

Page 12: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Input/output Resources• What is standard IO?• These are resources allocated to

the process at the time of creation:• From Wikipedia/Standard_streams

Page 13: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 13

Signals

• Signals provide a simple method for transmitting software interrupts to UNIX process

• Signals cannot carry information directly, which limits their usefulness as an general inter-process communication mechanism

• However each type of signal is given a mnemonic name; Ex: SIGINT

• See signal.h for others• SIGHUP, SIGINT, SIGILL, SIGTRAP, SIGFPE, SIGKILL• SIGALRM (sent by kernel to a process after an

alarm timer has expired)• SIGTERM • signal (signal id, function) simply arms the signal

Page 14: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Intercept Signals

Page 14

Task1

Task2

Two essential parameters are destination process identifierand the signal code number: kill (pid, signal)Signals are a useful way of handling intermittent data arrivals or rare errorconditions.

Page 15: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 15

Handling Signals

• Look at the examples:• Catching SIGALRM • Ignoring SIGALRM• sigtest.c• sigHandler.c• pingpong.c• See /usr/include/sys/iso/signal_iso.h for

signal numbers

Page 16: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 16

Signals and Alarms#include <signal.h> unsigned int alarm( unsigned int seconds ); alarm(a); will start a timer for a secsonds and will interrupt

the calling process after a secs.time(&t); will get you current time in the variable t

declared as time_t tctime(&t); will convert time to ascii formatAlarm has a sigaction function that is set for configuring

the alarm handler etc.sigaction(SIGALRM, &act, &oldact) ; the third paramter is

for old action configuration

Page 17: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 17

Sample programs

• Starting new tasks in linux: page 165• Programs in pages: 174-180 on signals

and alarms• See demos directory for first• See page 175 for the second program• See page 178 … for the third program

Page 18: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Pingpong

Page 18

Parent

Child

PSIG 43

CSIG 42

Page 19: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Observe in pingpong.c

• pause(): indefinite• sleep(): sleep is random/finite time• While loop• Signal handlers• Re-arming of the signals

Page 20: Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.

Page 20

Volatile (from code reading: last lecture)

• A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change: – Memory-mapped peripheral registers – Global variables modified by an

interrupt service routine – Global variables within a multi-threaded

application