Chapter 3 Processes we will completely ignore threads today

Similar documents
Process Creation in UNIX

CS240: Programming in C

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

OS Lab Tutorial 1. Spawning processes Shared memory

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

System Programming. Pipes I

Process Management 1

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

TCSS 422: OPERATING SYSTEMS

Shared Memory Memory mapped files

CS240: Programming in C

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Figure 1 Ring Structures

Process management 1

UNIX System Calls. Sys Calls versus Library Func

Lecture 5. Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Lab 5: Inter-Process Communication

Process Turnaround Time Total Wait Time P 1 12 ms 0 ms P 2 21 ms 12 ms P 3 23 ms 18 ms P 4 20 ms 17 ms

Environment Variables

628 Lecture Notes Week 4

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Recitation 8: Tshlab + VM

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

Parents and Children

CS 550 Operating Systems Spring Inter Process Communication

Process Management! Goals of this Lecture!

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

Prepared by Prof. Hui Jiang (COSC3221) 2/9/2007

Chapter 3: Processes. Operating System Concepts 8 th Edition,

518 Lecture Notes Week 3

Outlook. Process Concept Process Scheduling Operations on Processes. IPC Examples

Pipelines, Forks, and Shell

File Descriptors and Piping

Chapter 3: Processes

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CSE 421/521 - Operating Systems Fall 2013 Recitations. Recitation - II Unix Processes Prof. Tevfik Kosar. University at Buffalo

Processes COMPSCI 386

CSC 1600 Unix Processes. Goals of This Lecture

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

프로세스간통신 (Interprocess communication) i 숙명여대창병모

Unix Processes 1 / 31

Operating System Labs. Yuanbin Wu

System Programming. Process Control III

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

Multi-Process Programming in C

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CS 550 Operating Systems Spring Process III

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Inter Process Communication

W4118 Operating Systems. Junfeng Yang

Computer Systems Assignment 2: Fork and Threads Package

Lesson 2. process id = 1000 text data i = 5 pid = 1200

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Midterm Exam Questions (document version 1.1)

COMP 3100 Operating Systems

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Outline. Relationship between file descriptors and open files

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Inter-Process Communication

Interprocess Communication E. Im

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Process Management! Goals of this Lecture!

ECE 650 Systems Programming & Engineering. Spring 2018

CS Operating Systems Lab 3: UNIX Processes

Processes. Today. Next Time. Process concept Process model Implementing processes Multiprocessing once again. Scheduling processes

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

CSI Module 2: Processes

University of Ottawa School of Information Technology and Engineering

Gabrielle Evaristo CSE 460. Lab Shared Memory

SOFTWARE ARCHITECTURE 3. SHELL

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Logistics. Recitation 11: I/O Problems. Today s Plan. Why Use Robust I/O. Andrew Faulring Section A 18 November 2002

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Killing Zombies, Working, Sleeping, and Spawning Children

Operating System Structure

COE518 Lecture Notes Week 4 (Sept. 26, 2011)

Dept. of CS, York Univ. 1

Workshop on Inter Process Communication Solutions

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Processes. Processes (cont d)

CSCE 313: Intro to Computer Systems

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

CSE 410: Systems Programming

Transcription:

Chapter 3 Processes we will completely ignore threads today Images from Silberschatz Pacific University 1

Process Define: Memory Regions: Loaded from executable file: ELF: Executable and Linkable Format Linux What does this contain? Pacific University 2

State Machine While a process is active it is in a particular state How many processes can be in each state? Data Structures? Where? Which kind? Why? Pacific University 3

Process Control Block Who owns this data structure? CPU Scheduling data Memory Management data Accounting data Pacific University 4

Types of Processes I/O Bound CPU Bound How does this affect the OS? Pacific University 5

Process Scheduler Purpose: Process Scheduling Data structures: Dispatched: Pacific University 6

Schedulers Job Scheduler Long term Why is this important? CPU Scheduler Short term Constraints? Many OSes (Unix/Windows) don't really have a Job Scheduler Pacific University 7

Pacific University 8

Pacific University 9

Context Switch Context: What happens during a Context Switch? Speed? Pacific University 10

Pacific University 11

Pacific University 12

#include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> Process Creation int main() { pid_t pid; int value = 0; value = 9; /* fork another process */ pid = fork(); fprintf(stderr,"the value: %d pid: %d\n\n", value, pid); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } Pacific University 13

Process Creation else if (0 == pid) { /* I AM A CHILD */ fprintf(stderr,"the value: %d pid: %d\n\n", value, pid); value = 10; fprintf(stderr,"the value: %d pid: %d\n\n", value, pid); } execlp("/usr/bin/echo", "echo", "HELLO", NULL); } else { /* I AM A PARENT */ wait(null); printf ("\n\nchild Complete value: %d pid: %d\n\n", value, pid); } return 0; Pacific University 14

kill(pid, signal) $ man kill $ ps u $ kill -9 pid $ man -s 2 kill $ man -s 7 signal Process Termination Cascading termination: Pacific University 15

CreateProcess() Windows (Win32 API) fork() and exec() rolled into one 10 parameters! WaitForSingleObject() TerminateProcess() Pacific University 16

Interprocess Communication Why do we want this? Types: Shared memory: Message passing: Pacific University 17

Pacific University 18

#include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h> #include <fcntl.h> Shared Memory int main() { int segment_id; char *shared_memory; const long size = sysconf(_sc_pagesize); pid_t pid; /* allocate shared memory segment */ segment_id = shmget (IPC_PRIVATE, size, S_IRUSR S_IWUSR); /* attach the shared memory segment */ shared_memory = (char*) shmat (segment_id, NULL, 0); pid = fork(); Pacific University 19

if ( 0!= pid) { /* Write to shared memory */ sprintf(shared_memory, "Hello CS 460!"); } return 0; } /* for child to terminate */ wait(null); Shared Memory /* detach from shared memroy */ /* the child should have already detached before termination */ shmdt(shared_memory); /* remove shared memory */ shmctl(segment_id, IPC_RMID, NULL); } else { /* wait for parent to write */ /* hacky synchronization */ sleep (2); /* read/print from shared segment */ printf("message: %s\n", shared_memory); /* detach from shared memroy */ shmdt(shared_memory); Pacific University 20

Pacific University 21

Pacific University 22

Functions int execl(const char *path, const char *arg,...) int execlp(const char *file, const char *arg,...) int execle(const char *file, const char *arg,..., char const* envp[]) int execv(const char *path, char *const argv[]) int execvp(const char *file, char *const argv[]) int dup2(int oldfd, int newfd) int pipe(int filedes[2]) pid_t waitpid(pid_t pid, int *status, int options) char* strtok_r(char *str, const char* delim, char **saveptr) Pacific University 23

Pacific University 24

#include <unistd.h> #include <stdio.h> #include <fcntl.h> #include <string.h> dup2() #define MAXLEN 1024 int main() { /* dup2(int oldfd, int newfd) makes newfd be * the copy of oldfd closing newfd first if necessary. */ char data[maxlen]; int fd; int save_stdout_fd; save_stdout_fd = dup(stdout_fileno); fd = open("test.txt", O_WRONLY O_CREAT O_TRUNC, S_IRWXU); dup2(fd, STDOUT_FILENO); // stderr fprintf(stderr,"> "); // stdin fgets(&(data[0]),maxlen,stdin); Pacific University 25

// test.txt via fd write(fd, &(data[0]), strlen(data)); dup2() //???? printf("%d: %s\n", LINE, data); //???? close(fd); //???? printf("%d: ONCE MORE: %s\n", LINE, data); //???? dprintf(save_stdout_fd, "%d: SCREEN!\n\n", LINE ); // why is this necessary? fflush(null); dup2(save_stdout_fd, STDOUT_FILENO); //???? printf("%d: FINALLY: %s \n", LINE, data); } return 0; Pacific University 26

dup2() Let's investigate with gdb... (gdb) break dup2 Breakpoint 1 at 0x400720 (gdb) disas 0x400720 Dump of assembler code for function dup2@plt: 0x0000000000400720 <+0>: jmpq *0x20090a(%rip) 0x0000000000400726 <+6>: pushq $0x3 0x000000000040072b <+11>: jmpq 0x4006e0 End of assembler dump. (gdb) run Breakpoint 1, 0x00007ffff7b18a80 in dup2 () from /lib64/libc.so.6 (gdb) disas $pc Dump of assembler code for function dup2: => 0x00007ffff7b18a80 <+0>: mov $0x21,%eax 0x00007ffff7b18a85 <+5>: syscall 0x00007ffff7b18a87 <+7>: cmp $0xfffffffffffff001,%rax 0x00007ffff7b18a8d <+13>: jae 0x7ffff7b18a90 <dup2+16> 0x00007ffff7b18a8f <+15>: retq 0x00007ffff7b18a90 <+16>: mov 0x2bd3d1(%rip),%rcx 0x00007ffff7b18a97 <+23>: neg %eax 0x00007ffff7b18a99 <+25>: mov %eax,%fs:(%rcx) 0x00007ffff7b18a9c <+28>: or $0xffffffffffffffff,%rax 0x00007ffff7b18aa0 <+32>: retq End of assembler dump. Pacific University 27

x86 Assembly 0x21 =? arch/x86/include/generated/uapi/asm/unistd_64.h arch/x86/entry/syscalls/syscall_64.tbl https://en.wikibooks.org/wiki/x86_assembly/interfacing_with_linux#making_a_syscall Pacific University 28

Pacific University 29

#define MAXLEN 1024 #define READ 0 #define WRITE 1 simple pipe() int main() { char datapipewrite[maxlen]; char datapiperead[maxlen]; int thepipe[2]; pid_t childpid; memset(&(datapipewrite[0]), '\0', MAXLEN); memset(&(datapiperead[0]), '\0', MAXLEN); pipe(thepipe); /* get data from user */ readfromcommandline(datapipewrite, MAXLEN); write(thepipe[write], &(datapipewrite[0]), strlen(datapipewrite)); read(thepipe[read], &(datapiperead[0]), MAXLEN); fprintf(stderr,"read FROM PIPE: %s\n",&(datapiperead[0])); } close(thepipe[write]); close(thepipe[read]); Pacific University 30

void readfromcommandline(char * data, int maxsize) { memset(data, '\0', maxsize); fprintf(stderr,"> "); fgets(data, maxsize, stdin); } pipe() int main() { /* pipe(int filedes[2]) creates a pair of file * descriptors, pointing to a pipe inode, and places * them in the array pointed to by filedes. * filedes[0] is for reading, * filedes[1] is for writing. */ char data[maxlen]; int thepipe[2]; pid_t childpid; memset(&(data[0]), '\0', MAXLEN); pipe(thepipe); childpid = fork(); Pacific University 31

if(0 == childpid) { /* I AM A CHILD */ close(thepipe[write]); read(thepipe[read], &(data[0]), MAXLEN); pipe() while(strncmp( &(data[0]), "STOP", 4)!= 0 ) { printf("child> %s\n", &(data[0])); read(thepipe[read], &(data[0]), MAXLEN); } close(thepipe[read]); } else { close(thepipe[read]); readfromcommandline(&(data[0]), MAXLEN); write(thepipe[write], &(data[0]), strlen(data)); } while(strncmp(&(data[0]), "STOP", 4)!= 0) { readfromcommandline(&(data[0]), MAXLEN); write(thepipe[write], &(data[0]), strlen(data)); } close(thepipe[write]); Pacific University 32

ELF: Executable and Linkable Format Executable file a non-running process on disk! Tools readelf objdump Pacific University 33

Resources http://www.cs.stevens.edu/~jschauma/810/elf.html refspecs.linuxfoundation.org/elf/x86_64-abi-0.99.pdf refspecs.linuxfoundation.org/elf/gabi4+/contents.html skyfree.org/linux/reference/elf_format.pdf www.muppetlabs.com/~breadbox/software/elf.txt www.muppetlabs.com/~breadbox/software/tiny/teensy.html github.com/br903/elfkickers iecc.com/linker/linker10.html iecc.com/linker (number: 1, 3, 10) users.eecs.northwestern.edu/~kch479/docs/notes/linking.html Pacific University 34