Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Creating a shared memory using Server Process:
- #include<sys/shm.h>
- #include<sys/ipc.h>
- #include<stdio.h>
- #include<sys/types.h>
- #include<stdlib.h>
- #define SHMSZ 27
- main()
- {
- char c;
- int shmid;
- key_t key;
- char *shm,*s;
- key=1004;
- if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
- {
- perror("shmget");
- exit(1);
- }
- if((shm=shmat(shmid,NULL,0))==(char *)-1)
- {
- perror("shmat");
- exit(1);
- }
- s=shm;
- for(c='a';c<='z';c++)
- *s++=c;
- *s='\0';
- printf("Shared Memory Contents:%s\n",shm);
- while(*shm!='*')
- sleep(1);
- printf("Shared Memory Contents:%s\n",shm);
- exit(0);
- }
- Client Program:
- #include<sys/shm.h>
- #include<sys/ipc.h>
- #include<stdio.h>
- #include<sys/types.h>
- #include<stdlib.h>
- #define SHMSZ 27
- main()
- {
- char c;
- int shmid;
- key_t; key
- char *shm,*s;
- key=1004;
- if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
- {
- perror("shmget");
- exit(1);
- }
- if((shm=shmat(shmid,NULL,0))==(char *)-1)
- {
- perror("shmat");
- exit(1);
- }
- for(s=shm;*s!='\0';s++)
- putchar('\n');
- *shm='*';
- exit(0);
- }
Add Comment
Please, Sign In to add comment