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



Similar documents
Networks class CS144 Introduction to Computer Networking Goal: Teach the concepts underlying networks Prerequisites:

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

TCP/IP - Socket Programming

Socket Programming. Srinidhi Varadarajan

Socket Programming. Kameswari Chebrolu Dept. of Electrical Engineering, IIT Kanpur

Implementing Network Software

Socket Programming. Request. Reply. Figure 1. Client-Server paradigm

Socket Programming in C/C++

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Unix Network Programming

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

System Calls and Standard I/O

Communication Networks. Introduction & Socket Programming Yuval Rochman

NS3 Lab 1 TCP/IP Network Programming in C

Network Programming with Sockets. Process Management in UNIX

Tutorial on Socket Programming

Introduction to Socket programming using C

Lab 4: Socket Programming: netcat part

Programmation Systèmes Cours 9 UNIX Domain Sockets

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

How To Write On A Non-Blocking File On A Thumbtongue (Windows) (Windows 2) (Macintosh) (Amd64) (Apple) (Powerpoint) (Networking) (Unix) (Program) (

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

UNIX. Sockets. mgr inż. Marcin Borkowski

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

Overview. Socket Programming. Using Ports to Identify Services. UNIX Socket API. Knowing What Port Number To Use. Socket: End Point of Communication

BSD Sockets Interface Programmer s Guide

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

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

Application Architecture

Porting applications & DNS issues. socket interface extensions for IPv6. Eva M. Castro. ecastro@dit.upm.es. dit. Porting applications & DNS issues UPM

VMCI Sockets Programming Guide VMware ESX/ESXi 4.x VMware Workstation 7.x VMware Server 2.0

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

The POSIX Socket API

Writing a C-based Client/Server

Network Programming with Sockets. Anatomy of an Internet Connection

Implementing and testing tftp

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

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

Computer Networks Network architecture

UNIX Sockets. COS 461 Precept 1

KATRAGADDA INNOVATIVE TRUST FOR EDUCATION NETWORK PROGRAMMING. Notes prepared by D. Teja Santosh, Assistant Professor, KPES, Shabad, R.R. District.

Programmation Systèmes Cours 7 IPC: FIFO

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

Operating Systems. Privileged Instructions

Operating Systems Design 16. Networking: Sockets

System Calls Related to File Manipulation

LOW LEVEL FILE PROCESSING

Division of Informatics, University of Edinburgh

Packet Sniffing and Spoofing Lab

Unix System Calls. Dept. CSIE

DESIGN AND IMPLEMENT AND ONLINE EXERCISE FOR TEACHING AND DEVELOPMENT OF A SERVER USING SOCKET PROGRAMMING IN C

Lecture 16: System-Level I/O

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

Lecture 24 Systems Programming in C

Writing Client/Server Programs in C Using Sockets (A Tutorial) Part I. Session Greg Granger grgran@sas. sas.com. SAS/C & C++ Support

Giving credit where credit is due

CSE 333 SECTION 6. Networking and sockets

1 Posix API vs Windows API

Chapter 3. Internet Applications and Network Programming

University of Amsterdam

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

Generalised Socket Addresses for Unix Squeak

Operating Systems and Networks

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

Chapter 11. User Datagram Protocol (UDP)

SSL/TLS Programming. sslclient.c. /* A simple SSL client. It connects and then forwards data from/to the terminal to/from the server */

Objectives of Lecture. Network Architecture. Protocols. Contents

Concurrent Server Design Alternatives

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

Network programming, DNS, and NAT. Copyright University of Illinois CS 241 Staff 1

15-441: Computer Networks Homework 1

Using IPM to Measure Network Performance

1 Organization of Operating Systems

accf_smtp: FreeBSD kernel protection measures against SMTP DDoS and DoS attacks

Programming with TCP/IP Best Practices

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

Programming With Sockets 2

IOS Server Load Balancing

Process definition Concurrency Process status Process attributes PROCESES 1.3

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

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

Lab 2 : Basic File Server. Introduction

An Introductory 4.4BSD Interprocess Communication Tutorial

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

Network Programming. Writing network and internet applications.

Network File System (NFS)

Socket Programming in the Data Communications Laboratory

Transport Layer Protocols

Lecture 25 Systems Programming Process Control

An Advanced 4.4BSD Interprocess Communication Tutorial

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

ADH8060/8066 GSM/GPRS Module

CS 213, Fall 2000 Lab Assignment L5: Logging Web Proxy Assigned: Nov. 28, Due: Mon. Dec. 11, 11:59PM

IOS Server Load Balancing

File Transfer Protocol (FTP) Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Fall 2007, TAIWAN

Transcription:

System calls Problem: How to access resources other than CPU - Disk, network, terminal, other processes - CPU prohibits instructions that would access devices - Only privileged OS kernel can access devices Applications request I/O operations from kernel Kernel supplies well-defined system call interface - Applications set up syscall arguments and trap to kernel - Kernel performs operation and returns result Higher-level functions built on syscall interface - printf, scanf, gets, etc. all user-level code

I/O through the file system Applications open files/devices by name - I/O happens through open files int open(char *path, int flags,...); - flags: O RDONLY, O WRONLY, O RDWR - O CREAT: create the file if non-existent - O EXCL: (w. O CREAT) create if file exists already - O TRUNC: Truncate the file - O APPEND: Start writing from end of file - mode: final argument with O CREAT Returns file descriptor used for all I/O to file

Error returns What if open fails? Returns -1 (invalid fd) Most system calls return -1 on failure - Specific kind of error in global int errno #include <sys/errno.h> for possible values - 2 = ENOENT No such file or directory - 13 = EACCES Permission Denied perror function prints human-readable message - perror ("initfile"); initfile: No such file or directory

Operations on file descriptors int read (int fd, void *buf, int nbytes); - Returns number of bytes read - Returns 0 bytes at end of file, or -1 on error int write (int fd, void *buf, int nbytes); - Returns number of bytes written, -1 on error off t lseek (int fd, off t pos, int whence); - whence: 0 start, 1 current, 2 end - Returns previous file offset, or -1 on error int close (int fd); int fsync (int fd); - Guarantee that file contents is stably on disk

File descriptor numbers File descriptors are inherited by processes - When one process spawns another, same fds by default Descriptors 0, 1, and 2 have special meaning - 0 standard input (stdin in ANSI C) - 1 standard output (stdout, printf in ANSI C) - 2 standard error (stderr, perror in ANSI C) - Normally all three attached to terminal Example: type.c

The rename system call int rename (const char *p1, const char *p2); - Changes name p2 to reference file p1 - Removes file name p1 Guarantees that p2 will exist despite any crashes - p2 may still be old file - p1 and p2 may both be new file - but p2 will always be old or new file fsync/rename idiom used extensively - E.g., emacs: Writes file.#file# - Calls fsync on file descriptor - rename (".#file#", "file");

int fork (void); Creating processes - Create new process that is exact copy of current one - Returns process ID of new proc. in parent - Returns 0 in child int waitpid (int pid, int *stat, int opt); - pid process to wait for, or -1 for any - stat will contain exit value, or signal - opt usually 0 or WNOHANG - Returns process ID or -1 on error

Deleting processes void exit (int status); - Current process ceases to exist - status shows up in waitpid (shifted) - By convention, status of 0 is success, non-zero error int kill (int pid, int sig); - Sends signal sig to process pid - SIGTERM most common value, kills process by default (but application can catch it for cleanup ) - SIGKILL stronger, kills process always

Running programs int execve (char *prog, char **argv, char **envp); - prog full pathname of program to run - argv argument vector that gets passed to main - envp environment variables, e.g., PATH, HOME Generally called through a wrapper functions int execvp (char *prog, char **argv); - Search PATH for prog - Use current environment int execlp (char *prog, char *arg,...); - List arguments one at a time, finish with NULL Example: minish.c

Manipulating file descriptors int dup2 (int oldfd, int newfd); - Closes newfd, if it was a valid descriptor - Makes newfd an exact copy of oldfd - Two file descriptors will share same offset (lseek on one will affect both) int fcntl (int fd, F SETFD, int val) - Sets close on exec flag if val = 1, clears if val = 0 - Makes file descriptor non-inheritable by spawned programs Example: redirsh.c

Pipes int pipe (int fds[2]); - Returns two file descriptors in fds[0] and fds[1] - Writes to fds[1] will be read on fds[0] - When last copy of fds[1] closed, fds[0] will return EOF - Returns 0 on success, -1 on error Operations on pipes - read/write/close as with files - When fds[1] closed, read(fds[0]) returns 0 bytes - When fds[0] closed, write(fds[1]): - Kills process with SIGPIPE, or if blocked - Fails with EPIPE For example code, see pipesh.c on web site

Sockets: Communication between machines Datagram sockets: Unreliable message delivery - With IP, gives you UDP - Send atomic messages, which may be reordered or lost - Special system calls to read/write: send/recv Stream sockets: Bi-directional pipes - With IP, gives you TCP - Bytes written on one end read on the other - Reads may not return full amount requested must re-read

Socket naming Recall how TCP & UDP name communication endpoints - 32-bit IP address specifies machine - 16-bit TCP/UDP port number demultiplexes within host - Well-known services listen on standard ports: finger 79, HTTP 80, mail 25, ssh 22 - Clients connect from arbitrary ports to well known ports A connection can be named by 5 components - Protocol (TCP), local IP, local port, remote IP, remote port - TCP requires connected sockets, but not UDP

System calls for using TCP Client Server socket make socket bind assign address listen listen for clients socket make socket bind* assign address connect connect to listening socket accept accept connection *This call to bind is optional; connect can choose address & port.

Client interface struct sockaddr_in { short sin_family; /* = AF_INET */ u_short sin_port; /* = htons (PORT) */ struct in_addr sin_addr; char sin_zero[8]; } sin; int s = socket (AF_INET, SOCK_STREAM, 0); bzero (&sin, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_port = htons (13); /* daytime port */ sin.sin_addr.s_addr = htonl (IP_ADDRESS); connect (s, (sockaddr *) &sin, sizeof (sin));

Server interface struct sockaddr_in sin; int s = socket (AF_INET, SOCK_STREAM, 0); bzero (&sin, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_port = htons (9999); sin.sin_addr.s_addr = htonl (INADDR_ANY); bind (s, (struct sockaddr *) &sin, sizeof (sin)); listen (s, 5); for (;;) { socklen_t len = sizeof (sin); int cfd = accept (s, (struct sockaddr *) &sin, &len); /* cfd is new connection; you never read/write s */ do_something_with (cfd); close (cfd); }

A fetch-store server Clients sends commands, gets responses over TCP Fetch command - Command consists of string fetch\n - Response contains last contents of file stored there Store command - Command consists of store\n followed by file - Response is OK or ERROR What if server or network goes down during store? - Don t say OK until data safely on disk (c.f. email) Example: fetch store.c

EOF in more detail What happens at end of store? - Server receives EOF, renames file, responds OK - Client reads OK, after sending EOF so didn t close fd! int shutdown (int fd, int how); - Shuts down a socket w/o closing file descriptor - how: 0 = reading, 1 = writing, 2 = both - Note: Applies to socket, not descriptor so copies of descriptor (through dup or fork affected) - Note 2: With TCP, can t detect if other side shuts for reading Many network applications detect & use EOF - Common error: leaking file descriptor via fork, so not closed (and no EOF) when you exit

Using UDP Call socket with SOCK DGRAM, bind as before New system calls for sending individual packets - int sendto(int s, const void *msg, int len, int flags, const struct sockaddr *to, socklen t tolen); - int recvfrom(int s, void *buf, int len, int flags, struct sockaddr *from, socklen t *fromlen); - Must send/get peer address with each packet Example: udpecho.c Can use UDP in connected mode (Why?) - connect assigns remote address - send/recv syscalls, like sendto/recvfrom w/o last 2 args

Uses of connected UDP sockets Kernel demultplexes packets based on port - So can have different processes getting UDP packets from different peers - For security, ports < 1024 usually can t be bound - But can safely inherit UDP port below that connected to one particular peer Feedback based on ICMP messages (future lecture) - Say no process has bound UDP port you sent packet to... - With sendto, you might think network dropping packets - Server sends port unreachable message, but only detect it when using connected sockets

Performance definitions Bandwidth Number of bits/time you can transmit - Improves with technology Latency How long for message to cross network - Propagation + Transmit + Queue - We are stuck with speed of light... 10s of milliseconds to cross country Throughput TransferSize/Latency Jitter Variation in latency What matters most for your application?

Small request/reply protocol Client Server request reply Small message protocols typically dominated by latency

Large reply protocol Client request Server reply For bulk tranfer, throughput is most important

Bandwidth-delay Delay Bandwidth Can view network as a pipe - For full utilization want bytes in flight bandwidth delay - But don t want to overload the network (future lectures) What if protocol doesn t involve bulk transfer? - Get throughput through concurrency service multiple clients simultaneously

Traditional fork-based servers When is a server not transmitting data - Read or write of a socket connected to slow client can block - Server may be busy with CPU (e.g., computing response) - Server might be blocked waiting for disk I/O Can gain concurrency through multiple processes - Accept, fork, close in parent; child services request Advantages of one process per client - Don t block on slow clients - May scale to multiprocessors if CPU intensive - For disk-heavy servers, keeps disk queues full (similarly get better scheduling & utilization of disk)

Threads One process per client has disadvantages: - High overhead fork+exit 100 µsec - Hard to share state across clients - Maximum number of processes limited Can use threads for concurrency - Data races and deadlock make programming tricky - Must allocate one stack per request - Many thread implementations block on some I/O or have heavy thread-switch overhead

Non-blocking I/O fcntl sets O NONBLOCK flag on descriptor int n; if ((n = fcntl (s, F_GETFL)) >= 0) fcntl (s, F_SETFL, n O_NONBLOCK); Non-blocking semantics of system calls: - read immediately returns -1 with errno EAGAIN if no data - write may not write all data, or may return EAGAIN - connect may fail with EINPROGRESS (or may succeed, or may fail with real error like ECONNREFUSED) - accept may fail with EAGAIN if no pending connections

How do you know when to read/write? struct timeval { long tv_sec; /* seconds */ long tv_usec; /* and microseconds */ }; int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); FD_SET(fd, &fdset); FD_CLR(fd, &fdset); FD_ISSET(fd, &fdset); FD_ZERO(&fdset);