代码示例:?
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/sem.h>
#include "semun.h"
int main(int argc,char *argvp[]){
int i ;
int pause_time ;
char op_char ='O' ;
srand((unsigned int)getpid());
sem_id = semget((key_t)1234,1,0666 | IPC_CREAT);
if(argc > 1){
if(!set_semvalue()){
fprintf(stderr,"Failed to initialize semaphore . \n");
exit(EXIT_FAILURE);
}
op_char = 'X' ;
sleep(2);
}
for ( i = 0; i < 10; i++) {
if( !semaphore_p()){
exit(EXIT_FAILURE);
}
pause_time = rand() %3 ;
sleep(pause_time);
printf("%c",op_char);
fflush(stdout);
if(!semaphore_v()){
exit(EXIT_FAILURE);
}
pause_time = rand() %2 ;
sleep(pause_time);
}
printf("\n%d - finished\n",getpid());
if(argc >1){
sleep(10);
del_semvalue();
}
exit(EXIT_SUCCESS);
}
static int set_semvalue(void) {
union semun s ;
s.val = 1 ;
if(semctl(sem_id,0,SETVAL,s ) == -1){
return (0);
}
return(1);
}
static int del_semvalue(void){
union semun sem_union ;
if(semctl(sem_id,0,IPC_RMID,sem_union) == -1){
fprintf(stderr,"Failed to delete semaphore \n ");
return (0);
}
return (1);
}
static int semaphore_p(void){
struct sembuf sem_b ;
sem_b.sem_num = 0 ;
sem_b.sem_op = -1 ;
sem_b.sem_flg = SEM_UNDO ;
if(semop(sem_id,&sem_b,1 ) == -1){
fprintf(stderr,"semaphore_p failed \n ");
return (0);
}
return (1);
}
static int semaphore_v(void){
struct sembuf sem_b ;
sem_b.sem_num = 0 ;
sem_b.sem_op = 1 ;
sem_b.sem_flg = SEM_UNDO ;
if(semop(sem_id,&sem_b,1) == -1){
fprintf(stderr,"semaphore_v failed \n ");
return (0);
}
return (1);
}
?共享内存有两种方式,即 shm(shared memory) 和 mmap 方式。前者直接共享物理内存,后者通过一个中间文件间接共享内存。
利用mmap实现内存文件映射,代码示例:?
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
typedef struct {
int integer ;
char string[4] ;
}RECORD ;
#define NRECORDS (100)
int main(){
RECORD record,*mapped ;
int i,f ;
f=open("records.dat",O_RDWR);
mapped = (RECORD*)mmap(0,NRECORDS * sizeof(record),PROT_READ | PROT_WRITE,MAP_SHARED,f,0);
// 读到第43条数据 ,并且重新赋值
mapped[43].integer = 111111 ;
printf("save3 -%d,",mapped[43].integer);
// 在该内存修改写回被映射的文件
msync((void*)mapped,NRECORDS*sizeof(record),MS_ASYNC);
// 释放内存
munmap((void*)mapped,NRECORDS*sizeof(record));
close(f);
exit(0);
}
shm(shared memory)共享物理内存,代码示例:??
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#include"shm_com.h"
int main(){
int running = 1 ;
void *shared_memory = (void *)0 ;
struct share_use_st *shared_stuff ;
int shmid ;
srand((unsigned int) getpid());
shmid = shmget((key_t)1234,sizeof(struct share_use_st),0666|IPC_CREAT);
if(shmid == -1){
fprintf(stderr,"shmget failed \n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid,(void *)0,0);
if(shared_memory == (void*)-1 ){
fprintf(stderr,"shmat failed \n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %p\n",shared_memory);
shared_stuff = (struct share_use_st*)shared_memory ;
shared_stuff->written_by_you = 0 ;
while (running)
{
if(shared_stuff ->written_by_you){
printf("You wrote : %s",shared_stuff->some_text);
sleep(rand() % 4);
shared_stuff->written_by_you = 0 ;
if(strncmp(shared_stuff->some_text,"end",3) == 0){
running = 0 ;
}
}
}
if(shmdt(shared_memory) ==-1){
fprintf(stderr,"shmdt failed \n");
exit(EXIT_FAILURE);
}
if(shmctl(shmid,IPC_CREAT,0) ==-1){
fprintf(stderr,"shmctl (IPC_CREAT) failed \n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
?
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#include"shm_com.h"
int main(){
int running = 1 ;
void *shared_memory = (void*)0 ;
struct share_use_st *shared_stuff ;
char buffer[BUFSIZ] ;
int shmid ;
shmid = shmget((key_t)1234,sizeof(struct share_use_st),0666|IPC_CREAT);
if(shmid == -1){
fprintf(stderr,"shmget failed \n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid,(void*)0,0);
if(shared_memory == (void*)-1 ){
fprintf(stderr,"shmat failed \n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %p\n",shared_memory);
shared_stuff = (struct share_use_st*)shared_memory ;
while (running)
{
while (shared_stuff ->written_by_you ==1)
{
sleep(1);
printf("waiting for client ...\n");
}
printf("Enter some text :");
fgets(buffer,BUFSIZ,stdin);
strncpy(shared_stuff->some_text,buffer,TEXT_SZ);
shared_stuff->written_by_you = 1 ;
if(strncmp(buffer,"end",3) ==0){
running = 0 ;
}
}
if(shmdt(shared_memory) ==-1){
fprintf(stderr,"shmdt failed \n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
从队列读取数据 ,代码示例:?
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/msg.h>
#include<errno.h>
struct my_msg_st {
long int my_msg_type ;
char some_text[BUFSIZ];
};
int main(){
int running = 1 ;
int msgid ;
struct my_msg_st some_data ;
long int msg_to_receive = 0 ;
msgid = msgget((key_t)1234,0666|IPC_CREAT);
if(msgid ==-1){
fprintf(stderr,"msgget fialed with error : %d\n",errno);
exit(EXIT_FAILURE);
}
while (running) {
if(msgrcv(msgid,(void*)&some_data,BUFSIZ,msg_to_receive,0) ==-1){
fprintf(stderr,"msgrcv failed with error :%d\n",errno);
exit(EXIT_FAILURE);
}
printf("You wrote : %s",some_data.some_text);
if(strncmp(some_data.some_text,"end",3) ==0){
running = 0 ;
}
}
if(msgctl(msgid,IPC_CREAT,0) ==-1){
fprintf(stderr,"msgctl(IPC_CREAT) failed \n");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
输入数据带队列,代码示例:?
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/msg.h>
#include<errno.h>
#define MAX_TEXT 512
struct my_msg_st {
long int my_msg_type ;
char some_text[MAX_TEXT];
};
int main(){
int running = 1 ;
struct my_msg_st some_data ;
int msgid ;
char buffer[BUFSIZ];
msgid = msgget((key_t)1234,0666|IPC_CREAT);
if(msgid ==-1){
fprintf(stderr,"msgget fialed with error : %d\n",errno);
exit(EXIT_FAILURE);
}
while (running) {
printf("Enter some text :");
fgets(buffer,BUFSIZ,stdin);
some_data.my_msg_type = 1 ;
strcpy(some_data.some_text,buffer);
if(msgsnd(msgid,(void*)&some_data,MAX_TEXT,0) ==-1){
fprintf(stderr,"msgsnd failed\n");
exit(EXIT_FAILURE);
}
if(strncmp(buffer,"end",3) ==0){
running = 0 ;
}
}
exit(EXIT_FAILURE);
}