Knowledge / Computer Science and Engineering / Processes and threads

Processes and threads

Computer Science and Engineering (B.E./B.Tech) · Operating systems · free study notes for exams, placements and interviews

In short

A process is a program in execution - the passive code on disk plus its current activity: registers, program counter, stack, heap and the operating system's bookkeeping about it. A thread is a single flow of control inside a process; threads of one process share its code, data and open files but keep their own stack and registers. Multiprogramming (many processes) and multithreading (many threads per process) are how an OS keeps the CPU and I/O devices busy at once, and are the base on which scheduling, synchronisation and memory management are built. This is examined heavily in GATE CSE, ISRO/BARC scientist papers, UGC-NET and university OS papers (fork-output counting, state diagrams, process-vs-thread comparisons), and asked in almost every software interview - TCS, Infosys, Wipro, Cognizant, Zoho, Amazon, Microsoft, Samsung R&D, Qualcomm, Nvidia and Bangalore/Pune systems teams.

Explained

Process concept, image and PCB

Process states, queues and context switching

Process creation, termination and the UNIX calls

Interprocess communication (IPC)

IPC methodDirectionSpeedScope
Shared memoryAnyFastestSame machine; needs explicit synchronisation
PipeHalf-duplexFastRelated processes (parent-child)
FIFO (named pipe)Half-duplexFastUnrelated processes on one host
Message queueAnyModerateOne host; messages are discrete and can be typed/prioritised
SocketFull-duplexModerate/slowSame host or across the network
RPCRequest-replySlowestAcross machines, language-neutral

Threads: motivation, structure and models

Concurrency, parallelism and multicore

Key points

Formulas and examples

Total processes after n sequential, unconditional fork() calls = 2ⁿ (children = 2ⁿ − 1). Same for for(i=0;i<n;i++) fork();
CPU utilisation with degree of multiprogramming: U = 1 − pⁿ, where p = fraction of time a process spends waiting for I/O, n = processes resident in memory
Context-switch overhead = (switches per second × time per switch) ÷ 1 s, expressed as a fraction of CPU time
Amdahl's law: S(N) = 1 / [(1 − P) + P/N]; maximum speedup = 1/(1 − P). P = parallel fraction, N = processing cores
Gustafson's law (scaled): S(N) = (1 − P) + N·P
Parallel efficiency E = S(N)/N (expressed as %)
Memory for T threads ≈ T × (stack size + TCB size); process memory ≈ text + data + heap + Σ thread stacks
Effective CPU time per job = (useful work) + (switches × switch cost)

Example 1 - counting processes created by fork.

int main(void) {
    for (int i = 0; i < 3; i++) fork();
    printf("Hello\n");
    return 0;
}

Example 2 - short-circuit fork (a standard GATE trick).

fork() && fork();

Example 3 - separate vs shared data.

int x = 5;                      /* with fork */            int x = 5;   /* with threads */
pid_t p = fork();                                          void *f(void *a){ x += 10; return 0; }
if (p == 0) { x += 10; printf("child  x=%d\n", x); }       pthread_create(&t,0,f,0); pthread_join(t,0);
else { wait(NULL); printf("parent x=%d\n", x); }           printf("x=%d\n", x);

Example 4 - multiprogramming and utilisation. Processes spend p = 80% of their time waiting on I/O.

n (processes in memory)U = 1 − 0.8ⁿ
120.0%
236.0%
348.8%
459.0%
567.2%
1089.3%

Example 5 - context-switch overhead and Amdahl's law.

AttributeProcessThread
Address spaceOwn, isolatedShared with peer threads
Creation costHigh (page tables, descriptors)Low
Context switchExpensive (memory map, TLB flush)Cheap (registers + stack pointer)
CommunicationIPC: pipes, shared memory, socketsShared variables directly
Fault isolationA crash kills only that processA crash/invalid write kills the whole process
OwnsCode, data, heap, files, PCBPC, registers, stack, TLS, TCB
CalledHeavyweightLightweight

Where it is used

Interview questions

Common mistakes

Related topics

CPU scheduling · Process synchronisation and deadlocks · Memory management and virtual memory · Linux internals: processes, signals and permissions · Virtualisation and containers · I/O systems and device management

Open in the Knowledge reader →Find jobs →