Unformatted text preview:

111CMSC 212 – S07 (lect 17)AnnouncementsProgram #4 is on the web– Change is submit setup• 2 tokens/24 hours• Release tests will show outputMidterm #2– Tuesday, April 10 (6:00-7:30pm in ARM 0126)– No class on April 10 Reading– Bryant & O’Hallaron 8.5, 11.{6,7,8} (Today)– Bryant & O’Hallaron Chapter 12 (Thursday)2CMSC 212 – S07 (lect 17)File Descriptor Vs. FILE*File Descriptors– Permit unbuffered I/O– Are integers– Used or returned by - open, read, writeFile Pointers– FILE*– Are used for buffered I/O– Are pointers to a struct• Struct contains a file descriptor and buffers– Are used or returned by fopen, fwrite, fprintf, ….223CMSC 212 – S07 (lect 17)File DescriptorsAll UNIX I/O is done via file descriptors– They are integer numbers– Represent an abstract data type used by the operating system– open system call returns a new file descriptor– Read and write system call use file descriptors0123File posRef CntFile posRef CntDescriptor Table(per process)File SizeFile AccessV-node table(per file)File SizeFile Access4CMSC 212 – S07 (lect 17)File Table After Forking a Process0123File posRef CntFile posRef CntDescriptor Table(parent)File SizeFile AccessV-node table(per file)File SizeFile Access0123Descriptor Table(child)335CMSC 212 – S07 (lect 17)Pipe System Callint pipe(int fd[2]);– Creates a communication channel between two file descriptors– Data written to fd[1] can be read from fd[0]– Data written to fd[0] can be read from fd[1]0123FIFORef CntDescriptor Table(per process)46CMSC 212 – S07 (lect 17)Changing Standard Input/OutputUseful to combine pipe, dup2, and exec….pipe(pipefds);pid = fork();if (pid == 0) {dup2(0, pipefd[0]);close(pipefd[1]);ret = execvp("ls", args);….else if (pid > 0) {dup2(1, pipefd[1]);close(pipefd[0]);} else {….}447CMSC 212 – S07 (lect 17)A Simple Shell Programwhile (1) {ret = fgets(line, sizeof(line), stdin);…/* convert line into array of arguments */pid = fork();if (pid == 0) {ret = execvp(args[0], args);if (ret) {printf("Command not found\n");exit(-1);} } else if (pid > 0) {ret = waitpid(pid, &status, NULL);} else { … }}8CMSC 212 – S07 (lect 17)Adding a Pipe Command to the Shell/* skipped: split line into an array of commands to run */for (i=0; i < num; i++) {if (i < num-1) pipe(outfds);cmd[i].pid = fork();if (pid == 0) {if (i > 0) dup2(0, infds[0]);if (i < num-1) dup2(1, outfds[1]);close(infds[1]);close(outfds[0]);ret = execvp(cmd[i].args[0], cmd[i].args);if (ret) {printf("Command not found\n");exit(-1);}} else if (pid > 0) {memcpy(infds, outfds, sizeof(int)*2));}}/* wait for all forked commands to exit */559CMSC 212 – S07 (lect 17)SignalsA way for processes to communicate exceptional eventsCan orignate from:– Hardware events: • SIGFPE – floating point exception• SIGILL – illegal instruction• SIGSEGV – segmentation violation– User interaction: • SIGKILL – kill the program• SIGINT – suspend process (control-z)• SIGCHLD – child process stopped or terminated10CMSC 212 – S07 (lect 17)Process GroupsEvery process is the member of one group– A small integer of type pid_tA child process inherits its parent’s groupA process can change its group– Setpgid(pid_t pid, pid_t pgid)• setpgid(0,0) – create a new group for the current proccess6611CMSC 212 – S07 (lect 17)Sending SignalsKill command sends a signal– From C: int kill(pid_t pid, int sig);– From command line: kill –signal <pid>A negative pid– Sends a signal to all processes in group abs(pid)When a process receives a signal– Might terminate– Might ignore signal– Might run a function12CMSC 212 – S07 (lect 17)Action Taken on a signalHandler_t *signal(int signum, handler_t *handler)– Defines that on signal signum, handler function will be called– Returns • previous handler on success,• SIG_ERR on failure– Special handlers• SIG_IGN – ignore the signal• SIG_DFL – restore default action for this signal number7713CMSC 212 – S07 (lect 17)Timer Driven SignalsCan schedule a signal to be delivered– Unsigned int alarm(unsigned int secs);• In secs seconds a SIGALARM will be send to this process• Return value is the time remaining on a previous alarmExample:void alarmHandler(int sig) {static int nag = 0;Printf(“NAG!\n”);If (++nag < 5) {alarm(1);} else {exit(0);}}int main(int argc, char *argv[]) {signal(SIGALARM, alarmHandler);while (1) {…. Do some other


View Full Document

UMD CMSC 212 - Lecture 18

Download Lecture 18
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 18 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 18 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?