Vars namevalue strings eg pwdhomesiwspjh execve

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: -­‐level diagram of what happens when you run the command ”ls” in a Linux shell: Heap Data Code: /usr/bin/bash fork(): parent child Stack Stack child Stack exec(): Heap Heap Data Code: /usr/bin/bash Data Code: /usr/bin/bash Processes Data Code: /usr/bin/ls 31 University of Washington execve: Loading and Running Programs ¢  ¢  Null-­‐terminated env var strings int execve( char *filename, char *argv, char *envp ) Null-­‐terminated cmd line arg strings Loads and runs in current process: §  Executable filename §  With argument list argv §  And environment variable list envp §  Stack boKom Env. vars: “name=value” strings (e.g. “PWD=/homes/iws/pjh”) ¢  execve does not return (unless error) ¢  Overwrites code, data, and stack §  Keeps pid, open files, a few other items Processes unused envp[n] == NULL envp[n-­‐1] … envp[0] argv[argc] == NULL argv[argc-­‐1] … argv[0] Linker vars envp argv argc Stack frame for main Stack top 32 University of Washington exit: Ending a process ¢  void exit(int status) §  Exits a process Status code: 0 is used for a normal exit, nonzero for abnormal exit §  atexit() registers func:ons to be executed upon exit §  void cleanup(void) { printf("cleaning up\n"); } void fork6() { atexit(cleanup); fork(); exit(0); } Processes 33 University of Washington Zombies ¢  Idea §  When process terminates, it s:ll consumes system resources Various tables maintained by OS §  Called a “zombie” §  A living corpse, half alive and half dead §  ¢  Reaping §  Performed by parent on terminated child §  Parent is given exit status informa:on §  Kernel discards process ¢  What if parent doesn’t reap? §  If any parent terminates without reaping a child, then child will be reaped by init process (pid == 1) §  But in long-­‐running processes we need explicit reaping §  e.g., shells and servers Processes 34 University of Washington wait: Synchronizing with Children ¢  int wait(int *child_status) §  Suspends current process (i.e. the parent) un:l one of its children terminates §  Return value is the pid of the child process that terminated §  On successful return, the child process is reaped §  If child_status != NULL, then the int that it points to will be set to a status indica:ng why the child process terminated §  There are special macros for interpre:ng this status – see wait(2) ¢  If parent process has mul:ple children, wait() will return when any of the children terminates §  waitpid() can be used to wait on a specific child process Processes 35 University of Washington wait Example void fork_wait() { int child_status; pid_t child_pid; if (fork() == 0) { printf("HC: hello from child\n"); } else { child_pid = wait(&child_status); printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0); HC Bye CT Bye } Processes 36 University of Washington Process management summary ¢  ¢  fork gets us two copies of the same process (but fork() returns different values to the two processes) execve has a new process subsJtute itself for the one that called it §  Two-­‐process program: First fork() §  if (pid == 0) { //child code } else { //parent code } §  Two different programs: §  First fork() §  if (pid == 0) { execve() } else { //parent code } §  Now running two completely different programs §  ¢  wait / waitpi...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online