Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos.

Similar presentations


Presentation on theme: "Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos."— Presentation transcript:

1 Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos

2 Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 Pipes: an interprocess channel Pipe--- communication between two processes on the same machine #include int pipe(int fildes[2]); Creates 2 file descriptors int fd[2]; if (pipe(fd) == -1) perror(“Failed to create the pipe”); Creates two fds: fd[0] is for reading. fd[1] is for writing.

3 Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Example 1: Parent writes string to Child #include #define BUFSIZE 10 int main(void) { char bufin[BUFSIZE] = "empty"; char bufout[] = "hello"; int bytesin; pid_t childpid; int fd[2]; if (pipe(fd) == -1) { perror("Failed to create the pipe"); return 1; }

4 Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Parent writes string to Child childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid) /* parent code */ write(fd[1], bufout, strlen(bufout)+1); else /* child code */ bytesin = read(fd[0], bufin, BUFSIZE); fprintf(stdout, "[%ld]:my bufin is {%s}, my bufout is {%s}\n", (long)getpid(), bufin, bufout); return 0; }

5 Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Example 2: Redirection #include int main(void) { pid_t childpid; int fd[2]; if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) { perror("Failed to setup pipe"); return 1; }

6 Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Child redirects stdout to write channel of pipe if (childpid == 0) { /* This is the child executing ls */ if (dup2(fd[1], STDOUT_FILENO) == -1) perror("Failed to redirect stdout of ls") else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) perror("Failed to close extra pipe descriptors on ls") else { execl("/bin/ls", "ls", "-l", NULL); perror("Failed to exec ls"); } return 1; }

7 Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 if (dup2(fd[0], STDIN_FILENO) == -1) perror("Failed to redirect stdin of grep") else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) perror("Failed to close extra pipe file descriptors on grep") else { execl("/bin/grep", ”grep", ”mystring", NULL); perror("Failed to exec grep"); } return 1; } Parent redirects stdin to read channel of pipe

8 Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 A PIPE disappears after all the processes have closed its file descriptors FIFOs are named pipes that are special files that persist even after all the processes have closed them. FIFO: named pipe

9 Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 FIFO: named pipe mkfifo() creates a FIFO special file (a named pipe) #include int mkfifo(const char *path, mode_t mode); mkfifo() makes a FIFO special file with name pathname. mode specifies the FIFO's permissions. The permissions are modified by the process's umask in the usual way: the permissions of the created file are (mode & ~umask).

10 Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Example of using FIFO: modeling producer-consumer Producer writes to fifo Consumer reads from fifo and outputs data to stdout Once you have created a FIFO special file, any process can open it for reading or writing, in the same way as an ordinary file.

11 Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Example 3: named_pipe #include /* To test: 1) run the program like./named_pipe fifo */ /* 2) from another terminal run: ls -la > fifo */ int main (int argc, char *argv[]) { int requestfd, sz; char *buffer; if (argc != 2) { /* name of consumer fifo is passed on the command line */ fprintf(stderr, "Usage: %s fifoname \n", argv[0]); return 1; } /* create a named pipe to handle incoming requests */ if ((mkfifo(argv[1], S_IRWXU | S_IWGRP| S_IWOTH) == -1) && (errno != EEXIST)) { perror("Server failed to create a FIFO"); return 1; }

12 Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Example 3: named_pipe /* open a read communication endpoint for the pipe */ if ((requestfd = open(argv[1], O_RDONLY)) == -1) { perror("Server failed to open its FIFO"); return 1; } /* Read from pipe like you would from a regular file */ buffer = (char *) malloc(100 * sizeof(char)); printf("==>> READING FROM FIFO!!! <<== \n\n"); while(1) { sz = read(requestfd, buffer, 50); buffer[sz] = '\0'; printf("%s", buffer); }

13 Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Memory

14 Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 OS & memory management Main memory is normally divided into two parts: kernel memory and user memory In a multiprogramming system, the “user” part of main memory needs to be divided to accommodate multiple processes. The user memory is dynamically managed by the OS (known as memory management) to satisfy following requirements: Protection Relocation Sharing Memory organization (main mem.+secondary mem.) [classbook pag.757-771]

15 Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Memory partitioning Nowadays memory management is based on a sophisticated technique known as virtual memory. Before studying virtual memory, we will review simpler but outdated memory management mechanisms for multiprogramming systems: Fixed partitioning Dynamic partitioning

16 Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Fixed partitioning A simple scheme to manage the available user memory is to partition it in several regions of equal size (e.g., 8Mb partitions) As another option the memory can be partitioned in fixed regions of different sizes (e.g., 2Mb,4Mb,6Mb,8Mb partitions) Problems: If a program does not fit in the available fixed size, the program must be designed by using overlays (only a part of the program needs to be in main memory at any given time) Internal fragmentation

17 Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Overlays Overlay Manager Overlay Area Main Program Overlay 1 Overlay 2 Overlay 3 Secondary Storage Overlay 1Overlay 2Overlay 3Overlay 1 0K 5k 7k 12k Fixed partition

18 Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Memory assignment for fixed partitioning OS New processes New processes 2Mb 4Mb 8Mb 2Mb 4Mb 8Mb A process is always reloaded in the same partition (relocation is not needed) A process can be reloaded in any partition (relocation is needed)

19 Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Separate input queue for each partition Requires sorting the incoming jobs and putting them into separate queues Inefficient utilization of memory when the queue for a large partition is empty but the queue for a small partition is full. Small jobs have to wait to get into memory even though memory has plenty of free space. One single input queue for all partitions. Allocate a partition where the job fits in. Best Fit Worst Fit First Fit Memory assignment for fixed partitioning

20 Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Dynamic partitions Partitions are of variable length and number When a process is loaded, it is allocated exactly as much memory as it needs and no more Problems: External fragmentation It requires periodic compaction and task relocation Free Space Task A Task B Task C Task D

21 Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Fragmentation Internal Fragmentation When an allocated block is larger than data it holds External Fragmentation When aggregate free space would be large enough to satisfy request but no single free block is large enough MonitorJob 3 Free Job 5Job 6Job 7Job 8 Legend Free Space

22 Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 Relocation If relocation is not supported, starting address is decided when a program starts up Different jobs will run at different addresses When a program is linked, the linker must know at what address the program will begin in memory. It is a strong limitation since a swapped process must always be reloaded in the same partition Relocation allows a process to occupy different partitions during its lifetime. It relies on notion of logical and physical addresses (it requires hardware support) Logical addresses: range from 0 to max Physical addresses: range from R+0 to R+max (given base value R). User program never sees the real physical addresses

23 Copyright ©: Nahrstedt, Angrave, Abdelzaher 23 Relocation Register Memory Base Register CPU Instruction Address + BA MA MA+BA Physical Address Logical Address

24 Copyright ©: Nahrstedt, Angrave, Abdelzaher 24 Question 1 - Protection Problem: How to prevent a malicious process from writing or jumping into other user's or OS partitions Solution: Base bounds registers

25 Copyright ©: Nahrstedt, Angrave, Abdelzaher 25 Base Bounds Registers Memory Bounds RegisterBase Register CPU Address <+ Memory Address MA Logical Address LA Physical Address PA Fault Base Address Limit Address MA+BA Base Address BA


Download ppt "Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos."

Similar presentations


Ads by Google