240-322 Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes 240-322, Semester 1, 2005-2006 5. Processes.

Slides:



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

Processes & Threads Today Next Time Process concept Process model
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CSSE Operating Systems
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
1 UNIX System Programming v Objectives –look at how to program UNIX processes –fork( ), exec( ), wait( ) Processes.
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.
LINUX System Programming with Processes. Overview 1. What is a Process? 2. fork() 3. exec() 4. wait() 5. Process Data 6. File Descriptors across Processes.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions.
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
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CE Operating Systems Lecture 10 Processes and process management in Linux.
System calls for Process management
Operating Systems Processes 1.
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.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
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.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
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)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
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” –
Unix Process Management
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Section 8: Processes What is a process Creating processes Fork-Exec
LINUX System : Lecture 8 Programming with Processes
Processes A process is a running program.
UNIX System Programming
System Structure and Process Model
System Structure and Process Model
Lecture 5: Process Creation
Fork and Exec Unix Model
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
System Structure and Process Model
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Tutorial 3 Tutorial 3.
Processes Prof. Ikjun Yeom TA – Mugyo
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Process Description and Control in Unix
Process Description and Control in Unix
System Programming: Process Management
Presentation transcript:

Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes

Cli/Serv.: procs/52 Overview 1. What is a Process? 2. fork() 3. Example: talkto.c 4. exec() 5. wait() 6. Process Data 7. Special Exit Cases 8. Process IDs

Cli/Serv.: procs/53 1. What is a Process? v A process is an executing program. v A process: $ cat file1 file2 & v Two processes: $ ls | wc - l  Each user can run many processes at once (e.g. using & )

Cli/Serv.: procs/54 A More Precise Definition v A process is the context (the information/data) maintained for an executing program. v Many more details in section 18, “Advanced Programming in the UNIX Environment”, W.R. Stevens, Addison- Wesley, 1992.

Cli/Serv.: procs/55 Some of the Context Information –Process ID ( pid )unique integer –Parent process ID ( ppid ) –Real User IDID of user/process which started this process –Effective User IDID of user who wrote the process’ program –Current directory –File descriptor table –Environment VAR=VALUE pairs continued

Cli/Serv.: procs/56 –Pointer to program code –Pointer to dataMemory for global vars –Pointer to stackMemory for local vars –Pointer to heapMalloc’d memory –Execution priority –Signal information

Cli/Serv.: procs/57 2. fork()  #include #include pid_t fork(void); v Creates a child process by making a copy of the parent process. v Both the child and the parent continue running.

Cli/Serv.: procs/58 fork() as a diagram Parent pid = fork() pid == 5 (say) Data Shared Program Data Copied Child pid == 0

Cli/Serv.: procs/59 Process IDs (pids)  pid = fork();  In the child: pid == 0 ; In the parent: pid == the process ID of the child.  A program can use this pid difference to do different things in the parent and child.

Cli/Serv.: procs/510 fork() Example(parchld.c) #include #include #include int main() { pid_t pid; /* could be int */ int i; pid = fork(); if (pid > 0) { /* parent */ for (i=0; i < 1000; i++) printf(“\t\t\tPARENT %d\n”, i); } else { /* child */ for (i=0; i<1000; i++) printf(“CHILD %d\n”, i); } return 0; }

Cli/Serv.: procs/511 Possible Output CHILD 0 CHILD 1 CHILD 2 PARENT 0 PARENT 1 PARENT 2 PARENT 3 CHILD 3 CHILD 4 PARENT 4 :

Cli/Serv.: procs/512 Things to Note  i is copied between parent and child. v The switching between the parent and child depends on many factors: –machine load, system process scheduling v I/O buffering affects amount of output shown. v Output interleaving is nondeterministic –cannot determine output order by looking at code

Cli/Serv.: procs/ Example: talkto.c  A simple communications program that copies chars from stdin to a specified port, and from that port to stdout.  Use port at /dev/ttya talkto parentchild / dev / ttya stdout stdin

Cli/Serv.: procs/514 Code for talkto.c #include #include #include #include #include int main() { int fd, count; char buffer[BUFSIZ]; if (fd = open(“/dev/ttya”,O_RDWR) < 0){ fprintf(stderr,“Cannot open port\n”); exit(1); } : continued

Cli/Serv.: procs/515 if (fork() > 0) { /* parent */ /* copy port input to stdout */ while(1) { count = read(fd, buffer, BUFSIZ); write(1, buffer, count); } } else { /* child: copy stdin to port */ while(1) { count = read(0, buffer, BUFSIZ); write(fd, buffer, count); } } return 0; }

Cli/Serv.: procs/516 ps Output $ ps -l UIDPIDPPID...COMMAND ad580579csh ad talkto ad talkto csh talkto (parent) talkto (child) parent hierarchy forks

Cli/Serv.: procs/ exec()  Family of functions for replacing process’s program with the one inside the exec() call. e.g. #include int execlp(char *file, char *arg0, char *arg1,..., (char *)0); #include int execlp(char *file, char *arg0, char *arg1,..., (char *)0); execlp(“sort”, “sort”, “-n”, “foobar”, (char *)0); execlp(“sort”, “sort”, “-n”, “foobar”, (char *)0); Same as "sort -n foobar"

Cli/Serv.: procs/518 tinymenu.c #include #include void main() { char *cmd[] = {“who”, “ls”, “date”}; int i; printf(“0=who 1=ls 2=date : “); scanf(“%d”, &i); execlp(cmd[i], cmd[i], (char *)0); printf(“execlp failed\n”); }

Cli/Serv.: procs/519 Execution tinymenu execlp() cmd[i] printf() not executed unless there is a problem with execlp()

Cli/Serv.: procs/ wait()  #include #include pid_t wait(int *statloc); v Suspends calling process until child has finished. Returns the process ID of the terminated child if ok, -1 on error.  statloc can be (int *)0 or a variable which will be bound to status info. about the child.

Cli/Serv.: procs/521 wait() Actions  A process that calls wait() can: –suspend (block) if all of its children are still running, or –return immediately with the termination status of a child, or –return immediately with an error if there are no child processes.

Cli/Serv.: procs/522 menushell.c #include #include #include #include void main() { char *cmd[] = {“who”, “ls”, “date”}; int i; while (1) { printf(“0=who 1=ls 2=date : “); scanf(“%d”, &i); : continued

Cli/Serv.: procs/523 if (fork() == 0) { /* child */ execlp(cmd[i], cmd[i], (char *)0); printf(“execlp failed\n”); exit(1); } else { /* parent */ wait((int *)0); printf(“child finished\n”); } } } if (fork() == 0) { /* child */ execlp(cmd[i], cmd[i], (char *)0); printf(“execlp failed\n”); exit(1); } else { /* parent */ wait((int *)0); printf(“child finished\n”); } } }

Cli/Serv.: procs/524 Execution menushell execlp() cmd[i] child wait() fork()

Cli/Serv.: procs/ Process Data v Since a child process is a copy of the parent, it has copies of the parent’s data. v A change to a variable in the child will not change that variable in the parent.

Cli/Serv.: procs/526 Example(globex.c) #include #include #include int globvar = 6; char buf[] = “stdout write\n”; int main(void) { int w = 88; pid_t pid; : continued

Cli/Serv.: procs/527 write(1, buf, sizeof(buf)-1); printf(“Before fork()\n”); if ((pid = fork()) == 0) { /* child */ globvar++; w++; } else if (pid > 0) /* parent */ sleep(2); else perror(“fork error”); printf(“pid = %d, globvar = %d, w = %d\n”, getpid(), globvar, w); return 0; }

Cli/Serv.: procs/528 v $ globex stdout write Before fork() pid = 430, globvar = 7, w = 89 /*child chg*/ pid = 429, globvar = 6, w = 88 /* parent no chg */ v $ globex > temp.out $ cat temp.out stdout write Before fork() pid = 430, globvar = 7, w = 89 Before fork() pid = 429, globvar = 6, w = 88 Output

Cli/Serv.: procs/529 Process File Descriptors v A child and parent have copies of the file descriptors, but the R-W pointer is maintained by the system: –the R-W pointer is shared  This means that a read() or write() in one process will affect the other process since the R-W pointer is changed.

Cli/Serv.: procs/530 Example: File used across processes #include #include #include #include #include void printpos(char *msg, int fd); void fatal(char *msg); void main(void) { int fd;/* file descriptor */ pid_t pid; char buf[10];/* for file data */ : (shfile.c) continued

Cli/Serv.: procs/531 if ((fd=open(“data-file”, O_RDONLY)) < 0) perror(“open”); read(fd, buf, 10); /* move R-W ptr */ printpos(“Before fork”, fd); if ((pid = fork()) == 0) { /* child */ printpos(“Child before read”, fd); read(fd, buf, 10); printpos(“Child after read”, fd); } : if ((fd=open(“data-file”, O_RDONLY)) < 0) perror(“open”); read(fd, buf, 10); /* move R-W ptr */ printpos(“Before fork”, fd); if ((pid = fork()) == 0) { /* child */ printpos(“Child before read”, fd); read(fd, buf, 10); printpos(“Child after read”, fd); } : continued

Cli/Serv.: procs/532 else if (pid > 0) { /* parent */ wait((int *)0); printpos(“Parent after wait”, fd); } else perror(“fork”); } else if (pid > 0) { /* parent */ wait((int *)0); printpos(“Parent after wait”, fd); } else perror(“fork”); } continued

Cli/Serv.: procs/533 void printpos(char *msg, int fd) /* Print position in file */ { long int pos; if ((pos = lseek(fd, 0L, SEEK_CUR)) < 0L) perror(“lseek”); printf(“%s: %ld\n”, msg, pos); }

Cli/Serv.: procs/534 Output $ shfile Before fork: 10 Child before read: 10 Child after read: 20 Parent after wait: 20 what's happened?

Cli/Serv.: procs/ Special Exit Cases Two special cases:  1) A child exits when its parent is not currently executing wait() –the child becomes a zombie –status data about the child is stored until the parent does a wait() continued

Cli/Serv.: procs/536 v 2) A parent exits when 1 or more children are still running –children are adopted by the system’s initialization process ( /etc/init ) u it can then monitor/kill them

Cli/Serv.: procs/ Process IDs v Special processes: 0process scheduler 1system initialisation process ( /etc/init ); the ancestor of every process v Process IDs tend to be assigned in increasingly numerical order –normally, child pid is parent pid+1

Cli/Serv.: procs/538 Process ID Functions v #include #include pid_t getpid(void); pid_t getppid(void); Returns process ID of calling process. Return parent process ID of calling process.