Inter-Process Communication fork-exec[-wait] Signal Pipe

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213, Fall 06 Outline Shell Lab Processes Signals.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Understand Process concept Process scheduling Creating.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Introduction to Processes CS Intoduction to Operating Systems.
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
Operating Systems Chapter 2
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Computer Studies (AL) Operating System Process Management - Process.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
Operating Systems Processes 1.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Project1: Unix Shell with History Feature Goals Descriptions Methodology Submission.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Operating Systems Process Creation
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
Operating Systems Recitation 4, April th, 2002 Signals.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Shell Execution Basic: fork, child execs, parent waits code of program in box –RC == return value from fork() Call fork RC=0 Call exec Subsequent instructions.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
G.Jyostna.
Topic 3 (Textbook - Chapter 3) Processes
Using Processes.
Chapter 3: Process Concept
Process Description and Control
Chapter 3: Processes.
Processes A process is a running program.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
CGS 3763 Operating Systems Concepts Spring 2013
System Structure B. Ramamurthy.
EECE.4810/EECE.5730 Operating Systems
Chapter 3: Processes.
Lab 5 Process Control Operating System Lab.
Recitation 9: Processes, Signals, TSHLab
Process Creation Process Termination
Unix System Calls and Posix Threads
Programming Assignment # 2 – Supplementary Discussion
fork() and exec() David Ferry CSCI 3500 – Operating Systems
Processes in Unix, Linux, and Windows
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
EECE.4810/EECE.5730 Operating Systems
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 3: Process Concept
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Recitation 9: Processes, Signals, TSHLab
System Programming: Process Management
Presentation transcript:

Inter-Process Communication fork-exec[-wait] Signal Pipe Lab 02 Inter-Process Communication fork-exec[-wait] Signal Pipe

fork-exec[-wait] 1 int main(void) { 2 pid_t return_pid = fork(); 3 4   /* Error check for fork(). */ 5 6   if (return_pid == 0) { 7     int exec_return_value = execlp("/bin/ls", 8                                   "ls", "-l", (char *) NULL); 9     /* Error check execlp(). */ 10   } 11   else { 12     int wait_status; 13     pid_t terminated_child_pid = wait(&wait_status); 14 15     /* Error check wait(). */ 16   } 17 18   exit(EXIT_SUCCESS); 19 }

Parent-Block and Child-Block 1 int main(void) { 2   pid_t return_pid = fork(); 3 4   /* Error check for fork(). */ 5 6   if (return_pid == 0) { 7     int exec_return_value = execlp("/bin/ls", 8                                   "ls", "-l", (char *) NULL); 9     /* Error check execlp(). */ 10   } 11   else { 12     int wait_status; 13     pid_t terminated_child_pid = wait(&wait_status); 14 15     /* Error check wait(). */ 16   } 17 18   exit(EXIT_SUCCESS); 19 } When the parent process issues the wait system call, it enters the blocked state and does not try to burn unnecessary CPU cycles. The child process continues executes its instructions independently from the parent process. Once the child finishes its task and terminates normally, the OS will notify the parent process by signaling it with SIGCHLD. Then, the parent process wakes up and continues to the next instruction.

Signal Signal is a notification to a process that an event has occurred. Different types of event determine different types of signal. The process may choose to handle the signal in a different way than the default behavior. List all the signals: kill -l

C Function Prototype for the Installer of Signal Handler void (*installer(int sig, void (*handler)(int)))(int); Before tackling this complex beast, let’s look at some simpler cases. int x; int *p; int a[5]; int f(); int ***ppp; int (**ppa)[]; int *fp(); int (*pf)(); int *(*func())(); To handle the event, we need to associate our own signal handler with the intended signal. Thanks to Rick Ord. http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html First, symbols. Read * as "pointer to" - always on the left side [] as "array of" - always on the right side () as "function returning" - always on the right side as you encounter them in the declaration. STEP 1 ------ Find the identifier. This is your starting point. Then say to yourself, "identifier is." You've started your declaration. STEP 2 ------ Look at the symbols on the right of the identifier. If, say, you find "()" there, then you know that this is the declaration for a function. So you would then have "identifier is function returning". Or if you found a "[]" there, you would say "identifier is array of". Continue right until you run out of symbols *OR* hit a *right* parenthesis ")". (If you hit a left parenthesis, that's the beginning of a () symbol, even if there is stuff in between the parentheses. More on that below.) STEP 3 ------ Look at the symbols to the left of the identifier. If it is not one of our symbols above (say, something like "int"), just say it. Otherwise, translate it into English using that table above. Keep going left until you run out of symbols *OR* hit a *left* parenthesis "(".

void (*installer(int sig, void (*handler)(int)))(int); void (*func)(int); installer(int sig, void (*handler)(int)) void (*handler)(int) Ready to translate the installer prototype?

Define and Change Our Own Signal Handler void (*handler)(int signal); Change the default behavior of handing the signal. man sigaction

More Readable Installer Prototype typedef void sigfunc_t(int); sigfunc_t *install_signal_handler( int signal_number, sigfunc_t *signal_handler);

How to Send a Signal? man 2 kill https://oscourse.github.io/examples/signals_ex.c

Pipe Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin, "Operating System Concepts, Ninth Edition ", Chapter 3 fd[0]: read end. fd[1]: write end.

Pipe Example Code https://oscourse.github.io/examples/pipe1.c

What’s the game? A die has 6 faces. Each face has a different number from 1 to 6. After the parent rolls the die, its child will guess the number facing up. The parent will pipe a hint to its child. 1: child’s number is larger than the target/right number. 0: child guesses it right. -1: child’s number is smaller than the target/right number. The child can guess 3 times in total. A student can define any other details of this game. Do not turn in your game. This may be a student’s first AI. How to make the child process guess smarter? or to have an educated guess 

Function Pointer Example https://oscourse.github.io/examples/function_poin ter_example.c