1 / 25

Process Control in Unix

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.

tab
Download Presentation

Process Control in Unix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Process Control in Unix Operating Systems Hebrew University Spring 2004

  2. Unix Process Model • What is a processes? • Properties of a process • Processes organization • Interacting with a process

  3. Resources • Advanced Programming in the Unix Environment, Stevens [243.51 St 48] • POSIX.1 Spec

  4. What is a process • An entry in the kernel’s process table • Most common unit of execution • Execution state • Machine instructions, data and environment

  5. 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

  6. Properties of a Process - cont • Controlling terminal • Current working directory • Root directory • Open files descriptors • File mode creation mask • Resource limits • Process times

  7. Only an existing process can create a new process Parent-Child relations Process Trees 0 1 init

  8. getpid Returns the PID of the current process #include <sys/types.h> #include <unistd.h> pid_t getpid(void); Who am I?

  9. getppid Returns the PID of the parent of the current process #include <sys/types.h> #include <unistd.h> pid_t getppid(void); Who is my parent?

  10. 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) Talking directly to a process

  11. 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

  12. 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); Fork

  13. Fork Example if ( (pid = fork()) == 0 ) { code for child } else { code for parent }

  14. 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[]);

  15. Exec example if ((pid = fork()) == 0 ){ exec( arguments ); exit(-1); } // parent continues here

  16. 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[]);

  17. 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);

  18. wait status returns • WIFEXITED(status) • WEXITSTATUS(status) • WIFSIGNALED(status) • WIFTERMSIG(status) • WIFSTOPPED(status) • WSTOPSIG(status)

  19. 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); Leaving a process

  20. 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); Leaving a process (quickly)

  21. When to use _exit • In the fork branch of a C++ child • Fail Fast!

  22. 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.

  23. 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

  24. Daemons • Leaving Home: Disconnecting from my parent so that I can live my own life • Somewhat tricky to do correctly • Examples: • inetd • atd • nfsd

  25. 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)

More Related