Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMP320 Operating Systems Lecture 07, 08 Operating System Concepts October 05 2009 Arif Butt Note: Some slides and/or pictures are adapted from Lecture.

Similar presentations


Presentation on theme: "CMP320 Operating Systems Lecture 07, 08 Operating System Concepts October 05 2009 Arif Butt Note: Some slides and/or pictures are adapted from Lecture."— Presentation transcript:

1 CMP320 Operating Systems Lecture 07, 08 Operating System Concepts October Arif Butt Note: Some slides and/or pictures are adapted from Lecture slides / Books of Dr Mansoor Sarwar. Dr Kubiatowicz. Dr P. Bhat. Dr Hank Levy. Dr Indranil Gupta. Text Book - OS Concepts by Silberschatz, Galvin, and Gagne. Ref Books Operating Systems Design and Implementation by Tanenbaum. Operating Systems by William Stalling. Operating Systems by Colin Ritchie.

2 Todays Agenda Review of previous lecture Cooperating Processes Producer Consumer Problem IPC – Message Passing System Direct Communication Indirect Communication – Shared Memory System Using pipes and FIFOS on the shell Using pipe() system call in UNIX programs Using mknod() system call in UNIX programs Using shmget() system call in UNIX programs 2CMP320 PUCIT Arif Butt1/12/2014

3 Cooperating Processes Independent process is a process that cannot affect or cannot be affected by the execution of another process. A process that does not share data with another process is independent Cooperating process is a process that can affect or can be affected by the execution of another process. A process that share data with other process is a cooperating process Advantages of Cooperating processes: – Information sharing – Computation speed up – Modularity – Convenience 3CMP320 PUCIT Arif Butt1/12/2014

4 Inter Process Communication 4CMP320 PUCIT Arif Butt1/12/2014 IPC is a mechanism for processes to communicate and to synchronize their actions. There are two fundamental models of IPC: Shared Memory – A region of memory that is shared by cooperating processes is established. Processes can then exchange information by reading and writing data to the shared region Messaging system – Communication takes place by means of messages exchanged between the cooperating processes. Processes communicate with each other without resorting to shared variables. (pipes, FIFO, sockets, message queues, semaphores, signals)

5 Inter Process Communication 5CMP320 PUCIT Arif Butt1/12/2014

6 Producer-Consumer Problem 6CMP320 PUCIT Arif Butt1/12/2014 Producer Consumer Empty Pool Full Pool

7 Producer-Consumer Problem (cont…) A producer process produces information that is consumed by a consumer process To allow producer and consumer run concurrently we must have a buffer that can be filled by the producer and emptied by the consumer. Buffer can be bounded or unbounded Unbounded Buffer – Places no practical limit on the size of the buffer – The consumer may have to wait for new items (if the buffer is empty), but the producer can always produce new items Bounded Buffer – Assumes a fixed size buffer – The consumer must wait if the buffer is empty, and the producer must wait if the buffer is full 7CMP320 PUCIT Arif Butt1/12/2014

8 Solution 1-Bounded Buffer Problem 8CMP320 PUCIT Arif Butt #define BUFFER_SIZE 5 typedef struct{ ---- } item; item buffer[BUFFER_SIZE]; int in = 0; //points to location where next item will be placed, will be used by producer process int out = 0; //points to location from where item is to beconsumed, will be used by consumer process 1/12/2014 Producer Process item nextProduced; while(1) { nextProduced = getItem(); while(((in + 1)%BUFFER_SIZE) == out) ; //do nothing buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Consumer Process item nextConsumed; while(1) { while(in == out) ; //do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } What is the problem with this solution?

9 Solution 2 - Bounded Buffer Problem 9CMP320 PUCIT Arif Butt #define BUFFER_SIZE 5 typedef struct{ ---- } item; item buffer[BUFFER_SIZE]; int in = 0; //points to location where next item will be placed, will be used by producer process int out = 0; //points to location from where item is to beconsumed, will be used by consumer process int ctr = 0; 1/12/2014 Producer Process item nextProduced; while(1) { nextProduced = getItem(); while(ctr == BUFFER_SIZE) ; //do nothing buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; ctr++; } Consumer Process item nextConsumed; while(1) { while(ctr == 0) ; //do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; ctr--; } Can you do it without using the ctr variable?

10 Messaging System 10CMP320 PUCIT Arif Butt1/12/2014 Message passing provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space It is particularly useful in a distributed environment, where the communicating processes may reside on different computers connected by a network For example; a chat program used on the www Message Passing is one solution to the communication requirement. The added bonus is: It works with shared memory and with distributed systems

11 Messaging System (cont…) 11CMP320 PUCIT Arif Butt1/12/2014 A message passing facility provides at least two operations: – send(message) – message size fixed or variable – receive(message) If P and Q wish to communicate, they need to: – establish a communication link between them – exchange messages via send/receive

12 Messaging System (cont…) 12CMP320 PUCIT Arif Butt1/12/2014 Direct Communication Processes must name each other explicitly: – send (P, message) – send a message to process P – receive(Q, message) – receive a message from process Q Send primitive includes a specific identifier of the destination process Receive primitive could know ahead of time which process a message is expected Receive primitive could use source parameter to return a value when the receive operation has been performed Example is a letter mailed via Pakistan Postal Service Properties of communication link in Direct Comm.: – A Link is established automatically – A link is associated with exactly one pair of communicating processes – Between each pair there exists exactly one link

13 Messaging System (cont…) 13CMP320 PUCIT Arif Butt1/12/2014 Indirect Communication Messages are directed and received from mailboxes (also referred to as ports) – Each mailbox has a unique id – Processes can communicate only if they share a mailbox – We can think of a mailbox as an abstract object into which a message can be placed in or removed from Properties of communication link in Indirect Comm.: – Link established only if processes share a common mailbox. – A link may be associated with many processes – Each pair of processes may share several communication links

14 Messaging System (cont…) 14CMP320 PUCIT Arif Butt1/12/2014 Indirect Communication (cont…) Operations – create a new mailbox. – send and receive messages through mailbox. – destroy a mailbox. Primitives are defined as: send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A Mailbox Sharing P 1, P 2, and P 3 share mailbox A P 1, sends; P 2 and P 3 receive Who gets the message? Solutions 1.Allow a link to be associated with at most two processes. 2.Allow only one process at a time to execute a receive operation. 3.Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

15 Indirect Process Communication (…) 1/12/201415CMP320 PUCIT Arif Butt

16 General Message Format 1/12/201416CMP320 PUCIT Arif Butt

17 Messaging System (cont…) 17CMP320 PUCIT Arif Butt1/12/2014 Synchronization Message passing may be either blocking or non-blocking Blocking is considered synchronous – Blocking send. The sending process is blocked until the message is received by the receiving process or by the mailbox. (Producer process blocks when the buffer is full) – Blocking receive. The receiver blocks until a message is available. (Consumer process blocks when the buffer is empty) Non-blocking is considered asynchronous – Non-blocking send. The sending process sends the message and resumes operation – Non-blocking receive. The receiver receives either a valid message or a null

18 Messaging System (cont…) 18CMP320 PUCIT Arif Butt1/12/2014 Buffering Whether communication is direct or indirect, messages exchanged by communicating processes reside in a temporary queue. Such queues can be implemented in three ways: Zero Capacity. The queue has a length of zero. Thus the link cannot have any messages waiting in it. The sender must block until the recipient receives the message Bounded Capacity. The queue has finite length n. Sender must wait if link is full Unbounded Capacity. The queue is of infinite length. So any number of messages can wait in it. Thus the sender never blocks

19 UNIX / LINUX IPC Tools 19CMP320 PUCIT Arif Butt1/12/2014 Pipes are used for communication between related processes (parent-child-sibling) on the same UNIX system. P1P2 UNIX/Linux System Pipe

20 UNIX / LINUX IPC Tools 20 CMP320 PUCIT Arif Butt 1/12/2014 Named pipes (FIFO) are used for communication between related or unrelated processes on the same UNIX system. P1P2 UNIX/Linux System FIFO p1p2

21 UNIX / LINUX IPC Tools 21 CMP320 PUCIT Arif Butt 1/12/2014 Sockets are used for communication between related or unrelated processes on the same or different UNIX systems. Computer 1Computer 2 P1P2 Network Connection Socket

22 USING PIPES IN CMD LINE & PROGRAMMING 22CMP320 PUCIT Arif Butt1/12/2014

23 Cmd Line use of Pipes 23 CMP320 PUCIT Arif Butt 1/12/2014 The UNIX system allows stdout of a command to be connected to stdin of another command using the pipe operator |. cmd1 | cmd2 | cmd3 | … | cmdN Stdout of cmd1 is connected to stdin of cmd2, stdout of cmd2 is connected to stdin of cmd3, … and stdout of cmdN-1 is connected to stdin of cmdN. cmd2 cmdN pipe cmd1

24 Cmd Line Use of Pipes 24 CMP320 PUCIT Arif Butt 1/12/2014 Example 1. Write a command that displays the contents of /etc/ directory, one page at a time. $ ls –l /etc/ 1> temp $ less 0< temp This will display the contents of file temp on screen one page at a time. After this we need to remove the temp file as well. $ rm temp So we needed three commands to accomplish the task, and the command sequence is also extremely slow because file read and write operations are involved. Lets use the pipe symbol $ ls –l /etc/ | less The net effect of this command is the same. It does not use a disk to connect standard output of ls –l to standard input of less because pipe is implemented in the main memory.

25 Cmd Line Use of Pipes (cont…) 25 CMP320 PUCIT Arif Butt 1/12/2014 Example 2. Write a command that will sort the contents of file students.txt and displays those contents on screen after removing duplication if any. $ sort 0 stds_sorted $ uniq stds_sorted $ rm stds_sorted Lets use the pipe symbol $ sort students.txt | uniq

26 Cmd Line Use of Pipes (cont…) 26 CMP320 PUCIT Arif Butt 1/12/2014 Pipes and redirection operators alone cannot be used to redirect stdout of a command to a file as well as another command in a single command tee command copies stdin to stdout and at the same time copies it to one or more files $ tee tout1.txt tout2.txt tee command is used to redirect the stdout of a command to one or more files, as well as to another command cmd1 | tee file1 … fileN | cmd2 Stdout of cmd1 is connected to stdin of tee, and tee sends its input to files file1, … fileN and as stdin of cmd2 $ who | tee who.txt

27 System Call - pipe() Call Fails when: Opening a pipe is equivalent to opening two files, so at least two slots need to be empty in the PPFDT, otherwise, the call will fail. Buffer space not available in the kernel. File table is full. int pipe(int pipefd[2]); pipefd[0] and pipefd[1] are read and write descriptors respectively. Will return -1 on failure Bounded buffer, maximum data written is PIPE_BUF, 4096 bytes 1/12/201427CMP320 PUCIT Arif Butt

28 System Call - pipe() 1/12/201428CMP320 PUCIT Arif Butt P1 P2 fork parentchild Write end Read end Process P1 will create a pipe (having two ends, a read end and a write end). Then the Parent Process P1 will create a child process P2. There are two descriptors associated with the pipe. The writer process (child) will use the write descriptor of the pipe. The reader process (parent) will use the read descriptor of the pipe. The child process will write data onto the pipe and the parent process will read that data at the read end and will display it on the screen. Parent read from the pipe. It blocks if the pipe is empty. Child writes in the pipe. It blocks if the pipe is full. pipe Child Process will send a string to Parent Process and Parent Process will print that on screen.

29 Sample Program using pipe() 1/12/201429CMP320 PUCIT Arif Butt /* pipe1.c (Parent creates pipe, forks a child, child writes into pipe, and parent reads from pipe).*/ #include #include #include int main(){ int pipefd[2], cpid, n, rc, nr; char *msg = "Hello, world!\n; char * buf[1024]; rc = pipe(pipefd); //create a pipe if (rc == -1) { //if pipe call fails printf(\n Pipe failed"); exit(1);} cpid = fork(); //if pipe call is successful we create a child if (cpid == -1) { printf(\n Fork failed"); //if fork fails exit(1); }

30 1/12/201430CMP320 PUCIT Arif Butt if (cpid == 0) { // Childs Code (child is the writer process) write(pipefd[1], msg, strlen(msg)); exit(0); } else { // Parents Code (parent is the reader process) n = strlen(msg); nr = read(pipefd[0], buf, n); rc = write(1, buf, nr); //use write sc to display the data //read from the pipe on the screen printf("Good work child!\n"); return(0); } Sample Program using pipe() (cont…)

31 USING FIFOS IN CMD LINE & PROGRAMMING 31CMP320 PUCIT Arif Butt1/12/2014

32 Named Pipes / FIFOs 32 CMP320 PUCIT Arif Butt 1/12/2014 First In First Out are special file types in UNIX used for communication between related or unrelated processes on a computer. Processes communicating with pipes must be related to each other through a common ancestor process that you execute, processes communicating with FIFOs do not have to have this kind of relationship. They can be independently executed programs on a system. A pipe is a main memory buffer and has no name, while FIFO is created on disk and has a name like a file name. So like files and unlike a pipe, a FIFO has to be created and opened before using it for communication. We can use mkfifo command to create a FIFO file in a shell session. We can use mknod() system call or mkfifo() library call to create a FIFO in a process. UNIX system calls read(), write(), close(), etc. can be used with FIFOs.

33 Named Pipes / FIFOs 33 CMP320 PUCIT Arif Butt 1/12/2014 Two common uses of FIFOs are: Used by shell commands to pass data from one shell pipeline to another, without creating temporary files In client server applications, FIFOs are used to pass data between a server process and client processes

34 Cmd Line Use of FIFOs 34 CMP320 PUCIT Arif Butt 1/12/2014 Example. Write a command that will count the number of lines in the man page of ls. $ man ls | wc –l 239 $ Example. Write a command that will count only the number of lines containing string ls in the man page of ls. $ man ls | grep ls | wc –l 9 $

35 Cmd Line Use of FIFOs (cont…) 35 CMP320 PUCIT Arif Butt 1/12/2014 $ mkfifo fifo1 $ prog3 0< fifo1 & $ prog1 0< infile | tee fifo1 | prog2 $ mkfifo fifo1 man ls 1> ls.dat $ cat 0< fifo1 | grep ls | wc –l & [1] $ sort 0< ls.dat | tee fifo1 | wc –l wc -l infile fifo1 wc -l Pipe sort tee grep Pipe First line will create an empty FIFO named fifo1. In second line, prog3 is trying to read its input from fifo1. Since fifo1 is empty therefore prog3 will block at the read operation. More so the & shows that it will run in the back ground. In third line, prog1 will read its input from infile and send its o/p to tee utility and tee will do two things: i.Write the o/p to fifo1 as well as to the stdout which will be sent to prog3 ii.Through the pipe will give the o/p to prog2

36 Client Server Comm with FIFOs 36CMP320 PUCIT Arif Butt1/12/2014 Server will open a well known FIFO, which it will use to read the request of client. Each client will open a client FIFO and send its name to the server process, so now the server knows via which client FIFO it can send reply to a particular client

37 System Call - mknod() Call Fails when: File with the given name already exists Pathname too long A component in the pathname not searchable or non existent Destination directory is read only Not enough memory space available #include int mknod(const char * pathname, mode_t mode, dev_t dev); mode should be permission mode ORed with S_IFIFO flag. dev is set to 0 for a FIFO, i.e. we are not making a device file. 1/12/201437CMP320 PUCIT Arif Butt

38 Library Call - mkfifo() Call Fails when: File with the given name already exists Pathname too long A component in the pathname not searchable or non existent Destination directory is read only Not enough memory space available #include int mkfifo(const char * pathname, mode_t mode); mode sets permission on FIFO Calls mknod() 1/12/201438CMP320 PUCIT Arif Butt

39 Example - FIFO 1/12/201439CMP320 PUCIT Arif Butt FIFO1 ServerClient Display Screen FIFO2 Reading Writing Reading Writing

40 Client - Server Code 1/12/201440CMP320 PUCIT Arif Butt //myheader.h #include #define FIFO1 "/tmp/fifo1" #define FIFO2 "/tmp/fifo2" #define PERMS 0666 #define MESSAGE1 "Hello, world!\n" #define MESSAGE2 "Hello, class!\n extern int errno;

41 Server Code - 1 1/12/201441CMP320 PUCIT Arif Butt #include myheader.h main() { char buff[1024]; int readfd, writefd, n, size; /*create fifo1 using mknod system call*/ if (mknod (FIFO1, S_IFIFO | PERMS, 0) < 0) { perror ("mknod FIFO1"); exit (1); } /*create fifo2 using mkfifo library call. if mkfifo() fails in creating FIFO2, before exiting we must remove FIFO1 from file system*/ if (mkfifo(FIFO2, PERMS) < 0) { unlink (FIFO1); perror("mknod FIFO2"); exit (1); }

42 Server Code - 2 1/12/201442CMP320 PUCIT Arif Butt if ((readfd = open(FIFO1, 0)) < 0) { //open FIFO1 for reading perror ("open FIFO1"); exit (1); } if ((writefd = open(FIFO2, 1)) < 0){ //open FIFO2 for writing perror ("open FIFO2"); exit (1); } size = strlen(MESSAGE1) + 1; /*if client has yet not run, the server process will block on following read. As soon as some client executes and writes some msg in FIFO1, the server process will automatically unblock.*/ if ((n = read(readfd, buff, size)) < 0) { perror ("server read"); exit (1); } if (write (1, buff, n) < n) { //write on screen perror("server write1"); exit (1); } size = strlen(MESSAGE2) + 1; if(write(writefd,MESSAGE2,size) != size){ //write on FIFO2 perror ("server write2"); exit (1); } close (readfd); close (writefd); }

43 Client Code - 1 1/12/201443CMP320 PUCIT Arif Butt #include myheader.h main() { char buff[BUFSIZ]; int readfd, writefd, n, size; // open FIFO1 for writing if ((writefd = open(FIFO1, 1)) < 0) { perror ("client open FIFO1"); exit (1); } // open FIFO2 for reading if ((readfd = open(FIFO2, 0)) < 0) { perror ("client open FIFO2"); exit (1); } size = strlen(MESSAGE1) + 1; // write message in FIFO1 if (write(writefd, MESSAGE1, size) != size) { perror ("client write1"); exit (1); }

44 Client Code - 2 1/12/201444CMP320 PUCIT Arif Butt /*if server has yet not run, the client process will block on following read. As soon as the server writes some msg in FIFO2, the client process will automatically unblock.*/ if ((n = read(readfd, buff, size)) < 0) { perror ("client read"); exit (1); } else if (write(1, buff, n) != n) { perror ("client write2"); exit (1); } close(readfd); close(writefd); /* Remove FIFOs now that we are done using them */ if (unlink (FIFO1) < 0) { perror("client unlink FIFO1"); exit (1); } if (unlink (FIFO2) < 0) { perror("client unlink FIFO2"); exit (1); } exit (0); }

45 FIFO-Running the Code 1/12/201445CMP320 PUCIT Arif Butt Use any editor to type your program and then to compile use gcc compiler: $ gcc server.c –o server $ gcc client.c –o client $ server & [1] $ client Hello, World! Hello, Class! $

46 Shared Memory 46CMP320 PUCIT Arif Butt1/12/2014

47 Shared Memory System 47CMP320 PUCIT Arif Butt1/12/2014 Shared memory allows two or more processes to share a given region of memory. This is the fastest form of IPC because the data does not need to be copied between the client and server. The only trick in using shared memory is synchronizing access to a given region among multiple processes. If the server is placing data into a shared memory region, the client shouldn't try to access the data until the sever is done. Often semaphores are used to synchronize shared memory access. We can use record locking as well.

48 Prog 1 Logical Address Space 1 Prog 2 Logical Address Space 2 Data 2 Stack 1 Heap 1 Code 1 Stack 2 Data 1 Heap 2 Code 2 Shared In shared memory we declare a given section in the memory as one that will be used simultaneously by several processes. Typically a shared memory region resides in the address space of the process creating the shared memory segment. Other processes that wish to communicate using this shared memory segment must attach it to their address space. Code Data Heap Stack Shared Code Data Heap Stack Shared Shared Memory System (cont…) 1/12/201448CMP320 PUCIT Arif Butt

49 System Call shmget() 49 A process must first create a shared memory segment using shmget() system call (SHared Memory GET). On success, shmget() returns a shared memory identifier that is used in subsequent shared memory functions. On failure, it returns -1. int segment_id = shmget(IPC_PRIVATE, 4096, S_IRUSR | S_IWUSR); #include int shmget(key_t key, int size, int flag); There is a special key value, IPC_PRIVATE, that creates shared memory private to the process. The second parameter, size, specifies the amount of memory required in bytes. The third parameter, flag consists of nine permission flags. Normally it is IPC_CREAT | /12/2014CMP320 PUCIT Arif Butt

50 System Call shmat() 50 CMP320 PUCIT Arif Butt Processes that wish to access a shared memory segment must attach it to their address space using the shmat() system call. (SHared Memory ATach) On success, shmat() returns a pointer to the first byte of shared memory. On failure, it returns -1. char * shared_memory = (char*) shmat(segment_id, NULL, 0); void* shmat(int shmid, const void *shmaddr, int flag); First parameter, shm_id, is the shared memory identifier returned from shmget. Second parameter is a pointer location in memory indicating where the shared memory will be attached. NULL pointer allows the system to choose the address at which the memory appears. Third parameter identifies the mode, a 0 means read- write. 1/12/2014

51 System Call shdt() 51CMP320 PUCIT Arif Butt When a process no longer requires access to the shared memory segment, it detaches the segment form its address space. This does not remove the identifier and its associated data structure from the system. The identifier remains in existence until some process specifically removes it by calling shmctl() with a command of IPC_RMID. To detach a region of shared memory, the process can pass the pointer of the shared memory region to the shmdt() system call. (SHared Memory DeTach) shmdt(shared_memory); int shmdt(void * addr); The addr argument is the value that was returned by a previous call to shmat. 1/12/2014

52 System Call shctl() 52CMP320 PUCIT Arif Butt Shared memory segment can be removed from the system with the shmctl() system call. (SHared Memory ConTroL) Returns 0 if OK, -1 on error. shmctl(segment_id, IPC_RMID, NULL); void* shmctl(int shmid, int cmd, struct shmid_ds * buf); First parameter, shmid, is the shared memory identifier returned from shmget(). Second parameter can take five different values. IPC_RMID removes the shared memory segment set from the system. Third parameter is normally a NULL. 1/12/2014

53 Sample Program-Shared Memory 1/12/201453CMP320 PUCIT Arif Butt /* Program creates a shared memory area, attaches it to its address space, writes a string in it. Reads and print the contents of shared memory. Detach and finally remove shared memory segment.*/ #include int main(){ const int size = 4096; int segment_id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0666); char * shared_memory = (char*) shmat(segment_id, NULL, 0); sprintf(shared_memory, Hello World); printf(%s\n,shared_memory); shmdt(shared_memory); shmctl(segment_id, IPC_RMID, NULL); return 0;}

54 SUMMARY 54CMP320 PUCIT Arif Butt1/12/2014

55 Were done for now, but Todos for you after this lecture… 55CMP320 PUCIT Arif Butt If you have problems visit me in counseling hours.... Go through the slides and Book Sections: 3.1 to 3.4, 3.51 Solution 2 to the bounded buffer problem is using an additional variable ctr. Suggest another solution that do not use any additional variable (do not mention the one discussed in class). Write down the programs discussed in class in vim editor and execute them. Make variations in the programs and understand the underlying details. 1/12/2014


Download ppt "CMP320 Operating Systems Lecture 07, 08 Operating System Concepts October 05 2009 Arif Butt Note: Some slides and/or pictures are adapted from Lecture."

Similar presentations


Ads by Google