displays a list of processes executed in current shell

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

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

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

Lecture 25 Systems Programming Process Control

5. Processes and Memory Management

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

Process definition Concurrency Process status Process attributes PROCESES 1.3

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

Libmonitor: A Tool for First-Party Monitoring

Interlude: Process API

7.2 Examining Processes on the Command Line

TOP(1) Linux User s Manual TOP(1)

Shared Memory Segments and POSIX Semaphores 1

Unix System Calls. Dept. CSIE

System Administration

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

Linux Syslog Messages in IBM Director

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

SQLITE C/C++ TUTORIAL

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

System Calls and Standard I/O

Program 5 - Processes and Signals (100 points)

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Set-UID Privileged Programs

Process Monitor HOW TO for Linux

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

1 Posix API vs Windows API

II. Systems Programming using C (Process Control Subsystem)

Facultat d'informàtica de Barcelona Univ. Politècnica de Catalunya. Administració de Sistemes Operatius. System monitoring

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

Introduction. dnotify

Linux Kernel Architecture

System Security Fundamentals

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

PCI-SIG ENGINEERING CHANGE REQUEST

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

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

Informatica e Sistemi in Tempo Reale

Introduction to Java

Parallelization: Binary Tree Traversal

System Calls Related to File Manipulation

System calls. Problem: How to access resources other than CPU

Unix Scripts and Job Scheduling

Comp151. Definitions & Declarations

Lecture 24 Systems Programming in C

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

Network Programming with Sockets. Process Management in UNIX

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

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

Compartmented Mode Workstations. James A. Rome Oak Ridge National Laboratory

CS161: Operating Systems

HP-UX Essentials and Shell Programming Course Summary

PetaLinux SDK User Guide. Application Development Guide

Grundlagen der Betriebssystemprogrammierung

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

Setting up PostgreSQL

Minix Mini Unix (Minix) basically, a UNIX - compatible operating system. Minix is small in size, with microkernel-based design. Minix has been kept

The Linux Kernel: Process Management. CS591 (Spring 2001)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Answers to Even-numbered Exercises

Partitioning. Files on the Hard Drive. Administration of Operating Systems DO2003. Partition = Binder with index. Write file = Insert document

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

Hands-On UNIX Exercise:

Answers to Even- Numbered Exercises

6.828 Operating System Engineering: Fall Quiz II Solutions THIS IS AN OPEN BOOK, OPEN NOTES QUIZ.

Programmation Systèmes Cours 4 Runtime user management

Illustration 1: Diagram of program function and data flow

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Leak Check Version 2.1 for Linux TM

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

How ToWrite a UNIX Daemon

AN INTRODUCTION TO UNIX

Angels (OpenSSL) and D(a)emons. Athula Balachandran Wolfgang Richter

CS 103 Lab Linux and Virtual Machines

How to test and debug an ASP.NET application

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer

STEP 4 : GETTING LIGHTTPD TO WORK ON YOUR SEAGATE GOFLEX SATELLITE

Betriebssysteme KU Security

5 Arrays and Pointers

Keil C51 Cross Compiler

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

INASP: Effective Network Management Workshops

Using the Remote Access Library

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

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Process Monitor HOW TO for Linux

Programming the Cell Multiprocessor: A Brief Introduction

Lecture 16: System-Level I/O

Project 2: Bejeweled

Shared Memory Introduction

A Crash Course on UNIX

CPSC 226 Lab Nine Fall 2015

Tutorial on C Language Programming

Running your first Linux Program

Stack Overflows. Mitchell Adair

File Handling. What is a file?

Logging. Working with the POCO logging framework.

Basics of I/O Streams and File I/O

Transcription:

A process consists of : - an executing (running) program - its current values - state information - the resources used by the operating system to manage the execution ps displays a list of processes executed in current shell % ps PID TTY STAT TIME COMMAND 14429 p4 S 0:00 -bash 14431 p4 R 0:00 ps % terminal execut. status time command execution ps l shows full information (long format): FLAGS UID PID PPID PRI NI SIZE RSS WCHAN STA TTY TIME COMMAND 100 1002 379 377 0 0 2020 684 c0192be3 S p0 0:01 -bash 100 1002 3589 3588 0 0 1924 836 c0192be3 S p2 0:00 -bash 100 1002 14429 14427 10 0 1908 1224 c0118060 S p4 0:00 -bash 100000 1002 14611 14429 11 0 904 516 0 R p4 0:00 ps -l % owner parent priority size of size in event status exec. execution process text+ mem. for which time command PID data+stack the process terminal is sleeping ps ax information about all processes running currently in the system (a show processes of other users too, x show processes without controlling terminal) Every process can create other process : - the initiating process is called parent - a newly created process is called child. Processes create a hierarchical structure, the root is init process ( id = 1) pstree top 1

kill [-name_or_signal_number] process_identifier By convention, if the signal is not specified, a process with a given PID is terminated with SIGTERM signal (signal number 15) kill 14285 killall vi kill l shows names of all signals defined in the system striking ^C key from terminal keyboard - the active shell will send immediately the SIGINT signal to all active child processes. Not all processes can be stopped this way. Some special processes (as shell process) can be killed only by the SIGKILL signal (signal number 9) % kill -9 14280 % kill -KILL 14280 2

int main (int argc, char* argv[]){ cc prog1.c./a.out cc prog1.c o prog1./prog1 3

1 Creating a process #include <sys/types.h> #include <unistd.h> pid_t fork(); RETURNS: success : - process identifier of the child process (a value greater than 0) to the parent process - value of 0 to the child process error: -1 The fork system call creates a child process - is called once but returns twice The return value allows the process to determine if it is the parent or the child. Example 1 Creating a new process void main() { printf("start\n"); if (fork()==0) printf("hello from child\n"); else printf("hello from parent\n"); printf("stop\n"); 4

A process terminates either by calling exit (normal termination) or due to receiving an uncaught signal #include <unistd.h> void exit(int status); The argument status is an exit code, which indicates the reason of termination, and can be obtained by the parent process. exit(0)- process ends correctly exit(value<>0) failure occured main(){ fork() fork() if (fork()==0) fork(); fork(); main(){ fork() fork() if (fork()==0) exit(0); fork(); main(){ if (fork()!=0){ if (fork()==0); fork(); else exit(0); fork(); 5

Processes are concurrent Each process is identified by a unique value:pid #include <unistd.h> int getpid(); int getppid(); RETURNS: success : PID or PPID. error: -1 2

Waiting for a process to terminate To wait for an immediate child to terminate, a wait system call is used. #include <sys/wait.h> int wait(int *statusp) RETURNS: success : PID of child process which stopped or died error: -1 The location pointed to by statusp. It indicates the cause of termination and other information about the terminated process in the following manner: If the child process terminated normally, the low-order byte will be 0 and the high-order byte will contain the exit code passed by the child as the argument to the exit system call. If the child process terminated due to an uncaught signal, the low-order byte will contain the signal number, and the high-order byte will be 0. sleep (time) 2

#include <stdio.h> main(){ int pid1, pid2, status; pid1 = fork(); if (pid1 == 0) exit(7); Example 3 wait and correct ending of the process printf("child id : %d\n", pid1); pid2 = wait(&status); printf("process ending status %d: %x\n", pid2, status); #include <stdio.h> main(){ int pid1, pid2, status; Example 4 wait and killing the process pid1 = fork(); if (pid1 == 0){ sleep(10); exit(7); printf("child id : %d\n", pid1); kill(pid1, 9); pid2 = wait(&status); printf("ending status %d: %x\n", pid2, status); 3

Starting a new code To start the execution of a new code an exec system call is used. The system call replaces the current process image (i.e. the code, data and stack segments) with a new one contained in the file the location of which is passed as the first argument. It does not influence other parameters of the process e.g. PID, parent PID, open files table. There is no return from a successful exec call because the calling process image is overlaid by the new process image 4

#include <unistd.h> int execl(const char *path, const char *arg0,..., const char *argn, char * /*NULL*/); int execv(const char *path, char *const argv[]); int execle(const char *path,char *const arg0[],..., const char *argn, char * /*NULL*/, char *const envp[]); int execve(const char *path, char *const argv[], char *const envp[]); int execlp(const char *file, const char *arg0,..., const char *argn, char * /*NULL*/); int execvp (const char *file, char *const argv[]); RETURNS: success : execution of program passed as the argument error: -1 path a pointer to the path name of the file containing the program to be executed. arg0,..., argn list of arguments available to the new program. By convention, arg0 points to a string that is the same as path (or the last component of path). The list of argument strings is terminated by a (char*)0 argument. argv an array of character pointers to null-terminated strings that constitute the argument list available to the new process image. By convention, argv must have at least one member, and it should point to a string that is the same as path (or its last component). argv is terminated by a null pointer. 5

This form of exec system call is useful when the list of arguments is known at the time of writing the program. The execv version is useful when the number of arguments is not known in advance. The execle and execve system calls allow passing an environment to a process execl( /bin/ls, ls, -l,null) execlp( ls, ls, -l,null) char* const av[]={ ls, -l, null execv( /bin/ls, av) char* const av[]={ ls, -l, null execvp( ls, av) Example: #include <stdio.h> main(){ printf("poczatek\n"); execlp("ls", "ls", "-a", NULL); printf("koniec\n"); 6

Excersices: 1. Write a program which prints the list of files from the current directory. The file list should be preceded by the word Beginning, and ended with the word End 2. Write a program that creates three zombie processes 7