Topics Processes Process management Overview Processes User Space

  • Slides: 22
Download presentation
Topics • Processes • Process management

Topics • Processes • Process management

Overview Processes User Space System Call Interface OS CPU Memory File system Device scheduling

Overview Processes User Space System Call Interface OS CPU Memory File system Device scheduling Mgmt Network Stack

Process • Informal definition: A process is a program in execution. • Process is

Process • Informal definition: A process is a program in execution. • Process is not the same as a program. – Program is a passive entity stored in disk – Process is an actively executing entity – Program is just one part of the process.

So what else constitutes a process? • Process context: – Memory space (static, dynamic)

So what else constitutes a process? • Process context: – Memory space (static, dynamic) – Procedure call stack – Open files, connections – Registers and counters : • Program counter, Stack pointer, General purpose registers – ……

Examining Processes in Unix • ps command – Standard process attributes • /proc directory

Examining Processes in Unix • ps command – Standard process attributes • /proc directory – More interesting information if you are the root. • Top, vmstat command – Examining CPU and memory usage statistics.

Process Creation • Reasons: – User runs a program – OS creates a process

Process Creation • Reasons: – User runs a program – OS creates a process to provide a service – One process starts another process

Example of Server Operation Server 1 X Client 1

Example of Server Operation Server 1 X Client 1

Example of Server Operation Server 1 Client 1

Example of Server Operation Server 1 Client 1

Example of Server Operation Server 1 Client 2

Example of Server Operation Server 1 Client 2

Example of Server Operation Server 1 Client 1 Server 2 Client 2

Example of Server Operation Server 1 Client 1 Server 2 Client 2

Example of Server Operation Server 1 Client 1 Server 2 Client 2

Example of Server Operation Server 1 Client 1 Server 2 Client 2

Example of Server Operation Server 1 Server 2 X Client 1 Client 2

Example of Server Operation Server 1 Server 2 X Client 1 Client 2

Example of Server Operation Server 1 Client 1 Server 2 Client 2

Example of Server Operation Server 1 Client 1 Server 2 Client 2

Creating a New Process - fork() Example code fork_ex. c pid = fork(); if

Creating a New Process - fork() Example code fork_ex. c pid = fork(); if (pid == -1) { fprintf(stderr, "fork failedn"); exit(1); } if (pid == 0) { printf(“This is the childn"); exit(0); } if (pid > 0) { printf(“This is parent. The child is %dn", pid); exit(0); }

Points to Note • fork() is called once … • … but it returns

Points to Note • fork() is called once … • … but it returns twice!! – Once in the parent and – Once in the child – See example 1. c • How to distinguish parent and child? ? – Return value in child = 0 – Return value in parent = process id of child – See example 2. c

Running another program in child – exec()

Running another program in child – exec()

Running another program in child – exec() Parent

Running another program in child – exec() Parent

Running another program in child – exec() Parent Fork(…) Child

Running another program in child – exec() Parent Fork(…) Child

Running another program in child – exec() See example 3. c Parent Fork(…) Child

Running another program in child – exec() See example 3. c Parent Fork(…) Child Exec(…) New program image in execution

Properties of exec() • Replaces current process image with new program image. – E.

Properties of exec() • Replaces current process image with new program image. – E. g. parent image replaced by the new program image. – If successful, everything after the exec() call will NOT be executed.

Different Types of exec() • int execl(char * pathname, char * arg 0, …

Different Types of exec() • int execl(char * pathname, char * arg 0, … , (char *)0); • int execv(char * pathname, char * argv[]); • int execle(char * pathname, char * arg 0, … , (char *)0, char envp[]); • int execve(char * pathname, char * argv[], char envp[]); • int execlp(char * filename, char * arg 0, … , (char *)0); • int execvp(char * filename, char * argv[]);

– exit (int status) • Clean up the process (e. g close all files)

– exit (int status) • Clean up the process (e. g close all files) • Tell its parent processes that he is dying (SIGCHLD) • Tell child processes that he is dying (SIGHUP) • Status can be accessed by the parent process. – Wait for a child process to die: wait, waitpid • pid_t wait(int *stat_loc) • Suspend the calling process to wait for a child process to die, return the pid and status of the child process. – See example 4. c