Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdio.h>
- int main()
- {
- int Len;
- int ShmId;
- int res;
- int suma = 0;
- key_t Klucz = 3120;
- void *Addr;
- char *Napis;
- int tab[5] = {1,2,3,4,5};
- int i;
- ShmId = shmget(Klucz, 10000, IPC_CREAT | 0660);
- if (ShmId < 0)
- {
- fprintf(stderr, "\nWystapil w trakcie kreowania sagment pamieci wspolnej");
- exit(1);
- } //if
- if (fork() == 0)
- {
- Addr = shmat(ShmId, 0, 0);
- Napis = (char*) Addr;
- for (i=0;i<5;i++){
- suma = suma + tab[i];
- }//for
- sprintf(Napis, "---->Proces Potomny %d<-----\nSuma tablicy: %d", getpid(),suma);
- if (Addr < 0)
- {
- fprintf(stderr, "\nWystapil w trakcie dolaczania pamieci wspolnej");
- exit(1);
- } //if addr
- fprintf(stderr, "\nShared memory atteched from %x to %x", Addr, Addr + 1000);
- res = shmdt(Addr);
- if (res < 0)
- {
- fprintf(stderr, "\nWystapil w trakcie odlaczania sagmentu pamieci wspolnej");
- exit(1);
- } //if res
- sleep(10);
- printf("\nProces potomny zakonczony");
- exit(0);
- } //if fork
- wait(0); //proces gowny
- Addr = shmat(ShmId, 0, 0);
- Napis = (char*) Addr;
- printf("\n\nKOMUNIKAT: %s", Napis);
- if (Addr < 0)
- {
- fprintf(stderr, "\nWystapil w trakcie dolaczania pamieci wspolnej");
- exit(1);
- } //if addr
- fprintf(stderr, "\nShared memory attached from %x to %x \n", Addr, Addr + 1000);
- res = shmdt(Addr);
- if (res < 0)
- {
- fprintf(stderr, "\nWystapil w trakcie odlaczania sagmentu pamieci wspolnej");
- exit(1);
- } //if res
- res = shmctl(ShmId, IPC_RMID, 0);
- if (res < 0)
- {
- fprintf(stderr, "\nWystapil w trakcie usuwania pamieci wspolnej");
- exit(1);
- } //if res
- printf("\n \n");
- } //main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement