Processes & Threads, Slides of Operating Systems

A set of slides from a course on Operating Systems at Cornell University. It covers the concepts of processes and threads, including what a process is, how to create and manage processes, and the life cycle of a process. The slides also cover system call interfaces, creating a process via CreateProcess and fork, and built-in UNIX shell commands. code examples and possible outputs.

Typology: Slides

2015/2016

Uploaded on 05/11/2023

obesix
obesix 🇺🇸

4.3

(19)

237 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Processes'&'Threads
CS'4410,'Opera5ng'Systems'
Fall'2016'
Cornell'University
Rachit'Agarwal'
Anne'Bracy'
See:$Ch$3&4$in$OSPP$textbook$
The$slides$are$the$product$of$many$rounds$of$teaching$CS$4410$by$Professors$Sirer,$
Bracy,$Agarwal,$George,$and$Van$Renesse.$Some$content$from$Markus$Püschel$at$CMU.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Processes & Threads and more Slides Operating Systems in PDF only on Docsity!

Processes & Threads

CS 4410, Opera5ng Systems

Fall 2016

Cornell University

Rachit Agarwal

Anne Bracy

See: Ch 3&4 in OSPP textbook

The slides are the product of many rounds of teaching CS 4410 by Professors Sirer,

Bracy, Agarwal, George, and Van Renesse. Some content from Markus Püschel at CMU.

What is a Process?

  • An instance of a program
  • An abstrac5on of a computer: Address Space + Execu5on Context + Environment A good abstracOon:
  • is portable and hides implementa5on details
  • has an intui5ve and easy-to-use interface
  • can be instan5ated many 5mes
  • is efficient and reasonably easy to implement

Who should be allowed to start a process? Possibility #1: Only the kernel may start a process Possibility #2: User-level processes may start processes

System Call Interface System Call Interface Portable Operating System Kernel Portable OS Library Compilers Web Servers Source Code Control Web Browsers Email Databases Word Processing x86 ARM PowerPC 10Mbps/100Mbps/1Gbps Ethernet 802.11 a/b/g/n SCSI IDE Graphics Accelerators LCD Screens Why so skinny? Example: Crea%ng a Process Windows: CreateProcess(…); UNIX fork + exec

Abstract Life of a Process New Runnable Running Zombie Waiting admiSed done I/O CompleOon I/O OperaOon dispatch interrupt, descheduling Details on Thursday.

CreateProcess (Simplified) System Call if (!CreateProcess( NULL, // No module name (use command line) argv[1], // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ) // Ptr to PROCESS_INFORMATION structure ) [Windows]

Beginning a Process via CreateProcess Fork Kernel has to:

  • Create & ini5alize PCB in the kernel
  • Create and ini5alize a new address space
  • Load the program into the address space
  • Copy arguments into memory in address space
  • IniOalize the address space with a copy of the enOre contents of the address space of the parent
  • Ini5alize hw context to start execu5on at “start”
  • Inherit execuOon context of parent (e.g. open files)
  • Inform scheduler that new process is ready to run [UNIX]

Code example

* Corresponds to Figure 3.5 in the textbook

#include <stdio.h>

#include <unistd.h>

int main() {

int child_pid = fork();

if (child_pid == 0) { // child process

printf("I am process #%d\n", getpid());

return 0;

} else { // parent process.

printf("I am the parent of process #%d\n", child_pid);

return 0;

Possible outputs?

QuesOons

  • Can UNIX fork() return an error? Why?
  • (^) Can UNIX exec() return an error? Why?
  • Can UNIX wait() ever return immediately? Why?

What is a Shell? Job control system

  • runs programs on behalf of the user
  • allows programmer to create/manage set of programs
    • sh^ Original Unix shell (Stephen Bourne, AT&T Bell Labs, 1977)
    • csh BSD Unix C shell (tcsh: enhanced csh at CMU and elsewhere)
    • bash^ “Bourne-Again” Shell Runs at user-level. What system calls does it use?

Signals [UNIX] 16 ID Name^ Default Action^ Corresponding Event 2 SIGINT^ Terminate^ Interrupt (e.g., ctrl-c from keyboard) 9 SIGKILL^ Terminate^ Kill program (cannot override or ignore) 14 SIGALRM^ Terminate^ Timer signal 17 SIGCHLD^ Ignore^ Child stopped or terminated 20 SIGTSTP^ Stop until next SIGCONT Stop signal from terminal (e.g. ctrl-z from keyboard) A virtualized interrupt. Allow applica5ons to behave like opera5ng systems. youtube?

Sending a Signal Kernel delivers a signal to a des5na5on process For one of the following reasons:

  • Kernel detected a system event (e.g. div-by-zero (SIGFPE) or termina5on of a child (SIGCHLD))
  • A process invoked the kill system call reques5ng kernel to send signal to another process - debugging - suspension - resump5on - (^) 5mer expira5on

Signal Example 19 void int_handler(int sig) { printf("Process %d received signal %d\n", getpid(), sig); exit(0); } int main() { pid_t pid[N]; int i, child_status; signal(SIGINT, int_handler); for (i = 0; i < N; i++) // N forks if ((pid[i] = fork()) == 0) { while(1); //child infinite loop } for (i = 0; i < N; i++) { // parent continues executing printf("Killing proc. %d\n", pid[i]); kill(pid[i], SIGINT); } for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) // parent checks for each child’s exit printf("Child %d terminated w exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } exit(0); }

Blocked Signals A process can block the receipt of certain signals

  • Blocked signals can be delivered, but will not be received un5l the signal is unblocked Kernel maintains pending and blocked bit vectors in the context of each process
  • blocked : represents the set of blocked signals Can be set and cleared by using the sigprocmask func5on