盆景网站建设swot分析,西宁网站建设建站,宁波网络seo哪家有名,招远做网站价格进程同步 一组并发进程进行相互合作、相互等待#xff0c;使得各进程按一定的顺序执行的过程称为进程间的同步。 进程同步与进程互斥 进程同步问题的关键在于生产者不需要获取信号量#xff0c;消费者不需要释放信号量#xff0c;所以信号量的初值设置为0。但是进程互斥问题…进程同步 一组并发进程进行相互合作、相互等待使得各进程按一定的顺序执行的过程称为进程间的同步。 进程同步与进程互斥 进程同步问题的关键在于生产者不需要获取信号量消费者不需要释放信号量所以信号量的初值设置为0。但是进程互斥问题中双方都需要获取和释放信号量所以信号量的初值至少为1。 producor.c #include sys/types.h
#include sys/stat.h
#include fcntl.h
#include unistd.h#include sys/ipc.h
#include sys/sem.hvoid main(){//创建文件int fd;fd open(product.txt, O_RDWR | O_CREAT, 0777);//睡眠等待sleep(10);//写入数据write(fd, The product is finished!, 25);//关闭文件close(fd);//创建信号量int key;int semid;key ftok(product.txt, 0);semid semget(key, 1, IPC_CREAT);//设置信号量semctl(semid, 0, SETVAL, 0); //设置信号量0的值为0//释放信号量struct sembuf sops;sops.sem_num 0;sops.sem_op 1;sops.sem_flg SEM_UNDO;semop(semid, sops, 1);
} customer.c #include stdlib.h#include sys/ipc.h
#include sys/sem.hvoid main(){//打开信号量int key;int semid;key ftok(product.txt, 0);semid semget(key, 1, IPC_CREAT);//获取信号量struct sembuf sops;sops.sem_num 0;sops.sem_op -1;sops.sem_flg SEM_UNDO;semop(semid, sops, 1);//拷贝文件system(cp product.txt ship.txt);
} 转载于:https://www.cnblogs.com/d442130165/p/5225409.html