CS162B: Forking Jacob T. Chan. Fork  Forks are:  Implement with two or more prongs that is used for taking up or digging  Division into branches or.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

CS162B: POSIX Threads Jacob Chan. Objectives ▪ Review on fork() and exec() – Some issues on forking and exec-ing ▪ POSIX Threads ▪ Lab 8.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Introduction to Linux and Shell Scripting Jacob Chan.
Lecture Topics: 11/3 Address spaces Memory protection Creating a process –NT style –Unix style.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
CS162B: Assembly and C Jacob T. Chan. Objectives ▪ System calls ▪ Relation of System calls to Assembly and C ▪ Special System Calls (exit, write, print,
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
1 Homework / Exam HW7 is due next class Starting Glass chapter 4 and parts of 7 Exam 3 – Class 26 –Open Book / Open Notes –Up through End of K&R Chapter.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Creating and Executing Processes
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
System calls for Process management
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.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
CSCI 330 UNIX and Network Programming
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)
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
2.1 Processes  process = abstraction of a running program  multiprogramming = CPU switches from running program to running program  pseudoparallelism.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Section 8: Processes What is a process Creating processes Fork-Exec
Unix Process Management
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Lab 5 Process Control Operating System Lab.
Recitation 9: Processes, Signals, TSHLab
Tutorial 3 Tutorial 3.
Processes Prof. Ikjun Yeom TA – Mugyo
Processes in Unix, Linux, and Windows
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Recitation 9: Processes, Signals, TSHLab
System Programming: Process Management
Presentation transcript:

CS162B: Forking Jacob T. Chan

Fork  Forks are:  Implement with two or more prongs that is used for taking up or digging  Division into branches or the place where something branches  Life lesson: a person can’t be in different forks at the same time  Application in programming: processes can’t have the same process ID’s (especially forked processes)

Determining the Process ID (in C) #include #include //needed to run getpid() int main(void) { int id = getpid(); // or pid_t id =... printf("This process' ID is: %d\n", id); return 0; }

Process Identification  Operating systems keep track of the processes running and store the information about them (process ID, name, owner, etc.)  Also known as a PCB (Process Control Block)  To view a process in Unix, type this: ps –ax (all process, without controlling terminal) ps –ef //alternate (every process, full output format)

fork() int fork(void);  Copies the current process and concurrently executes it  It’s like a cell reproducing itself during mitosis (in biology)  It’s like your typical jelly blob monster that you slice but multiplies instead (in games)  Return values  -1 if error  0 if child process  Child’s process ID if parent

Sample program using fork() #include int main(void){ int forkRet = fork(); // beware: using fork() recklessly is dangerous! if(forkRet < 0){ // error exit(1); } else if(forkRet == 0){ // child process printf("I'm a kid process!\nMy ID is %d.\n", getpid()); } else{ // parent process printf("Parent ID: %d.\nfork() returned %d.\n", getpid(), forkRet); } return 0; }

fork()  When fork() succeeds, it copies the current process and begins executing where fork() was called  Child copies (almost) everything from the parent  Note that concurrency occurs when forking  No point in creating a duplicate process that does the exact same thing (which is why there are return values)  Which was shown in the previous slide

fork() with no return value printf(“hihi\n"); i = i + 2; fork(); //child begins here i = i + 3; printf("%d\n", i); //implication: children are a copy of their parents

Fork Bombs  A very good way on how to crash a Unix system (joke!)  Don’t fork too many times!  Operating systems have a limit as to how many processes can run  UNLESS specifications say otherwise  Cases if(fork() == 0)... else if(fork() == 0)  Create a two-child process for a fork() if(fork() == 0) { if(fork() == 0)...  Child can have another child

Additional note on fork  There is no guarantee which processes (either child or parent) will execute first  You can sleep() one of the process  Forces the non-active process to sleep first to let the other process run everything  There’s still a probability that the sleep time will be ended before the first process finishes

Running Programs in C  Just use the system() function system (“cat /etc/passwd”);

But…  Using system() might also include set of user or group IDs  In such cases, use fork() and exec() instead  Why?  Prevent unforeseen manipulation the environment variables by whoever is running the program  Because editing these environment variables can cause confusion  It’s easier to pass arguments to exec

exec() Family function: execv() int execv (char *name, char **argv);  name = String that notes the filename to be executed (usually the absolute filename)  What is absolute filename again?  argv = works the same as argv function for your main() method  Example: running another C program using a C program (using this argv passes through the main method’s argv  argv[0] should match the name!  Suggestion: try setting extra null for the last argument (for safety purposes, but usually trivial)  You might need malloc(). MIGHT.

exec()  If exec() is successful, the current process is overlaid by what exec() executed  Returns -1 if error  No return value if there is no error  What is overlaying?

Example of exec() in Action int main(int argc, char* argv[]) { if(execv("/usr/bin/gedit", argv) == -1) { printf("Error.\n"); } printf("Will this line still be printed?\n"); }

Try it!  Will the last line be printed?  Try using execl() instead execl(“/usr/bin/gedit”, “NOT GEDIT!”, NULL)  Then check out the process list with ps –ax or ps –ef  execl() works like execv() except that argv is not an array and is set individually (variable arguments, unlike execv() that has fixed arguments)  Study more on the other exec() functions (if you have time, since there are a whole lot more!  Using either man or emacs on your Terminal

Lab 7: forkexec.c  Create a C program that:  Uses fork() to create a child process  Parent process loops infinitely (but sleeps first so that child will run first) sleep(10); someVariable = gettimeofday();  The function gettimeofday() should display the CURRENT time in this form: [yyyy- mm-dd] hh:mm:ss  Note: you need time.h in order to run time functions in C  For every third print out of date and time, display this text: “This program has gone far too long. Cancel using Ctrl + C” (INCLUDE THE QUOTATION MARKS)  In any case, terminate the program with Ctrl + C

Lab 7: forkexec.c  Child process:  Use the xclock application (find it in the Terminal using whereis xclock command (to get its absolute path)  Then run xclock using exec() function  Set the process name to myXClock  When I type ps –ax, I should be able to find the process named myXClock  This should run until the xclock application is closed  Hint: specify myXClock somewhere in the correct exec() call

Lab 7: forkexec.c  Submit a Certificate of Authorship along with the program with the filename: CS162B_Lab7_ _ _.tar  Deadline: Next Week