Docsity

Shell and Glue - Operating Systems - Lecture Notes, Study notes of Operating Systems

Partial preview of the text

Download Shell and Glue - Operating Systems - Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

The shell Thursday, September 27, 2012 4:28 PM Docsity.com

Same as "kill - INT pid" where pid is the process-id. Default action: process ends. Typing control-C sends SIGINT to the process. Same as "kill - TSTP pid" where pid is the process-id. Default action: process suspends. Continue with "fg". Typing control-Z sends SIGTSTP to the process. Same as "kill - QUIT pid" where pid is the process-id. Default action: process dumps core and ends! If you override control-C you can still stop the process this way. Typing control-\ sends SIGQUIT to the process. Source: man tcsh, bash(!) Demystifying shell control characters Demystifying control characters Wednesday, September 29, 2010 3:43 PM Docsity.com

preserve everything. fork(): make two identical copies of yourself. preserve environment, limits, open files. erase all of memory, including file buffers. execl(path,arg1,arg2,…, NULL): execute a new command instead of yourself. wait(&status): block and reap an exit code from a dead child. wait3(&status,WNOHANG,&usage): don't block, read exit code and resource usage for child if available. wait: retrieve an exit code from a child. setrlimit(what,&limits): set a limit on execution for self and children (forever). limit setting setenv("name", "value", 1): set an environment variable *const char c = getenv("name"): read an environment variable. environment Interesting system calls so far: So far, we have the following interesting system calls Tuesday, September 27, 2011 6:12 PM Docsity.com

Allows one to execute commands. Allows one to string commands into pipelines. Allows one to either wait for or fail to wait for a process to complete (&) The shell (tcsh or bash): The shell Wednesday, September 29, 2010 3:35 PM Docsity.com

foreground. ./a.out outfile get input and output from files instead of keyboard ./a.out & run a process in the background. control-C kill the running process control-Z suspend the running process; continue with 'fg' control-\ cause the current process to abort and core-dump! Docsity.com

how processes execute and return statuses how to wait for a child to end with wait how not to wait and reap using a signal handler for SIGCHLD, thus executing the process in the background (&). how parents execute children and reap child statuses. So far, we know: how processes communicate. But, we don't know yet: a.out < file > file what happens when I type to read input from one file and print output to another??? a.out | more what happens when I type to paginate an output??? a.out |& more what happens when I type to combine stdout and stderr??? Basic questions: So far, we know Wednesday, September 29, 2010 6:16 PM Docsity.com

When a process starts running, it assumes that standard input file descriptor 0 FILE *stdin; cin standard output file descriptor 1 FILE *stdout; cout standard error file descriptor 2 FILE *stderr; cerr The file descriptor is the input to system calls read and write: a positive integer between 0 and 31. A pointer to a file buffer. fileno(stdin)=0, fileno(stdout)=1, fileno(stderr)= The file pointer is the input to printf, fgets, scanf, fprintf. The default I/O environment Wednesday, September 29, 2010 6:20 PM Docsity.com

concept C++ C input to program cin stdin output from program cout stdout error output from program cerr stderr reading input cin>>j scanf("%d",&j) read a character cin>>c c=getchar() or c=getc(stdin) write output cout<<k printf("%d",k) or fprintf(stdout,%d,k) write a character cout <<c putchar(c) or putc(c,stdout) read a line cin.getline(buffer,size) fgets(buffer,size,stdin) or gets(buffer) Never use gets unless you want to be hacked! Recall: standard I/O Thursday, September 30, 2010 12:12 PM Docsity.com

fork() parent can read statistics of child. kill(some_pid, SOME_SIGNAL) one process can send a signal to another: signal(WHICH_SIGNAL, what_to_do) a process can respond to a signal via: one process can write to a file, then the other process can read it. But there is a much more efficient way…! Methods for process communication: How do processes communicate: Monday, September 27, 2010 12:40 PM Docsity.com

 methodology: pipeline programming.  typical implementation: shell programming UNIX philosophy: make big programs out of smaller ones  ps - ef  step 1: list all processes  awk '$1=="couch" { print $2 }'  step 2: select just the ones for me  ps - ef | awk '$1=="couch" { print $2 }'  step 3: glue these together  kill - 9 ps - ef | awk '$1=="couch" { print $2 }'  step 4: use as arguments to kill command  a | b: glue output of a to input of b. c: substitute the output of c on the command line itself!

 Result: complex program written in simple pieces Example: write a simple program that kills all of my own processes on a machine Glue Monday, September 18, 2006 5:13 PM Docsity.com

Simple answer: an operating system buffer to which one can write data and from which one can read it. A FIFO. What's a pipe? Simple example of one process writing to a pipe: http://www.cs.tufts.edu/comp/111/examples/The_Shell_and_Glue/pipe1.c Simple example of a forked pair of processes sharing information via a pipe. http://www.cs.tufts.edu/comp/111/examples/The_Shell_and_Glue/pipe2.c What's a pipe? Monday, September 18, 2006 5:13 PM Docsity.com

purpose of dup: move file descriptors around in number, to change standard input or output.

Second part of the puzzle: dup

  • open something you want to use as input or output.
  • close one of the main descriptors call dup to copy an existing other descriptor into one of the main slots.

standard usage:

  • take the file descriptor you give it.
  • start from descriptor 0.
  • look for a closed descriptor.
  • copy its argument into the first available closed slot. what dup does: dup Monday, September 18, 2006 5:13 PM Docsity.com

http://www.cs.tufts.edu/comp/111/examples/The_Shell_and_Glue/dup1.c implements parent | child Example: fooling cat, by typing input to it from a program: http://www.cs.tufts.edu/comp/111/examples/The_Shell_and_Glue/dup2.c implements child | parent Example: capturing the output of ps - ef: Advanced dup examples Tuesday, September 27, 2011 6:51 PM Docsity.com

fd[0] is read side. fd[1] is write side. Direction of pipe pairs is important: Reading and writing to the same process is more difficult: cannot just dup both descriptors because of synchronization problems; more about this later. Pipe and dup notes: Pipe and dup caveats Tuesday, September 27, 2011 6:53 PM Docsity.com