Friday, June 09, 2006 “I think there is a world market for maybe five computers”. - Thomas Watson, Chairman of IBM, 1943.

Slides:



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

15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
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.
Project 1. Process ID (pid) Synopsis #include pid_t getpid(void) – returns the pid of the currently running process. pid_t getppid(void) – returns the.
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.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
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.
Chapter 4 UNIX I/O Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
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.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
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
Unix System Calls Gwan-Hwan Hwang Dept. CSIE National Taiwan Normal University
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Chapter 6 UNIX Special Files Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
System calls for Process management
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
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.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
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.
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.
OPERATING SYSTEMS 3 - PROCESSES PIETER HARTEL 1. Principle of concurrency - giving a process the illusion that it owns the whole machine  A process has:
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.
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
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.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
CSCI 4061 Recitation 2 1.
Protection of System Resources
Using Processes.
Unix Process Management
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
More on UART Interrupts; System Calls
Recitation 9: Tshlab + VM
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Presentation transcript:

Friday, June 09, 2006 “I think there is a world market for maybe five computers”. - Thomas Watson, Chairman of IBM, 1943

System Calls Example : §UNIX read system call l Highly machine dependant (Assembly code) l We can make system calls through C program using library procedures.

System Calls §UNIX has an almost 1-1 relationship between system calls and library procedures §open §read §write §close POSIX defines about 100 procedure calls

Library procedures: §A procedure call may itself make a system call e.g. fopen malloc

§In Windows the library calls and actual system calls are decoupled §Win32 API calls number in thousands. §Large number of calls or managing windows, menus, scroll bars, dialog boxes, fonts, text… l Not very clear which are system calls and which are user-space library calls

Process Management  fork creates a new process in Unix §It creates an exact duplicate of the original parent process including file descriptors, registers, etc (everything) §All variables have identical values at the time of fork l Remember: Parent’s data is copied l Subsequent changes do not affect each other

Process Management int pid = fork(); §pid contains zero in child process §pid contains the process id of child in parent

int main(void){ int counter=0, i, statloc, pid; pid=fork(); if (pid==-1){ perror("fork"); exit(1); } else if (pid==0){ counter=100; for(i=0;i<5; i++){ printf("Child counter is %d\n", counter); counter++; sleep(2); }

else { counter=500; for(i=0;i<5; i++){ printf("Parent counter is %d\n", counter); counter++; sleep(2); } waitpid(pid, &statloc, 0); } return 0; }

Parent counter is 500 Child counter is 100 Child counter is 101 Parent counter is 501 Parent counter is 502 Child counter is 102 Child counter is 103 Parent counter is 503 Parent counter is 504 Child counter is 104 One Possible Output

Parent counter is 500 Child counter is 100 Child counter is 101 Parent counter is 501 Parent counter is 502 Child counter is 102 Parent counter is 503 Child counter is 103 Child counter is 104 Parent counter is 504 Another possible output

sleep suspends execution for an interval waitpid can wait for a specific child or any child by setting first parameter to -1 man sleep

int main(void){ int counter=0, i, statloc, pid; pid=fork(); if (pid==-1){ perror("fork"); exit(1); } else if (pid==0){ counter=100; for(i=0;i<5; i++){ printf("Child counter is %d\n", counter); counter++; sleep(2); }

else { waitpid(pid, &statloc, 0); counter=500; for(i=0;i<5; i++){ printf("Parent counter is %d\n",counter); counter++; sleep(2); } return 0; }

Child counter is 100 Child counter is 101 Child counter is 102 Child counter is 103 Child counter is 104 Parent counter is 500 Parent counter is 501 Parent counter is 502 Parent counter is 503 Parent counter is 504

int main(void){ int i; int* counter; int pid; counter=(int*) malloc(sizeof(int)); pid=fork(); if (pid==-1){ perror("fork"); exit(1); } else if (pid==0){ *counter=100; for(i=0;i<5; i++){ printf("Child counter is %d\n", *counter); (*counter)++; sleep(2); }

else { *counter=500; for(i=0;i<5; i++){ printf("Parent counter is %d\n",*counter); (*counter)++; sleep(2); } waitpid(pid, &statloc, 0); } return 0; }

Parent counter is 500 Child counter is 100 Child counter is 101 Parent counter is 501 Parent counter is 502 Child counter is 102 Child counter is 103 Parent counter is 503 Parent counter is 504 Child counter is 104 One Possible Output

exec system call replaces the current process image with a new process image.

int main(int argc, char *argv[]) { int i; int pid; int child_status; pid = fork(); if (pid<0){ printf("Fork Failed"); exit(-1); } else if (pid==0){ printf("Child first line\n"); execlp("/bin/ls","ls",”-l”,NULL); }

else { wait(&child_status); printf("Child Complete\n"); } return 0; }

Child first line -rwxr-xr-x 1 username group 9203 Jun 4 09:43 fork-ex -rw-r--r-- 1 username group 348 Jun 4 09:43 fork-ex.c -rw-r--r-- 1 username group 355 May 6 14:44 ex1.c -rw-r--r-- 1 username group 657 May 6 17:13 ex2.c -rw-r--r-- 1 username group 657 May 6 17:00 ex3.c Child Complete Output

int main(int argc, char *argv[]){ int i, pid, pid2; int child_status; pid = fork(); if (pid==0){ pid2=fork(); if (pid2==0){ printf("%d GChild first line\n", getpid()); } else{ printf("%d Child first line\n", getpid()); waitpid(pid2, &child_status, 0); } else { printf("%d Parent first line\n", getpid()); waitpid(pid, &child_status, 0); } return 0;}

13242 GChild first line Parent first line Child first line One Possible Output

13243 Parent first line Child first line GChild first line Another Possible Output

int main(int argc, char *argv[]){ int pid; int child_status; pid = fork(); if (pid<0){ printf("Fork Failed"); exit(-1); } else if (pid==0){ printf("Child first line\n"); execlp("/bin/cal","cal",NULL); //what happens here? printf("Child second line\n"); printf("Child third line\n"); }

else { //what if we move wait statement down? wait(&child_status); printf("Parent line 1\n"); printf("Parent line 2\n"); printf("Parent line 3\n"); } return 0; }

Child first line June 2006 S M Tu W Th F S Parent line 1 Parent line 2 Parent line 3 OUTPUT

Simple shell while(1) { type_prompt(); read_command(command, parameters); if (fork()!=0){ waitpid(-1, &status, 0); } else { exec(command, parameters, 0); }

Signals §Notification sent to a process in order to notify it of important events. §Each signal has an integer number (as well as symbolic name) that represents it

Signals §Why signals?

int main(int argc, char *argv[]) { int pid; int i; signal(SIGCHLD, catch_child); pid=fork(); if (pid==0){ execlp("/bin/ls", "ls", NULL); } for (i=0; i<10; i++){ printf("%d\n", i); sleep(1); } return 0; }

void catch_child(int sig_num) { int child_status; wait(&child_status); printf("child exited with code %d\n", child_status); }

int main(int argc, char *argv[]) { int i; int pid, pid2, pid3; int child_status; pid = fork(); //pid contains zero for child process if (pid==0){ pid2=fork(); if (pid2==0){ printf("hello"); } else{ printf("there"); wait(&child_status); } else { pid3=fork(); if (pid3==0){ printf("Good"); } else{ printf("Luck"); wait(&child_status); } return 0; }

Process Termination §Last instruction §exit() system call

After fork() call: § The child process inherits all files that were open in the parent process. If one of them reads from such an open file, the read/write pointer is advanced for both of them. §If one process closes a shared file, it is still kept open in the other process. § Files opened after the call to fork() are not shared by both processes.

§Ctrl-C SIGINT §Ctrl-Z SIGTSTP §Default signal handlers §SIGSEGV – Segmentation violation – Core dumped

Comparison

System Calls for I/O § 5 basic UNIX system calls for file I/O open close read write lseek §Different from regular procedure calls. How?

open System Call open ( path, mode flags) int fd = open("/dir1/file1", O_RDONLY); int fd = open("/dir1/file1", O_WRONLY | O_APPEND);

System Calls for I/O §Every process in Unix starts out with three file descriptors §file descriptor 0 is standard input §file descriptor 1 is standard output §file descriptor 2 is standard error §read(0, …) //read from standard input §write(1, …) //write to standard output

open System Call §O_RDONLY l Open the file in read-only mode. §O_WRONLY l Open the file in write-only mode. §O_RDWR l Open the file for both reading and writing. §In addition, any of the following flags may be OR-ed with the mode flag: §O_CREAT l If the file does not exist already - create it.

open System Call §O_EXCL If used together with O_CREAT, the call will fail if the file already exists. §O_TRUNC l If the file already exists, truncate it (i.e. erase its contents). §O_APPEND l Open the file in append mode. Any data written to the file is appended at the end of the file.

close System Call close (filedescriptor) if (close(fd) == -1) { perror("close"); exit(1); }

read System Call int num_read = read(filedescriptor, buffer, nbytes); § num_read == 0 ? § num_read < 0 ?

write System Call int num = write(filedescriptor, buffer, nbytes); § num < 0 ?

File permissions access( “/dir1/file1”, R_OK) ==0 read access granted access( “/dir1/file1”, R_OK | W_OK ) ==0 read/ write access granted §R_OK Test for read permission. §W_OK Test for write permission. § X_OK Test for execute or search permission. § F_OK Check existence of file

File permissions chmod( “/dir1/file1”, S_IRGRP)

ps -ef | grep yourusername ps -ef | grep init execlp("/bin/ls","ls","-l",”..”, NULL); cat filea fileb filec | sort > /dev/lp & man -s 2 read man –a read gcc -g -Wall fork-ex.c -o fork-ex It is good practice to always use the -Wall option

lseek System Call int num = lseek( filedescriptor, offset, whence); § num < 0 ? Practice Homework

open System Call §S_IRWXU l Owner of the file has read, write and execute permissions to the file. §S_IRUSR l Owner of the file has read permission to the file. §S_IWUSR l Owner of the file has write permission to the file. §S_IXUSR l Owner of the file has execute permission to the file. Practice Homework

open System Call §S_IRWXG l Group of the file has read,write and execute permissions to the file. §S_IRGRP l Group of the file has read permission to the file. §S_IWGRP l Group of the file has write permission to the file. §S_IXGRP l Group of the file has execute permission to the file. Practice Homework

open System Call §S_IRWXO l Other users have read,write and execute permissions to the file. §S_IROTH l Other users have read permission to the file. §S_IWOTH l Other users have write permission to the file. §S_IXOTH l Other users have execute permission to the file. Practice Homework

Directories #include /* open the directory for reading. */ DIR* dir = opendir("/dir1/subdir2/"); if (!dir) { perror("opendir"); exit(1); } if (closedir(dir) == -1) { perror("closedir"); exit(1); } Practice Homework

Directories struct dirent* entry; while ( (entry = readdir(dir)) != NULL) { printf("%s\n", entry->d_name); } Practice Homework

void function(DIR* dirp, char* dir_path){ struct dirent* dir_entry; char* full_path; while ( (dir_entry = readdir(dirp)) != NULL) { if (strcmp(dir_entry->d_name, ".") == 0) continue; if (strcmp(dir_entry->d_name, "..") == 0) continue; full_path=(char*)malloc(strlen(dir_path) +strlen(dir_entry->d_name)+1); strcpy(full_path, dir_path); strcat(full_path, dir_entry->d_name); printf("dir_entry->d_name = %s\n",dir_entry->d_name); if (access(full_path, R_OK) == -1) { printf("read permission to file denied.\n"); return; } free(full_path); } Practice Homework

File status struct stat file_status stat( “/dir1/file1”, &file_status) if (S_ISDIR(file_status.stmode)) if (S_ISFIFO(file_status.stmode)) Practice Homework