28-Jun-15Advanced Programming Spring 2002 Unix processes and threads Henning Schulzrinne Dept. of Computer Science Columbia University.

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.
Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money.
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
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 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Process Management: Processes and Threads CS 342 – Operating Systems Ibrahim.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Operating Systems Processes (Ch 4.1). Processes “A program in execution” Modern computers allow several at once –“pseudoparallelism” A B C Program Counter.
CSSE Operating Systems
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Phones OFF Please Processes Parminder Singh Kang Home:
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Lecture 5 Process, Thread and Task September 22, 2015 Kyu Ho Park.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Processes: program + execution state
Creating and Executing Processes
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
Processes Dr. Yingwu Zhu. Process Concept Process – a program in execution – What is not a process? -- program on a disk - a process is an active object,
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
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,
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
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.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
Chapter 3: Process Concept
Unix Process Management
Processes A process is a running program.
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Operating Systems Lecture 6.
Process & its States Lecture 5.
Operating Systems Processes (Ch 4.1).
Jonathan Walpole Computer Science Portland State University
Chapter 3: Processes.
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Processes – Part I.
CS510 Operating System Foundations
Operating Systems Processes (Ch 2.1).
Presentation transcript:

28-Jun-15Advanced Programming Spring 2002 Unix processes and threads Henning Schulzrinne Dept. of Computer Science Columbia University

28-Jun-15Advanced Programming Spring 2002 Unix processes and threads  Process model  creation  properties  owners and groups  Threads  threads vs. processes  synchronization

28-Jun-15Advanced Programming Spring 2002 What's a process?  Fundamental to almost all operating systems  = program in execution  address space, usually separate  program counter, stack pointer, hardware registers  simple computer: one program, never stops

28-Jun-15Advanced Programming Spring 2002 What's a process?  timesharing system: alternate between processes, interrupted by OS:  run on CPU  clock interrupt happens  save process state  registers (PC, SP, numeric)  memory map  memory (core image)  possibly swapped to disk  process table  continue some other process

28-Jun-15Advanced Programming Spring 2002 Process relationships  process tree structure: child processes  inherit properties from parent  processes can  terminate  request more (virtual) memory  wait for a child process to terminate  overlay program with different one  send messages to other processes

28-Jun-15Advanced Programming Spring 2002 Processes  Reality: each CPU can only run one program at a time  Fiction to user: many people getting short (~ ms) time slices  pseudo-parallelism  multiprogramming  modeled as sequential processes  context switch

28-Jun-15Advanced Programming Spring 2002 Process creation  Processes are created:  system initialization  by another process  user request (from shell)  batch job (timed, Unix at or cron )  Foreground processes interact with user  Background processes (daemons)

28-Jun-15Advanced Programming Spring 2002 Processes – example bart:~> ps -ef UID PID PPID C STIME TTY TIME CMD root Mar 31 ? 0:17 sched root Mar 31 ? 0:09 /etc/init - root Mar 31 ? 0:00 pageout root Mar 31 ? 54:35 fsflush root Mar 31 ? 0:00 /usr/lib/saf/sac -t 300 root :38:45 console 0:00 /usr/lib/saf/ttymon root Mar 31 ? 1:57 /usr/local/sbin/sshd root Mar 31 ? 0:01 /usr/sbin/inetd -s daemon Mar 31 ? 0:00 /sbin/lpd root Mar 31 ? 0:37 /usr/sbin/rpcbind root Mar 31 ? 0:06 /usr/sbin/in.rdisc -s root Mar 31 ? 0:00 /usr/sbin/keyserv hgs :58:13 pts/16 0:00 -tcsh daemon Mar 31 ? 0:00 /usr/lib/nfs/statd root Mar 31 ? 0:00 /yp/ypbind -broadcast

28-Jun-15Advanced Programming Spring 2002 Unix processes  0: process scheduler ("swapper") system process  1: init process, invoked after bootstrap – /sbin/init

28-Jun-15Advanced Programming Spring 2002 Processes – example  task manager in Windows NT, 2000 and XP  cooperative vs. preemptive

28-Jun-15Advanced Programming Spring 2002 Unix process creation: forking #include pid_t fork(void); int v = 42; if ((pid = fork()) < 0) { perror("fork"); exit(1); } else if (pid == 0) { printf("child %d of parent %d\n", getpid(), getppid()); v++; } else sleep(10);

28-Jun-15Advanced Programming Spring 2002 fork()  called once, returns twice  child: returns 0  parent: process ID of child process  both parent and child continue executing after fork  child is clone of parent (copy!)  copy-on-write: only copy page if child writes  all file descriptors are duplicated in child  including file offset  network servers: often child and parent close unneeded file descriptors

28-Jun-15Advanced Programming Spring 2002 User identities  Who we really are: real user & group ID  taken from /etc/passwd file: hgs:7C6uo:5815:92:H. Schulzrinne:/home/hgs:/bin/tcsh  Check file access permissions: effective user & group ID, supplementary group ID  supplementary IDs via group membership: /etc/group  special bits for file: "when this file is executed, set the effective IDs to be the owner of the file"  set-user-ID bit, set-group-ID bit  /usr/bin/passwd needs to access password files

28-Jun-15Advanced Programming Spring 2002 Aside: file permissions S_IRUSR user-read S_IWUSR user-write S_IXUSR user-execute S_IRGRP group-read S_IWGRP group-write S_IXGRP group-execute S_IROTH other-read S_IWOTH other-write S_IXOTH other-execute

28-Jun-15Advanced Programming Spring 2002 Process identifiers pid_t getpid(void) process identifier pid_t getpgid(pid_t pid); process group pid_t getppid(void); parent PID uid_t getuid(void); real user ID uid_t geteuid(void); effective user ID gid_t getgid(void); real group ID gid_t getegid(void); effective group ID

28-Jun-15Advanced Programming Spring 2002 Process properties inherited  user and group ids  process group id  controlling terminal  setuid flag  current working directory  root directory (chroot)  file creation mask  signal masks  close-on-exec flag  environment  shared memory  resource limits

28-Jun-15Advanced Programming Spring 2002 Differences parent-child  Return value of fork()  process IDs and parent process IDs  accounting information  file locks  pending alarms

28-Jun-15Advanced Programming Spring 2002 Waiting for a child to terminate  asynchronous event  SIGCHLD signal  process can block waiting for child termination pid = fork();... if (wait(&status) != pid) { something's wrong }

28-Jun-15Advanced Programming Spring 2002 Waiting for a child to terminate pid_t waitpid(pid_t pid, int *statloc, int options) pid=-1any child process pid>0specific process pid=0any child with some process group id pid<0any child with PID = abs(pid)

28-Jun-15Advanced Programming Spring 2002 Race conditions  race = shared data + outcome depends on order that processes run  e.g., parent or child runs first?  waiting for parent to terminate  generally, need some signaling mechanism  signals  stream pipes

28-Jun-15Advanced Programming Spring 2002 exec: running another program  replace current process by new program  text, data, heap, stack int execl(const char *path, char *arg,...); int execle(const char *path, const char *arg0, /* (char *) 0, char *const envp[] */); int execv(const char *path, char *const arg[]); int execvp(char *file, char *const argv[]); file : /absolute/path or one of the PATH entries

28-Jun-15Advanced Programming Spring 2002 exec example char *env_init[] = {"USER=unknown", "PATH=/tmp", NULL}; int main(void) { pid_t pid; if ((pid = fork()) < 0) perror("fork error"); else if (pid == 0) { if (execle("echoall", "echoall", "myarg1", "MY ARG2", NULL, env_init) < 0) perror("exec"); } if (waitpid(pid, NULL, 0) < 0) perror("wait error"); printf("child done\n"); exit(0); }

28-Jun-15Advanced Programming Spring 2002 system: execute command  invokes command string from program  e.g., system("date > file");  handled by shell (/usr/bin/ksh)  never call from setuid programs #include int system(const char *string);

28-Jun-15Advanced Programming Spring 2002 Threads  process: address space + single thread of control  sometimes want multiple threads of control (flow) in same address space  quasi-parallel  threads separate resource grouping & execution  thread: program counter, registers, stack  also called lightweight processes  multithreading: avoid blocking when waiting for resources  multiple services running in parallel  state: running, blocked, ready, terminated

28-Jun-15Advanced Programming Spring 2002 Why threads?  Parallel execution  Shared resources  faster communication without serialization  easier to create and destroy than processes (100x)  useful if some are I/O-bound  overlap computation and I/O  easy porting to multiple CPUs

28-Jun-15Advanced Programming Spring 2002 Thread variants  POSIX (pthreads)  Sun threads (mostly obsolete)  Java threads

28-Jun-15Advanced Programming Spring 2002 Creating a thread int pthread_create(pthread_t *tid, const pthread_attr_t *, void *(*func)(void *), void *arg);  start function func with argument arg in new thread  return 0 if ok, >0 if not  careful with arg argument

28-Jun-15Advanced Programming Spring 2002 Network server example  Lots of little requests (hundreds to thousands a second)  simple model: new thread for each request  doesn't scale (memory, creation overhead)  dispatcher reads incoming requests  picks idle worker thread and sends it message with pointer to request  if thread blocks, another one works on another request  limit number of threads

28-Jun-15Advanced Programming Spring 2002 Worker thread while (1) { wait for work(&buf); look in cache if not in cache read page from disk return page }

28-Jun-15Advanced Programming Spring 2002 Leaving a thread  threads can return value, but typically NULL  just return from function (return void * )  main process exits  kill all threads  pthread_exit(void *status)

28-Jun-15Advanced Programming Spring 2002 Thread synchronization  mutual exclusion, locks: mutex  protect shared or global data structures  synchronization: condition variables  semaphores