This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

Similar documents
System Programming. Process Control III

CSC 1600 Unix Processes. Goals of This Lecture

Fall 2015 COMP Operating Systems. Lab #3

COMP 2355 Introduction to Systems Programming

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

CS240: Programming in C

Operating systems and concurrency - B03

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

UNIX System Calls. Sys Calls versus Library Func

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

Interacting with Unix

518 Lecture Notes Week 3

Announcement (1) sys.skku.edu is now available

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

CSC209H Lecture 5. Dan Zingaro. February 4, 2015

PROCESS PROGRAMMING INTERFACE

CS 201. Processes. Gerson Robboy Portland State University

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

SYSTEM INFORMATION. UNIX Programming 2015 Fall by Euiseong Seo

Unix Processes 1 / 31

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

Operating Systems. Lecture 05

Process management 1

Altering the Control Flow

Altering the Control Flow

CS240: Programming in C

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

Programming Assignment #1: A Simple Shell

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.

Processes. CSE 351 Autumn Instructor: Justin Hsia

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

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

Parents and Children

Process Management! Goals of this Lecture!

CITS2002 Systems Programming. Creating a new process using fork() 1 next CITS2002 CITS2002 schedule

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Project 2: Shell with History1

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

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

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

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

Preview. Review. System Data Files (Password File) System Data Files (Password File) System Data Files (Password File)

Exceptional Control Flow Part I September 22, 2008

University of Washington What is a process?

Exceptional Control Flow Part I

CS 261 Fall Mike Lam, Professor. Processes

Processes. CSE 351 Autumn Instructor: Justin Hsia

Process Management 1

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

System Programming. Introduction to Unix

Process Management! Goals of this Lecture!

Operating Systems Lab

Operating Systems. Processes

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

OPERATING SYSTEMS: Lesson 12: Directories

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

System Programming. Signals II

CSC209 Fall Karen Reid 1

Creating a Shell or Command Interperter Program CSCI411 Lab

Processes. CS3026 Operating Systems Lecture 05

Lecture 3 Process API in UNIX systems

Network Socket Programming - 1 BUPT/QMUL

Computer Science & Engineering Department I. I. T. Kharagpur

System Programming. Process Control II

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

CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux

Processes: Introduction. CS 241 February 13, 2012

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

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

Exceptional Control Flow Part I Oct. 17, 2002

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

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

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Process Management 1

Implementation of a simple shell, xssh

Introduction to Processes

SE350: Operating Systems

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

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

Interrupts, Fork, I/O Basics

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

UNIX Processes. by Armin R. Mikler. 1: Introduction

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CMPSCI 230 Computer Systems Principles. Processes

Exceptional Control Flow Part I

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

PESIT Bangalore South Campus

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

ADVANCED OPERATING SYSTEMS

Lab 4. Out: Friday, February 25th, 2005

ACSC 372 Systems Programming. exec() Functions. Outline

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Transcription:

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX. 1

Manages Resources: I/O devices (disk, keyboard, mouse, terminal) Memory Manages Processes: process creation, termination inter-process communication multi-tasking (scheduling processes) 2

We will focus on the Unix operating system. There are many flavors of Unix, but the libraries that provide access to the kernel are pretty standard (although there are minor some differences between different flavors of Unix). 3

Posix - Portable Operating System Interface Posix is a popular standard for Unix-like operating systems. Posix is actually a collection of standards that cover system calls, libraries, applications and more Posix 1003.1 defines the C language interface to a Unix-like kernel. 4

Most current Unix-like operating systems are Posix compliant (or nearly so). Linux, BSD, Solaris, IRIX We won t do anything fancy enough that we need to worry about specific versions/flavors of Unix (any Unix will do). 5

process primitives creating and managing processes managing process environment user ids, groups, process ids, etc. file and directory I/O terminal I/O system databases (passwords, etc) 6

System Calls A system call is an interface to the kernel that makes some request for a service. The actual implementation (how a program actually contacts the operating system) depends on the specific version of Unix and the processor. The C interface to system calls is standard (so we can write an program and it will work anywhere). 7

Every process has the following attributes: a process id (a small integer) a user id (a small integer) a group id (a small integer) a current working directory. a chunk of memory that hold name/value pairs as text strings (the environment variables). a bunch of other things 8

User ids and group ids are managed via a number of functions that deal with system databases. The passwsd database is one example. When you log in, the system looks up your user id in the passwd database. 9

For each user there is a record that includes: user s login name user id (a small integer 16 bits) group id home directory shell encrypted password user s full name 10

struct passwd { char *pw_name; /* user login name */ char *pw_passwd; /* encrypted password */ int pw_uid; /* user id */ int pw_gid; /* group id */ char *pw_gecos; /* full name */ char *pw_dir; /* home directory */ char *pw_shell; /* shell */ } 11

#include <pwd.h> /* given a user id get a record */ struct passwd *getpwuid( int uid); /* given a login name get a record */ struct passwd *getpwnam( char *name); 12

The Unix filesystem is based on directories and files. Directories are like folders (for you Windows Weenies). 13

Every file has a name. Unix file names can contain any characters (although some make it difficult to access the file). Unix file names can be long! how long depends on your specific flavor of Unix 14

Each file can hold some raw data. Unix does not impose any structure on files files can hold any sequence of bytes. Many programs interpret the contents of a file as having some special structure text file, sequence of integers, database records, etc. 15

A directory is a special kind of file - Unix uses a directory to hold information about other files. We often think of a directory as a container that holds other files (or directories). 16

Review: every file has a name. Each file in the same directory must have a unique name. Files that are in different directories can have the same name. 17

/ bin etc users tmp usr hollid2 scully bin etc netprog unix X ls who 18

The filesystem is a hierarchical system of organizing files and directories. The top level in the hierarchy is called the "root" and holds all files and directories. The name of the root directory is / 19

The pathname of a file includes the file name and the name of the directory that holds the file, and the name of the directory that holds the directory that holds the file, and the name of the up to the root The pathname of every file in a Unix filesystem is unique. 20

To create a pathname you start at the root (so you start with "/"), then follow the path down the hierarchy (including each directory name) and you end with the filename. In between every directory name you put a "/". 21

/ bin etc users tmp usr xxx scully bin etc netprog unix X ls who Syllabus /users/xxx/unix/syllabus /usr/bin/ls 22

The pathnames described in the previous slides start at the root. These pathnames are called "absolute pathnames". We can also talk about the pathname of a file relative to a directory. 23

If we are in the directory /users/hollid2, the relative pathname of the file Syllabus is: unix/syllabus Most unix commands deal with pathnames! We will usually use relative pathnames when specifying files. 24

The entire hierarchy can actually include many disk drives. some directories can be on other computers / bin etc users tmp usr xxx scully 25

Every process runs in a directory. This attribute is called the current working directory 26

char *getcwd(char *buf, size_t size); Returns a string that contains the absolute pathname of the current working directory. There are functions that can be used to change the current working directory (chdir). 27

#include <unistd.h> pid_t getpid(void); uid_t getuid(void); 28

The only way to create a new process is to issue the fork() system call. fork() splits the current process in to 2 processes, one is called the parent and the other is called Regis. Actually it s called the child. 29

The child process is a copy of the parent process. Same program. Same place in the program (almost we ll see in a second). The child process gets a new process ID. 30

The child process inherits many attributes from the parent, including: current working directory user id group id 31

fork() #include <unistd.h> pid_t fork(void); fork() returns a process id (a small integer). fork() returns twice! In the parent fork returns the id of the child process. In the child fork returns a 0. 32

#include <unistd.h> #include <stdio.h> void main(void) { if (fork()) printf( I am the parent\n ); else printf( I am the child\n ); printf( I am the walrus\n ); } 33

#include <unistd.h> #include <stdio.h> void main(void) { while (fork()) { printf("i am the parent %d\n,getpid()); } printf("i am the child %d\n,getpid()); } 34

Try pressing Ctrl-C to stop the program. It might be too late. If this is your own machine try rebooting. If this is a campus machine run for your life. If they catch you deny everything. Try listening next time 35

fork() is the only way to create a new process. This would be almost useless if there was not a way to switch what program is associated with a process. The exec() system call is used to start a new program. 36

exec There are actually a number of exec functions: execlp execl execle execvp execv execve The difference between functions is the parameters (how the new program is identified and some attributes that should be set). 37

When you call a member of the exec family you give it the pathname of the executable file that you want to run. If all goes well, exec will never return! The process becomes the new program. 38

int execl(char *path, char *arg0, char *arg1,, char *argn, (char *) 0); execl( /home/chrisc/reverse, reverse, Hidave,NULL); 39

execl #include <unistd.h> /* exec, getcwd */ #include <stdio.h> /* printf */ /* Exec example code */ /* This program simply execs "/bin/ls" */ void main(void) { char buf[1000]; } printf( Here are the files in %s:\n", getcwd(buf,1000)); execl("/bin/ls","ls","-al",null); printf("if exec works, this line won't be printed\n"); 40

fork() exec() Program does the following: fork() - results in 2 processes parent prints out it s PID and waits for child process to finish (to exit). child prints out it s PID and then execs ls and exits. 41

execandfork.c #include <unistd.h> /* exec, getcwd */ #include <stdio.h> /* printf */ #include <sys/types.h> /* need for wait */ #include <sys/wait.h> /* wait() */ 42

execandfork.c void child(void) { int pid = getpid(); } printf("child process PID is %d\n",pid); printf("child now ready to exec ls\n"); execl("/bin/ls","ls",null); 43

execandfork.c void parent(void) { int pid = getpid(); int stat; } printf("parent process PID is %d\n",pid); printf("parent waiting for child\n"); wait(&stat); printf("child is done. Parent now transporting to the surface\n"); 44

execandfork.c void main(void) { } printf("in main - starting things with a fork()\n"); if (fork()) { parent(); } else { } child(); printf("done in main()\n"); 45

execandfork.c >./execandfork In main - starting things with a fork() Parent process PID is 759 Parent process is waiting for child Child process PID is 760 Child now ready to exec ls exec execandfork fork exec.c execandfork.c fork.c Child is done. Parent now transporting to the surface Done in main() > 46