Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdio.h>
- int main() {
- int Len;
- int ShmId;
- int res;
- key_t Klucz=3120;
- //void ShowSMem(int);
- void * Addr;
- void * Napis;
- ShmId=shmget(Klucz, 10000, IPC_CREAT | 0660);
- if(ShmId<0){
- fprintf(stderr,"\nWystapil w trakcie kreowania segment pamieci wspolnej");
- exit(1);
- }
- if(fork()==0) {
- Addr=shmat(ShmId,0 ,0);
- Napis=(char *)Addr;
- sprintf(Napis,"-----------Proces Potomny------------", setpid());
- if(Addr<0){
- fprintf(stderr, "\nWystapil w trakcie dolaczania pamieci wspolnej");
- exit(1);
- }
- fprintf(stderr,"\nShared memory attached from %x to %x", Addr, Addr+1000);
- res=shmdt(Addr);
- if(res<0){
- fprintf(stderr,"\nWystapil w trakcie odlaczania segmentu pamieci wspolnej");
- exit(1);
- }
- sleep(10);
- printf("\nProces potomny zakonczony");
- exit(0);
- }
- wait(0);
- Addr=shmat(ShmId,0,0);
- Napis=(char *)Addr;
- printf("\nKOMUNIKAT:%s",Napis);
- if(Addr<0){
- fprintf(stderr,"\nWystapil w trakcie dolaczania amieci wspolnej");
- exit(1);
- }
- fprintf(stderr, "\nShared memory attached from %x to %x", Addr, Addr+1000);
- res=shmdt(Addr);
- if(res<0){
- fprintf(stderr, "\nWystapil w trakcie odlaczana segmentu pamieci wspolnej");
- exit(1);
- }
- //ShowSMem(ShmId);
- res=shmctl(ShmId, IPC_RMID,0);
- if(res<0) {
- fprintf(stderr,"\nWystapil w czasie usuwania pamieci wspolnej");
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement