Exercise #2: Process Creation/Termination and Interprocess Communication J. H. Wang Mar. 30, 2010.

Slides:



Advertisements
Similar presentations
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
Advertisements

Dr. Kalpakis CMSC 421, Operating Systems. Fall Processes.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 3.2: Processes modified by your instructor
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Chapter 3 Processes.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes Modified from the text book Slides. TY, Sept 2010.
CHAPTER 4 PROCESSES Operating Systems CHAPTER FOUR, OPERATING SYSTEMS 2 Processes Current-day computer systems allow multiple programs to be loaded into.
Chapter 3: Processes. Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes Modified from the text book Slides. TY, Sept 2011.
Processes Operating System Concepts chapter 3 CS 355 Operating Systems
Interprocess Communication. Process Concepts Last class.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Jan 19, 2005 Chapter 3: Processes Process Concept.
Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems.
1 Chapter 3: Processes  Process Concept  Process Scheduling  Operations on Processes  Cooperating Processes  Interprocess Communication  Communication.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes 170 UCSB T. Yang Some of slides are from the Chapter.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Chapter 6 UNIX Special Files Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 3: Processes.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes.
Chapter 3: Process-Concept. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Objectives To introduce a notion of a process – a program.
Chapter 3: Processes Adapted to COP4610 by Robert van Engelen.
Chapter 3: Process-Concept. Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
Java Server Programming Web Interface for Java Programs.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Chapter 3: Processes. 2 Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server.
Part 4: Network Applications Client-server interaction, example applications.
2.1 Processes  process = abstraction of a running program.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
2.1 Processes  process = abstraction of a running program  multiprogramming = CPU switches from running program to running program  pseudoparallelism.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Objectives To introduce the notion of a process -- a program in execution,
Chapter 3 The Programming Interface Chien-Chung Shen CIS/UD
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 3: Processes.
Chapter 3: Processes.
Chapter 3: Process Concept
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
CSS 430: Operating Systems - Processes
Chapter 3: Processes.
CSS 430: Operating Systems - Processes
Chapter 3: Processes.
2.1 Processes process = abstraction of a running program
Remote Process Explorer
CGS 3763 Operating Systems Concepts Spring 2013
Chapter 3: Processes.
Chapter 3: Processes.
Chapter 3: Processes.
Chapter 3: Processes.
Tutorial: The Programming Interface
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 3: Process Concept
Presentation transcript:

Exercise #2: Process Creation/Termination and Interprocess Communication J. H. Wang Mar. 30, 2010

Objectives To get you familiar with program development in Linux and Windows To learn how to create and terminate a process To understand the major system calls for interprocess communication

Processes Monitoring processes – Ctrl-Alt-Del (in Windows) – ps –el (in UNIX/Linux) Running processes in background – & Put background process into foreground – fg Terminating a process – kill -9

Program Compilation UNIX-like environment –Cygwin –Remote host telnet (SunOS) –Linux –Virtual machine running Linux (via VMPlayer) Editors –UNIX: vi, joe, … –Windows: notepad, … Compilers –UNIX: gcc, … –Windows: Dev-C++, Visual C++, …

Process Creation in UNIX #include int main() { pid_t pid; /* fork another process */ pid = ? (); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ ? (NULL); printf ("Child Complete"); exit(0); }

Process Creation in Win32 #include int main( VOID ) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process.

if( ! ? ( NULL, // No module name (use command line). "C:\\WINDOWS\\system32\\mspaint.exe", // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { printf( "CreateProcess failed (%d).\n", GetLastError() ); return -1; } // Wait until child process exits. ? ( pi.hProcess, INFINITE ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); }

Exercises Process creation in UNIX: Ex Process creation in Win32: Ex. 3.14

Interprocess Communication POSIX shared-memory (Fig. 3.16) socket (Fig. 3.19, 3.20) pipes (Fig ) –ordinary pipes in UNIX –anonymous pipes in Windows –named pipes

POSIX Shared Memory API #include int main() { /* the identifier for the shared memory segment */ int segment_id; /* a pointer to the shared memory segment */ char* shared_memory; /* the size (in bytes) of the shared memory segment */ const int segment_size = 4096; /** allocate a shared memory segment */ segment_id = ? (IPC_PRIVATE, segment_size, S_IRUSR | S_IWUSR); /** attach the shared memory segment */ shared_memory = (char *) ? (segment_id, NULL, 0); printf("shared memory segment %d attached at address %p\n", segment_id, shared_memory);

/** write a message to the shared memory segment */ sprintf(shared_memory, "Hi there!"); /** now print out the string from shared memory */ printf("*%s*\n", shared_memory); /** now detach the shared memory segment */ if ( ? (shared_memory) == -1) { fprintf(stderr, "Unable to detach\n"); } /** now remove the shared memory segment */ shmctl(segment_id, IPC_RMID, NULL); return 0; }

Socket To be explained in computer network programming BSD socket Java socket WinSock

A Sample Date Client in Java import java.net.*; import java.io.*; public class DateClient { public static void main(String[] args) { try { // this could be changed to an IP name or address other than the localhost Socket sock = new Socket(" ",6013); InputStream in = sock.getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); String line; while( (line = bin.readLine()) != null) System.out.println(line); sock.close(); } catch (IOException ioe) { System.err.println(ioe); }

A Sample Date Server in Java import java.net.*; import java.io.*; public class DateServer { public static void main(String[] args) { try { ServerSocket sock = new ServerSocket(6013); // now listen for connections while (true) { Socket client = sock.accept(); // we have a connection PrintWriter pout = new PrintWriter(client.getOutputStream(), true); // write the Date to the socket pout.println(new java.util.Date().toString()); // close the socket and resume listening for more connections client.close(); } catch (IOException ioe) { System.err.println(ioe); }

Ordinary Pipes in UNIX #include #define BUFFER_SIZE 25 #define READ_END0 #define WRITE_END1 int main(void) { char write_msg[BUFFER_SIZE] = "Greetings"; char read_msg[BUFFER_SIZE]; pid_t pid; int fd[2]; /** create the pipe */ if ( ? (fd) == -1) { fprintf(stderr,"Pipe failed"); return 1; }

/** now fork a child process */ pid = ? (); if (pid < 0) { fprintf(stderr, "Fork failed"); return 1; } if (pid > 0) { /* parent process */ /* close the unused end of the pipe */ close(fd[READ_END]); /* write to the pipe */ ? (fd[WRITE_END], write_msg, strlen(write_msg)+1); /* close the write end of the pipe */ close(fd[WRITE_END]); }

else { /* child process */ /* close the unused end of the pipe */ close(fd[WRITE_END]); /* read from the pipe */ ? (fd[READ_END], read_msg, BUFFER_SIZE); printf("child read %s\n",read_msg); /* close the write end of the pipe */ close(fd[READ_END]); } return 0; }

Anonymous Pipes in Windows – Parent Process #include #define BUFFER_SIZE 25 int main(VOID) { HANDLE ReadHandle, WriteHandle; STARTUPINFO si; PROCESS_INFORMATION pi; char message[BUFFER_SIZE] = "Greetings"; DWORD written; /* set up security attributes so that pipe handles are inherited */ SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL,TRUE}; /* allocate memory */ ZeroMemory(&pi, sizeof(pi)); /* create the pipe */ if ( ! ? (&ReadHandle, &WriteHandle, &sa, 0)) { fprintf(stderr,"Create Pipe Failed\n"); return 1; }

/* establish the START_INFO structure for the child process */ GetStartupInfo(&si); si.hStdError = GetStdHandle(STD_ERROR_HANDLE); si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); /* redirect the standard input to the read end of the pipe */ si.hStdInput = ReadHandle; si.dwFlags = STARTF_USESTDHANDLES; /* we do not want the child to inherit the write end of the pipe */ SetHandleInformation( WriteHandle, HANDLE_FLAG_INHERIT, 0); /* create the child process */ if (! ? (NULL, ".\\child.exe", NULL, NULL, TRUE, /* inherit handles */ 0, NULL, NULL, &si, &pi)) { fprintf(stderr, "Process Creation Failed\n"); return -1; }

/* close the unused end of the pipe */ CloseHandle(ReadHandle); /* the parent now wants to write to the pipe */ if (! ? (WriteHandle, message, BUFFER_SIZE, &written, NULL)) fprintf(stderr, "Error writing to pipe\n"); /* close the write end of the pipe */ CloseHandle(WriteHandle); /* wait for the child to exit */ ? (pi.hProcess, INFINITE); /* close all handles */ CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }

Anonymous Pipes in Windows – Child Process #include #define BUFFER_SIZE 25 int main(VOID) { HANDLE ReadHandle, WriteHandle; CHAR buffer[BUFFER_SIZE]; DWORD read; ReadHandle = GetStdHandle(STD_INPUT_HANDLE); WriteHandle= GetStdHandle(STD_OUTPUT_HANDLE); /* have the child read from the pipe */ if ( ? (ReadHandle, buffer, BUFFER_SIZE, &read, NULL)) printf("child: >%s<",buffer); else fprintf(stderr, "Child: Error reading from pipe\n"); return 0; }

Named Pipes FIFO in UNIX –mkfifo() –open() –read() –write() –close() Named pipes in Windows –CreateNamedPipe() –ConnectNamedPipe() –ReadFile() –WriteFile()

Exercises POSIX shared memory: Ex. 3.17, 3.20 Ordinary pipes: Ex. 3.18, 3.19, Java socket: Ex. 3.15, 3.16

Further Reading Programming Project 3.21: POSIX Message Passing –Overview –The message passing system –Creating the processes –Implementation hints