Lecture 25 Systems Programming Process Control



Similar documents
CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

ARUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have two

Process definition Concurrency Process status Process attributes PROCESES 1.3

Program 5 - Processes and Signals (100 points)

Unix Scripts and Job Scheduling

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

CP Lab 2: Writing programs for simple arithmetic problems

Introduction to Programming II Winter, 2014 Assignment 2

This presentation explains how to monitor memory consumption of DataStage processes during run time.

Linux Syslog Messages in IBM Director

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Systems Programming & Scripting

Networks. Inter-process Communication. Pipes. Inter-process Communication

Thirty Useful Unix Commands

Interlude: Process API

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

Informatica e Sistemi in Tempo Reale

Illustration 1: Diagram of program function and data flow

File Handling. What is a file?

1 Posix API vs Windows API

CS10110 Introduction to personal computer equipment

How ToWrite a UNIX Daemon

Setting up PostgreSQL

System Calls and Standard I/O

Programmation Systèmes Cours 4 Runtime user management

Bash shell programming Part II Control statements

Introduction to Java

IPC. Semaphores were chosen for synchronisation (out of several options).

Unix the Bare Minimum

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Lecture 22 The Shell and Shell Scripting

AN INTRODUCTION TO UNIX

Virtuozzo Virtualization SDK

Sample CSE8A midterm Multiple Choice (circle one)

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Hands-On UNIX Exercise:

Simple C Programs. Goals for this Lecture. Help you learn about:

Implementing a Web Service Client using Java

HP-UX Essentials and Shell Programming Course Summary

Set-UID Privileged Programs

A Crash Course on UNIX

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

UNIX, Shell Scripting and Perl Introduction

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Project 2: Bejeweled

Continuous Integration Part 2

Libmonitor: A Tool for First-Party Monitoring

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Example of a Java program

The Linux Operating System and Linux-Related Issues

Backing Up CNG SAFE Version 6.0

Syntax: cd <Path> Or cd $<Custom/Standard Top Name>_TOP (In CAPS)

5. Processes and Memory Management

Visual Basic Programming. An Introduction

CS 103 Lab Linux and Virtual Machines

7.2 Examining Processes on the Command Line

J a v a Quiz (Unit 3, Test 0 Practice)

The Einstein Depot server

Creating a Java application using Perfect Developer and the Java Develo...

Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Parallel Programming for Multi-Core, Distributed Systems, and GPUs Exercises

CPSC 2800 Linux Hands-on Lab #7 on Linux Utilities. Project 7-1

CISC 181 Project 3 Designing Classes for Bank Accounts

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

CS355 Hw 3. Extended Shell with Job Control

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Lecture 4: Writing shell scripts

13 Classes & Objects with Constructors/Destructors

Assignment 5: Adding and testing a new system call to Linux kernel

SendMIME Pro Installation & Users Guide

Laboratory Report. An Appendix to SELinux & grsecurity: A Side-by-Side Comparison of Mandatory Access Control & Access Control List Implementations

Green Telnet. Making the Client/Server Model Green

Introduction to the new mainframe Chapter 4: Interactive facilities of z/os: TSO/E, ISPF, and UNIX

PetaLinux SDK User Guide. Application Development Guide

The C Programming Language course syllabus associate level

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Lecture 24 Systems Programming in C

Sources: On the Web: Slides will be available on:

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

C++ INTERVIEW QUESTIONS

Answers to Even- Numbered Exercises

State of Michigan Data Exchange Gateway. Web-Interface Users Guide

/* File: blkcopy.c. size_t n

Hadoop Shell Commands

USEFUL UNIX COMMANDS

1 Basic commands. 2 Terminology. CS61B, Fall 2009 Simple UNIX Commands P. N. Hilfinger

MFCF Grad Session 2015

Hadoop Shell Commands

Threads Scheduling on Linux Operating Systems

Advanced Bash Scripting. Joshua Malone

Fundamentals of Programming

Lecture 22: C Programming 4 Embedded Systems

Transcription:

Lecture 25 Systems Programming Process Control A process is defined as an instance of a program that is currently running. A uni processor system or single core system can still execute multiple processes giving the appearance of a multi-core machine. A call to an executable program spawns a process. For examples, if a mail program is called by n users then n processes or instances are created and executed by the unix system. Many operating systems including windows and unix can execute many processes at the same time. When a program is called, a process is created and a process ID is issued. The process ID is given by the function getpid() defined in <unistd.h>. The prototype for pid( ) is given by #include <unistd.h> pid_t getpid(void); In a single core machine, each process takes turns running for a short duration in time interval called a timeslice. The unix command ps can be used to list all current process status in your shell. ps PID TTY TIME CMD 10150 pts/16 00:00:00 csh 31462 pts/16 00:00:00 ps The command ps lists the process ID (PID), the terminal name(tty), the amount of time the process has used so far(time) and the command it is executing(cmd). Ps command only displays the current user processes. But we can get all the processes with the flag (-a) and in long format with flag (-l) ps a ps -l ps -al Information provided by each process may include the following. PID The process ID in integer form PPID The parent process ID in integer form STAT The state of the process TIME CPU time used by the process (in seconds) TT Control terminal of the process COMMAND The user command that started the process

Each process has a process ID that can be obtained using the getpid() a system call declared in unistd.h. Here is an example of using the getpid function. printf("the current process %d \n",getpid()); printf("the parent process is %d \n",getppid()); printf("the owner of this process has uid %d \n",getuid()); sleep(1); Each process has a parent process and the parent process ID can be obtained by the function getppid(). A process can be terminated using the kill command kill -9 pid or if the process is in the foreground then it can also be killed by cntrl-c. Background Processes A long running program can be controlled by introducing a process running in the background. For example, we can run a C program in the background by typing >./a.out & Background processes continues to run even when you logout. You can check the status of a background process by typing > ps at the login. Process related Commands The process related system calls in UNIX include fork( ), exec( ) [many variations of this], wait( ) and exit( ) system calls. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. We will see an example of a simple shell soon. Forking new Processes A new process can be created using the command fork. The fork( ) function creates a child process which is exactly identical to the parent process except for the return value of fork( ). The value zero gets returned to the child and PID gets returned to the parent. An example of using fork( ) is if (fork() == 0) { printf( This is a message from the child\n ); else { printf( This is a message from the parent\n ); if the fork process is failed, no child process is created and fork returns -1. int PID = fork(); if (PID == -1) printf( the process creation failed\n );

Let us consider the following program that uses the child to compute partial sums and parent to compute the partial products of an array of integers. int A[]={1,2,3,4,5,6; int sum=0, pdt=1, PID, i; if ((PID=fork())==0){ for (i=0;i<6;i++) sum += A[i]; printf("this is child process computed sum %d \n", sum); if (PID <0) { fprintf(stderr,"problem creating a process \n"); if (PID >0) { for (i=0;i<6;i++) pdt *= A[i]; printf("the parent process completed the product %d \n", pdt); Executing another process There are many variations of the exec system call that can be used to execute a binary executable file. Exec programs do not return. It replaces the current process with the new process. If the exec command fails, then it returns 1. We will consider some useful forms of exec commands. execl --- takes the path name of a binary executable as its first argument, the rest of the arguments are the command line arguments ending with a NULL. Example: execl("./a.out", NULL) execv takes the path name of a binary executable as its first argument, and an array of arguments as its second argument. Example: static char* args[] = {, "cat.txt", "test1.txt", NULL; execv("/bin/cp", args); The empty string can be replaced by the command itself (eg ls ) or leave it as. execlp --- same as execl except that we don t have to give the full path name of the command. execlp("ls", NULL)

The UNIX Shell After login, we interact with unix through a program called shell. There are number of shells in use, csh(berkeley unix C-shell), bsh (bourne Shell) etc. You can find the current shell by typing ps. The shell program that is invoked upon login is called the login shell. The shell is a command interpreter as well as a command programming language with many modern programming constructs. We will learn how to write shell programs (or shell scripts) later. Shell takes commands such as ls, cp and calls the correct binaries to execute the command. Shell can also take a binary executable code of your C program, and start a process to execute that code. Shell continues to provide a prompt for the next command until shell is terminated. All shells are programmed in C. The command interpretation cycle of a shell is given as follows. Prompt Read SHELL Execute Command Command Transform Command A simple form of a command looks like this > command [arg(s)] Examples: > ls > cp file1 file2 After reading the command, the shell must interpret the command to understand its form. It needs to break down the command and command line arguments (if any). The shell then invokes a child process and calls the child process to execute the command by using one of the exec system calls. The binaries associated with the command are interpreted and executed. Let us now look at a program that implements a simple shell. This shell can only execute commands with no arguments such as ls or who. int PID; char cmd[256]; while (1) { printf("cmd: "); scanf( %s,cmd); if ( strcmp(cmd,"e")==0) /* loop terminates if type 'e'*/ exit(0); /* creates a new process. Parent gets the process ID. Child gets 0 */ if ((PID=fork()) > 0) wait(null); else if (PID == 0) /* child process */ { execlp (cmd,cmd,null);

/* exec cannot return. If so do the following */ fprintf (stderr, "Cannot execute %s\n", cmd); exit(1); /* exec failed */ else if ( PID == -1) { fprintf (stderr, "Cannot create a new process\n"); exit (2); Exiting a process System function exit allows termination of a process. The prototype for exit is #include <stdlib.h> void exit(int status); The value of the status may be EXIT_SUCCESS(0), EXIT_FAILURE(1) or any other value that is available to a parent process. Waiting for a Process wait, waitpid - wait for a child process to stop or terminate #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); It returns the PID of the child and the exit status gets placed in status. main() { int child_status, pid, pidwait; if ((pid = fork()) == 0) { printf( This is the child!\n ); else { pidwait = wait(&child_status); printf( child %d has terminated\n, pidwait); exit(); Questions about this Lecture 1. Why does not exec return (they have to maintain the same environment variables? See http://www.opengroup.org/onlinepubs/000095399/functions/exec.html 2. Do fork processes share the same variable space? Answer is no. But I need to know why.

Exercises 1. Modify the program (shell.c) so it can execute any shell command with upto 2 arguments. Eg: cp file1 file2, mv file1 file2, ls l 2. Write a program that will read a directory containing a set of files, create a new subfolder called backup on same folder and copy all the files from directory to the new sub directory backup. You are supposed to use exec() with cp command. Create a child process for each file that can execute each of the cp commands to copy the file from directory to subfolder backup 3. Why would you use fork() to create new processes in a uni-processor machine? Discuss a scenario where this can be useful.