Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/shm.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #define WAIT 10
- #define THINKING 0
- #define N 5
- void signal_handler(int);
- struct Message{
- long mtype;
- char *mvalue;
- };
- int forkID, philosophID, shmID, waitID;
- int main(){
- key_t keyFork, keyWait, keyPhilosoph, keyshm;
- int philoshopNumber = N;
- int i;
- int *stan;
- char bufor[2];
- struct Message message;
- signal(SIGINT, signal_handler);
- printf("Start!\n");
- if((keyPhilosoph = ftok(".", 'P')) == -1){
- fprintf(stderr, "Error ftok message!1\n");
- exit(1);
- }
- philosophID = msgget(keyPhilosoph, IPC_CREAT | IPC_EXCL | 0666);
- if(philosophID == -1){
- fprintf(stderr, "Error message queue!1\n");
- msgctl(philosophID, IPC_RMID, NULL);
- exit(1);
- }
- if((keyFork = ftok(".", 'F')) == -1){
- fprintf(stderr, "Error ftok message!1\n");
- exit(1);
- }
- forkID = msgget(keyFork, IPC_CREAT | IPC_EXCL | 0666);
- if(forkID == -1){
- fprintf(stderr, "Error message queue!1\n");
- msgctl(philosophID, IPC_RMID, NULL);
- exit(1);
- }
- for(i = 1; i<=N; i++){
- message.mtype=i;
- msgsnd(forkID, &message, sizeof(message.mvalue), 0);
- }
- if((keyWait = ftok(".", 'D')) == -1){
- fprintf(stderr, "Error ftok message!1\n");
- exit(1);
- }
- waitID = msgget(keyWait, IPC_CREAT | IPC_EXCL | 0666);
- if(waitID == -1){
- fprintf(stderr, "Error message queue!1\n");
- msgctl(waitID, IPC_RMID, NULL);
- msgctl(philosophID,IPC_RMID,NULL);
- exit(1);
- }
- message.mtype = WAIT;
- msgsnd(waitID, &message, sizeof(message.mvalue), 0);
- //USTAWIENIE PAMIECI DZIELONEJ
- if((keyshm = ftok(".", 'M')) == -1){
- fprintf(stderr, "Error ftok shared memory!\n");
- msgctl(philosophID, IPC_RMID, NULL);
- msgctl(waitID, IPC_RMID, NULL);
- shmctl(shmID, IPC_RMID, NULL);
- exit(1);
- }
- shmID = shmget(keyshm, philoshopNumber * sizeof(int), IPC_CREAT| IPC_EXCL | 0666);
- if(shmID == -1){
- fprintf(stderr, "Sharred memory error!\n");
- msgctl(philosophID, IPC_RMID, NULL);
- msgctl(waitID, IPC_RMID, NULL);
- shmctl(shmID, IPC_RMID, NULL);
- exit(1);
- }
- stan = (int*)shmat(shmID, NULL, 0);
- for( i = 0; i<N; i++){
- stan[i] = THINKING;
- }
- //TWORZENIE PROCESÓW FILOZOFÓW
- for(i = 0; i<5; i++){
- switch (fork()){
- case -1:
- fprintf(stderr, "Error fork main.\n");
- msgctl(philosophID, IPC_RMID, NULL);
- msgctl(waitID, IPC_RMID, NULL);
- shmctl(shmID, IPC_RMID, NULL);
- printf("NOOOO!!");
- exit(1);
- case 0:
- sprintf(bufor, "%d", i);
- execl("./filozof", "filozof", bufor, NULL);
- }
- }
- //ZAMYKANIE MECHANIZMÓW IPC
- for(i = 0; i<5; i++){
- wait(NULL);
- }
- printf("what?");
- msgctl(waitID, IPC_RMID, NULL);
- msgctl(forkID, IPC_RMID, NULL);
- msgctl(philosophID, IPC_RMID, NULL);
- shmctl(shmID, IPC_RMID, NULL);
- printf("MAIN: Koniec.\n");
- return 0;
- }
- void signal_handler(int sig){
- if(sig != SIGINT){
- return;
- }
- printf("SIGINT!\n");
- msgctl(waitID, IPC_RMID, NULL);
- msgctl(forkID, IPC_RMID, NULL);
- msgctl(philosophID, IPC_RMID, NULL);
- shmctl(shmID, IPC_RMID, NULL);
- exit(3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement