Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CS162B: Forking Jacob T. Chan

2 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)

3 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; }

4 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)

5 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

6 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; }

7 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

8 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

9 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

10 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

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

12 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

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

14 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?

15 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"); }

16 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

17 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

18 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

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


Download ppt "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."

Similar presentations


Ads by Google