CSCI 4061 Recitation 2 1.

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
1 Introduction to UNIX 2 Ke Liu
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
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.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
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.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Creating and Executing Processes
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
Operating Systems CMPSC 473 Processes August 31, Lecture 3 Instructor: Bhuvan Urgaonkar.
Operating Systems Processes 1.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
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,
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
CSCI 330 UNIX and Network Programming
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
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
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Using the GNU Debugger (GDB)‏ Techzemplary Pvt.Ltd February 24 th 2008 Pranav Peshwe.
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” –
DEBUG.
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Section 8: Processes What is a process Creating processes Fork-Exec
Using Processes.
Unix Process Management
CSC 382: Computer Security
Processes in Unix, Linux, and Windows
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
gdb gdb is the GNU debugger on our CS machines.
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
2.1 Processes process = abstraction of a running program
Process Creation Process Termination
Tutorial 3 Tutorial 3.
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
Process Programming Interface
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
EECE.4810/EECE.5730 Operating Systems
Debugging.
EECE.4810/EECE.5730 Operating Systems
Makefiles, GDB, Valgrind
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

CSCI 4061 Recitation 2 1

Today fork – exec – wait gdb Makefile 2

A Process What is a process in Unix? A process is a program in execution Difference between a process and program Different states READY RUNNING WAITING 3

Process pool How do you find out what processes your system is running currently? ps –a man ps 4

fork()‏ fork() – Creates a new process Parent process executes fork and creates an almost identical copy of itself Child process inherits parent’s state and context: Code, data, open files Program counter and stack #include <unistd.h> If fork() fails, it returns -1 and sets a errno to EAGAIN If fork() succeeds, it returns 0 to the child and the child’s pid to the parent. Potential pitfalls: duplicate memory can provide intermixed output (try stdout)‏ 5

fork() Parent process (pid = 1000) child_pid=fork() Error! Child Process (pid=2000) printf(“I am a parent”); printf(“I am a child”); End

fork_ex.c pid_t childpid; childpid = fork(); if (childpid == -1) { perror("fork() failed"); return 1; } if (childpid == 0) printf("I am a child with id %ld\n", (long)getpid()); else printf("I am a parent with id %ld\n", (long)getpid()); return 0; 7

wait()‏ When a process creates a child, both parent and child proceed execution from the point of fork()‏ The parent can execute wait() or waitpid() to block until the child executes wait() : waits for the termination of one of the children waitpid() : waits for the termination for specified child process 8

wait_ex.c pid_t childpid; pid_t waitreturn; int status; childpid = fork(); if(childpid==1) { perror("fork"); exit(0); } else if(childpid==0){ printf("I am a child\n"); exit(3); else { waitreturn = wait(&status); if(WIFEXITED(status)) { printf("child exited with status %d\n",WEXITSTATUS(status)); 9

exec()‏ exec – execute a shell command or program Int execv(const char *path, char *const argv[]); Six of them – execl, execlp and execle form one family while execv, execvp and execve form the other man them all – On your own time! 10

execl_ex.c childpid = fork(); if(childpid==-1){ pid_t childpid; childpid = fork(); if(childpid==-1){ perror("Failed to fork"); return 1; } //child code if(childpid == 0){ execl("/bin/ps", "ps", "-af",NULL); perror("child failed to exec all_ids"); if(childpid != wait(NULL)){ perror("parent failed to wait due to signal or error"); 11

Debugging using GDB GDB (GNU Debugger) is the standard command-line debugger for the GNU operating system Compile source files with –g option to enable debugging -g option tells compiler to put debug information in the object file cc –g –o HelloWord HelloWorld.c gdb ./HelloWorld How do I? GDB commands Compile with debugging symbols gcc –c –o helloWorld helloWorld.c Run programs in GDB gdb ./helloWorld Restart program in debugger kill run Exit debugger quit 12

Executing GDB GDB allows – To execute and stop your program at specified points Examine what has happened and inspect your program after stop-point Make changes to variables and run How do I? Gdb Commands Restart execution continue See where the program stopped list Step through code line-by-line Next step Examine variables print 13

GDB Stack-breakpoints-watchpoints How do I? Gdb Commands Get a backtrace backtrace Change stack frame frame Set breakpoint on line/function Break line_number/function_name Set watchpoint on variable Watch variable Get list of breakpoints/watchpoints Info breakpoints Disable breakpoints Disable/clear breakpoints To debug programs with multiple processes, use set follow-fork-mode mode where mode can be child or parent 14

Debugging fork_ex.c Compile fork_ex.c with –g option: gcc –g –o fork_ex fork_ex.c Start GDB with a.out: gdb a.out Set breakpoint in main function: break main Check all breakpoints: info breakpoints Run the program: run Do “next” and observe the prints To switch between parent and child processes: set follow-fork-mode mode where mode can be child or parent

Makefile What is the utility of makefiles You don’t have to compile each file in your project separately When you change only a subset of the files in your project, you don’t have to compile all the files. Makefiles take care of compiling only the changed files Makefile contains instructions and rules to compile the source-files in your project and build executables 16

Makefile ‘make’ is a utility which reads the makefiles and builds target/executables according to the rules specified in makefile Type – make –f <name of makefile> If you don’t use option -f, by default make reads the file with name ‘makefile’ 17

Makefile Rules A rule tells when and how to make a target e.g. to compile a source file or build an executable Rules syntax – target : prerequisites command helloworld : helloworld.o gcc –o helloworld helloworld.o helloworld.o : helloworld.c gcc –w –c helloworld.c Here helloworld.c will be compiled only when modification time of helloworld.c is more recent than helloworld.o 18 18

Makefile Example #the C compiler CC = gcc CFLAGS = -g #this rule is to link the object code into an executable helloworld: helloworld.o $(CC) -o helloWorld helloWorld.o #this rule is to compile the source code helloworld.o: helloworld.c $(CC) $(CFLAGS) -c helloWorld.c clean: rm -f *.o rm -f helloWorld 19

Questions? 20