1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define PRVI_KEY 10001
#define DRUGI_KEY 10002
#define TRECI_KEY 10003
#define MEM_KEY 10004
#define N 1024
union semun{
int val;
struct semid_ds *buf;
ushort *array;
struct seminfo * __buf;
void * __pad;
};
int main()
{
int sem1,sem2,sem3,memid;
int i,j;
FILE *f;
char * shmem;
char buff[N+1];
struct sembuf lock={0,-1,0};
struct sembuf unlock={0,1,0};
union semun opts;
sem1=semget(PRVI_KEY,1,IPC_CREAT | 0777);
sem2=semget(DRUGI_KEY,1,IPC_CREAT | 0777);
sem3=semget(TRECI_KEY,1,IPC_CREAT | 0777);
opts.val=1;
semctl(sem1,0,SETVAL,opts);
opts.val=0;
semctl(sem2,0,SETVAL,opts);
opts.val=0;
semctl(sem3,0,SETVAL,opts);
memid=shmget(MEM_KEY,N*sizeof(char),IPC_CREAT | 0777);
if(fork()==0)
{
shmem=(char *)shmat(memid,NULL,0);
for(i=0;i<10;i++)
{
semop(sem1,&lock,1);
for(j=0;j<N/2;j++)
shmem[j]='a'+rand()%('z'-'a'+1);
semop(sem2,&unlock,1);
}
shmdt(shmem);
return 0;
}
if(fork()==0)
{
shmem=(char *)shmat(memid,NULL,0);
for(i=0;i<10;i++)
{
semop(sem2,&lock,1);
for(j=N/2;j<N;j++)
shmem[j]='0'+rand()%('9'-'0'+1);
semop(sem3,&unlock,1);
}
shmdt(shmem);
return 0;
}
shmem=(char *)shmat(memid,NULL,SHM_RDONLY);
for(i<0;i<10;i++)
{
semop(sem3,&lock,1);
char buff[N+1];
strncpy(buff,shmem,N);
buff[N]='\0';
f=fopen("sadrzaj","w");
fprintf(f,"%s\n",buff);
fclose(f);
semop(sem1,&unlock,1);
}
shmdt(shmem);
semctl(sem1,0,IPC_RMID,opts);
semctl(sem2,0,IPC_RMID,opts);
semctl(sem3,0,IPC_RMID,opts);
shmctl(memid,IPC_RMID,0);
return 0;
}
| |