Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 30 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
30
Dung lượng
906,57 KB
Nội dung
IPC Programming (cont) Faculty of Computer Science and Engineering Ho chi Minh city University of Technology SinhVienZone.com https://fb.com/sinhvienzonevn 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 Shared memory Semaphore SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT SystemV IPC $ipcs Shared Memory Segments -key shmid owner perms bytes 0x00000000 65536 root 644 110592 nattch 11 Semaphore Arrays -key semid owner perms nsems Message Queues -key msqid owner perms used-bytes SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT status dest messages Shared memory xy Process Process Process Process xy xy SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Shared memory See all shared memory segments ipcs ipcs –a ipcs -m Remove a shared memory segment ipcrm shm shm_id ipcrm -m shm_id SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Shared memory Allow a lot of processes using the same memory segment Minimum/Maximum shared memory segment size is 1byte/4MB Maximum number of shared memory segments: 4096 Usage Shared memory segment must be created first Attach shared memory segment to process’s address space before using Detach shared memory segment from process’s address space after finishing using it SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Operations on shared memory shmget() shmat() shmdt() shmctl() SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT shmget() #include #include #include int shmget(key_t key,int size,int shmflg); key: key of shared memory segment size: size of shared memory segment (bytes) shmflg: IPC_CREAT, IPC_EXCL or with its permission Example shm_id = shmget(123, 4096, IPC_CREAT | 0660) SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Create IPC object key #include #include key_t ftok(const char *path, int id); path: refer to an existing, accessible file id: project identifier Return value: key_t value: if successful -1: if fail Example key = ftok(“/tmp/file_123”, 321); SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Example else { /*parent*/ sleep(1); printf("Process %d writes to shared memory \n", getpid()); shm[2]=shm[0]+shm[1]; shmdt((void *)shm); } return(0); } SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT IPC Programming Shared memory Semaphore SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT SystemV IPC $ipcs Shared Memory Segments -key shmid owner perms bytes 0x00000000 65536 root 644 110592 nattch 11 Semaphore Arrays -key semid owner perms nsems Message Queues -key msqid owner perms used-bytes SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT status dest messages Semaphore See all shared semaphore ipcs ipcs –a ipcs -s Remove a semaphore ipcrm sem semid ipcrm -s semid SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Operations on semaphore semget() semop() semctl() SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT semget() #include #include #include int semget(key_t key, int nsems, int semflg); key: key of semaphore nsems: number of semaphore in the semaphore set semflag: IPC_CREAT, IPC_EXCL or with its permission Example sem_id1=semget(IPC_PRIVATE,8,IPC_CREAT|0600); sem_id2=semget(123,1,IPC_CREAT|IPC_EXCL|0660); SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Hàm semop() #include #include #include int semop(int semid, struct sembuf *sops, size_t nsops); semid: semaphore set ID returned from semget() function sops: a pointer to an array of nsops sembuf structures, each sembuf specifies an operation on a specific semaphore nsops: number of struct sembuf to be performed SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT struct sembuf struct sembuf { ushort sem_num; short sem_op; short sem_flg; } /* semaphore number */ /* semaphore operation */ /* operation flags */ sem_num: the order of this semaphore in the semaphore set sem_op: change the semaphore value sem_flg: IPC_NOWAIT: non-blocking mode SEM_UNDO: undo operation SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT semctl() #include #include #include int semctl(int semid, int semnum, int cmd); int semctl(int semid, int semnum, int cmd, union semun arg); union semun{ int val; struct semid_ds *buf; ushort *array; }; SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT semctl() - cmd argument Commands on semaphore set IPC_STAT: get semaphore information IPC_SET : set semaphore’s ownership and permissions IPC_RMID: remove the semaphore set immediately Commands on individual semaphore GETVAL: get the value of the semnum-th semaphore SETVAL: set value of the semnum-th semaphore to arg.val GETPID: get PID of process that executed the last semop call for the semnum-th semaphore of the set GETNCNT : get number of processes waiting for semval to increase GETZCNT: get number of processes waiting for semval to become zero Commands on all semaphores SETALL: set values for all semaphores of the set GETALL: get values of all semaphores of the set SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Example Implement semaphore functions seminit(): create a semaphore p (): decrease value of semaphore by v (): increase value of semaphore by semrel(): remove semaphore Write a program using semaphore to solve confliction on critical sections SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT #include #include #include #include #include union { int val; struct semid_ds *buf; ushort *array; } carg; int seminit() { int i, semid; if (semid=semget(IPC_PRIVATE,1,IPC_EXCL|0666)==-1) return(-1); carg.val=1; if (semctl(semid,0,SETVAL,carg)==-1) return(-1); return semid; } SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT void p(int sem){ struct sembuf pbuf; pbuf.sem_num=0; pbuf.sem_op=-1; /* decrease semaphore’s value */ pbuf.sem_flg=SEM_UNDO; if (semop(sem,&pbuf,1)==-1) { perror("semop"); exit(1); } } void v(int sem){ struct sembuf vbuf; vbuf.sem_num=0; vbuf.sem_op=1; vbuf.sem_flg=SEM_UNDO; if (semop(sem,&vbuf,1)==-1) { perror("semop"); exit(1); } } SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT int semrel(int semid){ return semctl(semid,0,IPC_RMID,0); } void func(int sem) { while(1) { p(sem); /* enter critical section */ printf("%d Do something in CS\n",getpid()); sleep(5); v(sem); /* exit critical section */ printf("%d Out of CS\n",getpid()); sleep(1); } } void main() { int sem=seminit();; if (fork()==0) func(sem); else func(sem); semrel(sem); } SinhVienZone.com https://fb.com/sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Questions??? SinhVienZone.com https://fb.com/sinhvienzonevn ... func(sem); semrel(sem); } SinhVienZone. com https://fb .com/ sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Questions??? SinhVienZone. com https://fb .com/ sinhvienzonevn ... https://fb .com/ sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT IPC Programming Shared memory Semaphore SinhVienZone. com https://fb .com/ sinhvienzonevn Faculty of Computer Science... https://fb .com/ sinhvienzonevn Faculty of Computer Science and Engineering - HCMUT Operations on semaphore semget() semop() semctl() SinhVienZone. com https://fb .com/ sinhvienzonevn Faculty of Computer