Created
January 20, 2019 00:44
-
-
Save must1/c274052c3c537405115e82c431077e06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/ipc.h> | |
#include <sys/msg.h> | |
#include <sys/shm.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
struct bufor { | |
int mtype; | |
int mvalue; | |
}; | |
int *pam; | |
#define MAX2 12 | |
#define MAX 10 | |
#define SP 1 | |
#define W 2 | |
#define zapis pam[MAX+1] | |
#define odczyt pam[MAX] | |
int main() { | |
key_t klucz, kluczm; | |
int msgID, shmID; | |
int i; | |
int pamm[12]; | |
struct bufor komunikat; | |
if ((klucz = ftok(".", 'A')) == -1 ) { | |
printf("Blad ftok (main)\n"); | |
exit(1); | |
} | |
msgID=msgget(klucz,IPC_CREAT|0666); //uzyskanie dosepu do kolejki komunikatow | |
if (msgID==-1) { | |
printf("blad kolejki komunikatow\n"); | |
exit(1); | |
} | |
kluczm=ftok(".",'B'); //uzyskanie dosepu do pamieci dzielonej | |
shmID=shmget(kluczm, MAX2*sizeof(int),IPC_CREAT|0666); | |
if((pam = shmat(shmID, NULL, 0)) == (int *)-1) { //przylaczenie pam. dzielonej | |
printf("Blad pamieci wspoldzielonej\n"); | |
exit(EXIT_FAILURE); | |
} | |
msgrcv(msgID, &komunikat, sizeof(int), W, 0); | |
pam[13] = pam[13] + 1; | |
if(pam[13]==1) | |
msgrcv(msgID, &komunikat, sizeof(int), SP, 0); | |
komunikat.mtype = W; | |
msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0); | |
for(i=0; i<MAX; i++) | |
fprintf(stderr,"czytam %d \n",pam[i]); | |
msgrcv(msgID, &komunikat, sizeof(int), W, 0); | |
pam[13] = pam[13] - 1; | |
if(pam[13]==0) { | |
komunikat.mtype = SP; | |
msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0); | |
} | |
komunikat.mtype = W; | |
msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include<sys/ipc.h> | |
#include<sys/msg.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
#define P 20 // liczba procesow czyt i pisz | |
#define MAX 10 //rozmiar buforu | |
#define MAX2 12 // + dwa pola na indeksy zapis/odczyt | |
#define PUSTY 1 //typ komunikatu | |
#define PELNY 2 //typ komunikatu | |
#define WOLNY 3 | |
//struktura komunikatu | |
struct bufor{ | |
long mtype; | |
int mvalue; | |
}; | |
int msgID; //id kolejki kom. | |
int shmID; //id pamieci dzielonej | |
void koniec(int a ) | |
{ | |
printf("ZĹ‚apano CTRL+C.\n"); | |
msgctl(msgID,IPC_RMID,NULL); | |
shmctl(shmID,IPC_RMID, NULL); | |
printf("MAIN: Koniec.\n"); | |
} | |
int main() | |
{ | |
key_t klucz, kluczm; //klucze do kk i pam. dz. | |
// int msgID; //id kolejki kom. | |
// int shmID; //id pamieci dzielonej | |
int i; | |
struct bufor komunikat; | |
signal(SIGINT, koniec); | |
if ( (klucz = ftok(".", 'A')) == -1 ) | |
{ | |
printf("Blad ftok (main)\n"); | |
exit(1); | |
} | |
msgID=msgget(klucz,IPC_CREAT|IPC_EXCL|0666); //tworzenie kk | |
if (msgID==-1) | |
{printf("blad kolejki komunikatow\n"); exit(1);} | |
kluczm=ftok(".",'B'); | |
shmID=shmget(kluczm, MAX2*sizeof(int), IPC_CREAT|IPC_EXCL|0666);//tworzenie pam. dz. | |
fflush(stdout); | |
for (i = 0; i < P; i++) | |
switch (fork()) | |
{ | |
case -1: | |
perror("Blad fork (mainprog)"); | |
exit(2); | |
case 0: | |
execl("./pisz","pisz", NULL ); | |
} | |
for(i=0;i<P;i++) | |
switch (fork()) | |
{ | |
case -1: | |
printf("Blad fork (mainprog)\n"); | |
exit(2); | |
case 0: | |
execl("./czyt","czyt",NULL); | |
} | |
for(i=0;i<2*P;i++) | |
wait(NULL); | |
msgctl(msgID,IPC_RMID,NULL); //zwalnianie zasobow | |
shmctl(shmID,IPC_RMID, NULL); | |
printf("MAIN: Koniec.\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/ipc.h> | |
#include <sys/msg.h> | |
#include <sys/shm.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
struct bufor{ | |
int mtype; | |
int mvalue; | |
}; | |
int *pam; | |
#define MAX2 12 | |
#define MAX 10 | |
#define SP 1 | |
#define zapis pam[MAX+1] | |
#define odczyt pam[MAX] | |
int main() | |
{ | |
key_t klucz, kluczm; | |
int msgID, shmID; | |
int i; | |
int pamm[12]; | |
struct bufor komunikat; | |
if ((klucz = ftok(".", 'A')) == -1 ){printf("Blad ftok (main)\n");exit(1);} | |
msgID=msgget(klucz,IPC_CREAT|0666); //uzyskanie dosepu do kolejki komunikatow | |
if (msgID==-1){printf("blad kolejki komunikatow\n"); exit(1);} | |
kluczm=ftok(".",'B'); //uzyskanie dosepu do pamieci dzielonej | |
shmID=shmget(kluczm, MAX2*sizeof(int),IPC_CREAT|0666); | |
if((pam = shmat(shmID, NULL, 0)) == (int *)-1) //przylaczenie pam. dzielonej | |
{ | |
printf("Blad pamieci wspoldzielonej\n"); | |
exit(EXIT_FAILURE); | |
} | |
msgrcv(msgID, &komunikat, sizeof(int), SP, 0); // | |
for(i=0; i<MAX; i++) { | |
pam[zapis]=i; | |
zapis=(zapis+1)%MAX; | |
fprintf(stderr,"zapisalem %d \n",pam[i]); | |
} | |
komunikat.mtype = SP; | |
msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment