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



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

Lecture 25 Systems Programming Process Control

Process definition Concurrency Process status Process attributes PROCESES 1.3

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

Libmonitor: A Tool for First-Party Monitoring

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

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

1 Posix API vs Windows API

System Security Fundamentals

Program 5 - Processes and Signals (100 points)

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

Set-UID Privileged Programs

5. Processes and Memory Management

Shared Memory Segments and POSIX Semaphores 1

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

Interlude: Process API

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

Programmation Systèmes Cours 4 Runtime user management

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

Migration of Process Credentials

Unix System Calls. Dept. CSIE

How ToWrite a UNIX Daemon

7.2 Examining Processes on the Command Line

Linux Driver Devices. Why, When, Which, How?

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

Lecture 16: System-Level I/O

Lecture 22: C Programming 4 Embedded Systems

64 Bit File Access. 1.0 Introduction. 2.0 New Interfaces. 2.1 New User Visible Types. Adam Sweeney

Shared Address Space Computing: Programming

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

6.S096 Lecture 1 Introduction to C

Parallelization: Binary Tree Traversal

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

II. Systems Programming using C (Process Control Subsystem)

Grundlagen der Betriebssystemprogrammierung

CSE331: Introduction to Networks and Security. Lecture 34 Fall 2006

CS420: Operating Systems OS Services & System Calls

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

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

CIS 551 / TCOM 401 Computer and Network Security

CS161: Operating Systems

/* File: blkcopy.c. size_t n

NDK: NOVELL NSS AUDIT

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

Comp151. Definitions & Declarations

For a 64-bit system. I - Presentation Of The Shellcode

CIS 551 / TCOM 401 Computer and Network Security. Spring 2005 Lecture 4

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

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

1 Abstract Data Types Information Hiding

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

SQLITE C/C++ TUTORIAL

CS10110 Introduction to personal computer equipment

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

Leak Check Version 2.1 for Linux TM

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

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

System Calls and Standard I/O

Outline: Operating Systems

Linux Kernel Architecture

Basics of I/O Streams and File I/O

13 Classes & Objects with Constructors/Destructors

Illustration 1: Diagram of program function and data flow

W4118 Operating Systems. Junfeng Yang

Lecture 24 Systems Programming in C

REAL TIME SYSTEMS. Piotr MALECKI

C Programming Review & Productivity Tools

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

Project 2: Bejeweled

Capability-Based Access Control

Priority Based Implementation in Pintos

Chapter 6, The Operating System Machine Level

Process Monitor HOW TO for Linux

Jorix kernel: real-time scheduling

CS414 SP 2007 Assignment 1

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

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

Tutorial: Getting Started

System Calls Related to File Manipulation

C++ INTERVIEW QUESTIONS

Process Monitor HOW TO for Linux

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

Operating System Manual. Realtime Communication System for netx. Kernel API Function Reference.

Keil C51 Cross Compiler

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

Operating Systems and Networks

RTOS Debugger for ecos

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

INTRODUCTION UNIX NETWORK PROGRAMMING Vol 1, Third Edition by Richard Stevens

Multi-core architectures. Jernej Barbic , Spring 2007 May 3, 2007

Informatica e Sistemi in Tempo Reale

I/O Device and Drivers

Hands-On UNIX Exercise:

OpenACC 2.0 and the PGI Accelerator Compilers

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Unix Scripts and Job Scheduling

Virtuozzo Virtualization SDK

CSC230 Getting Starting in C. Tyler Bletsch

Transcription:

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

System Calls for Processes Ref: Process: Chapter 5 of [HGS]. A program in execution. Several processes are executed concurrently by the scheduler. Each process has a unique ID (called process ID or pid). When a process P is created, there is a parent process for P. (Note: Process with pid zero is its own parent.) 13 2 / 17

Useful Shell commands: ps: Gives the list of processes that are currently running. kill: Command to kill one or more processes. Example: Suppose the ps command shows that processes with IDs 1274 and 1297 are running. To kill these processes, the shell command is the following: % kill -9 1274 1297 13 3 / 17

System calls for obtaining pid: Prototypes: pid_t pid_t getpid (void) getppid (void) Headers: <sys/types.h> and <unistd.h>. The type pid_t is usually unsigned long. getpid: getppid: Returns the pid of the process. Returns parent s pid. No error exit for either function. 13 4 / 17

Sample Program: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { printf("pid = %ld\n", getpid()); printf("parent s Pid = %ld\n", getppid()); return 0; } /* End of main. */ 13 5 / 17

System call fork: Prototype: pid_t fork (void) Headers: <sys/types.h> and <unistd.h>. Creates a new process by copying the parent s memory image. Both processes continue to execute after the call to fork. Returns zero to child and the pid of the child to the parent. Another system call (one from the family of exec system calls) is used to make parent and child execute different programs. Returns -1 in case of failure. 13 6 / 17

(Bad) Example: #include <stdio.h> #include <sys/types.h> #include <unistd.h>.. int x = 0; fork(); x = 1;.. After the call to fork, there are two independent processes. Each process has its own location for the variable x. Better Example: Handout 13.1. 13 7 / 17

Failure of fork call: The total number of processes in the system exceeds a preset limit or the total number of processes for the user exceeds a preset limit. No child process is created when fork fails. The value of errno is EAGAIN. 13 8 / 17

System calls getuid and geteuid: Prototypes: uid_t uid_t getuid (void) geteuid (void) Headers: <sys/types.h> and <unistd.h>. In addition to pid, each process has a (real) user id and an effective user id. getuid: Returns the (real) user id of the process. geteuid: Returns effective user id of the process. (Recall: Setuid bit for executables.) No error exit for either function. 13 9 / 17

Additional notes about processes: A child process inherits parent s privileges and resources such as files. The child process competes for the CPU along with the parent. There are situations where the parent waits for the child to complete. Common example: Shell Each shell command, except the cd command, is executed by a child process of the shell. 13 10 / 17

System call wait: Prototype: pid_t wait (int *estatus) Headers: <sys/types.h> and <sys/wait.h>. Causes the caller to wait until some child terminates: synchronization. Normally, returns the pid of the child that terminated. If no child is waiting, the call returns -1 and errno has the value ECHILD. one form of If estatus is NULL, it is ignored; otherwise, the exit status of terminating child is returned in *estatus. (The exit status is 0 if the child terminated normally; nonzero otherwise.) Program Example: Handout 13.2. 13 11 / 17

System call waitpid: Prototype: pid_t waitpid (pid_t pid, int *estatus, int options) Headers: Same as wait. Causes the caller to wait until the child with id given by pid terminates. If pid is -1 and options is 0, then waitpid behaves exactly like wait. Most common value for options is WNOHANG. In that case, if the specified child is still running, the call returns 0 and the caller does not wait. Helpful when the parent process wants to perform some actions while the specified child is running. 13 12 / 17

Reading Assignment: Program example on page 107 of [HGS]. Two Special Processes: The swapper process: Pid = 0; the swapper is its own parent. The init process: Pid = 1; its parent is the swapper process. Orphan Process: A process which is still running but whose parent has terminated. Doesn t stay an orphan for too long. Orphan processes are adopted by the init process. 13 13 / 17

Zombie Process: Dictionary meaning of zombie : than alive. One who seems more dead In Unix, a zombie process is one which has terminated before its parent had a chance to wait for it. Example Code Segment: if ((cid = fork()) == 0) { -- code for child -- } else { -- parent with many lines of code-- c = wait(&status); } Note: Child may exit before parent reaches the wait call; that is, the child may become a zombie process. 13 14 / 17

Why are zombies bad? Kernel maintains a process table, with one entry per process. (The size of this table is the maximum number of processes allowed in the system.) When a process P terminates, the exit status of P must be conveyed to P s parent. The parent may be going through a long program before waiting for the child. So, some information about process P must be kept in the process table even though P can t execute anymore. The process table entry given to P can t be given to another process until P is completely dead (i.e., the exit status of P has been given to P s parent). 13 15 / 17

The exec family of system calls: Used in conjunction with fork to create processes executing different code. Traditional way: Two sets of calls: Child executes an appropriate exec call. execl and execv. execl: Used when the command line arguments are known at compile time and can be passed as a list. execv: Used to pass command line arguments as an array (similar to argv[]). Commonly used forms: execlp and execvp. The p suffix indicates that the call will search the directories in the PATH environment variable. 13 16 / 17

System calls execlp and execvp: Prototypes: int execlp( const char *prog, const char *arg0,..., const char *argn, NULL ) int execvp( const char *prog, const char *argv[] ) Header: <unistd.h>. Note that execlp has a variable number of arguments; the NULL pointer indicates the end of the list. Using exec is different from usual function calls; in particular, a call to exec should not return if there are no errors. Program Examples: Handouts 13.3 and 13.4. 13 17 / 17