Operating Systems, 112 Practical Session 1, System Calls.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Operating Systems, 142 Tas: Vadim Levit, Dan Brownstein, Ehud Barnea, Matan Drory and Yerry Sofer Practical Session 1, System Calls.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Creating and Executing Processes
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
More on UART Interrupts; System Calls Reference on Interrupt Identification Register(IIR) slide 17 of
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Process Management Azzam Mourad COEN 346.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
System calls for Process management Process creation, termination, waiting.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Operating Systems 162 Practical Session 1 System Calls.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Error handling I/O Man pages
Practical Session 1 System Calls
Operating Systems Moti Geva
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Unix Process Management
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
Processes in Unix, Linux, and Windows
Operating Systems for Computer Engineering 151
Fork and Exec Unix Model
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Operating Systems, 112 Practical Session 1, System Calls

System Calls user application kernel A System Call is an interface between a user application and a service provided by the operating system (or kernel). These can be roughly grouped into five major categories: 1.Process control (e.g. create/terminate process) 2.File Management (e.g. read, write) 3.Device Management (e.g. logically attach a device) 4.Information Maintenance (e.g. set time or date) 5.Communications (e.g. send messages)

System Calls - motivation A process is not supposed to access the kernel. It can’t access the kernel memory or functions. This is strictly enforced (‘protected mode’) for good reasons: Can jeopardize other processes running. Cause physical damage to devices. Alter system behavior. The system call mechanism provides a safe mechanism to request specific kernel operations.

System Calls - interface Calls are usually made with C/C++ library functions: User ApplicationC - LibraryKernelSystem Call getpid()Load arguments, eax  _NR_getpid, kenel mode (int 80) Call Sys_Call_table[eax] sys_getpid() return syscall_exit resume_userspace return User-SpaceKernel-Space Remark: Invoking int 0x80 is common although newer techniques for “faster” control transfer are provided by both AMD’s and Intel’s architecture. Kernel 2.5 of Linux began using this approach when available.

System Calls – tips Kernel behavior can be enhanced by altering the system calls themselves: imagine we wish to write a message (or add a log entry) whenever a specific user is opening a file. We can re-write the system call open with our new open function and load it to the kernel (need administrative rights). Now all “open” requests are passed through our function. We can examine which system calls are made by a program by invoking strace.

Process control Fork pid_t fork(void); Fork is used to create a new process. It creates an exact duplicate of the original process (including all file descriptors, registers, instruction pointer, etc’…). Subsequent changes to one should not effect the other. Once the call is finished, the process and its copy go their separate ways. Subsequent changes to one should not effect the other. The fork call returns a different value to the original process (parent) and its copy (child): in the child process this value is zero, and in the parent process it is the PID of the child process. When fork is invoked the parent’s information should be copied to its child – however, this can be wasteful if the child will not need this information (see exec()…). To avoid such situations, Copy On Write (COW) is used for the data section.

Copy On Write (COW) How does Linux manage COW? Parent Process Parent Process DATA STRUCTURE (task_struct) RW Child Process Child Process DATA STRUCTURE (task_struct) Child Process DATA STRUCTURE (task_struct) RO RO fork() Copying is expensive. The child process will point to the parent’s pages write information protection fault! Well, no other choice but to allocate a new RW copy of each required page RW

Process control An example: int i = 3472; printf("my process pid is %d\n",getpid()); fork_id=fork(); if (fork_id==0){ i= 6794; printf(“child pid %d, i=%d\n",getpid(),i); } else printf(“parent pid %d, i=%d\n",getpid(),i); return 0; Output: my process pid is 8864 child pid 8865, i=6794 parent pid 8864, i=3472 Program flow: i = 3472 fork_id=0 i = 6794 PID = 8864 PID = 8865 fork () fork_id = 8865 i=3472 Is this the only possible output? Running the above code on some systems will almost always return this value. Why?

Process control - zombies When a process ends, the memory and resources associated with it are deallocated. However, the entry for that process is not removed from its parent process table. This allows the parent to collect the child’s exit status. When this data is not collected by the parent the child is called a “zombie”. Such a leak is usually not worrisome in itself, however, it is a good indicator for problems to come.

Process control - zombies In some (rare) occasions, a zombie is actually desired – it may, for example, prevent the creation of another child process with the same pid. Zombies are not the same as orphan processes (a process whose parent ended and is then adopted by init (process id 1)). Zombies can be detected with ps –el (marked with ‘Z’). Zombies can be collected with the wait system call.

Process control Wait pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); The wait command is used for waiting on child processes whose state changed (the process terminated, for example). The process calling wait will suspend execution until one of its children (or a specific one) terminates. Waiting can be done for a specific process, a group of processes or on any arbitrary child with waitpid. Once the status of a process is collected that process is removed from the process table of the collecting process. Kernel and later also introduced waitid(…) which gives finer control.

Process control exec* int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); exec…. The exec() family of function replaces current process image with a new process image (text, data, bss, stack, etc). Since no new process is created, PID remains the same. Exec functions do not return to the calling process unless an error occurred (in which case -1 is returned and errno is set with a special value). The system call is execve(…)

errno The header file includes the integer errno variable. This variable is set by many functions (including sys calls) in the event of an error to indicate what went wrong. errnos value is only relevant when the call returned an error (usually -1). A successful call to a function may also change the errno value. errno may be a macro. errno is thread local meaning that setting it in one thread does not affect its value in any other thread. Be wary of mistakes such as: If (call()==-1){ printf(“failed…”); if (errno==…..) } Code defensively! Use errno often!

Process control – simple shell #define… … int main(int argc, char **argv){ … while(true){ type_prompt(); read_command(command, params); pid=fork(); if (pid<0){ if (errno==EAGAIN) printf(“ERROR can not allocate sufficient memory\n”); coutinue; } if (pid>0) wait(&status); else execvp(command,parmas); }

File management In POSIX operating systems files are accessed via a file descriptor (Microsoft Windows uses a slightly different object: file handle). A file descriptor is an integer specifying the index of an entry in the file descriptor table held by each process. A file descriptor table is held be each process, and contains details of all open files. The following is an example of such a table: File descriptors can refer to files, directories, sockets and a few more data objects. FDNameOther information 0Standard Input (stdin)… 1Standard Output (stdout)… 2Standard Error (stderr)…

File management Open int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); Open returns a file descriptor for a given pathname. This file descriptor will be used in subsequent system calls (according to the flags and mode) Flags define the access mode: O_RDONLY (read only), O_WRONLY (write only), O_RDRW (read write). These can be bit-wised or’ed with more creation and status flags such as O_APPEND, O_TRUNC, O_CREAT. Close Int close(int fd); Closes a file descriptor so it no longer refers to a file. Returns 0 on success or -1 in case of failure (errno is set).

File management Read ssize_t read(int fd, void *buf, size_t count); Attempts to read up to count bytes from the file descriptor fd, into the buffer buf. Returns the number of bytes actually read (can be less than requested if read was interrupted by a signal, close to EOF, reading from pipe or terminal). On error -1 is returned (and errno is set). Note: The file position advances according to the number of bytes read. Write ssize_t write(int fd, const void *buf, size_t count); Writes up to count bytes to the file referenced to by fd, from the buffer positioned at buf. Returns the number of bytes actually wrote, or -1 (and errno) on error.

File management lseek off_t lseek(int fildes, off_t offset, int whence); This function repositions the offset of the file position of the file associated with fildes to the argument offset according to the directive whence. Whence can be set to SEEK_SET (directly to offset), SEEK_CUR (current+offset), SEEK_END (end+offset). Positioning the offset beyond file end is allowed. This does not change the size of the file. Writing to a file beyond its end results in a “hole” filled with ‘\0’ characters (null bytes). Returns the location as measured in bytes from the beginning of the file, or -1 in case of error (and set errno).

File management Dup int dup(int oldfd); int dup2(int oldfd, int newfd); The dup commands create a copy of the file descriptor oldfd. After a successful dup command is executed the old and new file descriptors may be used interchangeably. They refer to the same open file descriptions and thus share information such as offset and status. That means that using lseek on one will also affect the other! They do not share descriptor flags (FD_CLOEXEC). Dup uses the lowest numbered unused file descriptor, and dup2 uses newfd (closing current newfd if necessary). Returns the new file descriptor, or -1 in case of an error (and set errno).

File management Consider the following example: fileFD= open(“file.txt”…); close(1); /* closes file handle 1, which is stdout.*/ fd =dup(fileFD); /* will create another file handle. File handle 1 is free, so it will be allocated. */ close(fileFD); /* don’t need this descriptor anymore.*/ printf(“this did not go to stdout”); As a result (abstract): 0stdin… 1stdout… 2stderr… 3file.txt… 0stdin… 1file.txt… 2stderr… A similar idea can be useful for IPC (with pipe(…))

File management - example #define… … #define RW_BLOCK 10 int main(int argc, char **argv){ int fdsrc, fddst; ssize_t readBytes, wroteBytes; char *buf[RW_BLOCK]; char *source = argv[1]; char *dest = argv[2]; fdsrc=open(source,O_RDONLY); if (fdsrc<0){ perror("ERROR while trying to open source file:"); exit(-1); } fddst=open(dest,O_RDWR|O_CREAT|O_TRUNC, 0666); if (fddst<0){ perror("ERROR while trying to open destination file:"); exit(-2); } perror() produces a message on the standard error output describing the last error encountered during a call to a system call. Use with care: the message is not cleared when non erroneous calls are made. Bitwise OR: open for both reading and writing, if the file does not exist create it and always start at 0. exit() system call.

File management - example lseek(fddst,20,SEEK_SET); do{ readBytes=read(fdsrc, buf, RW_BLOCK); if (readBytes<0){ if (errno == EIO){ printf("I/O errors detected, aborting.\n"); exit(-10); } exit (-11); } wroteBytes=write(fddst, buf, readBytes); if (wroteBytes<RW_BLOCK) if (errno == EDQUOT) printf("ERROR: out of quota.\n"); else if (errno == ENOSPC) printf("ERROR: not enough disk space.\n"); } while (readBytes>0); lseek(fddst,0,SEEK_SET); write(fddst,"\\*WRITE START*\\\n",16); close(fddst); close(fdsrc); return 0; } Start writing at offset 20. If the file is opened with hexedit, the first 20 bytes will be 00. Using errno directly. Adding an extra comment at the beginning of the file.

Fork – example (1) How many lines of “Hello” will be printed in the following example: int main(int argc, char **argv){ int i; for (i=0; i<10; i++){ fork(); printf(“Hello \n”); } return 0; }

Fork – example (1) How many lines of “Hello” will be printed in the following example: int main(int argc, char **argv){ int i; for (i=0; i<10; i++){ fork(); printf(“Hello \n”); } return 0; } Program flow: Total number of printf calls: i=0 i=1 i=2

Fork – example (2) How many lines of “Hello” will be printed in the following example: int main(int argc, char **argv){ int i; for (i=0; i<10; i++){ printf(“Hello \n”); fork(); } return 0; }

Fork – example (2) How many lines of “Hello” will be printed in the following example: int main(int argc, char **argv){ int i; for (i=0; i<10; i++){ printf(“Hello \n”); fork(); } return 0; } Program flow: Total number of printf calls: i=0 i=1 i=2

Fork – example (3) How many lines of “Hello” will be printed in the following example: int main(int argc, char **argv){ int i; for (i=0; i<10; i++) fork(); printf(“Hello \n”); return 0; }

Fork – example (3) How many lines of “Hello” will be printed in the following example: int main(int argc, char **argv){ int i; for (i=0; i<10; i++) fork(); printf(“Hello \n”); return 0; } Program flow: Total number of printf calls: i=0 i=1 i=2

Tips Information sources are abundant: The internet. Man pages (apropos). In Linux it is often useful (and easy) to examine the included header files. You can easily find their location by using the whereis command (you may also find which useful). MSDN – this is less relevant to our course, but it also includes code examples. A list of system calls on CS dept. computers: /usr/include/asm/unistd_32.h