Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <semaphore.h>
- #include <sys/shm.h>
- #include <sys/msg.h>
- #define key 0x1112
- struct msq_que
- {
- long msg_type;
- double buffer[6];
- }msg;
- struct msq_que rec;
- int main()
- {
- char nazwa[20];
- printf("Podaj nazwe pliku, z ktorego maja zostac odczytane liczby: ");
- scanf("%s", nazwa);
- if(access(nazwa, F_OK) != -1)
- {
- printf("Plik istnieje.\n");
- if(access(nazwa, R_OK) != -1)
- {
- printf("Sa uprawnienia.\n");
- }
- else
- {
- printf("Brak uprawnien.\n");
- return 1;
- }
- }
- else
- {
- printf("Plik nie istnieje.\n");
- return 1;
- }
- FILE *fp;
- fprintf(stderr,"\na");
- //tworzenie kolejki
- //struct msq_que *msg;
- int msgid;
- fprintf(stderr,"\na");
- msgid = msgget(key, 0666 | IPC_CREAT);
- fprintf(stderr,"\na");
- msg.msg_type = 1;
- fprintf(stderr,"\na1");
- sleep(10);
- //procesy
- if(fork()==0)
- {
- int n=0;
- int a, i;
- double liczba;
- fprintf(stderr,"\na");
- fp = fopen(nazwa ,"r");
- a = fscanf(fp, "%lf", &liczba);
- fprintf(stderr,"\na");
- //msg.ilosc=n;
- //msg.polecenie=1;
- while(a != EOF)
- {
- msg.buffer[n]=liczba;
- n++;
- a = fscanf(fp, "%lf", &liczba);
- if(n == 6){
- //msg.ilosc=n;
- n = 0;
- msgsnd(msgid, &msg, sizeof(msg), 0);
- for(i=0;i<6;i++)
- {
- fprintf(stderr,"\n%lf", msg.buffer[i]);
- }
- }
- }
- //msg.ilosc = n;
- msgsnd(msgid, &msg, sizeof(msg), 0);
- //msg.polecenie = 0;
- fclose(fp);
- return 0;
- }
- if(fork()==0)
- {
- int i;
- int n = 0;
- int a;
- const char *plik="/home/bartar/LAB10/odp.txt";
- double suma=0.0;
- double srednia;
- double liczba;
- rec.msg_type=1;
- fprintf(stderr,"\na2");
- //msgrcv(msgid, &rec, sizeof(rec), 1, 0);
- msgrcv(msgid, &msg, sizeof(msg), 1, 0);
- for(i=0; i<6; i++)
- {
- liczba = msg.buffer[i];
- suma+=liczba;
- printf("%lf\n", liczba);
- n++;
- }
- if(n==0)
- {
- srednia = 0.0;
- }
- else
- {
- srednia=suma/n;
- }
- printf("Srednia: %lf\n", srednia);
- fp=fopen(plik, "w");
- fprintf(fp, "%lf", srednia);
- fclose(fp);
- return 0;
- }
- while(1)
- {
- }
- msgctl(msgid, IPC_RMID, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement