C 共享内存编程
时间:2010-11-01 来源:luozhiyong131
/****************
创建一个信号量集
*****************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int main()
{
int semid;
key_t key;
key = ftok("/home",'a');
if(key<0)
{
perror("ftok error");
exit(1);
}
semid = semget(key,1,IPC_CREAT|0666);
if(semid<0)
{
perror("semget error");
exit(1);
}
printf("Done\n");
return 0;
}
/***********
进程间同步
************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/wait.h>
int main()
{
int semid,i,ret,status;
struct sembuf lock ={0,-1,SEM_UNDO};
struct sembuf unlock ={0,1,SEM_UNDO|IPC_NOWAIT};
pid_t pid;
key_t key;
key = ftok("/home",'a');
if(key<0)
{
perror("ftok error");
exit(1);
}
semid = semget(key,1,IPC_CREAT|0666);
if(semid<0)
{
perror("semget error");
exit(1);
}
ret=semctl(semid,0,SETVAL,1);
if(ret==-1)
{
perror("semctl error");
exit(1);
}
pid = fork();
if(pid<0)
{
perror("fork error");
exit(1);
}
if(pid==0)
{
for(i=0;i<3;i++)
{
sleep(2);
ret=semop(semid,&lock,1);
if(ret==-1)
{
perror("semop lock error");
exit(1);
}
printf("Child proxdss access the resource\n");
sleep(2);
printf("Complete\n");
ret=semop(semid,&unlock,1);
if(ret==-1)
{
perror("semop unlock error");
exit(1);
}
}
}
else
{
for(i=0;i<3;i++)
{
sleep(2);
ret=semop(semid,&lock,1);
if(ret==-1)
{
perror("semop lock error");
exit(1);
}
printf("Parent proxdss access the resource\n");
sleep(2);
printf("Complete\n");
ret=semop(semid,&unlock,1);
if(ret==-1)
{
perror("semop unlock error");
exit(1);
}
}
if(pid!=wait(&status))
{
printf("wait error");
exit(1);
}
ret=semctl(semid,0,IPC_RMID,0);
if(ret==-1)
{
perror("semctl error");
exit(1);
}
}
printf("Done\n");
return 0;
}
/***********
创建共享内存
************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#define SIZE 1024
int main()
{
int shmid;
key_t key;
key = ftok("/home",'a');
if(key<0)
{
perror("ftok error");
exit(1);
}
shmid = shmget(key,SIZE,IPC_CREAT|0666);
if(shmid<0)
{
perror("semget error");
exit(1);
}
else
printf("Done\n");
return 0;
}
/***********
共享内存
************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#define SIZE 1024
int main(int argc, char **argv)
{
int shmid,psm;
struct shmid_ds dsbuf;
pid_t pid;
key_t key;
if(argc!=2)
{
printf("aruments error");
exit(1);
}
key = ftok("/home",'a');
if(key<0)
{
perror("ftok error");
exit(1);
}
shmid = shmget(key,SIZE,IPC_CREAT|0666);
if(shmid<0)
{
perror("semget error");
exit(1);
}
pid = fork();
if(pid<0)
{
perror("fork error");
exit(1);
}
if(pid==0)
{
printf("Child process\n");
printf("PID:%d\n",getpid());
psm=shmat(shmid,NULL,0);
if(psm==-1)
{
perror("shmat error");
exit(1);
}
else
{
strcpy((char *)psm,argv[1]);
printf("Send message:%s\n",(char *)psm);
if(shmdt((char *)psm)<0)
perror("shmdt error");
sleep(2);
}
}
else
{
sleep(2);
printf("Parent process\n");
printf("PID:%d\n",getpid());
if(shmctl(shmid,IPC_STAT,&dsbuf)<0)
{
perror("shmctl error");
exit(1);
}
else
{
printf("Share Memory Information\n");
printf("\tCreator PID:%d\n",dsbuf.shm_cpid);
printf("\tSize :%d\n",dsbuf.shm_segsz);
printf("\tLast Opeetor PID:%d\n",dsbuf.shm_lpid);
psm=shmat(shmid,NULL,0);
if(psm==-1)
{
perror("shmat error");
exit(1);
}
else
{
printf("Received message:%s\n",(char *)psm);
if(shmdt((char *)psm)<0)
{
perror("shmdt error");
exit(1);
}
}
}
if(shmctl(shmid,IPC_RMID,NULL)<0)
{
perror("shmctl error");
exit(1);
}
}
printf("Done\n");
return 0;
}