System V 信号量操作,进程间通信(…
来源:互联网 发布:男士服装搭配软件 编辑:程序博客网 时间:2024/06/11 15:31
//实现semWait, semPost封装
int val; struct semid_ds *buf; ushort *array;
if ( semid < 0 ) return -1;
struct sembuf op; int ret; //"return"
op.sem_num = idx; op.sem_op = -1; op.sem_flg = SEM_UNDO;
while ((ret = semop(semid, &op,1)) == -1 && errno == EINTR) ; return ret;
if ( semid < 0 ) return -1;
struct sembuf op; int ret;
op.sem_num = idx; op.sem_op = 1; op.sem_flg = SEM_UNDO;
while ((ret = semop(semid, &op,1)) == -1 && errno == EINTR) ; return ret;
fprintf(stdout, "sem val: %d\n", semctl(semid,idx, GETVAL));
//__nsems = 10, the number of semaphores in thissemaphore set. int semid = semget(321034, 10, IPC_CREAT |IPC_EXCL | 0666); if(semid < 0){ perror("semget : "); return -1; }
int limit = 5; pid_t pid; union semun arg;
ushort array[10] = {1,1,1,1,1,1,1,1,1,1}; arg.array = array;
// Set the values of all the semaphores in theset to the values in the array pointed to // by the array member of the passed-in unionsemun semctl(semid, 0, SETALL, arg); //Here, Thesemnum parameter to semctl() isn't used.
/ * Allocate resources. Block the callingprocess until the value of the semaphore isgreater than or equal to the absolute value ofsem_op. (That is, wait until enough resourceshave been freed by other processes for this one toallocate.) Then add (effectively subtract,since its negative) the value of sem_op to thesemaphore's value. * /
if((pid = fork()) < 0){ semctl(semid, 2,IPC_RMID); return -1; }
if(0 == pid){ srand(time(NULL)^getpid()); int i = 0;
while(i++ <limit){ semWait(semid, 2);displaySem(semid, 2);
int sec =rand() & 255;
fprintf(stdout, "[semWait]child doing work...\nafter %dmsfinish...\n\n", sec); usleep(sec);
semPost(semid, 2); }
}else{ srand(time(NULL)^getpid()); int i = 0;
while(i++ <limit){ semWait(semid, 2);
int sec =rand() & 255;
fprintf(stdout, "[semWait]parent doing work...\nafter %dmsfinish...\n\n", sec); usleep(sec);
semPost(semid, 2); }
}
usleep(1000);
semctl(semid, 2, IPC_RMID); return 0;
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/sem.h>
union semun {
};
//Allocate resources
int semWait(int semid, int idx)
{
}
//Release resources
int semPost(int semid, int idx)
{
}
void displaySem(int semid, int idx)
{
}
#if 01
int main()
{
// arg.val = 1;
}
#endif
- System V 信号量操作,进程间通信(…
- System V 信号量操作,进程间通信(…
- Linux进程间通信(IPC)编程实践(十)System V信号量---PV操作经典题目
- 进程间通信——System V信号量
- Linux程序设计学习笔记----System V进程间通信(信号量)
- UNPv2:进程间通信(四) System V信号量
- System V进程间通信--信号量机制(生产者消费者问题)
- linux进程通信之SYSTEM V信号量
- Linux进程间通信(IPC)编程实践(九)System V信号量---封装一个信号量操作的工具集
- 进程间通信——信号量(P、V操作)
- System v进程间通信
- System V进程间通信
- Linux 进程通信(System V) 第四节 ------>信号量
- Linux C编程--进程间通信(IPC)5--System V IPC 机制2--信号量
- Linux C编程--进程间通信(IPC)5--System V IPC 机制2--信号量
- UNP(卷2:进程间通信)—— 第11章:System V 信号量
- Linux: 进程间通信
- <linux进程>system-V信号量的基本操作
- 系统信号表格和一些资料
- 数据机构快速排序之c语言实现
- 线性插值原理
- epoll监听pipe,fork后父进程写给…
- System V 信号量操作,进程间通信(…
- System V 信号量操作,进程间通信(…
- 我的博客今天1岁352天了,我领取了…
- 将博客搬至CSDN
- Objective-C非正式协议与正式协议
- c++ /c malloc
- FireBreath学习之二 Summary--方法 属性 Attributes 事件和js的交互等
- head first-------abstract factory pattern
- 在ARM平台上移植ubuntu
- Android开发入门之通过GET和POST方式提交参数给web应用