Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Operating Systems Lecture 7.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
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 Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
CSSE 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.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
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.
LINUX System Programming with Processes. Overview 1. What is a Process? 2. fork() 3. exec() 4. wait() 5. Process Data 6. File Descriptors across Processes.
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.
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
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
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.
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.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
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.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
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,
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
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.
CSCI 330 UNIX and Network Programming
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
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.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
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.
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.
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
CSCI 4061 Recitation 2 1.
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
LINUX System : Lecture 8 Programming with Processes
Using Processes.
Unix Process Management
UNIX PROCESSES.
Fork and Exec Unix Model
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Programming Assignment # 2 – Supplementary Discussion
Operation System Program 1
Process Programming Interface
IPC Prof. Ikjun Yeom TA – Hoyoun
EECE.4810/EECE.5730 Operating Systems
System Programming: Process Management
Presentation transcript:

Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing at its main function - pid of the process isn’t changed Note: exec is usually used after fork(), when we want a child process to execute some program. exec(l/v)(e/p) (path/filename, listOfArgs/ArgvArray, --/(char *) 0, --/environment) There are six different ways to call exec function. Example: we want to create a child process and force it to execute “ls -l” program. char * argv [ ] = { “ls”, “-l”, NULL}; char * envp [ ] = { “USER=unknown”, “PATH=/tmp”, NULL}; 1) execl (“/bin/ls”, “ls”, ”-l”, (char *) 0); 2) execlp (“ls”, “ls”, “-l”, (char *) 0); 3) execle ( “ /bin/ls”, “ls”, “-l”, (char *) 0, envp); 4) execv (“/bin/ls”, argv); 5) execvp (“ls”, argv); 6) execve (“/bin/ls”, argv, envp); list of arguments (first argument is a new program name) full path to ls program “end of arguments” delimiter new environment to use

- if the first argument is filename (and not path), o.s. would look for it in all directories that appear in PATH system variable (you can examine this variable using “>echo $PATH” Unix command) - if don’t use exec function with environment option (with “e” at the end of the name), then the environment of the current process is copied to the new process (you can see you shell’s environment using “>set” Unix command) Let’s write a program for this example: #include int main (void) { pid_t pid; int status; pid = fork (); if (pid == 0) { //child execlp (“ls”, “ls”, “-l”, (char *) 0); fprintf (stderr, “couldn’t execute: %s”, “ls –l”); exit (-1); // abnormal termination } // there is no need to use “else” because child code was changed ! waitpid (pid, &status, 0) ; exit (0); } If this line is executed, then exec function failed (process code wasn’t changed). So, we print error into stderr and then exit abnormally (exit (-1)). This causes stderr to print the last error it got.

Important: there are some properties that the new process image inherits from the calling process: - pid & ppid - time left until alarm clock - current working directory - process signal mask - pending signals - file descriptors (by default, they are left opened) Notes : 1. usually only execve is a system call. The other five are just library functions that eventually invoke this system call 2. see shell.c & exec.c programs (from maman13 material) for more examples (we would also learn them in class). Pay attention that shell.c can’t execute commands with parameters because of the way it passes arguments to execlp.shell.cexec.c FIFO - named pipes FIFO - named pipes: - is a type of file - is used by unrelated processes to exchange data int mkfifo (const char * pathname, mode_t mode)

- pathname is a path to a file name, that would represent a fifo - mode is permissions set (for user, group and others) - return value: 0 if OK, -1 on error. Example: mkfifo (“myFifo”, S_IRUSR | S_IWUSR| S_IRGRP | S_IWGRP); In this example we created fifo that is represented by the file “myFifo” (in the current working directory), with the following permissions set: user can read & write to fifo, and group can also read & write to it. After we created a fifo, we should open it (like we open a regular file): myFifo = open (“myFifo”, O_RDWR) ); In this example we opened fifo for reading and writing to it. After this, we can write some message to it: char *message = “Marina”; write (myFifo, message, strlen(message)+1); Now, another process can read from this fifo. How? It should open the same file (with the same name, that is known for all the processes), and just read from it: myFifo = open (“myFifo”, O_RDWR) ); read (myFifo, buf, 100); // assuming that maximal message length is 100

Important: when open fifo file with O_RDONLY permission, the process would be blocked until some other process would open the same fifo file for writing, and write something into it. In order to avoid this, we should use select() function: int select (int maxpdp1, fd_set *readfds, fd_set *readfds, fd_set *readfds, struct timeval *tvptr) Example: mkfifo (“myFifo”, S_IRUSR | S_IWUSR| S_IRGRP | S_IWGRP); myFifo = open (“myFifo”, O_RDWR) ); fd_set r_set; FD_ZERO (&r_set); FD_SET (myFifo, &r_set ); select (myFifo+1, &r_set, NULL, NULL, NULL); In this example we create a set r_set, that would contain fifo file descriptor (the one we want to read from it). Third and fourth parameters are NULLs (file descriptors sets for another purposes). Fifth parameter defines if we would block on select system call or not: - NULL - block till get some message to read in fifo - empty struct timeval (zero values for each member) – returns immediately macro that set zeros to r_set macro that add file descriptor to r_set

- non empty struct timeval – returns after the given time First parameter is the maximal file descriptor in all the sets (since we can wait for messages from several fifo files, just by adding them to r_set, this function need to know the maximal file descriptor from all the fifo files in all the sets). Return value: 0 on success, -1 on failure. struct timeval { long tv_sec; // seconds long tv_usec; // microseconds } Example: struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 20; select (myFifo+1, &r_set, NULL, NULL, &tv); In this example, a process would be blocked by select () system call only for 20 microseconds, and then would return with success (0 return value) or error (-1 return value). Use myDeamon.c & myClient.c files to learn more about this topic. myClient sends message to myDeamon using one fifo, and myDeamon returns this message to myClient using another fifo.myDeamon.c myClient.c

In order to run these programs: - open two consoles - compile both myDeamon.c & myClient.c ( get two executable files ) - on one console run myDeamon > myDeamon - on the second console run myClient > myClient Marina Now you should get the following output: - on myDeamon console: > well known fifo established got the message: Marina, message length is: 6 wrote to pfifo 7 chars - on myClient console: > read 7 chars from pfifo Marina Notes: - for some reasons, fifo can be broken (closed, error when opened, etc). You should catch SIGPIPE signal in order to prevent your program to crash because of a broken fifo (just create signal handler for this signal, that exit the program when catch this signal). - when you write a message to fifo, it can’t be bigger than PIPE_BUF size (constant that is defined in file).

Note: I strongly recommend you to build your maman based on this example (it is pretty similar to what you have to do). Time function: - gets the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC from the system clock time_t time (time_t * timer) - save the time in timer - if send NULL argument, the time wouldn’t be saved, but still be returned value Example: #include #include int main () { time_t seconds; seconds = time (NULL); printf (“%ld hours since January 1, 1970\n”, seconds / 3600); }

Dup function: - duplicates an existing file descriptor - a new descriptor that returns is the first free file descriptor - a new descriptor points to the same file (there is no two files) int dup (int fd); - fd is a file descriptor that we want to duplicate Example: myFifo = open (“myFifo”, O_RDWR); close(1); dup (myFifo ); close (myFifo ); In this example we open a fifo. Then we close stdout (with file descriptor = 1), and duplicate myFifo (file descriptor). This causes a duplicate of myFifo to get a file descriptor = 1 (because it is a first free file descriptor) => now, for this process, each printing would be into myFifo file.