/>
The kill() system call sends the signal given by sig to pid, a process or
a group of processes.
#include <signal.h>
int kill(pid_t pid, int sig)
System call kill() takes two arguments.
1. pid, is the process ID you want to send a signal to, and
2. sig, is the signal you want to send.
If the call to kill() is successful, it returns 0; otherwise, negative.
pid = fork();
...
ret = kill(pid, 9);
if (ret==0)
cout << "Process " << pid << " has been terminated" << endl;
else
cout << "Process " << pid << " termination not successful" << endl;
time() Gets current time in seconds.
======
time_t start = time(0);
...
time_t stop = time(0);
elapsedTime = stop - start;
clock()
======
#include <time.h>
clock_t clock(void);
The clock() function returns the amount of CPU time (in microseconds)
used since the first call to clock() in the calling process.
The time reported is the sum of the user and system times of the calling
process and its terminated child processes for which it has executed
the wait function.
Dividing the value returned by clock() by the constant CLOCKS_PER_SEC,
defined in the header, will give the time in seconds.
If the process time used is not available or cannot be represented,
clock returns the value (clock_t) -1.
int startTime = clock();
A lack of process synchronization results in deadlock or starvation where race conditions are often involved Race: ===== A race condition is a flaw in a system or process where the output exhibits unexpected critical dependence on the relative timing of events. Starvation: =========== Infinite postponement of a job Scheduling algorithm can detect & correct Deadlock: ======== A system-wide tangle of resource requests Each job waiting for a vital resource to become available The jobs come to a standstill Resolved via external intervention Deadlock needs following four conditions: 1. Mutual exclusion 2. Resource holding 3. No preemption 4. Circular wait Removal of one condition can resolve the deadlock (ab)Mutual exclusion: only one process has access to a dedicated resource Resource holding: holding a resource and not releasing it No preemption: lack of temporary reallocation of resources Circular wait: each process is waiting for another to release the resource so that at least one will be able to continue
Model uses directed graphs:
Processes represented by circles
Resources represented by squares
Solid arrow from a resource to a process means
that process is holding that resource
Solid arrow from a process to a resource means
that process is waiting for that resource
Direction of arrow indicates flow
If there’s a cycle in the graph then there’s a deadlock
involving the processes and the resources in the cycle
p1 holds R1 p1 waiting for r1 cycle = deadlock
If jobs are allowed to request and hold files for duration of execution:
P1 has access to F1 but requires F2 also
P2 has access to F2 but requires F1 also
Deadlock remains until a program is withdrawn or forcibly removed
Other programs that require F1 or F2 wait indefinitely
Draw Graph
P1 requests and gets Ra
P2 requests and gets Rb, P2 requests Ra
Deadlock? Why or why not? (4 conditions)
Draw Graph
P1 requests and gets Ra
P2 requests and gets Rb, P2 requests Ra
P3 requests and gets Rc, P3 requests Rb
P1 requests Rc
Deadlock? Why or why not? (4 conditions)
Strategies to deal with deadlocks include
Prevention:
===========
Prevent one of the four conditions from occurring
Avoidance:
=========
Avoid deadlock if it becomes probable
Detection:
=========
Detect deadlock and recover from it gracefully
Deadlock must be untangled once detected
All recovery methods have at least one victim
Recovery Methods:
=================
Terminate every job that’s active and restart them
Terminate only jobs involved in the deadlock and resubmit them
Identify jobs involved in deadlock and terminate them one at a time
Select a nondeadlocked job, preempt its resources, allocate them
to a deadlocked process
Five philosophers sit around a circular table. Each philosopher spends his
life alternately thinking and eating. In the center of the table is a large
plate of rice. A philosopher needs two chopsticks to eat a helping of rice.
Unfortunately, as philosophy is not as well paid as computing, the
philosophers can only afford five chopsticks.
One chopstick is placed between each pair of philosophers and they agree
that each will only use the chopstick to his immediate right and left.
Each philosopher thinks. When he gets hungry, he picks up the two chopsticks
that are closest to him. If a philosopher can pick up both chopsticks, he
eats for a while.
After a philosopher finishes eating, he puts down the chopsticks and
starts to think.
Each chopstick is shared by two philosophers and hence a shared resource.
The philosophers act as concurrent processes, they will all attempt to eat
immediately when they stop thinking.
Suppose that two neighboring philosophers get hungry at the same time. They
both need the same chopstick to eat. If a philosopher attempts to pick up a
chopstick that has already been picked up by his neighbor, we have a race
condition - the first philosopher to grab the chopstick gets to eat, the
other must wait for him to finish.
Example:
"The dining philosophers table" Dijkstra (1968)
To avoid starvation:
Implement algorithm to track how long each job has been waiting (aging)
Block new jobs until the starving jobs have been satisfied
Strategies:
When a person is hungry, you must first try to get the left
chopstick, then the right. You may not reach for both at once!
If the chopstick you want is unavailable,
wait until it is put down again.
If you can get the left chopstick but not the right, hold onto it.
Once you have both chopsticks you can eat, then put down first the left
chopstick and then the right chopstick. Now spend some time thinking.
When you get hungry again, repeat the cycle.