Userland summary Nezer J. Zaidenberg. Today’s topics What have we learned - USERLAND summary Revisited topics execXXX functions POSIX cond Sync methods.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Userland summary Nezer J. Zaidenberg. Today’s topics What have we learned - USERLAND summary Revisited topics execXXX functions POSIX cond Daemons Sync.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Programming with POSIX* Threads Intel Software College.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
System calls for Process management
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.
Consider Letting inetd Launch Your Application. inetd daemon  Problems starting with /etc/rc(without inet daemon)  All the servers contains nearly identical.
Operating Systems Process Creation
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
CSCI 330 UNIX and Network Programming
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Principles of Operating Systems Lecture 11
Threads Threads.
Netprog: Threads Programming
Using Processes.
Unix Process Management
Processes in Unix, Linux, and Windows
System Structure and Process Model
System Structure and Process Model
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Fork and Exec Unix Model
System Structure B. Ramamurthy.
EECE.4810/EECE.5730 Operating Systems
Lecture 14: Pthreads Mutex and Condition Variables
Processes in Unix, Linux, and Windows
System Structure and Process Model
Tutorial 3 Tutorial 3.
Unix System Calls and Posix Threads
Issues in Client/Server Programming
Advanced UNIX progamming
Jonathan Walpole Computer Science Portland State University
Lecture 6: Multiprogramming and Context Switching
Lecture 14: Pthreads Mutex and Condition Variables
Realizing Concurrency using Posix Threads (pthreads)
Processes in Unix, Linux, and Windows
EECE.4810/EECE.5730 Operating Systems
Process Description and Control in Unix
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
POSIX Threads(pthreads)
System Programming: Process Management
Presentation transcript:

Userland summary Nezer J. Zaidenberg

Today’s topics What have we learned - USERLAND summary Revisited topics execXXX functions POSIX cond Sync methods we learned EX 2 Pseudo code What have we learned - USERLAND summary Revisited topics execXXX functions POSIX cond Sync methods we learned EX 2 Pseudo code

Topics we covered - Processes We learned how to create process (fork(2)) execute a run time (execXXX(2)) How to wait(2) and waitpid(2) for process We tried 2 methods of Inter process communications IPC (network and UNIX domain sockets) - THERE ARE MANY MORE We understand process environment - File descriptor table, environment (getenv(2)), heap, stack, global variables Daemon process Signals (sigaction(2)/signal(3)) We learned how to create process (fork(2)) execute a run time (execXXX(2)) How to wait(2) and waitpid(2) for process We tried 2 methods of Inter process communications IPC (network and UNIX domain sockets) - THERE ARE MANY MORE We understand process environment - File descriptor table, environment (getenv(2)), heap, stack, global variables Daemon process Signals (sigaction(2)/signal(3))

Topics we covered - Threads We learned to create POSIX threads We learned to join threads We learned to sync threads using POSIX cond POSIX mutex We know what re-enterant and non-reenterant function is (strtok(3) and strtok_r(3)) We discussed (in lecture) some syncing algorithms We learned to create POSIX threads We learned to join threads We learned to sync threads using POSIX cond POSIX mutex We know what re-enterant and non-reenterant function is (strtok(3) and strtok_r(3)) We discussed (in lecture) some syncing algorithms

Topics we covered - Files We know what file descriptor is (and to dup(2) it) We know how to open(2) and close(2) fd We know how to read(2) write(2) and lseek(2) files We know how to stat(2) to get file info We know how to unlink(2) to delete file or UDS. We know how to mmap(2) munmap(2) and msync(2) files We also know open/scan/readdir(2) API We know what file descriptor is (and to dup(2) it) We know how to open(2) and close(2) fd We know how to read(2) write(2) and lseek(2) files We know how to stat(2) to get file info We know how to unlink(2) to delete file or UDS. We know how to mmap(2) munmap(2) and msync(2) files We also know open/scan/readdir(2) API

Topics we covered - networking Behavior of AF_INET, AF_UNIX SOCK_STREAM, SOCK_DGRAM socket(2), bind(2), listen(2),accept(2), connect(2), send(2), recv(2), close(2) and shutdown(2) sendto(2) and recvfrom(2) select(2) Behavior of AF_INET, AF_UNIX SOCK_STREAM, SOCK_DGRAM socket(2), bind(2), listen(2),accept(2), connect(2), send(2), recv(2), close(2) and shutdown(2) sendto(2) and recvfrom(2) select(2)

Topics we covered UNIX At this point I assume you all know how to Edit a file Compile a project Create make file Run user commands efficiently Read a manpage Get the unix time(2) Hopefully you should also be able to Debug you code Generate core dump and process it Run strace(1) on a running server and see what’s up At this point I assume you all know how to Edit a file Compile a project Create make file Run user commands efficiently Read a manpage Get the unix time(2) Hopefully you should also be able to Debug you code Generate core dump and process it Run strace(1) on a running server and see what’s up

Revisited topics - execXXX Refs 8.9 in APUE 1st edition (p ) 8.10 in APUE 2nd edition (p ) Family of 6 functions (execlp, execvp, execv, execl, execve, execle) execve is the most generic (lowest layer) Those function replace the running image of the current process with new one Refs 8.9 in APUE 1st edition (p ) 8.10 in APUE 2nd edition (p ) Family of 6 functions (execlp, execvp, execv, execl, execve, execle) execve is the most generic (lowest layer) Those function replace the running image of the current process with new one

Exec functions Suffixes The function with l suffix builds argv The functions with the e suffix allow to change environ The functions with the p suffix try each path in PATH environment variable (so if we run ls it will find /bin/ls) Suffixes The function with l suffix builds argv The functions with the e suffix allow to change environ The functions with the p suffix try each path in PATH environment variable (so if we run ls it will find /bin/ls)

Exec functions What does replace the current process image means? The process has it’s memory space (data and program itself) released. Instead a new program is loaded. When the new program terminates the process terminates. There is no way to get back to the old program or to get its data Though it is by no means a must typical use of exec involves a call to fork(2) and wait(2) This is the way your shell (bash(1) or tcsh(1)) calls your programs What does replace the current process image means? The process has it’s memory space (data and program itself) released. Instead a new program is loaded. When the new program terminates the process terminates. There is no way to get back to the old program or to get its data Though it is by no means a must typical use of exec involves a call to fork(2) and wait(2) This is the way your shell (bash(1) or tcsh(1)) calls your programs

System(3) - exec example // system(3) is function that runs another program … smt like int my_system (const char *command) { int pid, status; if (command == 0) return 1; pid = fork(); if (pid == -1) return -1; if (pid == 0) { char *argv[4]; argv[0] = "sh"; argv[1] = "-c"; argv[2] = command; argv[3] = 0; execve("/bin/sh", argv, environ); exit(127); // This code will be reached only if execve has problems // system(3) is function that runs another program … smt like int my_system (const char *command) { int pid, status; if (command == 0) return 1; pid = fork(); if (pid == -1) return -1; if (pid == 0) { char *argv[4]; argv[0] = "sh"; argv[1] = "-c"; argv[2] = command; argv[3] = 0; execve("/bin/sh", argv, environ); exit(127); // This code will be reached only if execve has problems

System (cont’d) } do { if (waitpid(pid, &status, 0) == -1) { if (errno != EINTR) return -1; } else return status; } while(1); } // Note that system(3) is not very recommended function and I prefer you will use the above // instead of system(3) in your homework (but using system is legal) // the above code is from the BUGS section of man system. It is suggested as replacement // I suggest you follow the advice } do { if (waitpid(pid, &status, 0) == -1) { if (errno != EINTR) return -1; } else return status; } while(1); } // Note that system(3) is not very recommended function and I prefer you will use the above // instead of system(3) in your homework (but using system is legal) // the above code is from the BUGS section of man system. It is suggested as replacement // I suggest you follow the advice

POSIX cond Ref : APUE 2nd edition (doesn’t appear on first) 11.6 (specifically p ) We know of mutex - I came first therefore it is mine. But lets think of the following scenarios (hint - homework 2) I have a file being received and I need to compress it/uncompress it when receiving is done I have a file being compressed. I have to wake up some threads that will send the file but only after compression is done Ref : APUE 2nd edition (doesn’t appear on first) 11.6 (specifically p ) We know of mutex - I came first therefore it is mine. But lets think of the following scenarios (hint - homework 2) I have a file being received and I need to compress it/uncompress it when receiving is done I have a file being compressed. I have to wake up some threads that will send the file but only after compression is done

Cond Vs. Mutex In Mutex case I am entering the critical section AND prevent other threads from entering there. In cond case I am blocking myself from entering the critical section. I release myself after another thread has finished preparing the critical section for me In Mutex case I am entering the critical section AND prevent other threads from entering there. In cond case I am blocking myself from entering the critical section. I release myself after another thread has finished preparing the critical section for me

Cond example check out p of APUE 2e for a simpler example but their code is not a complete program In our example we will wait for child process to die. (that child process may have done some work for us… such as compress or uncompress file) then we wake up another thread when we are done check out p of APUE 2e for a simpler example but their code is not a complete program In our example we will wait for child process to die. (that child process may have done some work for us… such as compress or uncompress file) then we wake up another thread when we are done

Cond example - singal when child die pthread_cond_t readycond=PTHREAD_COND_INITIALIZER; pthread_mutex_t readymutex=PTHREAD_MUTEX_INIALIZER; void * waitingThread(void * arg) { pthread_mutex_lock(&readymutex); printf (“before waiting for cond\n”); pthread_cond_wait(&readycond); printf (“after cond was relesaed\n); // do something } pthread_cond_t readycond=PTHREAD_COND_INITIALIZER; pthread_mutex_t readymutex=PTHREAD_MUTEX_INIALIZER; void * waitingThread(void * arg) { pthread_mutex_lock(&readymutex); printf (“before waiting for cond\n”); pthread_cond_wait(&readycond); printf (“after cond was relesaed\n); // do something }

Cond example 2/2 Int main() { pthread_t tid; pthread_create(&tid,NULL,waitingThread,NULL); my_system(getenv(“program”)); // my_system from slide Regular notes regarding return codes pthread_cond_signal(&readycond); sleep(5); // do something so that we will not fall from main() } Int main() { pthread_t tid; pthread_create(&tid,NULL,waitingThread,NULL); my_system(getenv(“program”)); // my_system from slide Regular notes regarding return codes pthread_cond_signal(&readycond); sleep(5); // do something so that we will not fall from main() }

Cond function prototypes #include int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); #include int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);

Cond function prototypes 2/2 #include int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_destroy(pthread_cond_t *cond); #include int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_destroy(pthread_cond_t *cond);

Initializing conds and mutexes Can be done using the constant (PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER) Or using the functions (pthread_mutex_init(2), pthread_cond_init(2)) Unless you know better it is quite safe to give null for attributes. Cond and mutex attributes are discussed in 12.4 of APUE 2e and refer to things such as is the mutex recursive (i.e. if it is locked twice, by the same thread does it have to be released twice) Most attributes (with the exception of recursive mutex) are beyond the scope for us. Can be done using the constant (PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER) Or using the functions (pthread_mutex_init(2), pthread_cond_init(2)) Unless you know better it is quite safe to give null for attributes. Cond and mutex attributes are discussed in 12.4 of APUE 2e and refer to things such as is the mutex recursive (i.e. if it is locked twice, by the same thread does it have to be released twice) Most attributes (with the exception of recursive mutex) are beyond the scope for us.

Syncing methods For processes We can use file locking to implement mutex We use IPC (for example UDS) to implement cond - I.e. we send one byte when we are ready. We select-wait for that byte in similar way to cond. (this also works between threads) For processes We can use file locking to implement mutex We use IPC (for example UDS) to implement cond - I.e. we send one byte when we are ready. We select-wait for that byte in similar way to cond. (this also works between threads) For threads We use mutex to lock critical section We use cond to lock ourselves Those mechanisms do not work on Processes! (we cannot lock a thread that belongs to a different process using those devices!)

Ex 2 - pseudo code This is just ment to assist some student that are stack if you have your own implementation that work you do not have to follow those guidelines This assumes that we don’t support libbz2 or multiple clients This is just ment to assist some student that are stack if you have your own implementation that work you do not have to follow those guidelines This assumes that we don’t support libbz2 or multiple clients

Protocol We will specify in each packet the index and the number of packets (server knows the size of packet received) The protocol assumes fixed packet size for calculation of offset. (I.e. all packets, except for last are of the same size) We will specify in each packet the index and the number of packets (server knows the size of packet received) The protocol assumes fixed packet size for calculation of offset. (I.e. all packets, except for last are of the same size)

Server - main thread Start 10 handling threads Fnctl lock temporary file Socket/bind UDS datagram socket Start 10 handling threads Fnctl lock temporary file Socket/bind UDS datagram socket

Server mainthread cont While (file not completely received()) Select-recvfrom next packet // on first packet we must // save client UDS // record packet size // possible - handle special case first = last Unlock the file While (file not completely received()) Select-recvfrom next packet // on first packet we must // save client UDS // record packet size // possible - handle special case first = last Unlock the file

Server mainthread - cont Fork/exec/wait for bzip Write compressed file name in shared memory. Send signal to all waiting threads cond (either via loop and pthread_cond_signal or via pthread_cond_broadcast) Fork/exec/wait for bzip Write compressed file name in shared memory. Send signal to all waiting threads cond (either via loop and pthread_cond_signal or via pthread_cond_broadcast)

Client main thread Start recv thread // (select/recvfrom message on socket) Start 10 connection handling thread. Wait for signal on cond (decompression completed) Compare files (using memcmp) Start recv thread // (select/recvfrom message on socket) Start 10 connection handling thread. Wait for signal on cond (decompression completed) Compare files (using memcmp)

Client recv thread // Similar to server but Send signal to main thread cond once file recv is completed. While (file not completely received()) Select-recvfrom next packet // on first packet we must // save client UDS // record packet size // possible - handle special case first = last Send signal // Similar to server but Send signal to main thread cond once file recv is completed. While (file not completely received()) Select-recvfrom next packet // on first packet we must // save client UDS // record packet size // possible - handle special case first = last Send signal

Connection handling thread // it is assumed that filename is known (for example temp.bz2 in server and getenv( “ filename ” ) in client // I have implemented socket per connection you may // want just one socket to be shared among all threads 1. socket(2) 2. wait for cond() // only for server!!!! // wake me up when the compressed file is ready 3. read compressed file name() // it is assumed that filename is known (for example temp.bz2 in server and getenv( “ filename ” ) in client // I have implemented socket per connection you may // want just one socket to be shared among all threads 1. socket(2) 2. wait for cond() // only for server!!!! // wake me up when the compressed file is ready 3. read compressed file name()

Connection handling thread 4. while (file not finished) 5. { 6. read next chunk() 7. get packet number() // or offset 8. unlock() 9. create packet() // what you will send will include last packet/file size indication and offset/packet number 10. send packet via my socket() 11.} // pthread can exit/join now 4. while (file not finished) 5. { 6. read next chunk() 7. get packet number() // or offset 8. unlock() 9. create packet() // what you will send will include last packet/file size indication and offset/packet number 10. send packet via my socket() 11.} // pthread can exit/join now

Things to note USE of Threads Mmap/munmap/msync Open/Read/write/lseek/close Conds and mutexes Fcntl locking Execv/fork/wait YES I KNOW YOU CAN DO WITHOUT BUT I WANT YOU TO USE THEM! USE of Threads Mmap/munmap/msync Open/Read/write/lseek/close Conds and mutexes Fcntl locking Execv/fork/wait YES I KNOW YOU CAN DO WITHOUT BUT I WANT YOU TO USE THEM!

Things to note Make sure that the file received after uncompressing is identical to the file sent (We will send large random files make sure you get it right!) Make sure you sync! Assume filesize >> packetsize so each connection thread on average will send more then 1 packet Don’t assume file is text and don’t use any strXXX functions on the file (the file may have nulls in it) DON’T SUBMIT EX THAT DON”T RUN OR THAT DON”T RETURN MATCHING FILES Make sure that the file received after uncompressing is identical to the file sent (We will send large random files make sure you get it right!) Make sure you sync! Assume filesize >> packetsize so each connection thread on average will send more then 1 packet Don’t assume file is text and don’t use any strXXX functions on the file (the file may have nulls in it) DON’T SUBMIT EX THAT DON”T RUN OR THAT DON”T RETURN MATCHING FILES

Syncing pitfalls Problem 1 Thread 1 reads packet Thread 2 reads same packet Thread 1 names this packet 1 Thread 2 names this packet 2 Problem 2 Thread 1 reads packet Thread 2 reads next packet Thread 1 names his packet packet 1 Thread 2 names his packet packet 1 Problem 1 Thread 1 reads packet Thread 2 reads same packet Thread 1 names this packet 1 Thread 2 names this packet 2 Problem 2 Thread 1 reads packet Thread 2 reads next packet Thread 1 names his packet packet 1 Thread 2 names his packet packet 1

More pitfalls Packet 100 is last and arrived. You send signal But packet 99 is not yet arrived. *(you compress/uncompress garbage)* You receive packets from two clients but the packets mix…. Packet 100 is last and arrived. You send signal But packet 99 is not yet arrived. *(you compress/uncompress garbage)* You receive packets from two clients but the packets mix….

Supporting many clients Requires coding and planning. Build server that support one client, test it, then modify don’t start with the multiple client problem because you may run into problems and fail to finish at all

Using libbz2 Is actually up to you as a “learn-it-yourself” project. (don’t come to me with questions :-)) You are exempt from using exec (because you never exec bzip) If you are using ANY temp file in the server you must demonstrate locking. If you manage to do everything without any temp file you are exempt from locking Take note that you may still be asked regarding execv and fcntl on the test Is actually up to you as a “learn-it-yourself” project. (don’t come to me with questions :-)) You are exempt from using exec (because you never exec bzip) If you are using ANY temp file in the server you must demonstrate locking. If you manage to do everything without any temp file you are exempt from locking Take note that you may still be asked regarding execv and fcntl on the test

Starting next week we study kernel (AT LAST!)