Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //mainprogram.c
- #include <stdio.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- struct message{
- long mtype;
- int mvalue;
- };
- void wypisz_widelce(int *w){
- int i;
- for(i = 0;i<5;i++){
- printf("Widelec[%d] = %d\n",i+1,w[i]);
- }
- }
- int main(int argc, char *argv[]){
- key_t key_message, key_memory;
- int msgID,shmID;
- int i;
- int F = 5;
- char bufor[3];
- int *widelec;
- struct message kom;
- //kolejka
- if( (key_message = ftok(".",'A')) == -1){
- printf("Ftok message_key error (main)\n");
- exit(1);
- }
- if( (msgID = msgget(key_message,IPC_CREAT|IPC_EXCL|0666)) == -1){
- printf("Msgget error (main)\n");
- exit(1);
- }
- //pamiec
- if( (key_memory = ftok(".",'B')) == -1){
- printf("Ftok memory_key error (main)\n");
- exit(1);
- }
- if( (shmID = shmget(key_memory,5 * sizeof(int),IPC_CREAT|IPC_EXCL|0666)) == -1){
- printf("Shmget error (main)\n");
- exit(1);
- }
- fflush(stdout);
- widelec = (int*)shmat(shmID,NULL,0); //przydzielenie pamieci dzielonej.
- for(i = 0;i < F;i++){
- widelec[i] = 0;
- }
- wypisz_widelce(widelec);
- for(i = 0;i < F;i++){ //wyslanie komunikatu o pustych widelcach
- kom.mtype = i + 1;
- if(msgsnd(msgID,&kom,sizeof(kom.mvalue),0) == -1){
- printf("Msgsnd widelec error (main)\n");
- exit(1);
- }
- }
- printf("wyslano komunikaty\n");
- //tworzenie procesu filozofow
- for(i = 0;i < F;i++)
- switch(fork())
- {
- case -1:
- perror("Fork error (main)");
- exit(2);
- case 0:
- sprintf(bufor,"%d",i); //przekazanie numeru
- execl("./filozof","filozof",bufor,NULL);
- }
- for(i = 0;i < 5;i++){
- wait(NULL);
- }
- msgctl(msgID,IPC_RMID,NULL);
- shmctl(shmID,IPC_RMID,NULL);
- printf("MAIN: Koniec.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement