<<

Operating Systems

Lecture 06

System Calls (, , , ) Inter- Communication in / (PIPE), Use of PIPE on command line

March 04, 2013 exec()

• Typically the exec is used after a system call by one of the two processes to replace the process’ memory space with a new executable program. • The new process image is constructed from an ordinary, executable file. exec()

• There can be no return from a successful exec because the calling process image is overlaid by the new process image Synopsis of exec()

#include int execlp (const char *file, const char *arg0, ..., const char *argn, (char *)0); Sample Code—fork and exec

#include void main() { int pid, status;

pid = fork(); if(pid == -1) { printf(“fork failed\n”); exit(1); } Sample Code—fork and exec

if(pid == 0) { /* Child */ if (execlp(“/bin/ls”, “ls”, NULL)< 0) { printf(“exec failed\n”); exit(1); } } else { /* Parent */ wait(&status); printf(“Well done kid!\n”); exit(0); } } Semantics of fork parent parent parent

P P P

fork

P P ls ls exec

child child child Cooperating Processes

• Independent process cannot affect or be affected by the execution of another process. • Cooperating process can affect or be affected by the execution of another process Cooperating Processes

• Advantages of process cooperation • Information sharing • Computation speed-up • Modularity • Convenience Producer-Consumer Problem

• Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. • unbounded-buffer places no practical limit on the size of the buffer • bounded-buffer assumes that there is a fixed buffer size Bounded-Buffer Problem

Empty Pool

ProducerProducer ConsumerConsumer

Full Pool Interprocess Communication (IPC)

• Mechanism for processes to communicate and to synchronize their actions. • Message system – processes communicate with each other without resorting to shared variables. Interprocess Communication (IPC)

• IPC facility provides two operations: • Send (message) – message size fixed or variable • Receive (message) Interprocess Communication (IPC)

• If P and Q wish to communicate, they need to: • establish a communication link between them • exchange messages via send/receive Interprocess Communication (IPC)

• Implementation of communication link • physical (.g., shared memory, hardware bus) • logical (e.g., logical properties) Implementation Questions

• How are links established? • Can a link be associated with more than two processes? • How many links can be there between every pair of communicating processes? Implementation Questions

• What is the capacity of a link? • Is the size of a message that the link can accommodate fixed or variable? • Is a link unidirectional or bi-directional? Direct Communication

• Processes must name each other explicitly: • send (P, message) – send a message to process P • Receive (Q, message) – receive a message from process Q Direct Communication

• Properties of communication link • Links are established automatically. • A link is associated with exactly one pair of communicating processes. • Between each pair there exists exactly one link. • The link may be unidirectional, but is usually bi- directional. Indirect Communication

• Messages are directed and received from mailboxes (also referred to as ports). • Each mailbox has a unique id. • Processes can communicate only if they share a mailbox. Indirect Communication …

• Properties of communication link • Link established only if processes share a common mailbox • A link may be associated with many processes. • Each pair of processes may share several communication links. • Link may be unidirectional or bi-directional. Indirect Communication …

• Operations • create a new mailbox • send and receive messages through mailbox • destroy a mailbox • Primitives are defined as: send (A, message) receive (A, message) Indirect Communication …

Mailbox sharing

• P1, P2, and P3 share mailbox A.

• P1, sends; P2 and P3 receive. • Who gets the message? Indirect Communication …

Solutions • Allow a link to be associated with at most two processes. • Allow only one process at a time to execute a receive operation. • Allow the system to arbitrarily the receiver. Sender is notified who the receiver was. Synchronization

• Message passing may be either blocking or non- blocking. •Blockingis considered synchronous •Non-blockingis considered asynchronous •sendand receive primitives may be either blocking or non-blocking. Buffering

• Queue of messages attached to the link; implemented in one of three ways. Zero capacity – No messages Sender must wait for receiver Bounded capacity – n messages Sender must wait if link full. Unbounded capacity – infinite length Sender never waits. File Descriptors in UNIX/Linux

• An integer returned by open() system call • Used as an index in the per process file descriptor table (PPFDT) • Used in read, write, and calls File Descriptors in UNIX/Linux

• Size of PPFDT is equal to the number of files that a process can open simultaneously (OPEN_MAX in Linux—see • Used as an index in the per process file descriptor table (PPFDT) • Used in read, write, and close calls UNIX/Linux IPC Tools

• Pipe: For communication between related processes on a system

P1 P2

Pipe

UNIX/Linux System UNIX/Linux IPC Tools

(FIFO): For communication between related or unrelated processes on a system

P1 P2

FIFO

UNIX/Linux System UNIX/Linux IPC Tools

• Socket: For communication between related or unrelated processes on the same or different systems

P1 P2

Network Connection Socket Socket

Computer Computer 1 2 UNIX/Linux Pipe

• Important system calls open, read, write, close, pipe • open: Open or create a file • read: Read from a pipe • write: Write data to a pipe • close: Close/destroy a pipe • pipe: Create a pipe for IPC open System Call

#include #include #include int open (const char *, int oflag, /* mode_t mode */ ...); • ‘oflag’ specifies purpose of opening the file and ‘mode’ specifies permission on the file if it is to be created. open System Call

• Returns a file descriptor on success and –1 on failure • Can specify that read and write will be blocking or non-blocking • ‘oflag’ value is constructed by ORing various flags: O_RDONLY, O_WRONLY, O_RDWR, O_NDELAY (or O_NONBLOCK), O_APPEND, O_CREAT, etc. open System Call

• Call fails • Non-existent file • Operation specified is not allowed due to file permissions • Search not allowed on a component of pathname • User’s disk quota on the has been exhausted open System Call

• Call fails • No write permission on the in which the file is being created • Signal was caught during open • Process has reached the limit of maximum open files • System limit reached on maximum number of simultaneous open files read System Call

#include #include #include ssize_t read(int fildes, void *buf, size_t nbyte);

• Returns number of bytes read or -1 • Call fails and errno set accordingly • Invalid ‘fildes’, ‘buf’, or ‘nbyte’ • Signal caught during read write System Call

#include #include ssize_t write (int fildes, const void *buf, size_t nbyte); • Returns the number of bytes written or -1 write System Call

• Call fails • Invalid argument • limit for process or for system would exceed • Disk is full File Descriptors in UNIX/Linux

• An integer returned by open() system call • Used as an index in the per process file descriptor table (PPFDT) • Used in read, write, and close calls File Descriptors in UNIX/Linux

• Size of PPFDT is equal to the number of files that a process can open simultaneously (OPEN_MAX in Linux—see File Descriptor to File Contents

Per Process File File Descriptor Table File Descriptor Table Table

0 1 File’s 2 contents 3 … 4 … … … …

OPEN_MAX — 1 Questions..??