CS Operating Systems Lab 3: UNIX Processes

Similar documents
System Programming. Signals II

System Programming. Process Control II

CS240: Programming in C

System Programming. Pipes I

System Programming. Process Control III

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

Introduction to Processes

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

518 Lecture Notes Week 3

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 Fall Programming Assignment 1 (updated 9/16/2017)

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

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

CS240: Programming in C

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.

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

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

System Programming. Introduction to Unix

Operating Systems Lab

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

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

CSC 1600 Unix Processes. Goals of This Lecture

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

NETWORK AND SYSTEM PROGRAMMING

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

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

Interrupts, Fork, I/O Basics

CSC209 Fall Karen Reid 1

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

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

File Descriptors and Piping

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

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

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

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

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

Recitation 8: Tshlab + VM

Creating a Shell or Command Interperter Program CSCI411 Lab

CSC209H Lecture 5. Dan Zingaro. February 4, 2015

SOFTWARE ARCHITECTURE 3. SHELL

Part II Processes and Threads Process Basics

SE350: Operating Systems

CS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007

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

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

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

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

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

Parents and Children

CS 261 Fall Mike Lam, Professor. Processes

Processes. CS439: Principles of Computer Systems January 24, 2018

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

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

This tutorial covers a foundational understanding of IPC. Each of the chapters contain related topics with simple and useful examples.

Operating systems and concurrency - B03

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

Matt Ramsay CS 375 EXAM 2 Part 1

API Interlude: Process Creation (DRAFT)

CS Operating system Summer Midterm I -- July 11, 2017 You have 115 min (10:00am-11:55pm). Good Luck!

What is a Process? Processes and Process Management Details for running a program

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

Unix Processes 1 / 31

Computer Systems Assignment 2: Fork and Threads Package

Processes. CS439: Principles of Computer Systems January 30, 2019

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

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

CS Operating system Fall Midterm I -- Oct 5, 2017 You have 75 min. Good Luck!

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

CSC209S Midterm (L0101) Spring 1999 University of Toronto Department of Computer Science

CS240: Programming in C. Lecture 14: Errors

Fall 2015 COMP Operating Systems. Lab #3

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

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

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

COL100 Lab 2. I semester Week 2, Open the web-browser and visit the page and visit the COL100 course page.

UNIX System Calls. Sys Calls versus Library Func

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

UNIVERSITY OF TORONTO SCARBOROUGH Computer and Mathematical Sciences. APRIL 2016 EXAMINATIONS CSCB09H3S Software Tools & Systems Programming

Programming the DMCC in C

Killing Zombies, Working, Sleeping, and Spawning Children

Operating Systemss and Multicore Programming (1DT089)

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

Operating System Structure

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

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

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

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Workshop on Inter Process Communication Solutions

Operating Systems. Lecture 05

System Programming. Signals I

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

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

Figure 1 Ring Structures

Transcription:

CS 346 - Operating Systems Lab 3: UNIX Processes Due: February 15 Purpose: In this lab you will become familiar with UNIX processes. In particular you will examine processes with the ps command and terminate processes with the kill command. You will also see how you can run a program in the background. Finally you will write a C program to spawn a process. Instructions: 1. Remote login to kant and place copies of the files sequencer.c and generator.c into your working directory. (You can find the code for both programs at the end of the lab. I can show you a way to transfer files between Windows and kant using psftp in lab today.) Compile and run the program sequencer.c. First modify the program sequencer.c to introduce a 1 second delay in the loop with the statement sleep(1); Now introduce an infinite loop by replacing the for statement with a while (1). You will need to pull the initialization and update of the loop control variable into separate statements. What happens when you run the program? (Note: Ctrl-C will halt a runaway program.) 2. Open a second terminal window (how you do this will depend on the windowing environment you are running). In one terminal window start the program and in the second terminal window enter the command ps -ef This gives a complete list of the processes. Here are some additional uses of ps: ps ef grep <username> ps U <username> ps U <username> ps f p <pid> Find the line for the process that is running your program. How are you able to identify the process? What is the pid and ppid of this process? What do pid and ppid represent? You can terminate a process with the kill command: kill <pid> Terminate your program by terminating the process. Verify that it is gone by examining the list

of processes. Can you terminate the first terminal window (by terminating the shell)? Do so if you can and describe how you did it. 3. Repeat the situation you had in Step 2, that is, open two terminal windows and run the program from the first one. Review the process list in the second terminal window. Record the pid s for the first terminal window and the program. Record the ppid of the program. What connection do you see among these id s? Now terminate the first terminal window. (You may need to give the command kill 9 <pid> to really kill it.) What do you notice about the process list after terminating the first window? 4. Now run the program in the background by entering the command./a.out & Review the process list. What is the pid of the program? What process is the parent of the program? Can you terminate the program without going to a second terminal? Run three copies of the program from the first terminal window in the background. Record the pid s of each of the three processes. What do you observe about their ppid s? What process spawned the three processes? What happens when you terminate the spawning process? Terminate the three processes. 5. Compile and run the program generator.c. Note that the program requires a filename as a command line argument. Describe the output. Modify the code to generate the upper case letters A J. Compile and run the program to test your modification. 6. Now we want to write a program that spawns another process. In C one spawns another process with the fork() system call. The new process executes the code immediately following the fork() call. The effect is there are two processes executing the same code (and how cool is that!). In order to have the new process execute different code you need to branch on the pid returned by the fork() call. If the value returned is 0 then it is the new (child) process. If the value returned is not 0 then it is the original (parent) process and the value returned is the pid of the newly spawned process.

The code typically has the form: int pid; if ((pid = fork()) == 0) // child code... else // parent code... Modify the program generator.c so that a child process is spawned. Further have the child generate the lower case letters and have the parent generate upper case letters. When you modify the program be sure the buffer declaration and the opening of the file precede the code for the fork(). The child code should call exit(0); at the end of its block. Also, for this to work properly the parent will need to wait for the child to terminate before closing the file and terminating. The following statement will do this wait(&status); where status is an int variable. Run the program and describe the results. 7. Modify the program generator.c to print out the value of pid both in the child code and the parent code (appropriately labeled). Record your results. How can we know the non-zero value printed is the pid of the child process? Add additional statements to print out the pid and ppid of each process by calling the system functions getpid() and getppid(). Run the program and record your results. 8. As a last modification of the program introduce a one-second pause in each loop by using the system call sleep(1). Run the program with this modification. Does the program still have the same behavior? Run the program again and while it is running go to a different terminal window and examine the process list to find the pid and ppid of the two processes. Record your observations. 9. For the last step of the lab reflect on what you have learned about the relationship of a process with a process it has spawned. In particular examine the code in generator.c with the modifications you have made. Address the following questions: - Does the child (spawned) process inherit the resources owned by the parent? - Do the two processes share the same address space (memory)?

- Can the parent terminate independent of the child? Refer to your code and observations made in the lab and use them as evidence when addressing these questions. Hand In: Hand in a write-up that includes answers to questions posed in the steps above and results of executions along with observations you have made. Turn in the programs sequencer.c (Step 1), and generator.c (Step 8). E-mail the two source files to your instructor as well.

/* sequencer.c */ Programmer: ska Date: 8 Feb 2018 #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) int i; for (i = 0; i < 10; i++) printf("%d\n", i); return 0; /* generator.c */ Programmer: ska Date: 8 Feb 2018 #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define PERMS 0666 int main(int argc, char * argv[]) int fd, i; char buf[2]; buf[0] = 'a'; buf[1] = '\n'; if ((fd = creat(argv[1], PERMS)) == -1) printf("generator: can't create %s, mode %o.\n", argv[1], PERMS); exit(1); for (i = 0; i < 10; i++) write(fd, buf, 2); buf[0]++; close(fd); return 0;