Top Banner
Process Control in Unix Operating Systems Hebrew University Spring 2004
25

Process Control in Unix

Jan 19, 2016

Download

Documents

TAB

Process Control in Unix. Operating Systems Hebrew University Spring 2004. Unix Process Model. What is a processes? Properties of a process Processes organization Interacting with a process. Resources. Advanced Programming in the Unix Environment, Stevens [243.51 St 48] POSIX.1 Spec. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Process Control in Unix

Process Control in Unix

Operating Systems

Hebrew University

Spring 2004

Page 2: Process Control in Unix

Unix Process Model

• What is a processes?

• Properties of a process

• Processes organization

• Interacting with a process

Page 3: Process Control in Unix

Resources

• Advanced Programming in the Unix Environment, Stevens [243.51 St 48]

• POSIX.1 Spec

Page 4: Process Control in Unix

What is a process

• An entry in the kernel’s process table

• Most common unit of execution

• Execution state

• Machine instructions, data and environment

Page 5: Process Control in Unix

Properties of a process

• Process ID• Parent Process ID• Process group ID• Session ID• User ID of the process• Group ID of the process• Effective user ID• Effective group ID

Page 6: Process Control in Unix

Properties of a Process - cont

• Controlling terminal

• Current working directory

• Root directory

• Open files descriptors

• File mode creation mask

• Resource limits

• Process times

Page 7: Process Control in Unix

Process Trees

• Only an existing process can create a new process

• Parent-Child relations

1

0

init

Page 8: Process Control in Unix

Who am I?

• getpid

• Returns the PID of the current process

#include <sys/types.h>

#include <unistd.h>

pid_t getpid(void);

Page 9: Process Control in Unix

Who is my parent?

• getppid

• Returns the PID of the parent of the current process

#include <sys/types.h>

#include <unistd.h>

pid_t getppid(void);

Page 10: Process Control in Unix

Talking directly to a process

• Send a signal• The ONLY way to

talk to a process

• Lots of signals• More next week

#include <sys/types.h>

#include <signal.h>

int kill(pid_t pid, int sig)

Page 11: Process Control in Unix

Creating a process

• Only an existing process can create a new process

• Step 1: Make a clone of yourself

• Step 2: Have the clone change itself

Page 12: Process Control in Unix

Fork

• Make a clone of myself

• ONLY difference is PID and PPID!

• Very cheap, Copy on Write

• -1 = failure– Reason via ERRNO

#include <sys/types.h>#include <unistd.h>

pid_t fork(void);

Page 13: Process Control in Unix

Fork Example

if ( (pid = fork()) == 0 )

{ code for child }

else

{ code for parent }

Page 14: Process Control in Unix

Changing a process

• Execute a new program in this space• argv and envp terminated by null pointer

• NEVER returns on success, -1 and errno on failure

#include <unistd.h>

int execve(const char *filename,

char *const argv[],

char *const envp[]);

Page 15: Process Control in Unix

Exec example

if ((pid = fork()) == 0 ){

exec( arguments );

exit(-1);

}

// parent continues here

Page 16: Process Control in Unix

Better ways

#include <unistd.h>

extern char **environ;

int execl(const char *path, const char *arg, …);

int execlp(const char *file, const char *arg, …);

int execle(const char *path, const char *arg, …, char *const envp[]);

int execv(const char *path, char *const argv[]);

int execvp(const char *file, char *const argv[]);

Page 17: Process Control in Unix

Rendezvous

• Meeting up with your child processes

• Resources are freed only when the parent acknowledges the death of the child

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int *status);pid_t waitpid(pid_t pid, int *status, int options);

Page 18: Process Control in Unix

wait status returns

• WIFEXITED(status)

• WEXITSTATUS(status)

• WIFSIGNALED(status)– WIFTERMSIG(status)

• WIFSTOPPED(status)– WSTOPSIG(status)

Page 19: Process Control in Unix

Leaving a process

• Leave and tell my parent why I left

• NEVER returns• Flush output buffers• Close all open streams• Call exit handlers

#include <stdlib.h>

void exit(int status);

Page 20: Process Control in Unix

Leaving a process (quickly)

• Just like exit, but….

• Don’t call the exit handlers

• Don’t flush output buffers

• Close file descriptors

#include <stdlib.h>

void _exit(int status);

Page 21: Process Control in Unix

When to use _exit

• In the fork branch of a C++ child

• Fail Fast!

Page 22: Process Control in Unix

Orphans

• A process whose parent has exited.

• Orphaned processes are inherited by init

• Its slot in the process table is immediately released when an orphan terminates.

Page 23: Process Control in Unix

Zombies

• A process that no longer exists, but still ties up a slot in the system process table

• Equivalently:– A process that has terminated, but whose

parent exists and has not waited/acknowledged the child's termination

Page 24: Process Control in Unix

Daemons

• Leaving Home: Disconnecting from my parent so that I can live my own life

• Somewhat tricky to do correctly• Examples:

– inetd– atd– nfsd

Page 25: Process Control in Unix

How to make a daemon

• Fork -- Create pid-1

• Fork again – Create pid-2

• pid-1 exits, pid-2 is now an orphan

• Chdir(“/”) – free current directory

• Close all file descriptors (0…MAXINT)