System programming process management and IPC CS 2204

  • Slides: 13
Download presentation
System programming: process management and IPC CS 2204 Class meeting 11 Created by Dr.

System programming: process management and IPC CS 2204 Class meeting 11 Created by Dr. Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002 (C) Doug Bowman, Virginia Tech, 2001

UNIX system programming n n n Programming that uses special features of the UNIX

UNIX system programming n n n Programming that uses special features of the UNIX system Programs make system calls Types of system calls n n File I/O Process management Inter-process communication (IPC) Signal handling (C) Doug Bowman, Virginia Tech, 2001 2

Processes in UNIX n Process: basic unit of execution n n Executing instance of

Processes in UNIX n Process: basic unit of execution n n Executing instance of a program Has a process ID (PID) Is in a hierarchy of processes (parents, children) Has its own state/context/memory Shell commands dealing with processes: ps, top, kill, nice, … (C) Doug Bowman, Virginia Tech, 2001 3

Process management n System calls dealing with: n n n Process creation Setting the

Process management n System calls dealing with: n n n Process creation Setting the program a process executes Waiting for a process to terminate Process termination Sending signals to a process (C) Doug Bowman, Virginia Tech, 2001 4

Process creation n n pid = fork() Creates a new child process that is

Process creation n n pid = fork() Creates a new child process that is an exact copy of the current process n n n Same program running at same location Same variable values Same open files Only difference: child has new PID Returns 0 in the child process Returns child’s PID in the parent process (C) Doug Bowman, Virginia Tech, 2001 5

Setting the program a process executes n n n exec family of functions e.

Setting the program a process executes n n n exec family of functions e. g. execlp(executable_name, arg 0, arg 1, …); Replaces the current process with a new process image running the executable specified with the arguments listed New process retains old PID and any open files Other functions: execl, execle, execvp, execve (C) Doug Bowman, Virginia Tech, 2001 6

Waiting for a process to terminate n pid = wait(&status) n n Suspends execution

Waiting for a process to terminate n pid = wait(&status) n n Suspends execution of the calling process until any child process terminates Returns PID of terminating child Puts exit status of child in status pid = waitpid(pid, &status, options) n Suspends execution of the calling process until a specific child terminates (C) Doug Bowman, Virginia Tech, 2001 7

Using fork/exec/wait together pid = fork(); if(pid == 0) execl(“. /program”, “program”, arg 1,

Using fork/exec/wait together pid = fork(); if(pid == 0) execl(“. /program”, “program”, arg 1, NULL); else pid = wait(&status); continue execution (C) Doug Bowman, Virginia Tech, 2001 8

Process termination n n exit(status) Terminates the calling process Closes all open file descriptors

Process termination n n exit(status) Terminates the calling process Closes all open file descriptors Returns status to the parent process (C) Doug Bowman, Virginia Tech, 2001 9

Sending signals to a process n n retval = kill(pid, signal) Some common signals

Sending signals to a process n n retval = kill(pid, signal) Some common signals a user program might send: n n n SIGINT: interrupt (CTRL-c) SIGKILL: kill SIGUSR 1, SIGUSR 2: user-defined (C) Doug Bowman, Virginia Tech, 2001 10

Inter-process communication (IPC) n n Information passing between processes Two basic paradigms n n

Inter-process communication (IPC) n n Information passing between processes Two basic paradigms n n Message passing: processes send information back and forth in messages/packets Shared Memory: processes share a chunk of physical memory and read/write data there to share that information (C) Doug Bowman, Virginia Tech, 2001 11

IPC with pipes n n Example of message passing int fds[2]; retval = pipe(fds);

IPC with pipes n n Example of message passing int fds[2]; retval = pipe(fds); Creates two file descriptors (a pipe is a file), the first for reading, and the second for writing How does another process connect to this pipe? (C) Doug Bowman, Virginia Tech, 2001 12

Basic pipe example else int fds[2]; char s[100]; write(fds[1], “hello”, 6); retval = pipe(fds);

Basic pipe example else int fds[2]; char s[100]; write(fds[1], “hello”, 6); retval = pipe(fds); pid = fork(); NOTES: if(pid == 0){ n data is written/read in order (FIFO) read(fds[0], s, 100); n reads block until there’s something to read printf(“Read %sn”, n writes block if the pipe is full s); n closing the writing fd causes EOF to be read on the other end } (C) Doug Bowman, Virginia Tech, 2001 13