Add IPC semaphore sample code, not finish yet
New file |
| | |
| | | #include <stdio.h> |
| | | #include <string.h> |
| | | #include <errno.h> |
| | | #include <unistd.h> |
| | | #include <stdlib.h> |
| | | |
| | | #include <sys/types.h> |
| | | #include <sys/ipc.h> |
| | | #include <sys/sem.h> |
| | | |
| | | #define FTOK_PATH "/dev/zero" |
| | | #define FTOK_PROJID 0x666 |
| | | |
| | | union semun |
| | | { |
| | | int val; |
| | | struct semid_ds *buf; |
| | | unsigned short *arry; |
| | | }; |
| | | |
| | | int main(int argc, char **argv) |
| | | { |
| | | int semid; |
| | | |
| | | if( (semid=init_set(1)) < 0) |
| | | { |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | int init_set(int nums) |
| | | { |
| | | key_t key; |
| | | int semid; |
| | | union semun sem_union; |
| | | |
| | | if( (key=ftok(FTOK_PATH, FTOK_PROJID)) < 0 ) |
| | | { |
| | | printf("ftok() get IPC token failure: %s\n", strerror(errno)); |
| | | return -1; |
| | | } |
| | | |
| | | semid = semget(key, nums, IPC_CREAT|IPC_EXCL|0644); |
| | | if( semid < 0) |
| | | { |
| | | printf("semget() get semid failure: %s\n", strerror(errno)); |
| | | return -2; |
| | | } |
| | | |
| | | |
| | | sem_union.val = 1; |
| | | if( semctl(semid, 0, SETVAL, sem_union)<0 ) |
| | | { |
| | | printf("semctl() set initial value failure: %s\n", strerror(errno)); |
| | | return -3; |
| | | } |
| | | |
| | | return semid; |
| | | } |