System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.

Slides:



Advertisements
Similar presentations
Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
Advertisements

File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
UC Santa Barbara Project 1 Discussion Bryce Boe 2011/04/12.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 Processes and Pipes. 2 "He was below me. I saw his markings, manoeuvred myself behind him and shot him down. If I had known it was Saint-Exupery, I.
Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Inter Process Communication. Introduction Traditionally describe mechanism for message passing between different processes that are running on some operating.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Today’s topic Inter-process communication with pipes.
Operating systems Lab 04 System Call, Nested Fork(),Exec(),Pipe()
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
1 UNIX Systems Programming Interprocess communication.
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.
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
CS 3214 Computer Systems Lecture 13 Godmar Back.
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.
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.
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.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
File IO and command line input CSE 2451 Rong Shi.
Chapter 71 Deadlock Detection revisited. Chapter 72 Message Passing (see Section 4.5 in Processes Chapter)  A general method used for interprocess communication.
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.
S -1 Pipes. S -2 Inter-Process Communication (IPC) Chapter Data exchange techniques between processes: –message passing: files, pipes, sockets.
Operating Systems Yasir Kiani. 13-Sep Agenda for Today Review of previous lecture Interprocess communication (IPC) and process synchronization UNIX/Linux.
Pipe-Related System Calls COS 431 University of Maine.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
Pipes Pipes are an inter-process communication mechanism that allow two or more processes to send information to each other.
Interprocess Communication
Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
CSCI 330 UNIX and Network Programming
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
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)
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
Shell Execution Basic: fork, child execs, parent waits code of program in box –RC == return value from fork() Call fork RC=0 Call exec Subsequent instructions.
Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Error handling I/O Man pages
Lecture 5 Systems Programming: Unix Processes: Orphans and Zombies
Week 3 Redirection, Pipes, and Background
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Inter Process Communication (IPC)
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Pipe.
File redirection ls > out
LINUX System Programming with Processes (additional)
תרגול 8 – ק/פ ותקשורת תהליכים ב-Linux
Programming Assignment # 2 – Supplementary Discussion
IPC Prof. Ikjun Yeom TA – Hoyoun
dup, dup2 An existing file descriptor (filedes) is duplicated
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

System Commands and Interprocess Communication

chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This directory will be used for path names beginning with /. The root directory is inherited by all children of the current process. Why might you want to do this before the exec for a CGI program?

Pipes A special file that can store a limited amount of data in a first-in-first-out (FIFO) manner The include file or contains a defined constant PIPE_BUF that specifies the maximum number of bytes a pipe may hold One process will write to the pipe (as if it were a file), while another process will read from the pipe The system provides the synchronization between writing and reading processes

Pipes By default, if a writing process attempts to write to a full pipe, the system will automatically block the process until the pipe is able to receive the data Likewise, if a read is attempted on an empty pipe, the process will block until data is available In addition, the process will block if a specified pipe has been opened for reading, but another process has not opened the pipe for writing Data is written to the pipe and read from the pipe using the unbuffered I/O write and read system calls

Read and Write Include File(s) Summaryssize_t write( int fildes, const void *buf, size_t nbyte ); Return SuccessFailureSets errno Number of bytes writtenYes Include File(s) Summaryssize_t read( int fildes, const void *buf, size_t nbyte ); Return SuccessFailureSets errno Number of bytes readYes

PIPES A call to the pipe() function returns a pair of file descriptors. One of these descriptors is connected to the write end of the pipe, and the other is connected to the read end. int pipe( int fildes[2] );

pipe() Example #include int main() { int pfds[2]; char buf[30]; pipe(pfds); if (fork()==0) { printf(" CHILD: writing to the pipe\n"); write(pfds[1], "test", 5); //close pfds[0] printf(" CHILD: exiting\n"); exit(0); } else { printf("PARENT: reading from pipe\n"); read(pfds[0], buf, 5); //close pfds[1] printf("PARENT: read \"%s\"\n", buf); wait(NULL); }

popen() Example #include int main () { // creates a pipe and changes it to a file pointer // only works one way. FILE* stream = popen (“sort”, “w”); fprintf (stream, “This is a test.\n”); fprintf (stream, “Hello, world.\n”); fprintf (stream, “My dog has fleas.\n”); fprintf (stream, “This program is great.\n”); fprintf (stream, “One fish, two fish.\n”); return pclose (stream); }

dup and dup2 The dup system call duplicates an original open file descriptor –int dup(int fd) –The new descriptor, fildes, references the system file table entry for the next available non-negative file descriptor. The dup2 system call duplicates a file descriptor (fd1) onto a specific file descriptor (fd2) –int dup2(int fd1, int fd2) –if fd2 is already open, it will be closed first The new descriptor will –Share the same file pointer(offset) –Have the same access mode as the original and –Will be set to remain open across an exec call

dup2 #include using namespace std; main() { int fd; fd = open("tmp360", O_CREAT|O_WRONLY, 00777); dup2(fd, 1); close(fd); cout << "what is going on?\n"; cerr << " \n"; }

#include int main () { int fds[2]; pid_t pid; pipe (fds); /* Create a pipe. File descriptors for the two ends of the pipe are placed in fds. */ pid = fork (); /* Fork a child process. */ if (pid == (pid_t) 0) { close (fds[1]); /* This is the child process. Close our copy of the write end of the file descriptor. */ dup2 (fds[0], STDIN_FILENO); /* Connect the read end of the pipe to standard input. */ execlp ("sort", "sort", 0); /* Replace the child process with the sort program. */ } else { /* This is the parent process. */ FILE* stream; close (fds[0]); /* Close our copy of the read end of the file descriptor. */ /* Convert the write file descriptor to a FILE object, and write to it. */ stream = fdopen (fds[1], "w"); fprintf (stream, "This is a test.\n"); fprintf (stream, "Hello, world.\n"); fprintf (stream, "My dog has fleas.\n"); fprintf (stream, "This program is great.\n"); fprintf (stream, "One fish, two fish.\n"); fflush (stream); close (fds[1]); /* Wait for the child process to finish. */ waitpid (pid, NULL, 0); } return 0; }

Web Servers & Pipes Client Web Server CGI program stdin stdout http use dup2 to redirect stdin and stdout back to the web server use dup2 to redirect stdin and stdout back to the web server CGI programs are defined to only read from stdin and write to stdout CGI programs are defined to only read from stdin and write to stdout

Steps for CGI launching Create two pipes using the pipe system call –childin and childout fork –now both the parent and child have the pipes Child –use dup2 to dup the pipe onto stdin and stdout appropriately –dup2( childin[0], 0);// Change stdin to come from the parent –dup2(childout[1], 1);// Change stdout to go back to the parent –close the unused parts of childin and childout –exec the CGI process – It will inherit the new stdin and stdout Parent –write the body of the HTTP message to childin[1] (the write side of childin) –read from childout[0] (the read side of childout) and send it back to client on the socket –You will know how much to read based on the Content-Length header coming back from the child process

FIFO A FIFO is sometimes known as a named pipe. That is, it's like a pipe, except that it has a name! In this case, the name is that of a file that multiple processes can open() and read and write to. Has to be open at both ends simultaneously before you can proceed to do any input or output operations on it. –int mkfifo(const char *path,mode_t mode); –mkfifo("/tmp/namedpipe", 0666);

#include int main () { int pid; char *namedpipe = “/tmp/MyNamedPipe”; mkfifo(namedpipe, 0660); /* Create a fifo. */ pid = fork (); /* Fork a child process. */ if (pid == (pid_t) 0) { /* This is the child process. */ FILE *input; char str[256]; input = fopen(namedpipe, "r"); /* Open the fifo just like any FILE and read from it */ while(fgets(str, 255, input)) { printf("CHILD: %s", str); } fclose(input); } else { /* This is the parent process. */ FILE* stream; stream = fopen (namedpipe, "w"); /* Open the fifo just like any FILE and write to it */ fprintf (stream, "This is a test.\n"); fprintf (stream, "Hello, world.\n"); fprintf (stream, "My dog has fleas.\n"); fprintf (stream, "This program is great.\n"); fprintf (stream, "One fish, two fish.\n"); fflush (stream); fclose (stream); waitpid (pid, NULL, 0); /* Wait for the child process to finish. */ } return 0; }