kỹ thuật điện hệ điều hành os + thí nghiệm 4 en sinhvienzone com

32 43 0
kỹ thuật điện hệ điều hành os + thí nghiệm 4 en sinhvienzone com

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

IPC Programming Faculty of Computer Science and Engineering Ho chi Minh city University of Technology SinhVienZone.com https://fb.com/sinhvienzonevn Introduction  Inter-Process Communication  Sending messages between processes  Sharing information between processes  Synchronization SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT IPC  Communication Transferring message Sharing information Mechanisms: Pipe Signal Message queue Shared memory Socket RPC/RMI SinhVienZone.com Synchronization Solving confliction Processing order Mechanisms: Lock file Semaphore Mutex (pthread) https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT IPC Programming  Pipe  Signal SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Pipe  Processes communicate to each other using pipe through FIFO mechanism P0 write SinhVienZone.com read https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT P1 Pipe operations  Write: #include ssize_t write(int fd, const void *buf, size_t count)  Read: #include ssize_t read(int fd, const void *buf, size_t count) SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Pipe types  Unnamed pipe  Local  Used in processes having parent-child relation  Named pipe (FIFO)  Global  Used by any processes SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Unnamed pipe #include int pipe(int filedes[2]);  Return value:  0: if successful, two file descriptors filedes[0], filedes[1] will be stored in filedes  -1: if error and error code is stored in external errno variable SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Unnamed pipe filedes[1] P0 P1 filedes[0]  Unidirectional/half-duplex  filedes[0] is only used to read  filedes[1] is only used to write SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT #include #include #include #include int main() { int fp[2]; char s1[BUFSIZ], s2[BUFSIZ]; pipe(fp); Compile and execute $gcc unpipe.c -o unpipe $./unpipe Input: I Love Penguin From pipe> I Love Penguin $ if (fork()==0) { /* Child Write */ printf("\nInput: "); fgets(s1,BUFSIZ,stdin); s1[strlen(s1)]=0; close(fp[0]); write(fp[1],s1,strlen(s1)+1); } else { /*Parent Read*/ close(fp[1]); read(fp[0],s2,BUFSIZ); printf("\nFrom pipe> %s\n", s2); } return 0; } SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Named pipe  Similar to unnamed pipe  Notice:  Similar to a file on file system (directory entry, file permission)  Can be used on any processes  Can be created from a command on shell (using mknod command) SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Create named pipe  System call #include #include int mknod(const char *path,mode_t mode,dev_t dev);  C/C++ library call #include #include int mkfifo(const char *pathname, mode_t mode); SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT #include #include #include #include #include extern int errno; Compile and execute $gcc fifo.c -o fifo $./fifo Parent writes to FIFO1: Test1 Child reads from FIFO1: Test1 Child feedbacks on FIFO2: Test2 Feedback data from FIFO2: Test2 $ #define FIFO1 "/tmp/fifo.1" #define FIFO2 "/tmp/fifo.2" #define PERMS 0666 int main(){ char s1[BUFSIZ], s2[BUFSIZ]; int childpid, readfd, writefd; SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT if ((mknod(FIFO1, S_IFIFO | PERMS, (errno!=EEXIST)) { printf("can't create fifo1: %s", exit(1); } if ((mknod(FIFO2, S_IFIFO | PERMS, (errno!=EEXIST)) { unlink(FIFO1); printf("can't create fifo2: %s", exit(1); } if ((childpid=fork())

Ngày đăng: 28/01/2020, 22:10

Tài liệu cùng người dùng

Tài liệu liên quan