Unix Basics Compiling and Using. CMPT 300 Operating Systems I Summer Segment 2: Unix Basics. Melissa O Neill

Similar documents
Why files? 1. Storing a large amount of data 2. Long-term data retention 3. Access to the various processes in parallel

Process Creation in UNIX

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

CS Operating Systems Lab 3: UNIX Processes

Figure 1 Ring Structures

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

UNIX System Calls. Sys Calls versus Library Func

CS240: Programming in C

A: We see the ps auxw execute and print on screen. The program holds the command in buffer then it is printed on screen.

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

CSC 1600 Unix Processes. Goals of This Lecture

UNIX input and output

SOFTWARE ARCHITECTURE 3. SHELL

File I/0. Advanced Programming in the UNIX Environment

Recitation 8: Tshlab + VM

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

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

CSE 333 SECTION 3. POSIX I/O Functions

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

Operating Systems Lab

Matt Ramsay CS 375 EXAM 2 Part 1

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

SE350: Operating Systems

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

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

File Descriptors and Piping

Computer Systems Assignment 2: Fork and Threads Package

Section 2: Processes

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

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

CSE 333 SECTION 3. POSIX I/O Functions

CS240: Programming in C

UNIX System Programming

Pipelines, Forks, and Shell

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

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

System Programming. Introduction to Unix

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

ปฏ บ ต การ #3. A Simple Shell Interpreter

Process management 1

CS167 Programming Assignment 1: Shell

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

Introduction to Supercomputing

Chapter 3 Processes we will completely ignore threads today

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

12: Filesystems: The User View

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

CSC209 Fall Karen Reid 1

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

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

Operating System Labs. Yuanbin Wu

PROCESS PROGRAMMING INTERFACE

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

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

Operating systems and concurrency - B03

Unix Processes 1 / 31

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

TCSS 422: OPERATING SYSTEMS

Operating System Structure

COMP s1 Lecture 1

System Programming. Process Control II

CSE 410: Systems Programming

Exercise 1: Basic Tools

Introduction to Processes

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

CS240: Programming in C

Creating a Shell or Command Interperter Program CSCI411 Lab

Programming Assignment #1: A Simple Shell

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

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

System Programming. Process Control III

Lecture 17. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture17/ $ cp r /home/hwang/cs375/lecture17/*.

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Project 2: Shell with History1

Problem Set 1: Unix Commands 1

Strategy for developing efficient programs:

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

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Linux Programming

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

API Interlude: Process Creation (DRAFT)

Introduction: The Unix shell and C programming

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

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

Interrupts, Fork, I/O Basics

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

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Interprocess Communication E. Im

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.

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

Lecture files in /home/hwang/cs375/lecture05 on csserver.

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Process Management! Goals of this Lecture!

Transcription:

CMPT 300 Operating Systems I Summer 1999 Segment 2: Unix Basics Melissa O Neill Unix Basics Compiling and Using You had to learn how to do the basics on a Unix system, including: Look up a manual page on a Unix system man fork man -s 2 intro Compile a C file with gcc unix% cat > hello.c #include <stdio.h> printf("\n"); unix% gcc -Wall -o hello hello.c unix% hello Segment 2, Page 1 Segment 2, Page 2 unix% cat > hello.c #include <stdio.h> puts(""); ^D unix% gcc -Wall -o hello hello.c unix% hello unix% truss hello ioctl(1, TCGETA, 0xEFFFEA3C) = 0 write(1, " H e l l o W o r l d\n", 12) = 12 llseek(0, 0, SEEK_CUR) = 693 _exit(0) Compile a C file with gcc (continued) unix% cat > hello.c #include <unistd.h> write(1, "\n", 12); ^D unix% gcc -Wall -o hello hello.c unix% hello unix% truss hello write(1, " H e l l o W o r l d\n", 12) = 12 _exit(0) Segment 2, Page 3 Segment 2, Page 4

Compile a C++ file with g++ unix% cat > bye.cc #include <iostream> cout << "Goodbye, cruel world..." << endl; ^D unix% g++ -Wall -o bye bye.cc unix% bye Goodbye, cruel world... Unix Basics The Shell A shell is a front end to a system. Usually Unix shells are textbased programs. Neat things to do with the shell: unix% echo unix% who csilop pts/0 May 11 08:34 (maia.cs) meoneill pts/1 May 13 21:26 (geranium.css) unix% who am i meoneill pts/1 May 13 21:26 (geranium.css) unix% pwd /gfs1/meoneill unix% mkdir cmpt300 unix% cd cmpt300 Segment 2, Page 5 Segment 2, Page 6 unix% pwd /gfs1/meoneill/cmpt300 unix% cd unix% pwd /gfs1/meoneill unix% grep SETUP_GNU.CSIL.rc setenv SETUP_GNU YES # FSF Software unix% wc <.CSIL.rc 56 353 2565 unix% grep SETUP.CSIL.rc \ perl -pe s/#.*// \ perl -pe s/.*setup_// \ perl -pe s/\s+/ / \ sort \ > setup.mine unix% echo Hello whoami Hello meoneill Unix Basics Processes and Files The two most important concepts: Process a program that is being executed File a named repository for data Segment 2, Page 7 Segment 2, Page 8

Two fundamental operations Unix Basics Processes fork create a clone of the current process execv/execve replace the program part of the process with another program #include <iostream> // declaration of cerr & << #include <unistd.h> // declaration of fork const char *const args[] = { "echo", "", 0 ; execv("/bin/echo", args); std::cerr << "execv failed!" << endl; return 1; Unix Basics forktest1.cc #include <iostream> // declaration of cout, cerr & << #include <unistd.h> // declaration of fork, getpid & getppid const int childpid = fork(); if (childpid > 0) { // in parent << ": created child with id " << childpid << endl; else if (childpid == 0) { // in child << ": parent is " << getppid() << endl; else { std::cerr << "fork failed" << endl; << ": exiting." << endl; Segment 2, Page 9 Segment 2, Page 10 Process 24994: created child with id 24995 Process 24995: parent is 24994 Process 24995: exiting. Process 24994: exiting. Process 25013: parent is 25012 Process 25013: exiting. Process 25012: created child with id 25013 Process 25012: exiting. Process 25024: parent is 25023 Process 25023: created child with id 25024 Process 25024: exiting. Process 25023: exiting. Process 25031: parent is 25030 Process 25030: created child with id 25031 Process 25030: exiting. unix% Process 25031: exiting. fork I lied! Here s what actually happened every time I ran the program on my workstation at home: Process 25039: created child with id 25040 Process 25039: exiting. Process 25040: parent is 1 Process 25040: exiting. Process 25041: created child with id 25042 Process 25041: exiting. unix% Process 25042: parent is 1 Process 25042: exiting. Process 25043: created child with id 25044 Process 25043: exiting. unix% Process 25044: parent is 1 Process 25044: exiting. Segment 2, Page 11 Segment 2, Page 12

fork I lied! (contd.) But here s what happened when I ran the program on a four processor SPARCStation: Process 23827: created child with id 23828 Process 23828: parent is 23827 Process 23827: exiting. Process 23828: exiting. Process 23829: created child with id 23830 Process 23830: parent is 23829 Process 23829: exiting. Process 23830: exiting. Class Exercise: Why do we see different results on different kinds of machine. Why does my computer say the parent is process 1? Unix Basics forktest2.cc #include <iostream> // declaration of cout, cerr & << #include <unistd.h> // declaration of fork, getpid & getppid const int childpid = fork(); if (childpid > 0) { // in parent << ": created child with id " << childpid << endl; wait(0); // < this added line is the only change else if (childpid == 0) { // in child << ": parent is " << getppid() << endl; else { std::cerr << "fork failed" << endl; << ": exiting." << endl; Segment 2, Page 13 Segment 2, Page 14 fork with wait running forktest2 With wait, the program behaves more consistently unix% forktest2 Process 25059: created child with id 25060 Process 25060: parent is 25059 Process 25060: exiting. Process 25059: exiting. unix% forktest2 Process 25061: created child with id 25062 Process 25062: parent is 25061 Process 25062: exiting. Process 25061: exiting. unix% forktest2 Process 25064: parent is 25063 Process 25064: exiting. Process 25063: created child with id 25064 Process 25063: exiting. #include <unistd.h> #include <fcntl.h> Unix Basics filecopy.cc (main) void copyout(const int in_fd, const int out_fd) { ; // we ll write code for this function shortly const char *const in_name = argv[1]; const char *const out_name = argv[2]; const int in_fd = open(in_name, O_RDONLY); const int out_fd = open(out_name, O_WRONLY O_CREAT O_TRUNC, 0666); copyout(in_fd, out_fd); // copy the file close(in_fd); close(out_fd); Segment 2, Page 15 Segment 2, Page 16

#include <iostream> #include <string.h> #include <errno.h> Unix Basics filecopy.cc (err_sys) void err_sys(const char *const error_message) { std::cerr << error_message << " (" << strerror(errno) << ")" << endl; exit(1); Unix Basics filecopy.cc (main) if (argc!= 3) err_sys("need exactly two arguments"); const char *const in_name = argv[1]; const char *const out_name = argv[2]; const int in_fd = open(in_name, O_RDONLY); if (in_fd < 0) // open failed, die err_sys("can t open input file"); const int out_fd = open(out_name, O_WRONLY O_CREAT O_TRUNC, 0666); if (out_fd < 0) // open failed, die err_sys("can t open output file"); copyout(in_fd, out_fd); // copy the file close(in_fd); if (close(out_fd) < 0) err_sys("error closing output file"); Segment 2, Page 17 Segment 2, Page 18 Unix Basics filecopy.cc (main, alternative) if (argc!= 3) err_sys("need exactly two arguments"); const char *const in_name = argv[1]; const char *const out_name = argv[2]; const int in_fd = Open(in_name, O_RDONLY); const int out_fd = Open(out_name, O_WRONLY O_CREAT O_TRUNC, 0666); copyout(in_fd, out_fd); /* copy the file */ Close(in_fd); Close(out_fd); void Close(int fd) { const int retval = close(fd); if (retval < 0) err_sys("close failed!"); Unix Basics filecopy.cc (copyout) const int BUFFER_SIZE = 4096; // copyout assumes the file descriptors are in blocking mode // (i.e., weren t opened with O_NONBLOCK) void copyout(int in_fd, int out_fd) { char buffer[buffer_size]; for( ; ; ) { const int read_count = read(in_fd, buffer, BUFFER_SIZE); if (read_count < 0) // Read failure err_sys("read failed"); else if (read_count == 0) return; // End of file return const int write_count = write(out_fd, buffer, read_count); if (write_count!= read_count) // Didn t write it all err_sys("write failed (or partial write only)"); Segment 2, Page 19 Segment 2, Page 20

Segment Review You should be able to write programs using: fork and exec open and close read and write Segment 2, Page 21