Applied Operating Systems
Topics based on Chapters 4 and 5
Processes and Threads
Notes based on those of Richard Furuta and Bart Childs, TAMU
Chapter overview Processes: Review of Terminology Multiprocessing Terminology Process Sequential Process Characteristics Process States As a process executes, it changes state: Process State Diagram
(See figure 4.1)
Notes on Process States Process Control Block (PCB) PCB Contents
(examples of possible fields) Process scheduling queues PCBs and Queues
(See figure 4.4)

Process Scheduling

Queuing diagram representation of process scheduling
(See figure 4.5)

Process Scheduling

Process Scheduling Possible Scheduling Objectives Context Switch Process Scheduling Operating System
Process Management Functions Process creation Steps in Process Creation Steps in Process Creation
Second Approach (fork) UNIX fork()
Distinguishing Parent and Child if(childpid=fork()) { /* this is the parent (fork returned childís PID  which is nonzero or "true" */ }
else { /* this is the child (fork returned 0, which
is "false" */
}
Unix fork() example #include <stdio.h>
int main()
{ int pid; char ch; int i, j;
pid = fork();
if(pid) ch = 'a'; else ch = 'b';
for(i=1;i<=25;i++) { fputc(ch,stdout); fflush(stdout);
for(j=1;j<100000;j++) ;
}
if(pid) { wait();
fputc('\n', stdout);
}
}
Unix fork() example bbbabbaabbbaaabaabbaabbabbbaaabbaabbbaaabaaabbaaba
bbbbbbbaaaaaaabbbabaaabbbaabbbabbaabbaaabababaaaba
bbaaaaaaaaaaaaaabbbbbbbbbbaaabbbabbbaabbaabaaabbbb
aaaaaaaaaaaaaabbbbbbabbbaabababbbaaabbbaaabbbbbbbb
bbbabbaaabbaaabaabbaabbbabbaaabaabbaaabbbbbaabbaaa
bbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaabbbaaabaabbaaab
aaaaaabbbbabbbaaabbbaabbabbbaabbaaaaabbbaaabbababb
bbbbbbaaabbaaabbaabaaabbbabbabbbabbbaabaaabaabaaaa
bbbbabaaabbbaaabbaabbbaaabaaabbbaabbaaabbaabbbaaab
bbbbbbbbbbbbbbbbaaaaaaaaaaaaaababbaabbaaabbbaabaaa
bbbbbaaaabbaaabbbaabbbaaabaabaabbbabbaabbaaabbaaba
bbbbbbbbaaaaaabbbaabbaaabaaabbababbaaabbaabbbaaaba
Process Creation via fork()
Some Options Process Termination Waiting Processes Waiting Processes Independent Processes Cooperating Processes Why have Cooperating Processes? Producer-Consumer problem Bounded buffer/Shared memory int n;
typedef item Ö;
item buffer[0..n-1];
int in, out;         // range of 0..n-1
while (true) { ---produce an item in nextp
while ( in+1 % n == out) /*no-op*/;
buffer[in] = nextp;
in = in+1 % n;
}
while (true) { while (in == out) do no-op;
nextc = buffer[out];
out = (out + 1) % n;
---consume the item in nextc
}  /* endwhile */
Note that this solution only can fill up n-1 buffers
Threads Lightweight vs Heavyweight Processes Thread Scheduling Example applications Threads in Solaris 2 Threads in Solaris 2
(See Figure 5.5)

Solaris 2 Threads

Interprocess Communication (IPC) Why use messages? IPC can be direct or indirect Direct Communication
Producer/Consumer example Indirect Communication IPC Buffering IPC exception conditions Remote Procedure Calls (RPC)